glOrtho OpenGL ES 2.0的变种,如何解决黑屏? [英] glOrtho OpenGL es 2.0 variant how fix blank screen?
问题描述
我尝试改造为Android HTTP与OpenGL ES 2.0的例子:/ /developer.android.com/resources/tutorials/opengl/opengl-es20.html
到Android NDK OpenGL ES 2.0的例子。结果
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.
我succeded在所有除作出正交/透视投影
I succeded in all except making a orthographic/perspective projection
所以,我画一个三角形
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)坐标系
P.S. i think i pass wrong things to ortho_matrix, cos opengl es has (-1,-1,-1) - (1,1,1) coordinate system.
推荐答案
一件事你三角形似乎是由近平面被裁剪。默认视图是0,0,0低头-Z轴方向,你的三角形Z轴位置0,和你有一个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.
无论是移动你的向下三角形负z轴(-5尝试为所有顶点),或移动您的近平面向后(你可以把它的负面,如果你想为好,尽量-10到10)。
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).
这篇关于glOrtho OpenGL ES 2.0的变种,如何解决黑屏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!