PDA

View Full Version : Background image behind 3D scene



Ivan_j_147
01-11-2012, 06:20 AM
Hi all!!
I'm trying to create a simple 3D game, and I want to put behind my scene a fixed background image.
I read that I need to draw a textured quad in glOrtho for the background, and to draw my 3D objects in glPerspective.
I tried to write my display function in this way:

void display () {
glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION );
glLoadIdentity();
glOrtho(0,1,0,1,-1,1);
glPushMatrix();
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDepthMask(GL_FALSE);
glMatrixMode(GL_MODELVIEW);
//draw 2D image
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat) W_WIDTH / (GLfloat) W_HEIGHT, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
CameraTemp.Pos.X,CameraTemp.Pos.Y, CameraTemp.Pos.Z,
CameraTemp.Target.X,CameraTemp.Target.Y, CameraTemp.Target.Z,
0.0, 1.0, 0.0);
//draw 3D objects
glutSwapBuffers();
}
...but I see only the 2D background image. Anybody can say what am I wrong? :(

tksuoran
01-11-2012, 07:00 AM
Do not glClear() after drawing the 2D image.

Check the bitmask you send to glClear(). Use bitwise or instead of logical or.

Check where your code actually draws the 3D object. I would suggest setting camera target to 0, 0, 0 and draw your object there first and then see how to make your object move properly.

Ivan_j_147
01-11-2012, 07:26 AM
Thanks for your reply.
I changed in this way:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION );
glLoadIdentity();
glOrtho(0,1,0,1,-1,1);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDepthMask(GL_FALSE);
glMatrixMode(GL_MODELVIEW);
//draw 2D image
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat) W_WIDTH / (GLfloat) W_HEIGHT, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
CameraTemp.Pos.X,CameraTemp.Pos.Y, CameraTemp.Pos.Z,
CameraTemp.Target.X,CameraTemp.Target.Y, CameraTemp.Target.Z,
0.0, 1.0, 0.0);
//draw 3D objects
glutSwapBuffers();

...but now I see only the 3D scene, without the background... :(

Ivan_j_147
01-11-2012, 07:41 AM
Ops, it was my fault! :)
I forgot to insert the reset of model view matrix when I draw the 2D image.
Now it's working! Thank you thank you thank you!! ;)
Here's the complete code, if somebody need it:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION );
glLoadIdentity();
glOrtho(0,1,0,1,-1,1);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDepthMask(GL_FALSE);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//draw 2D image
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat) W_WIDTH / (GLfloat) W_HEIGHT, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
CameraTemp.Pos.X,CameraTemp.Pos.Y, CameraTemp.Pos.Z,
CameraTemp.Target.X,CameraTemp.Target.Y, CameraTemp.Target.Z,
0.0, 1.0, 0.0);
//draw 3D objects
glutSwapBuffers();

bisconect
11-23-2012, 08:12 AM
Thank you, Ivan! Your code helped me a lot!!