2
votes

I am using following OpenGL ES 1.x code to set my projection coordinates.

glMatrixMode(GL_PROJECTION); 

float width = 320;
float height = 480;

glOrthof(0.0,                  // Left
         1.0,                  // Right
         height / width,       // Bottom
         0.0,                  // Top
         -1.0,                 // Near
         1.0);                 // Far
glMatrixMode(GL_MODELVIEW);

What is the equivalent method to setup this in OpenGL ES 2.0 ? What projection matrix should I pass to the vertex shader ?

I have tried following function to create the matrix but its not working:

void SetOrtho (Matrix4x4& m, float left, float right, float bottom, float top, float near, 
float far)
{
    const float tx = - (right + left)/(right - left);
    const float ty = - (top + bottom)/(top - bottom);
    const float tz = - (far + near)/(far - near);

    m.m[0] = 2.0f/(right-left);
    m.m[1] = 0;
    m.m[2] = 0;
    m.m[3] = tx;

    m.m[4] = 0;
    m.m[5] = 2.0f/(top-bottom);
    m.m[6] = 0;
    m.m[7] = ty;

    m.m[8] = 0;
    m.m[9] = 0;
    m.m[10] = -2.0/(far-near);
    m.m[11] = tz;

    m.m[12] = 0;
    m.m[13] = 0;
    m.m[14] = 0;
    m.m[15] = 1;
}

Vertex Shader :

uniform mat4 u_mvpMatrix;

attribute vec4 a_position;
attribute vec4 a_color;

varying vec4 v_color;

void main()
{
   gl_Position = u_mvpMatrix * a_position;
   v_color = a_color;
}

Client Code (parameters to the vertex shader):

float min = 0.0f;
float max = 1.0f;
const GLfloat squareVertices[] = {
    min, min,
    min, max,
    max, min,
    max, max
};
const GLfloat squareColors[] = {
    1, 1, 0, 1,
    0, 1, 1, 1,
    0, 0, 0, 1,
    1, 0, 1, 1,
};
Matrix4x4 proj;
SetOrtho(proj, 0.0f, 1.0f, 480.0/320.0, 0.0f, -1.0f, 1.0f );

The output i am getting in the iPhone simulator:

enter image description here

1
You have updated your question, but have actually seen and understood Tommy's answer?Christian Rau
@ChristianRau Yes, I've tried the transpose flag but it doesn't work. I am not applying any other matrix except the one mentioned above. I want to get the Ortho projection right, but it seems like I am getting perspective projection!kal21

1 Answers

1
votes

Your transcription of the glOrtho formula looks correct.

Your Matrix4x4 class is custom, but is it possible that m.m ends up being loaded directly as a glUniformMatrix4fv? If so check that you're setting the transpose flag as GL_TRUE, since you're loading data in row major format and OpenGL expects column major (ie, standard rules are that index [0] is the top of the first column, [3] is at the bottom of the first column, [4] is at the top of the second column, etc).

It's possibly also worth checking that —— assuming you've directly replicated the old world matrix stacks — you're applying modelview and projection in the correct order in your vertex shader or else compositing them correctly on the CPU, whichever way around you're doing it.