2011-08-20 5 views
5

Estoy tratando de implementar mi propia función glOtho de los documentos opengles http://www.khronos.org/opengles/documentation/opengles1_0/html/glOrtho.html
para modificar una matriz de proyección en mi sombreador de vértices. Actualmente no funciona correctamente, ya que veo mis vértices de triángulos simples fuera de lugar. ¿Puedes verificar el código a continuación y ver si estoy haciendo las cosas mal?¿Cómo se implementa glOrtho para opengles 2.0? Con o sin valores tx, ty, tz de la especificación glOrtho?

He intentado establecer tx, ty tz en 0 y eso parece hacer que se visualice correctamente. ¿Alguna idea de por qué sería esto así?

void ES2Renderer::_applyOrtho(float left, float right,float bottom, float top,float near, float far) const{ 

     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, tx, 
      0, b, 0, ty, 
      0, 0, c, tz, 
      0, 0, 0, 1 
     }; 


     GLint projectionUniform = glGetUniformLocation(_shaderProgram, "Projection"); 
     glUniformMatrix4fv(projectionUniform, 1, 0, &ortho[0]); 

} 

void ES2Renderer::_renderScene()const{ 
    GLfloat vVertices[] = { 
     0.0f, 5.0f, 0.0f, 
     -5.0f, -5.0f, 0.0f, 
     5.0f, -5.0f, 0.0f}; 

    GLuint positionAttribute = glGetAttribLocation(_shaderProgram, "Position"); 

    glEnableVertexAttribArray(positionAttribute); 


    glVertexAttribPointer(positionAttribute, 3, GL_FLOAT, GL_FALSE, 0, vVertices); 
    glDrawArrays(GL_TRIANGLES, 0, 3);  


    glDisableVertexAttribArray(positionAttribute); 

} 

Vert shader

attribute vec4 Position; 
uniform mat4 Projection; 
void main(void){ 
    gl_Position = Projection*Position; 
} 

Solución De respuesta Nicols continuación me Modifed mi matriz como tal y parecía representan correctamente

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

Nota importante No se puede utilizar GL_TRUE para el argumento de transposición para glUniform como abajo. opengles no lo admite. que debe ser GL_FALSE

glUniformMatrix4fv(projectionUniform, 1, GL_TRUE, &ortho[0]); 

De http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml

transponer Especifica si de transponer la matriz como los valores se cargan en la variable uniforme. Debe ser GL_FALSE.

Respuesta

4

Las matrices en OpenGL son columnas principales. A menos que pase GL_TRUE como el tercer parámetro para glUniformMatrix4fv, la matriz se transpondrá efectivamente en relación con lo que se pretende.

+0

gracias. ahora recuerdo haber escuchado eso una vez en uni =). la configuración de GL_TRUE no funcionó, pero mover los valores de tx, ty, tz a la parte inferior de la matriz fue – valmo

+0

Los uniformes de matriz de transposición automática no son compatibles con OpenGL ES 2.0; debe [siempre pasar GL_FALSE] (https: //www.khronos. org/opengles/sdk/docs/man/xhtml/glUniform.xml) como el parámetro de transposición de las funciones glUniformMatrix. –

Cuestiones relacionadas