-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimatedLightedCube.cpp
132 lines (112 loc) · 4 KB
/
AnimatedLightedCube.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//--------------------------------------------------------------------------
// Developer -- Bryan Crawley, et al.
// Course ----- CS3233
// Project ---- Class demo: Lighted Cube
// Due date --- N/A
//
// Draw a cube, and light it with one light source. Set reflectivity
// attributes for the faces of the cube. Use default attributes for the
// light source.
//--------------------------------------------------------------------------
#ifdef _WIN32
#include <GL/glut.h>
#elif __linux__
#include <GL/glut.h>
#elif __APPLE__
#define GL_SILENCE_DEPRECATION
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
#endif
void init()
{
glEnable(GL_LIGHTING); // Enable lighting.
glEnable(GL_LIGHT0); // Turn on a light. Use default light attributes.
glEnable(GL_NORMALIZE); // OpenGL will make all normal vectors into unit normals
glEnable(GL_CULL_FACE);
return;
}
void display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f);
glMatrixMode(GL_MODELVIEW);
// Use these points for the vertices of the cube.
int point[][3] = {
{0,0,0}, {0,0,1}, {0,1,0}, {0,1,1},
{1,0,0}, {1,0,1}, {1,1,0}, {1,1,1}
};
// Use this material for all the vertices of the cube. If we wanted
// to, we could set the material for each vertex individually. We
// don't want to for this application.
float cube_color[] = { 0.7f, 0.0f, 0.7f, 1.0f }; // Go Bisons!
glMaterialfv(GL_FRONT, GL_DIFFUSE, cube_color);
glMaterialfv(GL_FRONT, GL_SPECULAR, cube_color);
glMaterialfv(GL_FRONT, GL_AMBIENT, cube_color);
glMaterialf(GL_FRONT, GL_SHININESS, 50.0F);
//glPushMatrix();
// Adjust the location and orientation of the cube. Translate it
// so it is centered on the origin, and give it an attractive tilt.
//glTranslatef(-0.5f, -0.5f, -0.5f);
glRotatef(2.0, 1.0f, 1.0f, 1.0f);
// Use GL_QUADS to draw the cube. Remember that quads are deprecated.
// We could use triangles to draw each face of the cube if we wanted to.
glBegin(GL_QUADS);
glNormal3i(0, 0, 1); // Front face
glVertex3iv(point[3]);
glVertex3iv(point[1]);
glVertex3iv(point[5]);
glVertex3iv(point[7]);
glNormal3i(1, 0, 0); // Right face
glVertex3iv(point[7]);
glVertex3iv(point[5]);
glVertex3iv(point[4]);
glVertex3iv(point[6]);
glNormal3i(0, 0, -1); // Back face
glVertex3iv(point[6]);
glVertex3iv(point[4]);
glVertex3iv(point[0]);
glVertex3iv(point[2]);
glNormal3i(-1, 0, 0); // Left face
glVertex3iv(point[2]);
glVertex3iv(point[0]);
glVertex3iv(point[1]);
glVertex3iv(point[3]);
glNormal3i(0, 1, 0); // Top face
glVertex3iv(point[2]);
glVertex3iv(point[3]);
glVertex3iv(point[7]);
glVertex3iv(point[6]);
glNormal3i(0, -1, 0); // Bottom face
glVertex3iv(point[1]);
glVertex3iv(point[0]);
glVertex3iv(point[4]);
glVertex3iv(point[5]);
glEnd();
//glPopMatrix();
//glFlush(); // Render now
glutSwapBuffers(); // Substitute for glFlush() when double buffering.
//glutPostRedisplay();
}
void timer(int unused)
{
glutPostRedisplay();
// 17 msec is about 60 frames per second.
// 33 msec is about 30 frames per second.
glutTimerFunc(17, timer, 0);
}
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(640, 640);
glutInitWindowPosition(50, 50);
glutCreateWindow("Lighted Cube With Rotation");
glutDisplayFunc(display);
init(); // Various initializations; mostly lighting.
timer(0);
glutMainLoop();
return 0;
}