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 :
[ATTACH=CONFIG]1797[/ATTACH]
Problem for some angles :
[ATTACH=CONFIG]1798[/ATTACH]
[ATTACH=CONFIG]1799[/ATTACH]
(Yes it is supposed to be a rubiks cube)
The code :
Initialization :
// 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
", 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 :
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 :
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.