Opengl es 如何在OpenGL ES 2.0中将世界坐标转换为屏幕坐标

Opengl es 如何在OpenGL ES 2.0中将世界坐标转换为屏幕坐标,opengl-es,opengl-es-2.0,Opengl Es,Opengl Es 2.0,我使用以下OpenGLES1.x代码来设置投影坐标 glMatrixMode(GL_PROJECTION); float width = 320; float height = 480; glOrthof(0.0, // Left 1.0, // Right height / width, // Bottom 0.0, //

我使用以下OpenGLES1.x代码来设置投影坐标

glMatrixMode(GL_PROJECTION); 

float width = 320;
float height = 480;

glOrthof(0.0,                  // Left
         1.0,                  // Right
         height / width,       // Bottom
         0.0,                  // Top
         -1.0,                 // Near
         1.0);                 // Far
glMatrixMode(GL_MODELVIEW);
在OpenGL ES 2.0中设置此功能的等效方法是什么? 我应该传递给顶点着色器的投影矩阵是什么

我尝试了以下函数来创建矩阵,但它不起作用:

void SetOrtho (Matrix4x4& m, float left, float right, float bottom, float top, float near, 
float far)
{
    const float tx = - (right + left)/(right - left);
    const float ty = - (top + bottom)/(top - bottom);
    const float tz = - (far + near)/(far - near);

    m.m[0] = 2.0f/(right-left);
    m.m[1] = 0;
    m.m[2] = 0;
    m.m[3] = tx;

    m.m[4] = 0;
    m.m[5] = 2.0f/(top-bottom);
    m.m[6] = 0;
    m.m[7] = ty;

    m.m[8] = 0;
    m.m[9] = 0;
    m.m[10] = -2.0/(far-near);
    m.m[11] = tz;

    m.m[12] = 0;
    m.m[13] = 0;
    m.m[14] = 0;
    m.m[15] = 1;
}
顶点着色器:

uniform mat4 u_mvpMatrix;

attribute vec4 a_position;
attribute vec4 a_color;

varying vec4 v_color;

void main()
{
   gl_Position = u_mvpMatrix * a_position;
   v_color = a_color;
}
客户端代码(顶点着色器的参数):

我在iPhone模拟器中得到的输出:


您对
glOrtho
公式的转录看起来是正确的

您的Matrix4x4类是自定义的,但是m.m是否可能最终直接作为
glUniformMatrix4fv加载?如果是这样,请检查您是否将转置标志设置为GL_TRUE,因为您正在以行主格式加载数据,而OpenGL需要列主格式(即,标准规则是索引[0]位于第一列的顶部,[3]位于第一列的底部,[4]位于第二列的顶部,等等)


也可能值得检查一下——假设您已经直接复制了旧世界的矩阵堆栈——您正在顶点着色器中以正确的顺序应用modelview和projection,或者在CPU上正确地合成它们,无论您使用哪种方法。

您已经更新了您的问题,但是你真的看到并理解了汤米的答案吗?@ChristianRau是的,我试过转置标志,但它不起作用。除上述矩阵外,我不应用任何其他矩阵。我想得到正确的正射投影,但似乎我得到的是透视投影!它传递的转置标志为GL_FALSE,但当我将其设置为GL_TRUE时,四元组消失了!建议和代码是正确的。我在一个新的项目中尝试了同样的方法,效果很好。谢谢
float min = 0.0f;
float max = 1.0f;
const GLfloat squareVertices[] = {
    min, min,
    min, max,
    max, min,
    max, max
};
const GLfloat squareColors[] = {
    1, 1, 0, 1,
    0, 1, 1, 1,
    0, 0, 0, 1,
    1, 0, 1, 1,
};
Matrix4x4 proj;
SetOrtho(proj, 0.0f, 1.0f, 480.0/320.0, 0.0f, -1.0f, 1.0f );