PDA

View Full Version : glOrthof and coordinates.



ajbiv
01-14-2009, 10:14 PM
I am having trouble understanding something.

I am working through several different tutorials. My favorite right now is NeHe. I am working through the 1-5 tutorials.

THe problem I'm experiencing is around the coordinates provided for the shapes. All the examples show vertices with 0.0, 1.0 values etc.

The only way I can get my polygons to show up is to use the actual coordinates of my view. My view is 320/480. So, I'm using 50, 75, etc. If I use translate, I can move the shape. I understand that. However, the sizes of my shapes are very small if I use the coordinates in the examples.

Anyone know what I'm doing wrong...



All the examples draw a polygon like this:
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();

Because, I'm learning on the iphone with OpenGL ES, I've translated this to:

const GLfloat kTriangleVertices []= {
0.0f, 0.0f, 0.0f,
spriteWidth / 2, spriteHeight, 0.0f,
spriteWidth, 0.0f, 0.0f
};

glEnableClientState (GL_VERTEX_ARRAY);
glVertexPointer (3, GL_FLOAT , 0, kTriangleVertices);
glDrawArrays (GL_TRIANGLES, 0, 3);

I had to init my view with code like this before I could get anything:

glViewport(0, 0, backingWidth, backingHeight);

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity();
glOrthof(0.0f, width, 0.0f, height, -1.0f, 1.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity();

tkunert
01-14-2009, 10:47 PM
The interesting point here is that the glOrtho command specifies the region that will be mapped on the viewport of your screen.

"glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity();
glOrthof(0.0f, width, 0.0f, height, -1.0f, 1.0f);"

This will map all vertices whose x and y coordinate are within [0, width] and [0,height], respectively. The vertices that are out of these ranges are just clipped to the borders. Because you are using the same width and height of your viewport, the vertices won't be scaled when they are mapped.

"glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();"

Since the size of this triangle is very small compared to your viewport (320x480 pixels), you will notice just a point on the screen. If you try it with glOrthof(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f), the triangle will be shown largely, because its vertices will be mapped exactly on the viewport borders.

You can read more about projection and viewport transformations in the red book.
http://www.glprogramming.com/red/chapter03.html

ajbiv
01-15-2009, 09:42 AM
I suspected it was related to my glOrthof call.

What happens if you do not make a call to glOrthof and you just set the viewport to the size of the window? The examples seem to take that approach unless I'm missing a key line.

I can't get anything to show on my screen w/o a glOrthof call.

tkunert
01-15-2009, 11:09 PM
It might be that the default projection matrix is the identitiy matrix, but I wouldn't rely on it. You should explicitly set it like this:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

This equals a orthogonal projection such as
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);

With a identity matrix for projection the vertices within the range [-1.0f, 1.0f] will be mapped on your viewport and will be automatically scaled. You will find the exact transformation matrix in the OpenGL Specs, e.g. section "2.11.2 Matrices" in http://www.opengl.org/documentation/specs/version2.0/glspec20.pdf

dletozeun
01-16-2009, 06:37 AM
tkunert, after calling:
glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);

the projection matrix is not the identity matrix though apparently, it does not change anything in the rendering. The matrix is:

[[ 1 0 0 0]
[ 0 1 0 0]
[ 0 0 -1 0]
[ 0 0 0 1]]

So, using the identity will just affect the z coordinates that will not be reverted.

tkunert
01-17-2009, 12:25 AM
dietozeun, you're right.

The command that equals the identity matrix is then
glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f);