00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <GL/glut.h>
00012
00013 GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0};
00014 GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
00015 GLfloat n[6][3] = {
00016 {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
00017 {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };
00018 GLint faces[6][4] = {
00019 {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
00020 {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
00021 GLfloat v[8][3];
00022
00023 void
00024 drawBox(void)
00025 {
00026 int i;
00027
00028 for (i = 0; i < 6; i++) {
00029 glBegin(GL_QUADS);
00030 glNormal3fv(&n[i][0]);
00031 glVertex3fv(&v[faces[i][0]][0]);
00032 glVertex3fv(&v[faces[i][1]][0]);
00033 glVertex3fv(&v[faces[i][2]][0]);
00034 glVertex3fv(&v[faces[i][3]][0]);
00035 glEnd();
00036 }
00037 }
00038
00039 void
00040 display(void)
00041 {
00042 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00043 drawBox();
00044 glutSwapBuffers();
00045 }
00046
00047 void
00048 init(void)
00049 {
00050
00051 v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
00052 v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
00053 v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
00054 v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
00055 v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
00056 v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;
00057
00058
00059 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
00060 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00061 glEnable(GL_LIGHT0);
00062 glEnable(GL_LIGHTING);
00063
00064
00065 glEnable(GL_DEPTH_TEST);
00066
00067
00068 glMatrixMode(GL_PROJECTION);
00069 gluPerspective( 40.0,
00070 1.0,
00071 1.0,
00072 10.0
00073 );
00074 glMatrixMode(GL_MODELVIEW);
00075 gluLookAt(0.0, 0.0, 5.0,
00076 0.0, 0.0, 0.0,
00077 0.0, 1.0, 0.);
00078
00079
00080 glTranslatef(0.0, 0.0, -1.0);
00081 glRotatef(60, 1.0, 0.0, 0.0);
00082 glRotatef(-20, 0.0, 0.0, 1.0);
00083 }
00084
00085 int
00086 main(int argc, char **argv)
00087 {
00088 glutInit(&argc, argv);
00089 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
00090 glutCreateWindow("red 3D lighted cube");
00091 glutDisplayFunc(display);
00092 init();
00093 glutMainLoop();
00094 return 0;
00095 }