Richard Connamacher

01-15-2011, 09:15 PM

I'm teaching myself OpenGL programming and wrote a cube-drawing algorithm. Unfortunately, as I rotate it, the perspective ends up very weird. The back of the cube gets drawn, in part, over the front of the cube.

I have enabled GL_DEPTH_TEST and GL_CULL_FACE in my setup code.

I've banged away at this in my spare time for a few days, but still can't figure out what's going on or how to fix it. All I can guess is that cube faces are being drawn in the wrong order. But then again, I thought OpenGL is supposed to keep track of that?

(Another problem it's having, which may or may not be related, is that some faces are visible when viewed from one direction, but disappear when rotated in the other direction.)

I've attached the screen shot. And here's the C function I wrote to draw the cube. Any idea what I'm doing wrong?

Thanks!

void mygl_draw_cuboid(myglDimensions dimensions, GLfloat faceColors[6][3]) {

// Vertices (name key: Top/Bottom, Left/Right, Front/Back)

recVec vertTLF, vertTRF, vertBLF, vertBRF, vertTLB, vertTRB, vertBLB, vertBRB;

vertTLF = (recVec) { 0 - dimensions.width/2, 0 - dimensions.height/2, 0 - dimensions.depth/2};

vertTRF = (recVec) { dimensions.width/2, 0 - dimensions.height/2, 0 - dimensions.depth/2};

vertBLF = (recVec) { 0 - dimensions.width/2, dimensions.height/2, 0 - dimensions.depth/2};

vertBRF = (recVec) { dimensions.width/2, dimensions.height/2, 0 - dimensions.depth/2};

vertTLB = (recVec) { 0 - dimensions.width/2, 0 - dimensions.height/2, dimensions.depth/2};

vertTRB = (recVec) { dimensions.width/2, 0 - dimensions.height/2, dimensions.depth/2};

vertBLB = (recVec) { 0 - dimensions.width/2, dimensions.height/2, dimensions.depth/2};

vertBRB = (recVec) { dimensions.width/2, dimensions.height/2, dimensions.depth/2};

recVec cube_faces[6][4] = {

{vertTRB, vertTLB, vertBLB, vertBRB}, // Back

{vertTRF, vertTLF, vertBLF, vertBRF}, // Front

{vertBRF, vertBLF, vertBLB, vertBRB}, // Bottom

{vertTRF, vertTLF, vertTLB, vertTRB}, // Top

{vertTLF, vertTLB, vertBLB, vertBLF}, // Left

{vertTRF, vertTRB, vertBRB, vertBRF} // Right

};

GLint cube_start_face = 0;

GLint cube_num_faces = 6;

for (long f=cube_start_face; f<cube_start_face+cube_num_faces; f++) {

glColor3f(faceColors[f][0], faceColors[f][1], faceColors[f][2]);

glBegin(GL_POLYGON);

glVertex3d(cube_faces[f][0].x, cube_faces[f][0].y, cube_faces[f][0].z);

glVertex3d(cube_faces[f][1].x, cube_faces[f][1].y, cube_faces[f][1].z);

glVertex3d(cube_faces[f][2].x, cube_faces[f][2].y, cube_faces[f][2].z);

glVertex3d(cube_faces[f][3].x, cube_faces[f][3].y, cube_faces[f][3].z);

glEnd();

}

}

recVec and myglDimensions are just structs I made. recVec is {GLdouble x, GLdouble y, GLdouble z} and myglDimensions is {GLdouble width, GLdouble height, GLdouble depth}. The different name styles is because recVec was copied from some sample code.

I have enabled GL_DEPTH_TEST and GL_CULL_FACE in my setup code.

I've banged away at this in my spare time for a few days, but still can't figure out what's going on or how to fix it. All I can guess is that cube faces are being drawn in the wrong order. But then again, I thought OpenGL is supposed to keep track of that?

(Another problem it's having, which may or may not be related, is that some faces are visible when viewed from one direction, but disappear when rotated in the other direction.)

I've attached the screen shot. And here's the C function I wrote to draw the cube. Any idea what I'm doing wrong?

Thanks!

void mygl_draw_cuboid(myglDimensions dimensions, GLfloat faceColors[6][3]) {

// Vertices (name key: Top/Bottom, Left/Right, Front/Back)

recVec vertTLF, vertTRF, vertBLF, vertBRF, vertTLB, vertTRB, vertBLB, vertBRB;

vertTLF = (recVec) { 0 - dimensions.width/2, 0 - dimensions.height/2, 0 - dimensions.depth/2};

vertTRF = (recVec) { dimensions.width/2, 0 - dimensions.height/2, 0 - dimensions.depth/2};

vertBLF = (recVec) { 0 - dimensions.width/2, dimensions.height/2, 0 - dimensions.depth/2};

vertBRF = (recVec) { dimensions.width/2, dimensions.height/2, 0 - dimensions.depth/2};

vertTLB = (recVec) { 0 - dimensions.width/2, 0 - dimensions.height/2, dimensions.depth/2};

vertTRB = (recVec) { dimensions.width/2, 0 - dimensions.height/2, dimensions.depth/2};

vertBLB = (recVec) { 0 - dimensions.width/2, dimensions.height/2, dimensions.depth/2};

vertBRB = (recVec) { dimensions.width/2, dimensions.height/2, dimensions.depth/2};

recVec cube_faces[6][4] = {

{vertTRB, vertTLB, vertBLB, vertBRB}, // Back

{vertTRF, vertTLF, vertBLF, vertBRF}, // Front

{vertBRF, vertBLF, vertBLB, vertBRB}, // Bottom

{vertTRF, vertTLF, vertTLB, vertTRB}, // Top

{vertTLF, vertTLB, vertBLB, vertBLF}, // Left

{vertTRF, vertTRB, vertBRB, vertBRF} // Right

};

GLint cube_start_face = 0;

GLint cube_num_faces = 6;

for (long f=cube_start_face; f<cube_start_face+cube_num_faces; f++) {

glColor3f(faceColors[f][0], faceColors[f][1], faceColors[f][2]);

glBegin(GL_POLYGON);

glVertex3d(cube_faces[f][0].x, cube_faces[f][0].y, cube_faces[f][0].z);

glVertex3d(cube_faces[f][1].x, cube_faces[f][1].y, cube_faces[f][1].z);

glVertex3d(cube_faces[f][2].x, cube_faces[f][2].y, cube_faces[f][2].z);

glVertex3d(cube_faces[f][3].x, cube_faces[f][3].y, cube_faces[f][3].z);

glEnd();

}

}

recVec and myglDimensions are just structs I made. recVec is {GLdouble x, GLdouble y, GLdouble z} and myglDimensions is {GLdouble width, GLdouble height, GLdouble depth}. The different name styles is because recVec was copied from some sample code.