Graphics OpenGl es 2.0二维平面旋转

Graphics OpenGl es 2.0二维平面旋转,graphics,opengl-es,rotation,2d,projection,Graphics,Opengl Es,Rotation,2d,Projection,我一直在努力使用OpenGL es 2.0在iPhone平台上正确显示一些具有纹理的四边形。我从这里的教程开始 它基本上只是制作一个简单的三角形,并使用非常简单的着色器显示在屏幕上。当我在Z轴上旋转三角形时,问题就出现了 这是在不应用任何旋转且Z轴旋转90度的情况下traingle的外观 我使用了NEHE教程站点的源代码项目,我所做的唯一更改是在顶点着色器中添加世界矩阵 //the incoming vertex' position attribute vec4 position; //a

我一直在努力使用OpenGL es 2.0在iPhone平台上正确显示一些具有纹理的四边形。我从这里的教程开始

它基本上只是制作一个简单的三角形,并使用非常简单的着色器显示在屏幕上。当我在Z轴上旋转三角形时,问题就出现了

这是在不应用任何旋转且Z轴旋转90度的情况下traingle的外观

我使用了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到屏幕高度

-------------------q_____;b建议后的迭代2-----------------------------

因此,我编辑了代码,使其具有图形投影。 首先我把三角形改成了矩形。这是顶点设置

  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个链接


请帮助并感谢您迄今为止的建议和帮助。

问题是您的左下角的逻辑坐标为{-1,-1},右上角的逻辑坐标为{1,1},即正方形,但屏幕不是正方形,这就是您产生失真的原因,因此您必须通过设置正确的投影矩阵来调整。您可以使用GLKMatrix4MakeOrtho对您的案例进行计算。

问题是什么?不是你写的。若问题是三角形纵横比,那个么你们的矩阵有问题。对不起,我现在意识到我并没有提到这个问题。当三角形旋转时,它会被压扁以适应屏幕,而不是保持不变的大小。你如何旋转它?看起来您没有使用旋转矩阵,而是执行其他操作,因为调用GLKMatrix4MakeRotation的所有系数都是0和1。我在将旋转设置为0后粘贴了代码以拍摄屏幕截图。我现在已经修好了。我正在使用GLKmatrix4MakeRotation进行旋转,您如何设置视图?很明显,您在纵横比方面存在问题。很抱歉,我正在尝试使用GLKMatrix4MakeOrtho。我过一会儿再发代码。我已经在我原来的项目中尝试过了,但是我得到了同样的结果。我已经用实现编辑了原来的问题。请检查并帮助我,为什么你认为你面临着同样的问题,矩形是一样的,只是按照你想要的旋转。在我看来,它们肯定是压扁了。我会把纹理放在矩形上,这会给我们一个更好的主意。我一到家就会发布更新的屏幕截图。请再看一看。谢谢。我甚至把它复制粘贴到图像编辑器中检查,它们是一样的。这一次只是一个幻觉。