PDA

View Full Version : 2D projection/drawing question



blood.angel
04-19-2002, 09:35 AM
Following on from my previous problem (drawing a bitmap) Im having trouble with actually drawing it to a fixed part of the screen.
I draw my 3D scene, then switch to 2D and then draw the bitmap to the lower left corner. The problem is I find the bitmap stays at the corner and it wont draw anywhere else on the screen.

Heres the code Im using:




glMatrixMode(GL_PROJECTION);
glPushMatrix();
gluOrtho2D(0, WIDTH, 0, HEIGHT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
//glTranslatef(0, WIDTH, 0);
//glRasterPos2f(0.0, 0.5);
glDrawPixels(64, 32, GL_RGB, GL_UNSIGNED_BYTE, scramble_bitmap);
glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

Where HEIGHT and WIDTH are the window size.

I have tried both of the commented out lines, but one only draws it at the corner and the other doesnt at all.

Anyone see where Im going wrong?

Jamie
04-19-2002, 01:12 PM
You need to use glRasterPos()

blood.angel
04-19-2002, 01:39 PM
Yeah I know, but when I set it to (0.0, 0.0) the bitmap isnt drawn at all. (0,0) should be the center of my screen.

Jamie
04-19-2002, 02:06 PM
Sorry didn't look what you were doing. You're problem is that you are not treating rasterPos in window coordinates like it is.

blood.angel
04-19-2002, 02:12 PM
You lost me there http://www.opengl.org/discussion_boards/ubb/smile.gif

455h*L3
04-19-2002, 06:31 PM
That's OK. What he said wasn't actually in English.

Jamie
04-20-2002, 06:40 AM
I guess I should work on my english then, but I am trying to help...

Lets state it this way. You know the difference between screen coordinates, window coordinates, model coordniates, and world coordinates correct? Raster position is in window coordinates. So this means if you don't do any modification to your modelview matrix (no rotates and no translates) you will get the following results on a window that is 256x256 pixels.
0,0 = lower left corner
128,128 = center

The only reason to use floats with raster position is to take advantage of sub-pixel precision.

Bob
04-20-2002, 06:56 AM
The raster position is specified in world coordinates, not window coordinates, and is affected by both the modelview and projection matrix. It is treated just like any other vertex you pass to OpenGL; it get transformed by first the modelview, then the projection matrix, and after that, it's clipped agains the view volume, and will be marked as an invalid raster position if outside the view volume.

To be 100% sure where the raster position will be located, replace glRasterPos2(x,y) with



glBegin(GL_POINTS);
glVertex2(x,y);
glEnd();

and see where the point appears on the window.

So, with identity matrices in both the modelview and projection stack, raster position (0,0) will be in the middle of the screen, (-1, -1) will be the lower left corner, and (1, 1) will be the upper right corner. And that is independent of the physical size of the window.

Most people that wants a 1-to-1 correspondence between the raster position and window pixel coodinated usually sets an orthograpic projection matrix that matches the dimensions of the viewport. This is done because the raster position is affected by the projection matrix (as I said above), which maps the position to window coordinates in a convenient way.

zeckensack
04-20-2002, 08:33 AM
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, WIDTH, 0, HEIGHT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
//glTranslatef(0, WIDTH, 0);
//glRasterPos2f(0.0, 0.5);
glDrawPixels(64, 32, GL_RGB, GL_UNSIGNED_BYTE, scramble_bitmap);
glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

Pushing a matrix stack will produce a copy of the last matrix, if you want to start over (ie apply a new projection, new modelview), you still have to clear it to identity first.

zeckensack
04-20-2002, 08:36 AM
Also try ARB_window_pos (http://oss.sgi.com/projects/ogl-sample/registry/ARB/window_pos.txt) if it's supported on your card. It rocks.