glOrtho OpenGL es 2.0变种如何修复空白屏幕?

时间:2012-03-08 12:12:20

标签: android c opengl-es opengl-es-2.0

我尝试为Android http://developer.android.com/resources/tutorials/opengl/opengl-es20.html重新制作一个OpenGL ES 2.0示例 到Android NDK OpenGL ES 2.0示例。

除了制作正交/透视投影外,我取得了所有成功

所以我绘制一个三角形

typedef matrix float matrix[16];

matrix MVP_MATRIX; /* MODEL*VIEW*PROJECTION MATRIX */
matrix PROJECTION_MATRIX;
matrix MODEL_MATRIX;
matrix VIEW_MATRIX;

s_shader shader; /* just struct for holding shader program data (attribytes,uniforms, fragment shader, vertex shader etc)*/

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

   onInit(float screen_width, float screen_height)
   {
   /*shader load*/
   matrix MV_MATRIX;
   identity_matrix(MODEL_MATRIX); //any transitions/rotations works fine
   identity_matrix(VIEW_MATRIX);
   multiply_matrix(VIEW_MATRIX, MODEL_MATRIX, MV_MATRIX);
   glViewport(0, 0, screen_width, screen_height);
   ortho_matrix(0.0,screen_width,0.0,screen_height,1.0,10.0, PROJECTION_MATRIX);
   multiply_matrix(MV_MATRIX, PROJECTION_MATRIX , MVP_MATRIX);
   }


  onDraw()
  {
  glClear(GL_COLOR_BUFFER_BIT);
  glUseProgram(shader.program /* where i hold a compiled shader program */ );
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, triangle_vertices);
  glUniformMatrix4fv(shader.uniforms[0]/*"umWorld" uniform location*/, 1,   GL_FALSE, (const GLfloat*) MVP_MATRIX/*Well get blank screen, if change to MV_MATRIX everything is fine, so projection matrix is problem*/);
  glEnableVertexAttribArray(shader.atributes[0]/*"vPosition" attribute location*/);
  glDrawArrays(GL_TRIANGLES, 0, 3);
  }

void ortho_matrix(float left, float right, float bottom, float top, float near,
float  far, matrix result)
{
result[0] = 2.0 / (right - left);
result[1] = 0.0;
result[2] = 0.0;
result[3] = 0.0;

//Second Column
result[4] = 0.0;
result[5] = 2.0 / (top - bottom);
result[6] = 0.0;
result[7] = 0.0;

//Third Column
result[8] = 0.0;
result[9] = 0.0;
result[10] = -2.0 / (far - near);
result[11] = 0.0;

//Fourth Column
result[12] = -(right + left) / (right - left);
result[13] = -(top + bottom) / (top - bottom);
result[14] = -(far + near) / (far - near);
result[15] = 1;
}

顶点着色器

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

片段着色器

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

P.S。我想我把错误的东西传给了ortho_matrix,cos opengl es有(-1,-1,-1) - (1,1,1)坐标系。

1 个答案:

答案 0 :(得分:2)

一方面,你的三角形似乎被近平面夹住了。您的默认视图为0,0,0,向下看-z轴,您的三角形位于z轴的0位置,并且您的近平面为1.

将三角形向下移动到负z轴(对所有顶点都尝试-5),或向后移动近平面(如果需要,可以将其设为负值,尝试-10到10)。