In OpenGL, how do I make a simple background square?

Let's say I want to draw a simple square as follows:

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 any way to make this so that it appears behind all 3D objects and fills the entire screen? My camera moves with the mouse, so this quad should also appear stationary with the movement of the camera. The goal is to make a simple background.

Do I need to do some crazy transformations based on eye position / rotation, or is there an easier way to use glMatrixMode ()?

+3
source share
3 answers

, , , 2D HUD . , . , , , , , . , - .

, :

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

( , OpenGL). gluOrtho2D .

:

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

, . , .

:

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

, . , . , , , , . , , .

: , .

+5

, :

  • glClear(GL_DEPTH_BUFFER_BIT);
  • glDisable(GL_DEPTH_TEST);
  • gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
  • , .
  • .
  • .
+3

Could you just put it in the background, turn it on if you want everything around you. You must do this first to make it behind all the objects.

0
source

All Articles