views:

620

answers:

3

Let's say I want to draw a simple quad like this:

glBegin(GL_QUADS);
glVertex2f(-1.0,-1.0);
glVertex2f(1.0,-1.0);
glVertex2f(1.0, 1.0);
glVertex2f(-1.0, 1.0);
glEnd();

Is there a way to draw this such that it appears behind all 3D objects and fills up the whole screen? My camera moves with the mouse, so this quad also has to appear stationary with camera movement. The purpose is to make a simple background.

Do I have to do some crazy transformations based on eye position/rotation, or is there a simpler way using glMatrixMode()?

A: 

Cant you just place it in the background, box yourself in if you want it all around you. You need to draw it first to make it behind all the objects.

Rolf Anders
+3  A: 

Here are the steps that I'd use for each frame:

  1. Clear the depth buffer with glClear(GL_DEPTH_BUFFER_BIT);
  2. Turn off depth testing with glDisable(GL_DEPTH_TEST);
  3. Load in a fixed orthographic projection with gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
  4. Draw your quad as you described it.
  5. Turn depth testing back on.
  6. Load your your regular perspective projections and draw the rest of the view.
Boojum
Exactly what I was just trying to formulate into english :)
workmad3
+2  A: 

It sounds like what you want to do is similar to what you would when drawing a 2D HUD in a simple game, or just a background that is persistent. I'm no expert, but I did just that relatively recently. What you want to do is change the projection matrix to an orthographic projection, render your quad, and then switch back to whatever projection you had before. You can do this with the matrix stack, and it is completely independent of any camera.

So, first:

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
int w = glutGet(GLUT_WINDOW_WIDTH);
int h = glutGet(GLUT_WINDOW_HEIGHT);
gluOrtho2D(0, w, h, 0);

This pushes a new projection matrix onto the projection stack (I was using glut for this project, but it should translate to just normal OpenGL). Then, I get the window width and height, and use gluOrtho2D to set up my orthographic projection.

Next:

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// Draw your quad here in screen coordinates

Here I just push a new, clean matrix onto the modelview. You may not need to do this.

Lastly:

glPopMatrix() // Pops the matrix that we used to draw the quad
glMatrixMode(GL_PROJECTION);
glPopMatrix(); // Pops our orthographic projection matrix, which restores the old one
glMatrixMode(GL_MODELVIEW); // Puts us back into GL_MODELVIEW since this is probably what you want

I hope this helps. As you can tell, it requires no use of the eye position. This is mainly because when we use a new orthographic projection that perfectly fits our window, this is irrelevant. This may not put your quad in the back however, if this is executed after the other draw calls.

EDIT: Turning off depth testing and clearing the depth buffer could help as well, as Boojum suggests.

Mongoose
An important final step is before the last line, add this: glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(/*your perspective vars*/);If not, the 3D models go weird.