OpenGL: simple problem with 2 overlapping squares

Discussion in 'Mac Programming' started by Soulstorm, Jun 13, 2007.

  1. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    I am taking my first steps on OpenGL, and I have the following code written using GLUT Library:

    Code:
    #include <Carbon/Carbon.h>
    #include <GLUT/glut.h>
    #include <OpenGL/OpenGL.h>
    #include <iostream>
    #include <cmath>
    
    
    static GLfloat xRot = 0.0f;
    static GLfloat yRot = 0.0f;
    
    ///////////////////////////////////////////////////////////
    // Called to draw scene
    void RenderScene(void)
    {
    	glEnable(GL_DEPTH_TEST);
    	glPolygonMode(GL_BACK,GL_FILL);
    	//glEnable(GL_CULL_FACE);
    	// Clear the window and the depth buffer
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	
    	glColor3f(1.0f,1.0f,0.0f);
    	
    	glPushMatrix();
    	glRotatef(xRot, 1.0f, 0.0f, 0.0f);
    	glRotatef(yRot, 0.0f, 1.0f, 0.0f);
    	
    	glBegin(GL_QUADS);
    	
    	glVertex3f(0,0,0);
    	glVertex3f(50,0,0);
    	glVertex3f(50,50,0);
    	glVertex3f(0,50,0);
    	
    	glEnd();
    	
    	glColor3f(1,0,0);
    	
    	glBegin(GL_QUADS);
    	
    	glVertex3f(50,50,0);
    	glVertex3f(50,50,50);
    	glVertex3f(50,0,50);
    	glVertex3f(50,0,0);
    	
    	glEnd();
    	
    	
    	glPopMatrix();
    	glFlush();
    }
    
    
    
    // This function does any needed initialization on the rendering
    // context. 
    void SetupRC()
    {
    	// Black background
    	glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
    	
    	// Set drawing color to green
    	glColor3f(0.0f, 1.0f, 0.0f);
    	
    	// Set color shading model to flat
    	glShadeModel(GL_FLAT);
    	
    	// Clock wise wound polygons are front facing, this is reversed
    	// because we are using triangle fans
    	glFrontFace(GL_CW);
    	
    	glEnable(GL_DEPTH_TEST);
    	
    }
    
    
    ///////////////////////////////////////////////////////////
    // Called by GLUT library when the window has chanaged size
    void ChangeSize(int w, int h)
    {
        GLfloat nRange = 100.0f;
    	
        // Prevent a divide by zero
        if(h == 0)
            h = 1;
    	
        // Set Viewport to window dimensions
        glViewport(0, 0, w, h);
    	
    	
        // Reset coordinate system
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
    	
        // Establish clipping volume (left, right, bottom, top, near, far)
        if (w <= h) 
            glOrtho (-nRange, nRange, nRange*h/w, -nRange*h/w, -nRange*2.0f, nRange*2.0f);
        else 
            glOrtho (-nRange*w/h, nRange*w/h, nRange, -nRange, -nRange*2.0f, nRange*2.0f);
    	
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
    
    
    void SpecialKeys(int key, int x, int y)
    {
    	if(key == GLUT_KEY_UP)
    		xRot-= 5.0f;
    	
    	if(key == GLUT_KEY_DOWN)
    		xRot += 5.0f;
    	
    	if(key == GLUT_KEY_LEFT)
    		yRot -= 5.0f;
    	
    	if(key == GLUT_KEY_RIGHT)
    		yRot += 5.0f;
    	
    	if(key > 356.0f)
    		xRot = 0.0f;
    	
    	if(key < -1.0f)
    		xRot = 355.0f;
    	
    	if(key > 356.0f)
    		yRot = 0.0f;
    	
    	if(key < -1.0f)
    		yRot = 355.0f;
    	
    	// Refresh the Window
    	glutPostRedisplay();
    }
    
    ///////////////////////////////////////////////////////////
    // Program entry point
    int main(int argc, char* argv[])
    {
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_RGB | GLUT_RGB);
    	glutInitWindowSize(800,600);
    	glutCreateWindow("OpenGL Test");
    	
    	glutReshapeFunc(ChangeSize);
    	glutDisplayFunc(RenderScene);
    	glutSpecialFunc(SpecialKeys);
    	glutMainLoop();
    	
    	SetupRC();
    	return 0;
    }
    The code was supposed to show 2 squares, connected to each other like an open book. So far so good. But when I try to rotate the image, the red square always overlaps the yellow one. Why is this happening?
     
  2. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #2
    I think you need to attach a depth buffer to the window.

    Try:-

    Code:
    glutInitDisplayMode(GLUT_RGB | GLUT_RGB | GLUT_DEPTH);
    
    b e n
     
  3. Soulstorm thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #3
    It was a stupid problem after all... Thanks a lot!
     
  4. Soulstorm thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #4
    I have another problem. I want to make a 3D cube, but I still have the same problem. The depth testing is enabled this time. Here is my code.

    Code:
    #include <Carbon/Carbon.h>
    #include <GLUT/glut.h>
    #include <OpenGL/OpenGL.h>
    #include <iostream>
    #include <cmath>
    
    
    static GLfloat xRot = 0.0f;
    static GLfloat yRot = 0.0f;
    
    ///////////////////////////////////////////////////////////
    // Called to draw scene
    void RenderScene(void)
    {
    	// Angle of revolution around the nucleus
    	//static float fElect1 = 0.0f;
    	
    	// Clear the window with current clearing color
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glPushMatrix();
    	
    	glRotatef(xRot, 1.0f, 0.0f, 0.0f);
        glRotatef(yRot, 0.0f, 1.0f, 0.0f);
    	
    	glBegin(GL_QUADS);
    	
    	glColor3f(1,0,0);
    	//mprostini pleyra
    	glVertex3f(0,0,0);
    	glVertex3f(50,0,0);
    	glVertex3f(50,50,0);
    	glVertex3f(0,50,0);
    	
    	glColor3f(0,1,0);
    	//deksia pleyra
    	glVertex3f(50,0,0);
    	glVertex3f(50,0,50);
    	glVertex3f(50,50,50);
    	glVertex3f(50,50,0);
    	
    	glColor3f(0,0,1);
    	//panw pleyra
    	glVertex3f(50,50,0);
    	glVertex3f(50,50,50);
    	glVertex3f(0,50,50);
    	glVertex3f(0,50,0);
    	
    	glColor3f(1,1,1);
    	//aristeri pleyra
    	glVertex3f(0,0,50);
    	glVertex3f(0,0,0);
    	glVertex3f(0,50,0);
    	glVertex3f(0,50,50);
    	
    	glColor3f(0,1,1);
    	//katw pleyra
    	glVertex3f(0,0,0);
    	glVertex3f(50,0,0);
    	glVertex3f(50,0,50);
    	glVertex3f(0,0,50);
    	
    	glColor3f(1,1,0);
    	//pisw pleyra
    	glVertex3f(0,0,50);
    	glVertex3f(50,0,50);
    	glVertex3f(50,50,50);
    	glVertex3f(0,50,50);
    	
    	glEnd();
    	
    	glPopMatrix();
    	// Show the image
    	glutSwapBuffers();
    }
    
    // This function does any needed initialization on the rendering
    // context. 
    void SetupRC()
    {
    	// Black background
    	glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
    	
    	// Set drawing color to green
    	glColor3f(0.0f, 1.0f, 0.0f);
    	
    	// Set color shading model to flat
    	glShadeModel(GL_FLAT);
    	
    	// Clock wise wound polygons are front facing, this is reversed
    	// because we are using triangle fans
    	glFrontFace(GL_CCW);
    	glEnable(GL_CULL_FACE);
    	glEnable(GL_DEPTH_TEST);
    }
    
    
    ///////////////////////////////////////////////////////////
    // Called by GLUT library when the window has chanaged size
    void ChangeSize(int w, int h)
    {
        GLfloat nRange = 100.0f;
    	
        // Prevent a divide by zero
        if(h == 0)
            h = 1;
    	
        // Set Viewport to window dimensions
        glViewport(0, 0, w, h);
    	
    	
        // Reset coordinate system
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
    	
        // Establish clipping volume (left, right, bottom, top, near, far)
        if (w <= h) 
            glOrtho (-nRange, nRange, nRange*h/w, -nRange*h/w, -nRange*2.0f, nRange*2.0f);
        else 
            glOrtho (-nRange*w/h, nRange*w/h, nRange, -nRange, -nRange*2.0f, nRange*2.0f);
    	
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
    
    
    void SpecialKeys(int key, int x, int y)
    {
    	if(key == GLUT_KEY_UP)
    		xRot-= 5.0f;
    	
    	if(key == GLUT_KEY_DOWN)
    		xRot += 5.0f;
    	
    	if(key == GLUT_KEY_LEFT)
    		yRot -= 5.0f;
    	
    	if(key == GLUT_KEY_RIGHT)
    		yRot += 5.0f;
    	
    	if(key > 356.0f)
    		xRot = 0.0f;
    	
    	if(key < -1.0f)
    		xRot = 355.0f;
    	
    	if(key > 356.0f)
    		yRot = 0.0f;
    	
    	if(key < -1.0f)
    		yRot = 355.0f;
    	
    	// Refresh the Window
    	glutPostRedisplay();
    }
    
    ///////////////////////////////////////////////////////////
    // Program entry point
    int main(int argc, char* argv[])
    {
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
    	glutInitWindowSize(800,600);
    	glutCreateWindow("OpenGL Test");
    	
    	glutReshapeFunc(ChangeSize);
    	glutDisplayFunc(RenderScene);
    	glutSpecialFunc(SpecialKeys);
    
    
    	glutMainLoop();
    	
    	SetupRC();
    	return 0;
    }
    
    I am trying to rotate the cube using the arrow keys, but the sides are not drawing properly. Any ideas? Have I drawn them using the wrong direction? Sorry for the newbish questions, these are my first steps with OpenGL.
     
  5. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #5
    Hi

    You need to do a:-

    Code:
    	glEnable( GL_DEPTH_TEST ) ;

    A good place would be in your RenderScene() function just after clearing the viewport.

    b e n
     
  6. Soulstorm thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #6
    That worked, thanks a lot. But, I had put this command in my SetupRC() function. Why didn't it work? Why did I have to put it after I clear the viewport?
     
  7. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #7
    …because SetupRC() isn't called until after your main loop has exited!

    b e n
     

Share This Page