繁体   English   中英

如何在OpenGL ES 2.0中实现glOrthof

[英]How to achieve glOrthof in OpenGL ES 2.0

我试图将我的OpenGL ES 1应用程序转换为OpenGL ES 2应用程序,以便能够使用着色器。 现在我使用glOrthof函数来获得“真实大小的视口”,这样我就可以将顶点放在OpenGL视图中的“实际”像素上。

glOrthof(0, _frame.size.width, _frame.size.height, 0, -1, 1);

我无法在OpenGL ES 2找到如何实现这一点,是否有人可以告诉我如何做到这一点?

如果没有,有没有人有一个良好的OpenGL ES 1 to OpenGL ES 2教程/解释的链接?

glOrtho方法除了创建一个新矩阵并将当前投影矩阵乘以该矩阵之外别无其他。 使用OpenGL ES 2.0,您必须自己管理矩阵。 为了复制glOrtho行为,您需要在顶点着色器中为投影矩阵提供一个统一,然后将顶点与顶点相乘。 通常你也有一个模型和一个视图矩阵(或一个组合的模型视图矩阵,就像在OpenGL ES 1中一样),你可以在投影变换之前转换你的顶点:

uniform mat4 projection;
uniform mat4 modelview;

attribute vec4 vertex;

void main()
{
    gl_Position = projection * (modelview * vertex);
}

glOrtho构造的特定投影矩阵可以在这里找到。

正如Christian所描述的那样,处理顶点的所有矩阵数学都取决于你,所以你必须复制glOrthof()创建的矩阵。 在我的答案这里 ,我用于生成这样的正投影矩阵提供了以下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;
}

这里使用的矩阵定义为

GLfloat orthographicMatrix[16];

然后我使用以下内容在我的顶点着色器中应用矩阵:

gl_Position = modelViewProjMatrix * position * orthographicMatrix;

我的乘法顺序与Christian的不同,所以我可能会在这里做一些落后的事情,但这是我在我的OpenGL ES 2.0应用程序中处理这个问题(其源代码可以在这里找到)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM