我已经在 OpenGL 中完成了编程,并且我知道如何在其中设置可视区域gluOrtho()
,但是在 OpenGL ES 2.0 中不存在这样的功能。
我将如何在 OpenGL ES 2.0 中执行此操作?
PS:我正在使用 PowerVR SDK 模拟器在 Ubuntu 10.10 中进行 OpenGL ES 2.0 开发。
我已经在 OpenGL 中完成了编程,并且我知道如何在其中设置可视区域gluOrtho()
,但是在 OpenGL ES 2.0 中不存在这样的功能。
我将如何在 OpenGL ES 2.0 中执行此操作?
PS:我正在使用 PowerVR SDK 模拟器在 Ubuntu 10.10 中进行 OpenGL ES 2.0 开发。
正如 Nicol 建议的那样,您需要设置一个正交投影矩阵。例如,我用来执行此操作的 Objective-C 方法如下:
- (void)loadOrthoMatrix:(GLfloat *)matrix left:(GLfloat)left right:(GLfloat)right bottom:(GLfloat)bottom top:(GLfloat)top near:(GLfloat)near far:(GLfloat)far;
{
GLfloat r_l = right - left;
GLfloat t_b = top - bottom;
GLfloat f_n = far - near;
GLfloat tx = - (right + left) / (right - left);
GLfloat ty = - (top + bottom) / (top - bottom);
GLfloat tz = - (far + near) / (far - near);
matrix[0] = 2.0f / r_l;
matrix[1] = 0.0f;
matrix[2] = 0.0f;
matrix[3] = tx;
matrix[4] = 0.0f;
matrix[5] = 2.0f / t_b;
matrix[6] = 0.0f;
matrix[7] = ty;
matrix[8] = 0.0f;
matrix[9] = 0.0f;
matrix[10] = 2.0f / f_n;
matrix[11] = tz;
matrix[12] = 0.0f;
matrix[13] = 0.0f;
matrix[14] = 0.0f;
matrix[15] = 1.0f;
}
即使你不熟悉 Objective-C 方法的语法,这段代码的 C 主体也应该很容易理解。矩阵定义为
GLfloat orthographicMatrix[16];
然后,您将在顶点着色器中应用它来调整顶点的位置,使用如下代码:
gl_Position = modelViewProjMatrix * position * orthographicMatrix;
基于此,您应该能够设置显示空间的各种限制以适应您的几何图形。
没有调用函数gluOrtho
。There isgluOrtho2D
和 there is glOrtho
,两者都做非常相似的事情。但他们都没有设置视口。
OpenGL 管道的视口变换由glViewport
和控制glDepthRange
。你在说的是一个正交投影矩阵,它是glOrtho
计算gluOrtho2D
的。
OpenGL ES 2.0 没有桌面 OpenGL pre-3.1 的许多固定功能便利。因此,您必须自己创建它们。正交矩阵的创建非常容易;glOrtho和gluOrtho2D的文档都说明了它们如何创建矩阵。
您需要通过着色器制服将此矩阵传递给您的着色器。然后,您将需要使用此矩阵从眼睛空间转换顶点位置(以眼睛位置为原点,+X 向右,+Y 向上,+Z 朝向眼睛)。
您还可以使用 GLkit 中定义的以下便捷方法。
GLKMatrix4 GLKMatrix4MakeOrtho (
float left,
float right,
float bottom,
float top,
float nearZ,
float farZ
);
只需将 (-1, 1) 传递给 Z 值。