cube.c
Go to the documentation of this file.
00001 
00002 /* Copyright (c) Mark J. Kilgard, 1997. */
00003 
00004 /* This program is freely distributable without licensing fees 
00005    and is provided without guarantee or warrantee expressed or 
00006    implied. This program is -not- in the public domain. */
00007 
00008 /* This program was requested by Patrick Earl; hopefully someone else
00009    will write the equivalent Direct3D immediate mode program. */
00010 
00011 #include <GL/glut.h>
00012 
00013 GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0};  /* Red diffuse light. */
00014 GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};  /* Infinite light location. */
00015 GLfloat n[6][3] = {  /* Normals for the 6 faces of a cube. */
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] = {  /* Vertex indices for the 6 faces of a cube. */
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];  /* Will be filled in with X,Y,Z vertexes. */
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   /* Setup cube vertex data. */
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   /* Enable a single OpenGL light. */
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   /* Use depth buffering for hidden surface elimination. */
00065   glEnable(GL_DEPTH_TEST);
00066 
00067   /* Setup the view of the cube. */
00068   glMatrixMode(GL_PROJECTION);
00069   gluPerspective(  40.0, /* field of view in degree */
00070                    1.0,  /* aspect ratio */ 
00071                    1.0,  /* Z near */ 
00072                    10.0  /* Z far */
00073                    );
00074   glMatrixMode(GL_MODELVIEW);
00075   gluLookAt(0.0, 0.0, 5.0,  /* eye is at (0,0,5) */
00076             0.0, 0.0, 0.0,  /* center is at (0,0,0) */
00077             0.0, 1.0, 0.);  /* up is in positive Y direction */
00078 
00079   /* Adjust cube position to be asthetic angle. */
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;             /* ANSI C requires main to return int. */
00095 }


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Mar 9 2017 04:57:49