How do I set up the viewable area in a scene within OpenGL ES 2.0?
Asked Answered
D

3

1

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.

Depicture answered 23/6, 2011 at 3:46 Comment(0)
L
4

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.

Lilian answered 24/6, 2011 at 22:51 Comment(1)
This helped me out a good bit, the only part I got stuck on was not knowinh the origin of these calculations. I think this is good background material: songho.ca/opengl/gl_projectionmatrix.html . Also it should be noted that the matrix shown above is composed with the assumption that it's row major. The link here helped me realize this and transpose accordingly for OpenGL :)Stutz
Y
2

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

Yeld answered 23/6, 2011 at 4:5 Comment(1)
can you give me an example ..let say i have to display a triangle with 10 10 10 not less than this sides.Depicture
M
0

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.

Manstopper answered 1/7, 2012 at 2:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.