PDA

View Full Version : Whats wrong with this code



Anish
07-24-2005, 10:11 PM
Hai

I want to draw a background and then some 3D objects. But when i draw the 3D objects my background disappears. Please mention whats wrong with this code

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/****** Drawing Background Texture*******/

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glOrtho(0.0f,w,0.0f,h, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glPushAttrib(GL_LIGHTING_BIT);
glLoadIdentity();
glShadeModel(GL_FLAT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );

const GLfloat fw((GLfloat) w);
const GLfloat fh((GLfloat) h);

glBegin(GL_QUADS);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( fw, fh, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0, fh, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0, 0, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( fw, 0, 0.0f);
glEnd();

glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glPopAttrib();
/*******End Background Texture *********/

/**********Draw 3D objects and rotation*******/

glViewport(0 + AutoScrollPosition.X, 0 - (nRange - Height) - AutoScrollPosition.Y, nRange, nRange );

// Reset projection matrix stack
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

glShadeModel(GL_FLAT);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);

// Establish clipping volume (left, right, bottom, top, near, far)

glOrtho ((-nRange/2.0), (nRange/2.0), (nRange/2.0 ), -(nRange/2.0), -(nRange/2.0), (nRange/2.0));

// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


glRotatef(60,1.0,1.0,0.0);
glRotatef(45 * rot,0.0,1.0,0.0);

glScalef(scale3D,scale3D,scale3D);
DrawObject()

Whats wrong with this code.

Please help :)

Thanks ;)

dvm
07-25-2005, 03:28 AM
Well, for one, I believe glViewport has no right being there. It should be along with the projection code in a resize-reshape method. Also, the problem probably occurs when you reset your projection the second time. So the background texture is lost.
What you need to do is
a) create both the background and the 3d objects in one projection volume. You just need to experiment a bit to find out the proper size for the quad etc.

or b) this is kind of tricky, but I've used it in the past to create text overlays over a 3d world. Maybe someone else remembers how this is done properly. You set the projection matrix as usual, draw your background quad, then push the matrix, change the projection again for your 3D objects, draw them, and when you're finished you pop the matrix. But as I've said this is kind of tricky to get the right order of glMatrixMode, glPush-PopMatrix etc. Good luck!