Saturday, 1 June 2013

How to create a projection matrix in OpenGL ES 2.0

How to create a projection matrix in OpenGL ES 2.0

So I have the following...
float s = 0.5f;

void renderFrameLine() {
  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  GLfloat vVertices[] = { s, s, 0.0f, s, -s, 0.0f, -s, s,
    0.0f};
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
  glEnableVertexAttribArray(0);
  glDrawArrays(GL_TRIANGLES, 0, 3);
}
This works great so now I want to add depth so I get ready by changing my vertex shader as following...
GLbyte vShaderStr[] =
"attribute vec4 vPosition;   \n"
"uniform mat4 Projection;   \n"
"void main()                 \n"
"{                           \n"
"     gl_Position = Projection * vPosition; \n"
"}                           \n";
And based on this code in GLM...
template <typename valType>
    GLM_FUNC_QUALIFIER detail::tmat4x4<valType> perspectiveFov
    (
            valType const & fov,
            valType const & width,
            valType const & height,
            valType const & zNear,
            valType const & zFar
    )
    {
#ifdef GLM_FORCE_RADIANS
            valType rad = fov;
#else
            valType rad = glm::radians(fov);
#endif
            valType h = glm::cos(valType(0.5) * rad) / glm::sin(valType(0.5) * rad);
            valType w = h * height / width; ///todo max(width , Height) / min(width , Height)?

            detail::tmat4x4<valType> Result(valType(0));
            Result[0][0] = w;
            Result[1][1] = h;
            Result[2][2] = - (zFar + zNear) / (zFar - zNear);
            Result[2][3] = - valType(1);
            Result[3][2] = - (valType(2) * zFar * zNear) / (zFar - zNear);
            return Result;
    }
I create the following function to create a perspective matrix...
void glFrustumf(float near, float far){
        float aspectRatio = .5;
        float DEG2RAD = 3.14159f / 180.0f;
        float fov = 90*DEG2RAD;
        float h = cosf(0.5f*fov)/sinf(0.5f*fov);
        float w = h * aspectRatio;
        float a =  - (near+far)/(near - far);
        float b = - ((2*far*near)/(far-near));

        float proj[16] = {
                        w, 0, 0, 0,
                        0, h, 0, 0,
                        0, 0, a, 1,
                        0, 0, b, 0
                    };
        GLint projectionUniform = glGetUniformLocation(programObject, "Projection");
        glUniformMatrix4fv(projectionUniform, 1, 0, &proj[0]);
}
This doesn't seem to work and instead returns a blank screen. Can anyone tell me what I am doing wrong?
Update
I also tried
void glFrustumf(float near, float far, float left, float right, float bottom, float top){
        float deltaX = right - left;
        float deltaY = top - bottom;
        float deltaZ = far - near;

        float a = 2.0f * near / deltaX;
        float b = 2.0f * near / deltaY;
        float c = (right + left) / deltaX;
        float d = (top + bottom) / deltaY;
        float e = -(near + far) / deltaZ;
        float f = -2.0f * near * far / deltaZ;

        float proj[16] = {
                        a, 0, 0, 0,
                        0, b, 0, 0,
                        c, d, e, -1.0f,
                        0, 0, f, 0
                    };
        GLint projectionUniform = glGet

No comments:

Post a Comment