0
\$\begingroup\$

So I am very new to OpenGL out I am playing around and trying to create a rectangle.

GLfloat vVertices[] = { -0.5f, -0.5f, 0.0f, -0.5f, 0.5f, 0.0f, 0.5f, -0.5f,
                0.0f,0.5f,0.5f,0.0f};
GLfloat colors[] = {
            1.0f, 0.0f, 0.0f, 1.0f,
            1.0f, 0.0f, 0.0f, 1.0f,
            1.0f, 0.0f, 0.0f, 1.0f,
            1.0f, 0.0f, 0.0f, 1.0f,
            1.0f, 0.0f, 0.0f, 1.0f};
glClear(GL_COLOR_BUFFER_BIT);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, colors);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

But when I try to make the top further back than the bottom by increasing the z of 2 and 4....

GLfloat vVertices[] = { -0.5f, -0.5f, 0.0f, -0.5f, 0.5f, 1.0f, 0.5f, -0.5f,
                0.0f,0.5f,0.5f,1.0f};

I would expect the top to be larger than the bottom because it would be closer. This isn't what happens, instead it just looks like a regular old square. What am I missing here? From a little research online it looks like I might need to setup a frustum, however, I am not sure how to do that on GLES2. I tried the following...

glDepthRangef(-2.0f,2.0f);

But that didn't help either.

Update

Here is my function to set my projection ortho...

void glOrthof(float left, float right, float bottom, float top, float near, float far){
        LOGD("In glOrtho");
        float a = 2.0f / (right - left);
        float b = 2.0f / (top - bottom);
        float c = -2.0f / (far - near);

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

        float ortho[16] = {
            a, 0, 0, 0,
            0, b, 0, 0,
            0, 0, c, 0,
            tx, ty, tz, 1
        };

        GLint loc1= glGetUniformLocation(programObject, "project");
        glUniform1i(loc1, 1.0f);
        GLint projectionUniform = glGetUniformLocation(programObject, "Projection");
        glUniformMatrix4fv(projectionUniform, 1, 0, &ortho[0]);
        LOGD("In finished glOrtho");
}

Vertex Shader...

 GLbyte vShaderStr[] =
    "attribute vec4 vPosition;   \n"
    "uniform mat4 Projection;   \n"
    "uniform vec4 color;   \n"
    "attribute vec4 inColor;   \n"
    "varying vec4 fragColor;   \n"
    "uniform int project;  \n"
    "void main()                 \n"
    "{                           \n"
    "   if(project == 0){ \n"
    "     gl_Position = vPosition; \n"
    "   } \n"
    "   else{ \n"
    "     gl_Position = Projection * vPosition; \n"
    "   } \n"
    "   fragColor = inColor; \n"
    "}                           \n";

And finally my call

glOrthof(0, widthScreen, heightScreen, 0, 2.0, -2.0);

UPDATE 2

per here I tried the following vertex shader...

GLbyte vShaderStr[] =
    "attribute vec4 vPosition;   \n"
    "uniform mat4 Projection;   \n"
    "uniform mat4 Projection2;   \n"
    "uniform vec4 color;   \n"
    "attribute vec4 inColor;   \n"
    "varying vec4 fragColor;   \n"
    "uniform int project;  \n"
    "void main()                 \n"
    "{                           \n"
    "   if(project == 0){ \n"
    "     gl_Position = vPosition; \n"
    "   } \n"
    "   else{ \n"
    "     gl_Position = Projection2 * (Projection * vPosition); \n"
    "   } \n"
    "   fragColor = inColor; \n"
    "}                           \n";

And the following additional function...

void glFrustumf(float near, float far){
        LOGD("In glOrtho");
        float angleOfView = 180.0;
        float aspectRatio = 1;

        // Some calculus before the formula.
        float size = near * tanf(degree2Radian(angleOfView) / 2.0);
        float left = -size, right = size, bottom = -size / aspectRatio, top = size / aspectRatio;
        float a = 2.0f * near / (right - left);
        float b = 2.0f *near / (top - bottom);
        float c = (-2.0f*far*near) / (far - near);

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

        float ortho[16] = {
            a, 0, 0, 0,
            0, b, 0, 0,
            tx, ty, tz, -1,
            0, 0, c, 0
        };

        GLint loc1= glGetUniformLocation(programObject, "project");
        glUniform1i(loc1, 1.0f);
        GLint projectionUniform = glGetUniformLocation(programObject, "Projection2");
        glUniformMatrix4fv(projectionUniform, 1, 0, &ortho[0]);
        LOGD("In finished glOrtho");
}

But nothing shows up when I add the new matrix.

\$\endgroup\$
6
  • \$\begingroup\$ What kind of projection are you using? If it's orthographic, this would be expected (depending on where the camera is, too, of course). \$\endgroup\$
    – notlesh
    May 29, 2013 at 15:39
  • \$\begingroup\$ After reading your comment about the frustum, I'm guessing you've not set up a proper projection. I'd suggest you stick with OpenGL ES 1.1 or non-ES for a while until you get the basics down. OpenGL ES 2 requires you to do a LOT of things yourself that other versions don't, and it effectively makes the learning curve a lot steeper. \$\endgroup\$
    – notlesh
    May 29, 2013 at 15:42
  • \$\begingroup\$ I appreciate your advice, and I agree that it is very difficult but I would rather learn it this way. I also agree that it is probably a projection issue, I am looking for some sort of example of how to set up the vertex shader properly to handle that. I can add my existing vertex shader if that would help, but a generic idea of the matrix values I would need to multiply by would suffice. \$\endgroup\$
    – Jackie
    May 29, 2013 at 15:45
  • 1
    \$\begingroup\$ looks like some good results if you google "perspective opengl es 2" \$\endgroup\$
    – notlesh
    May 29, 2013 at 15:58
  • \$\begingroup\$ I'm not really understanding those posts can you point me to the one you thinks best describes the situation? \$\endgroup\$
    – Jackie
    May 29, 2013 at 16:53

1 Answer 1

1
\$\begingroup\$

You're using an orthographic projection matrix, which does not do perspective (things getting smaller in the distance). You need to use some kind of perspective projection matrix.

Libraries like GLM are intended to make this easy.

Here is an explanation of what is going on mathematically.

\$\endgroup\$
3
  • \$\begingroup\$ Did you see my second method? Here I tried to use projection other than Ortho but don't see anything show up... \$\endgroup\$
    – Jackie
    May 29, 2013 at 18:23
  • \$\begingroup\$ Are you sure you're generating it right? I'm not going to debug your math code for you. Use glm to generate your matrices. Also, why in your code are you multiplying by both Projection and Projection2. Why are you using the unitialized Projection still, and why make a whole separate variable in the first place? That's probably why you're seeing nothing; Projection is likely just a zero matrix you're multiplying by. You only need a single shader, just change how you set Projection. \$\endgroup\$ May 29, 2013 at 18:57
  • \$\begingroup\$ I was playing around, and I think I accidentally posted a playing around version of my code. I will go over the documentation and my math again, I will also look into GLM. \$\endgroup\$
    – Jackie
    May 29, 2013 at 19:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .