简体   繁体   English

如何在OpenGL ES 2.0中实现glOrthof

[英]How to achieve glOrthof in OpenGL ES 2.0

I am trying to convert my OpenGL ES 1 application to a OpenGL ES 2 application to be able to use Shaders. 我试图将我的OpenGL ES 1应用程序转换为OpenGL ES 2应用程序,以便能够使用着色器。 Now I use the glOrthof function to have a "real sized viewport" so I can place the vertices at the "actual" pixel in the OpenGL View. 现在我使用glOrthof函数来获得“真实大小的视口”,这样我就可以将顶点放在OpenGL视图中的“实际”像素上。

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

I am having trouble finding out how to achieve this in OpenGL ES 2 , is there anyone who can show me how to do this? 我无法在OpenGL ES 2找到如何实现这一点,是否有人可以告诉我如何做到这一点?

If not, does anyone have a link to a good OpenGL ES 1 to OpenGL ES 2 tutorial/explanation? 如果没有,有没有人有一个良好的OpenGL ES 1 to OpenGL ES 2教程/解释的链接?

The glOrtho method does nothing else than create a new matrix and multiply the current projection matrix by this matrix. glOrtho方法除了创建一个新矩阵并将当前投影矩阵乘以该矩阵之外别无其他。 With OpenGL ES 2.0 you have to manage matrices yourself. 使用OpenGL ES 2.0,您必须自己管理矩阵。 In order to replicate the glOrtho behaviour you need a uniform for the projection matrix in your vertex shader, which you then multiply your vertices with. 为了复制glOrtho行为,您需要在顶点着色器中为投影矩阵提供一个统一,然后将顶点与顶点相乘。 Usually you also have a model and a view matrix (or a combined modelview matrix, like in OpenGL ES 1) which you transform your vertices with before the projection transform: 通常你也有一个模型和一个视图矩阵(或一个组合的模型视图矩阵,就像在OpenGL ES 1中一样),你可以在投影变换之前转换你的顶点:

uniform mat4 projection;
uniform mat4 modelview;

attribute vec4 vertex;

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

The specific projection matrix that glOrtho constructs can be found here . glOrtho构造的特定投影矩阵可以在这里找到。

As Christian describes, all of the matrix math for processing your vertices is up to you, so you have to replicate the matrix that glOrthof() creates. 正如Christian所描述的那样,处理顶点的所有矩阵数学都取决于你,所以你必须复制glOrthof()创建的矩阵。 In my answer here , I provided the following Objective-C method for generating such an orthographic projection matrix: 在我的答案这里 ,我用于生成这样的正投影矩阵提供了以下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;
}

The matrix used here is defined as 这里使用的矩阵定义为

GLfloat orthographicMatrix[16];

I then apply the matrix within my vertex shader using something like the following: 然后我使用以下内容在我的顶点着色器中应用矩阵:

gl_Position = modelViewProjMatrix * position * orthographicMatrix;

My multiplication order differs from Christian's, so I may be doing something a little backward here, but this is what I've used to handle this within an OpenGL ES 2.0 application of mine (the source code of which can be found here ). 我的乘法顺序与Christian的不同,所以我可能会在这里做一些落后的事情,但这是我在我的OpenGL ES 2.0应用程序中处理这个问题(其源代码可以在这里找到)。

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

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