glOrtho OpenGL es 2.0 variant how fix blank screen

2019-03-06 04:34发布

问题:

I try to remake a OpenGL ES 2.0 example for Android http://developer.android.com/resources/tutorials/opengl/opengl-es20.html to Android NDK OpenGL ES 2.0 example.

I succeded in all except making a orthographic/perspective projection

So i draw a triangle

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;
}

Vertex shader

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

Fragment shader

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

P.S. i think i pass wrong things to ortho_matrix, cos opengl es has (-1,-1,-1) - (1,1,1) coordinate system.

回答1:

For one thing your triangle appears to be clipped by the near plane. Your default view is at 0,0,0 looking down the -z axis, and your triangle is on the z axis at position 0, and you have a near plane of 1.

Either move your triangle down the negative z axis (try -5 for all vertices), or move your near plane backwards (you can make it negative if you want as well, try -10 to 10).