Problems with glPushMatrix()

Started by
-1 comments, last by Zaphod0709 20 years, 11 months ago
When i paint a background (Ortho Mode) and then in the foreground (in Perspective Mode) 3 Layers of Alpha Blended Cubes having some special MAterial and Lighning but no textures the cubes do not rotate as i expected and also the background switches to solid white after a very short while. I played around with glPushMatrix() at some positions of my code, but nothing seems to help. I post parts of the Code here, because it might be easier to see what i''m doing wrong: Would be nice, if someone could help.... void Draw (void) { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); RenderBackground(); RenderCubes(); glFlush (); } void RenderBackground() { // switch to Ortho2D glMatrixMode (GL_PROJECTION); glLoadIdentity(); glOrtho(0.0f, g_window->init.width, g_window->init.height, 0.0f, -1.0f, 1.0f); glMatrixMode (GL_MODELVIEW); glLoadIdentity(); // glTranslatef(0.375, 0.375, 0.0); glEnable(GL_TEXTURE_2D); glDepthMask(GL_FALSE); // Draw Background int x_pos, y_pos; float width, height; y_pos = 0; x_pos = 0; for (int i=0; i < sizeof(tiles); i++) { glBindTexture(GL_TEXTURE_2D, tiles.texture); glBegin(GL_TRIANGLE_STRIP); glColor3f(1.0f, 1.0f, 1.0f); glTexCoord2d(1,1); glVertex2i( x_pos+tiles.width, y_pos); glTexCoord2d(0,1); glVertex2i( x_pos, y_pos); glTexCoord2d(1,0); glVertex2i( x_pos+tiles.width, y_pos+tiles.height); glTexCoord2d(0,0); glVertex2i( x_pos, y_pos+tiles.height); glEnd(); x_pos += tiles.width; if (x_pos >= g_window->init.width) { x_pos=0; y_pos += tiles.height; } } glDepthMask(GL_TRUE); } void RenderCubes() { long y = tickCount/1000; glPushMatrix(); // Switch to 3D Perspective glMatrixMode (GL_PROJECTION); glLoadIdentity (); // Calculate The Aspect Ratio Of The Window gluPerspective (45.0f, (GLfloat)(g_window->init.width)/(GLfloat)(g_window->init.height), 1.0f, 1000.0f); glMatrixMode (GL_MODELVIEW); glLoadIdentity (); // Setup Lights glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_AMBIENT, light0.ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0.diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light0.specular); glLightfv(GL_LIGHT0, GL_POSITION, light0.position); glEnable(GL_LIGHT1); glLightfv(GL_LIGHT1, GL_AMBIENT, light1.ambient); glLightfv(GL_LIGHT1, GL_DIFFUSE, light1.diffuse); glLightfv(GL_LIGHT1, GL_SPECULAR, light1.specular); glLightfv(GL_LIGHT1, GL_POSITION, light1.position); // Enable Alpha Blending glDisable( GL_DEPTH_TEST ); glEnable( GL_BLEND ); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glDisable(GL_COLOR_SUM_EXT); for (int i=0; i < noCubeLayers; i++) { for (int j=0; j < noCubes; j++) { // update y position to move cubes from bottom // to top of screen cubes[j].pos.y += speedLayers; if (cubes[j].pos.y > cubes[j].pos.z/-2 ) { cubes[j].pos.y = cubes[j].pos.z/2; } glTranslatef(cubes[j].pos.x, cubes[j].pos.y, cubes[j].pos.z); glRotatef(cubes[j].rot.rotX, 1.0f, 0.0f, 0.0f); glRotatef(cubes[j].rot.rotY, 0.0f, 1.0f, 0.0f); glRotatef(cubes[j].rot.rotZ, 0.0f, 0.0f, 1.0f); glBegin( GL_TRIANGLES ); SetMaterial(&outerMaterial); for (int count=0; count < outer.num_faces; count++) { glNormal3f( outer.normals[outer.n_idx[count]].x, outer.normals[outer.n_idx[count]].y, outer.normals[outer.n_idx[count]].z ); glVertex3f( outer.vertices[outer.v_idx[count]].x/100, outer.vertices[outer.v_idx[count]].y/100, outer.vertices[outer.v_idx[count]].z/100 ); } SetMaterial(&innerMaterial); for (int count=0; count < inner.num_faces; count++) { glNormal3f( inner.normals[innerCube.n_idx[count]].x, inner.normals[innerCube.n_idx[count]].y, inner.normals[innerCube.n_idx[count]].z); glVertex3f( inner.vertices[inner.v_idx[count]].x/100*1.35, inner.vertices[inner.v_idx[count]].y/100*1.35, inner.vertices[inner.v_idx[count]].z/100*1.35 ); } glEnd(); } } glDisable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); glDisable( GL_BLEND ); glPopMatrix(); } </i>

This topic is closed to new replies.

Advertisement