Hi all!
I wrote a program in which I draw a cube on a background image. The problem is that some faces of the cube are not visible and I can’t see how to correct it. I tried a lot of things (cull faces, depth test, …) but it still doesn’t work.
Here is my code :
- (void)setupView {
const GLfloat zNear = 0.1, zFar = 1000.0, fieldOfView = 60.0;
GLfloat size;
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
CGRect rect = self.bounds;
glFrustumf(-size, size, -size / (rect.size.width / rect.size.height), size / (rect.size.width / rect.size.height), zNear, zFar);
glViewport(0, 0, rect.size.width, rect.size.height);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
- (void)drawView {
const GLfloat cubeVertices[] = {
// face de devant
-1.0, 1.0, 1.0,
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
// haut
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
// arrière
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
// dessous
-1.0, -1.0, 1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
// gauche
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
-1.0, -1.0, 1.0,
-1.0, -1.0, -1.0,
// droit
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0
};
const GLshort cubeTextureCoords[] = {
// devant
0, 1,
0, 0,
1, 0,
1, 1,
// dessus
0, 1,
0, 0,
1, 0,
1, 1,
// derrière
0, 1,
0, 0,
1, 0,
1, 1,
// dessous
0, 1,
0, 0,
1, 0,
1, 1,
// gauche
0, 1,
0, 0,
1, 0,
1, 1,
// droite
0, 1,
0, 0,
1, 0,
1, 1,
};
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glViewport(0, 0, backingWidth, backingHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//-------------------------------------------
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrthof(-1, 1, -1, 1,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* I draw my background image here */
glPopMatrix();
//-------------------------------------------
[self setupView];
glMatrixMode(GL_MODELVIEW);
// dessin du cube
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0, 0.0, -5.0);
glRotatef(xRotate, 0.0, 1.0, 0.0);
glRotatef(yRotate, 1.0, 0.0, 0.0);
glVertexPointer(3, GL_FLOAT, 0, cubeVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(0.0, 0.0, 0.8, 1.0);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glColor4f(0.0, 0.8, 0.0, 1.0);
glDrawArrays(GL_TRIANGLE_FAN, 4, 4);
glColor4f(0.8, 0.0, 0.0, 1.0);
glDrawArrays(GL_TRIANGLE_FAN, 8, 4);
glColor4f(0.8, 0.0, 0.8, 1.0);
glDrawArrays(GL_TRIANGLE_FAN, 12, 4);
glColor4f(0.8, 0.8, 0.8, 1.0);
glDrawArrays(GL_TRIANGLE_FAN, 16, 4);
glColor4f(0.8, 0.8, 0.0, 1.0);
glDrawArrays(GL_TRIANGLE_FAN, 20, 4);
glPopMatrix();
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
Could someone tell me what is wrong ?
Thanks!