PDA

View Full Version : rotating a cube



Zork
11-15-2002, 07:48 AM
Hi!
I am using the following code to rotate a cube:

#include <gl/glut.h>

GLfloat angle1 = 0.0;
GLfloat angle2 = 0.0;


void display(void) {
GLfloat x1[] = {-0.5, 0.5, -0.5};
GLfloat x2[] = {-0.5, 0.5, 0.5};
GLfloat x3[] = {0.5, 0.5, 0.5};
GLfloat x4[] = {0.5, 0.5, -0.5};
GLfloat x5[] = {-0.5, -0.5, -0.5};
GLfloat x6[] = {-0.5, -0.5, 0.5};
GLfloat x7[] = {0.5, -0.5, 0.5};
GLfloat x8[] = {0.5, -0.5, -0.5};

glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle1, 0.0, 1.0, 0.0);
glRotatef(angle2, 1.0, 0.0, 0.0);
glBegin(GL_QUADS);
//top
glColor3f(1.0, 1.0, 1.0);
glVertex3fv(x1);
glVertex3fv(x2);
glVertex3fv(x3);
glVertex3fv(x4);
//bottom
glColor3f(0.0, 1.0, 0.0);
glVertex3fv(x5);
glVertex3fv(x6);
glVertex3fv(x7);
glVertex3fv(x8);
//front
glColor3f(1.0, 1.0, 0.0);
glVertex3fv(x1);
glVertex3fv(x4);
glVertex3fv(x8);
glVertex3fv(x5);
//back
glColor3f(0.0, 0.0, 1.0);
glVertex3fv(x2);
glVertex3fv(x3);
glVertex3fv(x7);
glVertex3fv(x6);
//left
glColor3f(1.0, 0.0, 1.0);
glVertex3fv(x2);
glVertex3fv(x1);
glVertex3fv(x5);
glVertex3fv(x6);
//right
glColor3f(0.0, 1.0, 1.0);
glVertex3fv(x4);
glVertex3fv(x3);
glVertex3fv(x7);
glVertex3fv(x8);
glEnd();//GL_QUADS

glutSwapBuffers();
glPopMatrix();
}

void keyboard(unsigned char key, int x, int y) {

switch(key) {
case 27: exit(0);
break;
}
}


void rotate(int key, int x, int y) {

switch(key) {
case GLUT_KEY_LEFT: angle1++;break;
case GLUT_KEY_RIGHT: angle1--;break;
case GLUT_KEY_UP: angle2++;break;
case GLUT_KEY_DOWN: angle2--;break;
}
}

int main(int argc, char * argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutGameModeString("800x600:32");
glutEnterGameMode();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(rotate);
glutIdleFunc(display);
glutMainLoop();

return 0;
}

but there is no red(allthough the top face must be red) and some faces become transparent when rotating the cube.How can I change my code to draw a solid cube with a red in it?

Allan Walton
11-15-2002, 08:08 AM
Hmmmm best guess is the polygon mode

use glPolygonMode(GL_SMOOTH); (or something - can't remember off the top off my head - use the red book)

ta.

Allan

Allan Walton
11-15-2002, 08:11 AM
Doh !

forget all that (it's late friday afternoon) look up info on back facing polygons, glCullFace() and depth testing glDepthFunc()

by transperent do you mean "not there"?

Bob
11-15-2002, 08:12 AM
You haven't enabled depth test.

Zork
11-15-2002, 08:38 AM
Thanks! I enabled the depth test, and all is ok for now.I have a question: Do I have to enable the depth test for all the solid objects I may draw?

nexusone
11-15-2002, 09:12 AM
once enabled it will stay that way until a glDisable command is issued.

Yes and not only for solids but also for lines and other object.



Originally posted by Zork:
Thanks! I enabled the depth test, and all is ok for now.I have a question: Do I have to enable the depth test for all the solid objects I may draw?