PDA

View Full Version : Rendering a Cube?



Thales
01-18-2002, 04:01 PM
Hello,

I have been working on drawing primitives using glBegin(GL_QUAD) and glEnd(). I'm specifically trying to render a simple cube.

However, upon rendering only the first two faces render, the next four don't render at all. I'm not sure why, because the data itself is right, and upon printing to console window the data looks right, yet the other four sides don't seem to want to render. The first two sides render perfectly.

I've color coded each face so that I know which side is being rendered, and have commented out the normals for now.

Anyone have any ideas? I'm sure I'm not doing something at the most basic level!

I placed the key code below.

Many thanks!

CODE:





struct face {
GLint a;
GLint b;
GLint c;
GLint d;
};

struct Vert {
GLfloat i;
GLfloat j;
GLfloat k;
GLfloat x;
GLfloat y;
GLfloat z;
};

Vert verts[8] = {
{ 0.577349185944,0.577349185944,-0.577349185944,
1.0,1.0,-1.0 },
{ 0.577349185944,-0.577349185944,-0.577349185944,
1.0, -1.0, -1.0 },
{ -0.577349185944,-0.577349185944,-0.577349185944,
-1.0,-1.0,-1.0 },
{ -0.577349185944,0.577349185944,-0.577349185944,
-1.0,1.0,-1.0 },
{ 0.577349185944,0.577349185944,0.577349185944,
1.0,1.0,1.0 },
{ 0.577349185944,-0.577349185944,-0.577349185944,
1.0,-1.0,1.0 },
{ -0.577349185944,-0.577349185944,0.577349185944,
-1.0,-1.0,1.0 },
{ -0.577349185944,0.577349185944,0.577349185944,
-1.0,1.0,1.0}
};

face faces[6]={
{0, 1, 2, 3},
{4, 7, 6, 5},
{0, 4, 5, 1},
{1, 5, 6, 2},
{2, 6, 7, 3},
{4, 0, 3, 7}
};

void display(void)
{
/* clear all pixels */
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/* draw white polygon (rectangle) with corners at
* (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
*/
glRotatef(60.0, 1.0, 0.0, 0.0);
//glRotatef(45.0, 0.0, 1.0, 0.0);
for (int i=0;i<6;i++) {
if (i == 0)
glColor3f(1.0,0.0,0.0); // Red
else if (i == 1)
glColor3f(0.0,1.0,0.0); // Green
else if (i == 2)
glColor3f(1.0,1.0,0.0); // Yellow
else if (i == 3)
glColor3f(0.0,0.0,1.0); // Blue
else if (i == 4)
glColor3f(1.0,0.0,1.0); // Magenta
else
glColor3f(0.0,1.0,1.0); // Cyan
glBegin(GL_QUADS);
//glNormal3f(verts[faces[i].a].i,verts[faces[i].a].j, \
verts[faces[i].a].k);
glVertex3f(verts[faces[i].a].x,verts[faces[i].a].y, \
verts[faces[i].a].z);
//glVertex3fv(verts[faces[i].a]);
//glNormal3f(verts[faces[i].b].i,verts[faces[i].b].j, \
verts[faces[i].a].k);
glVertex3f(verts[faces[i].b].x,verts[faces[i].b].y,
verts[faces[i].a].z);
//glNormal3f(verts[faces[i].c].i,verts[faces[i].c].j, \
verts[faces[i].a].k);
glVertex3f(verts[faces[i].c].x,verts[faces[i].c].y,
verts[faces[i].a].z);
// glNormal3f(verts[faces[i].d].i,verts[faces[i].d].j, \
verts[faces[i].a].k);
glVertex3f(verts[faces[i].d].x,verts[faces[i].d].y,
verts[faces[i].a].z);
glEnd();
}


/* don't wait!
* start processing buffered OpenGL routines
*/
glFlush ();
}

Questions Burner
01-18-2002, 04:08 PM
Did you glEnable(GL_DEPTH_TEST)?

Thales
01-18-2002, 04:16 PM
Yes, in the code below. Note, it does render the first two sides.





void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
}

styx
01-18-2002, 05:18 PM
did you define the vertice-indices of the faces in the right order ?
render the thing with glDisable(GL_CULL_FACE)
when it works, you defined the faces wrong

--styx

Thales
01-18-2002, 06:01 PM
Thanks both of you for your suggestions.


I just implemented glDisable(GL_CULL_FACE) and it had no effect.

Btw, I was under the impression that the depth buffer would deal with the issue of z ordering, so that it wouldn't matter the order of rendering. Is that right?


P.S. I'm rendering on a Win2000 platform, and I just tried the same code on Win98, and it gives the same results.


Thanks again.

Spartacus
01-19-2002, 02:56 AM
glVertex3f(verts[faces[i].b].x,
verts[faces[i].b].y, verts[faces[i].a].z);
*

Notice the letter 'a' marked with star. Should't it be 'b'. You have same typo in every glVertex3f call.

Thales
01-19-2002, 07:12 AM
Good catch, Spartacus. I should have caught that one. http://www.opengl.org/discussion_boards/ubb/frown.gif

Works just fine now.

Thanks.