Thread: The wall behind is showing over the wall in front. (Glut)

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question The wall behind is showing over the wall in front. (Glut)

    If you compile this, you will notice that the wall displayed last overlaps the wall displayed first, even though it's behind the first wall. How can I prevent this from happening?

    Code:
    #include <GL\glut.h>
    #include <windows.h>
    
    void glVertex( float c1, float c2, float c3, float c4, float x1, float y1, float z1,  float x2, float y2, float z2,  float x3, float y3, float z3, float x4, float y4, float z4 ){
    glTranslatef(0.0,0.0,0.0); 
    glColor3f(c1,c2,c3); 
     glVertex3f(x1,y1,z1); 
     glVertex3f(x2,y2,z2); 
     glColor3f(c1-c4,c2-c4,c3-c4); 
     glVertex3f(x3,y3,z3);  
    glVertex3f(x4,y4,z4);
     return; }
    
    #include "camera.h"
    #define ShowUpvector
    
    float s = -0.02;
    float t = 0.6;
    float x = 0;
    float y = 0;
    float z = 1.0;
    
    CCamera Camera;
    
    
    void DrawWall()
    {
      glBegin(7);
    
    glPushMatrix();
    glTranslatef(0.0,0.0,0.0);
    
       glVertex(  1.0,  1.0,  0.0,  0.3,
    				0.5,  0.5,  0.5,
    			   -0.5,  0.5,  0.5,
    			   -0.5,  0.5, -0.5,
    				0.5,  0.5, -0.5  );
     
       glVertex(  1.0,  1.0,  1.0,  0.3,
    				0.5,  0.5,  0.5,
    			   -0.5,  0.5,  0.5,
    			   -0.5, -0.5,  0.5,
    				0.5, -0.5,  0.5  );
    
    glPopMatrix();
      glEnd();
    
    }
    
    void reshape(int x, int y)
    {
     if (y == 0 || x == 0) return;  //Nothing is visible then, so return
     
     //Set a new projection matrix
     glMatrixMode(GL_PROJECTION);  
     glLoadIdentity();
     //Angle of view:40 degrees
     //Near clipping plane distance: 0.0
     //Far clipping plane distance: 20.0
     gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.0,20.0);
    
     glMatrixMode(GL_MODELVIEW);
     glViewport(0,0,x,y);  //Use the whole window for rendering
    }
    
    void Display(void)
    {
     glClear(GL_COLOR_BUFFER_BIT);
     glLoadIdentity();
      
     Camera.Render();
    
     //Draw the "world":
     glTranslatef(0.0,-0.5,-6.0);
    
     glScalef(3.0,1.0,3.0);
    
    	  glPushMatrix();
       DrawWall();
    	  glPopMatrix();
    
     //finish rendering:
     glFlush();  
     glutSwapBuffers();
    }
    
    void KeyDown(unsigned char key, int x, int y)
    {
     switch (key) 
     {
     case 27:  //ESC
      PostQuitMessage(0);
      break;
     case 'a':  
      Camera.RotateY(5.0);
      Display();
      break;
     case 'd':  
      Camera.RotateY(-5.0);
      Display();
      break;
     case 'w':  
      Camera.MoveForward( -0.20 ) ;
    //  Display();
      break;
     case 's':  
      Camera.MoveForward( 0.1 ) ;
      Display();
      break;
     case 'x':  
      Camera.RotateX(5.0);
      Display();
      break;
     case 'y':  
      Camera.RotateX(-5.0);
      Display();
      break;
     case 'c':  
      Camera.StrafeRight(-0.1);
      Display();
      break;
     case 'v':  
      Camera.StrafeRight(0.1);
      Display();
      break;
     case 'f':
      Camera.MoveUpward(-0.3);
      Display();
      break;
     case 'r':
      Camera.MoveUpward(0.3);
      Display();
      break;
    
     case 'm':
      Camera.RotateZ(-5.0);
      Display();
      break;
     case 'n':
      Camera.RotateZ(5.0);
      Display();
      break;
    
     }
    }
    void TimerRun()
    {
      Sleep(10);
      
      z+=s/3;
    
    //  Camera.MoveForward(s) ;
    
      if(GetAsyncKeyState(VK_RIGHT)){
      Camera.RotateY(-t);  }
      if(GetAsyncKeyState(VK_LEFT)){
      Camera.RotateY(t);  }
      if(GetAsyncKeyState(VK_DOWN)){
      Camera.RotateX(-t);  }
      if(GetAsyncKeyState(VK_UP)){
      Camera.RotateX(t);  }
    
      Display();
    }
    
    
    int main(int argc, char **argv)
    {
    // glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | WS_POPUP);
     glutInitWindowSize(500,300);
     glutCreateWindow("ShipBox");
     Camera.Move( F3dVector(0.0, 0.0, 3.0 ));
     Camera.MoveForward( 1.0 );
     glutDisplayFunc(Display);
     glutReshapeFunc(reshape);
     glutKeyboardFunc(KeyDown);
     glutIdleFunc(TimerRun);
    // glutFullScreen();
    // ShowCursor(FALSE);
     glutMainLoop();
     return 0;			 
    }
    Thanks, August.

  2. #2
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    the simplest way, reverse display order, so last displayed is front of scene.

    when working in 3D most people think front to back, yet the drawing of a scene works best when done back to front.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I did that, but then when I move the camera to the back then turn around, you get the exact same problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GLUT, window not showing up
    By Poincare in forum C Programming
    Replies: 2
    Last Post: 05-24-2009, 09:48 PM