0
votes

i'm quite new to opengl , and I'm trying to display a square with opengl es 2.0 with an orthographics projection ( on android using c++ and ndk ) , but all I get is a blank screen.

If i don't use the projection matrix I can get the square but it's stretched depending on surface's aspect ratio.

I'm building the matrix with the following code ( referring to https://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml ):

GLfloat projMat[16];

void ortho(float left, float right, float top, float bottom, float near, float far){

    float tx = ((right+left)/(right-left))*-1;
    float ty = ((top+bottom)/(top-bottom))*-1;
    float tz = ((far+near)/(far-near))*-1;

    projMat[0] = 2/right-left;
    projMat[1] = 0.0f;
    projMat[2] = 0.0f;
    projMat[3] = 0.0f;
    projMat[4] = 0.0f;
    projMat[5] = 2/top-bottom;
    projMat[6] = 0.0f;
    projMat[7] = 0.0f;
    projMat[8] = 0.0f;
    projMat[9] = 0.0f;
    projMat[10] = -2/far-near;
    projMat[11] = 0.0f;
    projMat[12] = tx;
    projMat[13] = ty;
    projMat[14] = tz;
    projMat[15] = 1.0f; }

And i call this function with : ortho(0.0,width,0.0, height,-1.0,1.0); , where width and height are surface's width and height.

My vertex shader :

attribute vec4 vPosition;
uniform mat4 projMatrix;
void main() {
    gl_Position = projMatrix * vPosition;
};

My fragment shader :

precision mediump float;
void main() {
    gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}

And my draw function :

    // 3D drawing
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(mProgram);
glUniformMatrix4fv(mPerspectivehandler, 1, GL_FALSE, projMat);
glEnableVertexAttribArray(mvPositionHandle);
glVertexAttribPointer(mvPositionHandle, 3, GL_FLOAT, GL_FALSE, 0, quadverts);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableVertexAttribArray(mvPositionHandle);

Do you know where's the mistake?

Edit : I added parentheses as suggested, but it still doesn't work

   GLfloat projMat[16];

void ortho(float left, float right, float top, float bottom, float near, float far){

    float tx = ((right+left)/(right-left))*-1;
    float ty = ((top+bottom)/(top-bottom))*-1;
    float tz = ((far+near)/(far-near))*-1;

    projMat[0] = 2/(right-left);
    projMat[1] = 0.0f;
    projMat[2] = 0.0f;
    projMat[3] = 0.0f;
    projMat[4] = 0.0f;
    projMat[5] = 2/(top-bottom);
    projMat[6] = 0.0f;
    projMat[7] = 0.0f;
    projMat[8] = 0.0f;
    projMat[9] = 0.0f;
    projMat[10] = -2/(far-near);
    projMat[11] = 0.0f;
    projMat[12] = tx;
    projMat[13] = ty;
    projMat[14] = tz;
    projMat[15] = 1.0f; }
3

3 Answers

2
votes

You're missing parentheses in the matrix calculation for the diagonal elements. They should be:

projMat[0] = 2.0f / (right - left);
projMat[5] = 2.0f / (top - bottom);
projMat[10] = -2.0f / (far - near);
0
votes

It can be hard to debug issues where nothing is rendered. You should simplify until you can isolate what is going wrong.

You've declared vPosition as a vec4, but in your comment, your vertex position array only has 12 floats (3 per vertex). a vec4 needs 4 floats per vertex. this is also a problem here:

glVertexAttribPointer(mvPositionHandle, 3, GL_FLOAT, GL_FALSE, 0, quadverts);

You use 3 as the second parameter in the line above, but vec4 in your shader means use 4 here. Be sure that quadVerts holds 4 floats per vertex!

To debug, don't use a projection matrix - just render your geometry directly to clip-coordinates in your shader like so:

attribute vec4 vPosition;
uniform mat4 projMatrix;
void main() {
    gl_Position = vPosition;
};

If the above shader renders (after you fix your vec4 problem), then you can start to debug your projection matrix.

Does your glClear() call actually take effect? try changing the color with glClearColor() and make sure the background color changes accordingly. If it doesn't, you might have set up your GL window incorrectly.

0
votes

This question is 4 months old and I don't know if you still need an aswer. I also had a lot of problems with orto projection matrices in OpenGL, but this works (for me):

result[0] = 2.0 / (right - left);
result[1] = 0.0;
result[2] = 0.0;
result[3] = 0.0;
result[4] = 0.0;
result[5] = 2.0 / (top - bottom);
result[6] = 0.0;
result[7] = 0.0;
result[8] = 0.0;
result[9] = 0.0;
result[10] = (1.0f / (near - far));
result[11] = 0.0;
result[12] = ((left + right) / (left - right));
result[13] = ((top + bottom) / (bottom - top));
result[14] = (near / (near - far));
result[15] = 1;