gpt4 book ai didi

android - glOrtho OpenGL es 2.0 变体如何修复空白屏幕?

转载 作者:行者123 更新时间:2023-11-30 14:26:06 26 4
gpt4 key购买 nike

我尝试为 Android 重新制作 OpenGL ES 2.0 示例 http://developer.android.com/resources/tutorials/opengl/opengl-es20.htmlAndroid 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;
}

fragment 着色器

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

附注我认为我将错误的东西传递给了 ortho_matrix,因为 opengl es 有 (-1,-1,-1) - (1,1,1) 坐标系。

最佳答案

一方面,你的三角形似乎被近平面剪切了。您的默认 View 位于 0,0,0 处,从 -z 轴往下看,您的三角形位于 z 轴上的位置 0,并且您的近平面为 1。

要么将三角形沿负 z 轴移动(对所有顶点尝试 -5),要么向后移动近平面(如果您愿意,也可以将其设为负数,尝试 -10 到 10)。

关于android - glOrtho OpenGL es 2.0 变体如何修复空白屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9617467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com