-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrackballLightedCube.cpp
124 lines (105 loc) · 4.32 KB
/
TrackballLightedCube.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
//--------------------------------------------------------------------------
// Developer -- Bryan Crawley, et al.
// Course ----- CS3233
// Project ---- Class demo
// 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. Use the trackball feature of David Eck's camera API
// to enable the user to spin the cube with a mouse/trackball/etc.
//--------------------------------------------------------------------------
#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
#include <cstdio>
#include "camera.h" // Eck's camera API. You also need to compile camera.cpp.
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
glutMouseFunc(trackballMouseFunction); // Use Eck's mouse function.
glutMotionFunc(trackballMotionFunction); // Use Eck's motion function.
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); // Removed for Eck's camera.
//glLoadIdentity(); // Removed for Eck's camera.
//glOrtho(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f); // Removed for Eck's camera.
//glMatrixMode(GL_MODELVIEW); // Removed for Eck's camera.
cameraSetLimits(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0); // Eck camera; replaces 4 previous lines
cameraApply(); // Eck camera; replaces 4 previous lines
// 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);
// Adjust the initial 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(50.0f, 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[0]);
glVertex3iv(point[4]);
glVertex3iv(point[5]);
glVertex3iv(point[1]);
glEnd();
glFlush(); // Render now
}
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(640, 640);
glutInitWindowPosition(50, 50);
glutCreateWindow("Lighted Cube With Mouse Rotation");
glutDisplayFunc(display);
init(); // Various initializations; mostly lighting.
glutMainLoop();
return 0;
}