Reputation:
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()?
Upvotes: 3
Views: 5842
Reputation: 4645
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.
Upvotes: 5
Reputation: 6692
Here are the steps that I'd use for each frame:
glClear(GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
Upvotes: 3
Reputation: 131
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.
Upvotes: 0