我一直致力于在iPhone平台上使用OpenGL es 2.0正确显示一些纹理四边形。我开始关注这里的教程
http://nehe.gamedev.net/tutorial/ios_lesson_02__first_triangle/50001/
这基本上只是制作一个简单的三角形,并使用非常简单的着色器显示在屏幕上。当我在Z轴上旋转三角形时会出现问题。
这是traingle在没有应用任何旋转的情况下看起来的样子,并且在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建议---------------------- -------
所以我编辑了代码以进行orhtographic投影。 首先我将三角形更改为矩形。这是顶点设置
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个链接
请帮助并感谢您提供的建议和帮助。
答案 0 :(得分:0)
问题是你的左下角有逻辑坐标是{-1,-1},右上角是{1,1},那是方形的,但屏幕不是正方形,这就是为什么你有一个失真,所以你有通过设置正确的投影矩阵来调整它。你可以用GLKMatrix4MakeOrtho来计算它。