Drawing a Cube - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Drawing a Cube

gosudrewgosudrew Posts: 6Member
Hi,

I'm trying to determine why my code below does not correctly draw a rotating cube. I specified normals for each of the vertices and enabled GL_CULL_FACE followed by a call to glCullFace(GL_BACK), however I can still see through some polygons. Although I can get a rotating cube to draw correctly by enabling GL_DEPTH_TEST, I can't seem to figure out why this way does not work. Could it be that I defined the normals incorrectly?

Any help would be greatly appreciated.



[code]
#include
#include

GLdouble spin = 0.0;

static GLfloat vertices[] = {1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0,
1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0,
1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0,
-1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0,
-1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0,
-1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0};

static GLfloat colors[] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0,
1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0,
1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0};

static GLfloat normals[] = {0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0,
1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0,
0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
-1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0};


void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glNormalPointer(GL_FLOAT, 0, normals);
glColorPointer(3, GL_FLOAT, 0, colors);

glTranslatef(0.0, 0.0, -100.0);
glRotatef(spin, 1.0, 0.5, 0.8);
glScalef(25.0, 25.0, 25.0);

glBegin(GL_QUADS);
for (int i = 0; i < 24; i = i + 4)
{
glArrayElement(i);
glArrayElement(i+1);
glArrayElement(i+2);
glArrayElement(i+3);
}
glEnd();

glutSwapBuffers();
glFlush();
}

void spin_display()
{
spin = spin + .5;
if (spin > 360.0)
spin = spin - 360;
glutPostRedisplay();
}

void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
}

void reshape (int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50, 50, -50, 50, 5, 1000);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(spin_display);
glutMainLoop();
return 0;
}
[/code]

Comments

  • gautamgautam Posts: 642Member
    [b][red]This message was edited by gautam at 2005-6-21 16:57:12[/red][/b][hr]
    Hi,

    You seem to be drawing clockwise rather than anti-clockwise

    glFrontFace(GL_CW); should fix your problem. By default OpenGL draws in CCW mode.

    : Hi,
    :
    : I'm trying to determine why my code below does not correctly draw a rotating cube. I specified normals for each of the vertices and enabled GL_CULL_FACE followed by a call to glCullFace(GL_BACK), however I can still see through some polygons. Although I can get a rotating cube to draw correctly by enabling GL_DEPTH_TEST, I can't seem to figure out why this way does not work. Could it be that I defined the normals incorrectly?
    :
    : Any help would be greatly appreciated.
    :
    :
    :
    : [code]
    : #include
    : #include
    :
    : GLdouble spin = 0.0;
    :
    : static GLfloat vertices[] = {1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0,
    : 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0,
    : 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0,
    : -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0,
    : -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0,
    : -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0};
    :
    : static GLfloat colors[] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
    : 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0,
    : 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,
    : 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0,
    : 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
    : 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0};
    :
    : static GLfloat normals[] = {0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0,
    : 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0,
    : 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
    : 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0,
    : 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
    : -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0, -1.0, 0.0, 0.0};
    :
    :
    : void display(void)
    : {
    : glClear(GL_COLOR_BUFFER_BIT);
    : glLoadIdentity();
    :
    : glEnableClientState(GL_VERTEX_ARRAY);
    : glEnableClientState(GL_NORMAL_ARRAY);
    : glEnableClientState(GL_COLOR_ARRAY);
    : glVertexPointer(3, GL_FLOAT, 0, vertices);
    : glNormalPointer(GL_FLOAT, 0, normals);
    : glColorPointer(3, GL_FLOAT, 0, colors);
    :
    : glTranslatef(0.0, 0.0, -100.0);
    : glRotatef(spin, 1.0, 0.5, 0.8);
    : glScalef(25.0, 25.0, 25.0);
    :
    : glBegin(GL_QUADS);
    : for (int i = 0; i < 24; i = i + 4)
    : {
    : glArrayElement(i);
    : glArrayElement(i+1);
    : glArrayElement(i+2);
    : glArrayElement(i+3);
    : }
    : glEnd();
    :
    : glutSwapBuffers();
    : glFlush();
    : }
    :
    : void spin_display()
    : {
    : spin = spin + .5;
    : if (spin > 360.0)
    : spin = spin - 360;
    : glutPostRedisplay();
    : }
    :
    : void init (void)
    : {
    : glClearColor (0.0, 0.0, 0.0, 0.0);
    : glShadeModel (GL_FLAT);
    : glEnable(GL_CULL_FACE);
    : glCullFace(GL_BACK);
    : }
    :
    : void reshape (int w, int h)
    : {
    : glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    : glMatrixMode(GL_PROJECTION);
    : glLoadIdentity();
    : glOrtho(-50, 50, -50, 50, 5, 1000);
    : glMatrixMode(GL_MODELVIEW);
    : }
    :
    : int main(int argc, char** argv)
    : {
    : glutInit(&argc, argv);
    : glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    : glutInitWindowSize(500, 500);
    : glutCreateWindow(argv[0]);
    : init();
    : glutDisplayFunc(display);
    : glutReshapeFunc(reshape);
    : glutIdleFunc(spin_display);
    : glutMainLoop();
    : return 0;
    : }
    : [/code]
    :



Sign In or Register to comment.