How to achieve glOrthof in OpenGL ES 2.0

泄露秘密 提交于 2019-12-04 20:07:56

问题


I am trying to convert my OpenGL ES 1 application to a OpenGL ES 2 application to be able to use Shaders. 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(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?

If not, does anyone have a link to a good OpenGL ES 1 to OpenGL ES 2 tutorial/explanation?


回答1:


The glOrtho method does nothing else than create a new matrix and multiply the current projection matrix by this matrix. With OpenGL ES 2.0 you have to manage matrices yourself. 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. 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:

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.




回答2:


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. In my answer here, I provided the following Objective-C method for generating such an orthographic projection matrix:

- (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).



来源:https://stackoverflow.com/questions/7317119/how-to-achieve-glorthof-in-opengl-es-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!