-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha2.cpp
More file actions
276 lines (225 loc) · 6.28 KB
/
a2.cpp
File metadata and controls
276 lines (225 loc) · 6.28 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "trackball.h"
#include "primitive.h"
// Define the size of the window using these two variables
int window_width = 500, window_height = 500;
// These constants define the motion mode of mouse control
#define MOTION_NONE 0
#define MOTION_TRACKBALL 1
#define MOTION_ZOOM 2
#define MOTION_PAN 3
// These variables are used for controlling the mouse motion
int motion = MOTION_NONE;
int lastx, lasty;
float quat[4];
float eyex = 0, eyey = 0, eyez = 800;
// The display list of the teapot scene
GLuint teapot_list;
// These constants represent the teapot/robot
#define DISPLAY_TEAPOT 0
#define DISPLAY_ROBOT 1
// Display teapot or robot
int current_display = DISPLAY_TEAPOT;
/*
* Initialize the OpenGL settings and create the display list of the scene
*/
void InitSettings()
{
// Reset the parameter for trackball motion
trackball(quat, 0.0, 0.0, 0.0, 0.0);
// Set up OpenGL parameters
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glClearColor(0.6, 0.6, 0.6, 1.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_COLOR_MATERIAL);
// Set up the light
GLfloat position[] = {500, 500, 500, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHT0);
// Set up the material values for specular reflection
GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat shininess[] = {50.0};
glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
// Create a display list for the teapot scene
teapot_list = glGenLists(1);
glNewList(teapot_list, GL_COMPILE);
// Create the objects: a teapot and a floor
// The vertex ordering of the GLUT teapot is clockwise, therefore
// glFrontFace(GL_CW) is used before drawing
glFrontFace(GL_CW);
glColor3f(1, 0.5, 0.5);
glutSolidTeapot(250);
glFrontFace(GL_CCW);
// Draw the floor, note that the vertex ordering is counter-clockwise
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glNormal3f(0, 1, 0);
glVertex3f(500, -190, 500);
glVertex3f(500, -190, -500);
glVertex3f(-500, -190, -500);
glVertex3f(-500, -190, 500);
glEnd();
glEndList();
}
/*
* The callback function when a redraw has been requested
*/
void DisplayFunc()
{
// Create the screen for both colour and depth values
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// The modelview matrix is pushed before drawing the scene
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// Set up the trackball transformation
float rotmatrix[4][4];
build_rotmatrix(rotmatrix, quat);
glMultMatrixf(&rotmatrix[0][0]);
// Draw the scene from the display list
if (current_display == DISPLAY_TEAPOT)
glCallList(teapot_list);
else {
/*
*
* Draw your hierarchical tin robot model here
*
*/
glColor3f(0.0, 0.0, 1.0);
DrawBox(400, 400, 400);
}
glPopMatrix();
// Redraw the screen by swapping the buffer
glutSwapBuffers();
}
/*
* The callback function when the window is resized
*/
void ReshapeFunc(int w, int h)
{
// Set the viewport to be the same as the window
glViewport(0, 0, w, h);
// Set up the eye view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, eyez, 0, 0, 0, 0, 1, 0);
// Set up the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (GLfloat) w / (GLfloat) h, 1.0, 2000.0);
window_width = w;
window_height = h;
}
/*
* The callback function when a key is pressed
*/
void KeyboardFunc(unsigned char key, int x, int y)
{
switch (key) {
/*
*
* Add your keys for controlling the tin robot here
*
*/
case ' ':
// Switch the scene between teapot/tin robot
if (current_display == DISPLAY_TEAPOT)
current_display = DISPLAY_ROBOT;
else
current_display = DISPLAY_TEAPOT;
glutPostRedisplay();
break;
case 27:
// Exit the program using the escape key
exit(0);
break;
}
}
/*
* The callback function when a mouse button is pressed
*/
void MouseFunc(int button, int state, int x, int y)
{
// Control the current motion of the mouse control
if (state == GLUT_DOWN) {
lastx = x;
lasty = y;
motion = MOTION_NONE;
if (button == GLUT_LEFT_BUTTON)
motion = MOTION_TRACKBALL;
else if (button == GLUT_RIGHT_BUTTON) {
if (glutGetModifiers() == GLUT_ACTIVE_SHIFT)
motion = MOTION_PAN;
else
motion = MOTION_ZOOM;
}
}
}
/*
* The callback function when the mouse is moved
*/
void MotionFunc(int x, int y)
{
// Calculate the transformation for trackball control
if (motion == MOTION_TRACKBALL) {
float dq[4];
trackball(dq,
(2.0 * lastx - window_width) / window_width,
(window_height - 2.0 * lasty) / window_height,
(2.0 * x - window_width) / window_width,
(window_height - 2.0 * y) / window_height);
add_quats(dq, quat, quat);
lastx = x;
lasty = y;
glutPostRedisplay();
}
// Adjust the eye position for zooming
else if (motion == MOTION_ZOOM) {
eyez = eyez * (1.0 - (float) (lasty - y) / window_height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyex, eyey, eyez, eyex, eyey, 0, 0, 1, 0);
lastx = x;
lasty = y;
glutPostRedisplay();
}
// Adjust the eye position for panning
else if (motion == MOTION_PAN) {
eyex -= 2.0 * (x - lastx);
eyey += 2.0 * (y - lasty);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyex, eyey, eyez, eyex, eyey, 0, 0, 1, 0);
lastx = x;
lasty = y;
glutPostRedisplay();
}
}
int main(int argc, char** argv)
{
// Initialize the window with double and depth buffering
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(window_width, window_height);
glutCreateWindow("Assignment 2 - Hierarchical Modeling");
// Initialize the OpenGL settings
InitSettings();
// Set up the callback functions
glutKeyboardFunc(KeyboardFunc);
glutMouseFunc(MouseFunc);
glutMotionFunc(MotionFunc);
glutReshapeFunc(ReshapeFunc);
glutDisplayFunc(DisplayFunc);
// Start the main loop
glutMainLoop();
}