I have done programming in OpenGL and I know how to set the viewable area in it with gluOrtho()
, but a function like this does not exist in OpenGL ES 2.0.
How would I do this in OpenGL ES 2.0?
P.S : I am doing my OpenGL ES 2.0 development in Ubuntu 10.10 with the PowerVR SDK emulator.
As Nicol suggests, you'll want to set up an orthographic projection matrix. For example, an Objective-C method I use to do this is as follows:
- (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;
}
Even if you're not familiar with Objective-C method syntax, the C body of this code should be easy to follow. The matrix is defined as
GLfloat orthographicMatrix[16];
You would then apply this within your vertex shader to adjust the locations of your vertices, using code like the following:
gl_Position = modelViewProjMatrix * position * orthographicMatrix;
Based on this, you should be able to set the various limits of your display space to accommodate your geometry.
There is no function called gluOrtho
. There is gluOrtho2D
, and there is glOrtho
, both of which do very similar things. But none of them set up the viewport.
The viewport transform of the OpenGL pipeline is controlled by glViewport
and glDepthRange
. What you are talking about is an orthographic projection matrix, which is what glOrtho
and gluOrtho2D
both compute.
OpenGL ES 2.0 does not have many of the fixed-function conveniences of desktop OpenGL pre-3.1. Therefore, you will have to create them yourself. The creation of an orthographic matrix is very easy; the docs for glOrtho and gluOrtho2D both state how they create matrices.
You will need to pass this matrix to your shader via a shader uniform. Then you will need to use this matrix to transform the vertex positions from eye space (defined with the eye position as the origin, +X to the right, +Y is up, and +Z is towards the eye).
You can also us the following convenience method defined in GLkit.
GLKMatrix4 GLKMatrix4MakeOrtho (
float left,
float right,
float bottom,
float top,
float nearZ,
float farZ
);
Just pass (-1, 1) for Z values.