Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: OpenGL newbie, depth problem.

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2018
    Posts
    3

    OpenGL newbie, depth problem.

    Hello,

    I am really new to OpenGL and I am trying to draw multiple cubes on my screen.
    The problem is when I turn around the cubes, some back cubes appear in front.

    I am using OpenGL 3.3, and SDL2.

    Images :
    Nice display :
    Click image for larger version. 

Name:	Deepin Capture-écran_zone de sélection _20180609005627.jpg 
Views:	26 
Size:	13.6 KB 
ID:	2782

    Problem for some angles :
    Click image for larger version. 

Name:	Deepin Capture-écran_zone de sélection _20180609005643.jpg 
Views:	24 
Size:	8.8 KB 
ID:	2783

    Click image for larger version. 

Name:	Deepin Capture-écran_zone de sélection _20180609005705.jpg 
Views:	22 
Size:	12.1 KB 
ID:	2784

    (Yes it is supposed to be a rubiks cube)

    The code :

    Initialization :
    Code :
    // SDL initialization
    	if(SDL_Init(SDL_INIT_VIDEO)) {
    		handleSDLError("Error during SDL2 initialization");
    		return false;
    	}
     
    	// Create the SDL Window
    	window = SDL_CreateWindow("Rubik's Cube Simulator",
    			SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
    			SCREEN_WIDTH, SCREEN_HIGH, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
     
    	// Handle errors
    	if(window == NULL) {
    		handleSDLError("Error during window creation");
    		return false;
    	}
     
    	context = SDL_GL_CreateContext(window);
     
    	if(context == NULL) {
    		handleSDLError("Error during context creation");
    		return false;
    	}
     
    	setOpenGLAttribute();
     
    	glewExperimental = GL_TRUE;
    	GLenum glewError = glewInit();
    	if (glewError != GLEW_OK) {
    		printf("Error during GLEW initialization : %s\n", glewGetErrorString(glewError));
    	}
     
    	if (!initializeOpenGL()) {
    		return false;
    	}
     
     
    bool initializeOpenGL() {
    	glDepthMask(GL_TRUE);
    	glEnable(GL_DEBUG_OUTPUT);
     
    	glDepthFunc(GL_LEQUAL);
    	glEnable(GL_DEPTH_TEST);
    	glEnable(GL_CULL_FACE);
     
    // Tried gluPerspective, but the cubes disappear when the zNear and zFar are not 0.
    //	gluPerspective(70, (double)SCREEN_WIDTH / SCREEN_HIGH, 0, 0);
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
     
    	return true;
    }
     
    void setOpenGLAttribute() {
    	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
    	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
     
    	if (SDL_GL_SetSwapInterval(1)) {
    		handleSDLError("Warning unable to set VSYNC");
    	}
    }

    Main loop :
    Code :
           glClearColor(0.0, 0.0, 0.0, 1.0);
     
            glScalef(0.2, 0.2, 0);
    	gluLookAt(3,3,-10,0,0,0,0,1,0);
    	do {
     
    		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    		for (short i = 0; i < 26; ++i) {
    			glPushMatrix();
    			drawCube(rubiks[i]);
    			glPopMatrix();
    		}
    		SDL_GL_SwapWindow(window);
    	} while (!quitApplication);

    drawCube :
    Code :
    void drawCube(Cube * kube) {
    	const char * SIGNES = "+++-++--+--++-++++++++-++--+--++-++++++++--+--+--+++++-++-+---------+-++---+--+-++-+--+---+------+--+-++-+--";
     
    	unsigned currentCoord = 0;
    	GLfloat vertices[108];
    	for (short i = 0; i < 108; ++i) {
    		if (SIGNES[i] == '+') {
    			vertices[i] = kube->coord[currentCoord] + 0.4;
    		} else {
    			vertices[i] = kube->coord[currentCoord] - 0.4;
    		}
    		currentCoord += 1;
    		currentCoord %= 3;
    	}
     
    	const GLfloat color[] = {
    			1, 1, 1,   1, 1, 1,   1, 1, 1,      // v0-v1-v2 (front)
    			1, 1, 1,   1, 1, 1,   1, 1, 1,      // v2-v3-v0
     
    			0, 1, 0,   0, 1, 0,   0, 1, 0,      // v0-v3-v4 (right)
    			0, 1, 0,   0, 1, 0,   0, 1, 0,      // v4-v5-v0
     
    			1, 0, 0,   1, 0, 0,   1, 0, 0,      // v0-v5-v6 (top)
    			1, 0, 0,   1, 0, 0,   1, 0, 0,      // v6-v1-v0
     
    			0, 0, 1,   0, 0, 1,   0, 0, 1,      // v1-v6-v7 (left)
    			0, 0, 1,   0, 0, 1,   0, 0, 1,      // v7-v2-v1
     
    			1, 0.5, 0,   1, 0.5, 0,   1, 0.5, 0,      // v7-v4-v3 (bottom)
    			1, 0.5, 0,   1, 0.5, 0,   1, 0.5, 0,      // v3-v2-v7
     
    			1, 1, 0,   1, 1, 0,   1, 1, 0,      // v4-v7-v6 (back)
    			1, 1, 0,   1, 1, 0,   1, 1, 0 };    // v6-v5-v4
     
     
     
    	glEnableClientState(GL_VERTEX_ARRAY);
    	glEnableClientState(GL_COLOR_ARRAY);
     
    	glVertexPointer(3, GL_FLOAT, 0, vertices);
    	glColorPointer(3, GL_FLOAT, 0, color);
     
    	glPushMatrix();
     
    	// draw a cube
    	glDrawArrays(GL_TRIANGLES, 0, 36);
     
    	glPopMatrix();
     
    	// Disable vertex arrays and color arrays after drawing
    	glDisableClientState(GL_VERTEX_ARRAY);
    	glDisableClientState(GL_COLOR_ARRAY);
    	glFlush();
    }

    I don't know how to correctly make work the depth in OpenGL.
    I checked the way I am drawing my vertices,I draw them all Counter Clockwise Winding.

    Hope I didn't missed any information.

    Sorry for my bad english...

    Thanks.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,926
    Quote Originally Posted by Airdeth View Post
    Code :
    	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
    Try 24. 32-bit depth isn't required to be supported.

    Also: are you setting any projection transformation? I see the gluPerspective() call is commented out, but are you using anything else instead? Note that the "standard" projection transformations (glOrtho, glFrustum, gluOrtho2D, gluPerspective) all flip the Z, so negative Z corresponds to increasing depth. An identity transformation won't do that.

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2018
    Posts
    3
    Hi,

    Setting the Z buffer to 24 didn't change the problem,
    Also for the projection, I don't use anything else, as mentioned just above the gluPerspective line, my cube disappear if my zNear and zFar aren't close to 0 (For example, when setting one of them to 1, my cubes disappear)

    I also tried to use glMatrixMode, but I really don't understand how it works, by putting glMatrixMode(GL_PROJECTION) before glClearColor in my main loop, I am able to put bigger number in the gluPerspective function (Like zNear = 3 and zFar = 5), but my cubes shows up like this :

    Click image for larger version. 

Name:	Deepin Capture-écran_zone de sélection _20180609112542.jpg 
Views:	24 
Size:	8.4 KB 
ID:	2785

    Click image for larger version. 

Name:	Deepin Capture-écran_zone de sélection _20180609112605.jpg 
Views:	21 
Size:	8.4 KB 
ID:	2786

    (Sorry for the screenshots I have huge difficulty turning around my cube)

    But of what I read online, I understood that I have to set the PROJECTION matrix when I am moving my camera and the MODELVIEW when drawing my cubes ?
    But by putting glMatrixMode(GL_PROJECTION) before my gluLookAt, and glMatrixMode(GL_MODELVIEW) before mt drawCube(...) function, my screen appears black.

    Hope I answered your question

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,926
    Quote Originally Posted by Airdeth View Post
    I also tried to use glMatrixMode, but I really don't understand how it works,
    glMatrixMode() controls which matrix is affected by subsequent matrix commands.

    The main reason for having separate projection and modelview matrices is that the projection matrix is often a perspective projection, and perspective projections interfere with the lighting calculations.

    Quote Originally Posted by Airdeth View Post
    by putting glMatrixMode(GL_PROJECTION) before glClearColor in my main loop, I am able to put bigger number in the gluPerspective function (Like zNear = 3 and zFar = 5), but my cubes shows up like this :
    I suspect that you may be accumulating transformations. The first operation on each matrix should normally be glLoadIdentity(), to reset the matrix. Otherwise, each matrix operation is accumulated with any previous operations on that matrix.

    Quote Originally Posted by Airdeth View Post
    But of what I read online, I understood that I have to set the PROJECTION matrix when I am moving my camera and the MODELVIEW when drawing my cubes ?
    Normally, the projection matrix contains a single glOrtho, glFrustum, gluOrtho2D or gluPerspective transformation, while everything else goes into the model-view matrix. The projection matrix is often set in the reshape callback, as the parameters usually depend upon the window size (or at least upon the aspect ratio).

  5. #5
    Junior Member Newbie
    Join Date
    Jun 2018
    Posts
    3
    Oh... Thank you for these informations !

    So now my gluPerspective line look like this :

    Code :
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(70, (double)SCREEN_WIDTH / SCREEN_HIGH, 2, 100);

    And the main loop is like this :
    Code :
           glClearColor(0.0, 0.0, 0.0, 1.0);
     
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    	glScalef(0.2, 0.2, 0.2);
    	gluLookAt(2,0,2,0,0,0,0,1,0);
    	do {
                    glLoadIdentity();
    		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    		for (short i = 0; i < 26; ++i) {
    			glPushMatrix();
    			drawCube(rubiks[i]);
    			glPopMatrix();
    		}
    		SDL_GL_SwapWindow(window);
    	} while (!quitApplication);

    I have quite impressive changes, but there is still a "camera" problem, here is a video (EPILEPSY WARNING):
    https://streamable.com/remxz

    As you can see the screen blink and when I don't rotate the cube it display nothing.
    So I tried to change my eyes coordinate in the gluLookAt, but it doesn't change anything, and when I change the zNear value, it's getting really messy when near 0 and show less thing when increased.

    I think my camera is in the cube, but do you know how to get out of here ?

    Thank you.

    EDIT : I Found the problem, I deleted the first glLoadIdentity in my while loop. Thank you so much for your help and knowledge !
    Last edited by Airdeth; 06-09-2018 at 07:08 AM.

  6. #6
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,926
    Quote Originally Posted by Airdeth View Post
    I think my camera is in the cube, but do you know how to get out of here ?
    That's down to the near distance of 2 in the gluPerspective() call. Reduce it a bit, but making it too small will reduce the accuracy of the depth buffer. 0.1 should be fine.

  7. #7
    Quote Originally Posted by Airdeth View Post
    I am using OpenGL 3.3
    These functions are deprecated in OpenGL 3+: glEnableClientState, glVertexPointer, glPushMatrix and so on.

    Start here:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •