我一直在努力让一些带有纹理的四边形在 iPhone 平台上使用 OpenGL es 2.0 正确显示。我从这里的教程开始
http://nehe.gamedev.net/tutorial/ios_lesson_02__first_triangle/50001/
它基本上只是制作一个简单的三角形并使用非常简单的着色器显示到屏幕上。当我在 Z 轴上旋转三角形时会出现问题。
这就是在没有应用任何旋转且在 Z 轴上旋转 90 度时三角形的外观。
我使用了 NEHE 教程站点的源代码项目,我所做的唯一更改是在顶点着色器中添加了一个世界矩阵
//the incoming vertex' position
attribute vec4 position;
//and its color
attribute vec3 color;
//the varying statement tells the shader pipeline that this variable
//has to be passed on to the next stage (so the fragment shader)
varying lowp vec3 colorVarying;
//added this matrix
uniform mat4 world;
//the shader entry point is the main method
void main()
{
colorVarying = color; //save the color for the fragment shader
gl_Position = world * position; //multiplied the matrix with the position
}
刚刚输出的像素着色器放置颜色。
在程序中已经建立并传递了世界矩阵如下
GLKMatrix4 _world = GLKMatrix4MakeRotation(1.5708, 0, 0, 1);
glUniformMatrix4fv(m_worldMat, 1, GL_FALSE, _world.m);
这是我可以为这个问题复制问题的最简单方法。我还尝试创建一个从 0 到屏幕宽度和从 0 到屏幕高度的正交相机。
--------------------迭代 2 在 q______b 的建议之后------------------------- ----
所以我编辑了代码以进行正交投影。首先,我将三角形更改为矩形。这是顶点设置
right = 0.75 * m_renderbufferWidth;
left = 0.25 * m_renderbufferWidth;
top = 0.55 * m_renderbufferHeight;
bottom = 0.45 * m_renderbufferHeight;
widthOver2 = (right - left) * 0.5;
heightOver2 = (bottom - top) * 0.5;
//push the vertex data into the buffer
//4 floats define one vertex (x, y, z and w), first one is lower left
geometryData.push_back(-widthOver2); geometryData.push_back(heightOver2 ); geometryData.push_back(1.0); geometryData.push_back(1.0);
//we go counter clockwise, so lower right vertex next
geometryData.push_back(widthOver2); geometryData.push_back(heightOver2); geometryData.push_back(1.0); geometryData.push_back(1.0);
//top left vertex is last
geometryData.push_back(-widthOver2); geometryData.push_back(-heightOver2); geometryData.push_back(1.0); geometryData.push_back(1.0);
//top right vertex is last
geometryData.push_back(widthOver2); geometryData.push_back(-heightOver2); geometryData.push_back(1.0); geometryData.push_back(1.0);
这是更改后的绘图代码。我现在让正交相机从 0、0 变为屏幕尺寸,我在原点周围创建了矩形并平移到屏幕中间。
GLKMatrix4 temp = GLKMatrix4Identity;
GLKMatrix4 _world = GLKMatrix4Identity;
temp = GLKMatrix4Multiply(GLKMatrix4MakeTranslation(left + widthOver2, top + heightOver2, 0), GLKMatrix4MakeRotation(1.5708, 0, 0, 1));
_world = GLKMatrix4Multiply(GLKMatrix4MakeScale(1, 1, 1), temp);
glUniformMatrix4fv(m_worldMat, 1, GL_FALSE, _world.m);
GLKMatrix4 _projection = GLKMatrix4MakeOrtho(0, m_renderbufferWidth, 0, m_renderbufferHeight , -100, 100);
glUniformMatrix4fv(m_projectionMat, 1, GL_FALSE, _projection.m);
//initiate the drawing process, we want a triangle, start at index 0 and draw 3 vertices
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
这是顶点着色器部分。
void main()
{
colorVarying = color; //save the color for the fragment shader
gl_Position = projection * world * position; //multiplied the matrix with the position
}
我仍然面临同样的问题。我已经用最新的截图更新了原始帖子的链接,因为我没有足够的代表来发布超过 2 个链接
请帮助并感谢您迄今为止的建议和帮助。