cube.c
Go to the documentation of this file.
1 
2 /* Copyright (c) Mark J. Kilgard, 1997. */
3 
4 /* This program is freely distributable without licensing fees
5  and is provided without guarantee or warrantee expressed or
6  implied. This program is -not- in the public domain. */
7 
8 /* This program was requested by Patrick Earl; hopefully someone else
9  will write the equivalent Direct3D immediate mode program. */
10 
11 #include <GL/glut.h>
12 
13 GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; /* Red diffuse light. */
14 GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0}; /* Infinite light location. */
15 GLfloat n[6][3] = { /* Normals for the 6 faces of a cube. */
16  {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
17  {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };
18 GLint faces[6][4] = { /* Vertex indices for the 6 faces of a cube. */
19  {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
20  {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
21 GLfloat v[8][3]; /* Will be filled in with X,Y,Z vertexes. */
22 
23 void
24 drawBox(void)
25 {
26  int i;
27 
28  for (i = 0; i < 6; i++) {
29  glBegin(GL_QUADS);
30  glNormal3fv(&n[i][0]);
31  glVertex3fv(&v[faces[i][0]][0]);
32  glVertex3fv(&v[faces[i][1]][0]);
33  glVertex3fv(&v[faces[i][2]][0]);
34  glVertex3fv(&v[faces[i][3]][0]);
35  glEnd();
36  }
37 }
38 
39 void
40 display(void)
41 {
42  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
43  drawBox();
44  glutSwapBuffers();
45 }
46 
47 void
48 init(void)
49 {
50  /* Setup cube vertex data. */
51  v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
52  v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
53  v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
54  v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
55  v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
56  v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;
57 
58  /* Enable a single OpenGL light. */
59  glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
60  glLightfv(GL_LIGHT0, GL_POSITION, light_position);
61  glEnable(GL_LIGHT0);
62  glEnable(GL_LIGHTING);
63 
64  /* Use depth buffering for hidden surface elimination. */
65  glEnable(GL_DEPTH_TEST);
66 
67  /* Setup the view of the cube. */
68  glMatrixMode(GL_PROJECTION);
69  gluPerspective( 40.0, /* field of view in degree */
70  1.0, /* aspect ratio */
71  1.0, /* Z near */
72  10.0 /* Z far */
73  );
74  glMatrixMode(GL_MODELVIEW);
75  gluLookAt(0.0, 0.0, 5.0, /* eye is at (0,0,5) */
76  0.0, 0.0, 0.0, /* center is at (0,0,0) */
77  0.0, 1.0, 0.); /* up is in positive Y direction */
78 
79  /* Adjust cube position to be asthetic angle. */
80  glTranslatef(0.0, 0.0, -1.0);
81  glRotatef(60, 1.0, 0.0, 0.0);
82  glRotatef(-20, 0.0, 0.0, 1.0);
83 }
84 
85 int
86 main(int argc, char **argv)
87 {
88  glutInit(&argc, argv);
89  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
90  glutCreateWindow("red 3D lighted cube");
91  glutDisplayFunc(display);
92  init();
93  glutMainLoop();
94  return 0; /* ANSI C requires main to return int. */
95 }
faces
GLint faces[6][4]
Definition: cube.c:18
light_diffuse
GLfloat light_diffuse[]
Definition: cube.c:13
light_position
GLfloat light_position[]
Definition: cube.c:14
main
int main(int argc, char **argv)
Definition: cube.c:86
argc
static int argc
Definition: transargv.c:56
drawBox
void drawBox(void)
Definition: cube.c:24
display
void display(void)
Definition: cube.c:40
n
GLfloat n[6][3]
Definition: cube.c:15
v
GLfloat v[8][3]
Definition: cube.c:21
init
void init(void)
Definition: cube.c:48


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Jun 15 2023 02:06:43