-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
370 lines (330 loc) · 9.09 KB
/
Copy pathmain.cpp
File metadata and controls
370 lines (330 loc) · 9.09 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*************************************************************************/
/* This is a mesh viewer written by Graham Tremper and Gabe Fierro */
/*************************************************************************/
#include <iostream>
#include <GLUT/glut.h>
#include "shaders.h"
#include "mesh.h"
#define BUFFER_OFFSET(i) (reinterpret_cast<void*>(i))
using namespace std;
/*** CONSTANTS ***/
const int MAXLIGHTS = 10;
const double WALKSPEED = 0.15;
const float SENSITIVITY = 0.3;
const vec3 UP = vec3(0.0,1.0,0.0);
const vec3 LEFT = vec3(-1.0,0.0,0.0);
const char * CONFIG = "config.txt";
/*** UPDATE FREQUENTLY ***/
vec3 trans;
float pitch;
float yaw;
float cameraPitch;
float cameraYaw;
int lastx, lasty; // For mouse motion
int currentLight;
int lastTime;
bool useWire;
bool useFlat;
bool moveLight;
bool cameraMode;
bool animate;
int collapseSpeed;
int width;
int height;
/*** SCENE PARAMETERS ***/
GLuint vertexshader, fragmentshader, shaderprogram ; // shaders
Mesh* mesh;
vec4 light_position[MAXLIGHTS]; //current position of the 10 lights
vec4 light_specular[MAXLIGHTS]; //color of lights
vec3 eye;
vec3 lookat;
float fovy;
int numLights;
/* Forward Declaration */
void parseConfig(const char*);
void draw();
Mesh* parseOFF(char*);
/* Variables to set uniform params for lighting fragment shader */
GLuint isWire;
GLuint isFlat;
GLuint numLightsShader;
GLuint ambient ;
GLuint diffuse ;
GLuint specular ;
GLuint shininess ;
GLuint emission ;
vec4 emis; //emission data
GLuint lightPosn;
GLuint lightColor;
void reshape(int w, int h){
glMatrixMode(GL_PROJECTION);
width = w;
height = h;
float widthf = w;
float heightf = h;
// zNear=0.1, zFar=99
mat4 mv = glm::perspectiveFov(fovy, widthf, heightf, 0.1f, 99.0f);
glLoadMatrixf(&mv[0][0]);
glViewport(0, 0, w, h);
}
void printHelp() {
std::cout << "\npress 'h' to print this message again.\n"
<< "press 'f' to toggle wireframe.\n"
<< "press 'p' to toggle flat shading.\n"
<< "use 'wasd' to move object.\n"
<< "use 'c' to move camera.\n"
<< "use 'm' to animate.\n"
<< "use 'up' or 'down' arrows to change level of detail.\n"
<< "use 'left' or 'right' arrows to change rate of collapse"
<< "use '1-9' to move lights.\n"
<< "press ESC to quit.\n\n";
}
/* Mouse Functions */
void mouseClick(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
lastx = x;
lasty = y;
}
}
void mouse(int x, int y) {
int diffx=x-lastx;
int diffy=y-lasty;
lastx=x; //set lastx to the current x position
lasty=y; //set lasty to the current y position
if (cameraMode) {
cameraYaw += diffx*SENSITIVITY*0.5f;
cameraPitch += diffy*SENSITIVITY*0.5f;
} else {
yaw += diffx*SENSITIVITY;
pitch -= diffy*SENSITIVITY;
}
if (yaw > 360) yaw -= 360.0f;
if (yaw < 0) yaw += 360.0f;
if (pitch > 180) pitch -= 360.0f;
if (pitch < -180) pitch += 360.0f;
}
/* Keyboard options */
void keyboard(unsigned char key, int x, int y) {
switch(key) {
case 'w':
if (moveLight) light_position[currentLight] -= vec4(0,0,SENSITIVITY,0);
else trans -= vec3(0,0,WALKSPEED);
break;
case 'a':
if (moveLight) light_position[currentLight] -= vec4(SENSITIVITY,0,0,0);
else trans -= vec3(WALKSPEED,0,0);
break;
case 's':
if (moveLight) light_position[currentLight] += vec4(0,0,SENSITIVITY,0);
else trans += vec3(0,0,WALKSPEED);
break;
case 'd':
if (moveLight) light_position[currentLight] += vec4(SENSITIVITY,0,0,0);
else trans += vec3(WALKSPEED,0,0);
break;
case 'q':
if (moveLight) light_position[currentLight] += vec4(0,SENSITIVITY,0,0);
else trans += vec3(0,WALKSPEED,0);
break;
case 'e':
if (moveLight) light_position[currentLight] -= vec4(0,SENSITIVITY,0,0);
else trans -= vec3(0,WALKSPEED,0);
break;
case 'h':
printHelp();
//mesh->debug();
break;
case 27: // Escape to quit
delete mesh;
exit(0);
break;
case 'p':
useFlat = !useFlat;
glUniform1i(isFlat, useFlat);
std::cout << "Flat shading is now set to" << (useFlat ? " true " : " false ") << "\n";
break;
case 'f':
useWire = !useWire;
moveLight = false;
glUniform1i(isWire, useWire);
if (useWire){
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
} else {
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
}
cout << "Wireframe is now set to" << (useWire ? " true " : " false ") << "\n";
break;
case 'c':
cameraMode = !cameraMode;
cameraPitch=0;
cameraYaw=0;
cout << "Camera rotation is now set to" << (cameraMode ? " true " : " false ") << "\n";
break;
case 'm':
animate = !animate;
lastTime = glutGet(GLUT_ELAPSED_TIME);
cout << "Animate is now set to" << (animate ? " true " : " false ") << "\n";
break;
case 48: //0
case 49: //1
case 50: //2
case 51: //3
case 52: //4
case 53: //5
case 54: //6
case 55: //7
case 56: //8
case 57: //9
int num = key-49;
if (num == -1) {
moveLight = false;
cout << "Controlling Model" << endl;
break;
} else if (num >= numLights) {
moveLight = false;
cout << "That light does not exist" << endl;
break;
}
moveLight = true;
currentLight = num;
cout << "Now controlling light "<<num+1<<"\n";
break;
}
glutPostRedisplay();
}
void specialKey(int key,int x,int y) {
switch(key) {
case 100: //left
if (collapseSpeed != 1){
collapseSpeed *= 0.1;
}
cout << "Now collapsing " << collapseSpeed << " triangles" << endl;
break;
case 101: //up
mesh->upLevelOfDetail(collapseSpeed);
mesh->update_buffer();
break;
case 102: //right
if (collapseSpeed != 100000){
collapseSpeed *= 10;
}
cout << "Now collapsing " << collapseSpeed << " triangles" << endl;
break;
case 103: //down
mesh->downLevelOfDetail(collapseSpeed);
mesh->update_buffer();
break;
}
glutPostRedisplay();
}
void init(char* filename) {
mesh = parseOFF(filename);
/* Default Values */
eye = vec3(0,0,-10);
trans = vec3(0,0,0);
numLights = 0;
collapseSpeed = 1;
fovy = 60;
useWire = false;
useFlat = false;
moveLight = false;
cameraMode = false;
animate = false;
width = 640;
height = 480;
glEnable(GL_DEPTH_TEST);
vertexshader = initshaders(GL_VERTEX_SHADER, "shaders/light.vert.glsl");
fragmentshader = initshaders(GL_FRAGMENT_SHADER, "shaders/light.frag.glsl");
shaderprogram = initprogram(vertexshader, fragmentshader);
isWire = glGetUniformLocation(shaderprogram,"wire");
isFlat = glGetUniformLocation(shaderprogram,"flat");
numLightsShader = glGetUniformLocation(shaderprogram,"numLights");
ambient = glGetUniformLocation(shaderprogram,"ambient");
diffuse = glGetUniformLocation(shaderprogram,"diffuse");
specular = glGetUniformLocation(shaderprogram,"specular");
shininess = glGetUniformLocation(shaderprogram,"shininess");
emission = glGetUniformLocation(shaderprogram,"emission");
lightPosn = glGetUniformLocation(shaderprogram,"lightPosn");
lightColor = glGetUniformLocation(shaderprogram,"lightColor");
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_INDEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, 32, BUFFER_OFFSET(0));
glNormalPointer(GL_FLOAT, 32, BUFFER_OFFSET(12));
glUniform1i(isFlat, useFlat);
glUniform1i(isWire, useWire);
parseConfig(CONFIG);
glUniform4fv(lightColor, MAXLIGHTS, (GLfloat*)&light_specular[0]);
glUniform4fv(lightPosn, MAXLIGHTS, (GLfloat*)&light_position[0]);
glUniform1i(numLightsShader, numLights);
}
vec3 direction(float &yaw, float &pitch, const vec3& dir) {
mat4 M = mat4(1.0f);
M = glm::rotate(M,yaw,UP);
vec3 final = mat3(M)*dir;
vec3 cross = glm::cross(final,UP);
M = glm::rotate(M,pitch,cross);
final = mat3(M)*final;
return final;
}
/* main display */
void display() {
if (useWire){
glClearColor(0,0,0,0);
} else {
glClearColor(135/225.0, 206/255.0, 250/255.0, 0);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
mat4 mv;
if (cameraMode) {
vec3 dir = direction(cameraYaw,cameraPitch, lookat-eye);
mv = glm::lookAt(eye, eye+dir, UP);
} else {
mv = glm::lookAt(eye, lookat, UP);
}
mv = glm::translate(mv,trans);
vec4 light[MAXLIGHTS];
for (int i=0; i<numLights; i++){
light[i] = mv * light_position[i];
if (moveLight) {
glUniform4fv(emission,1,&light_specular[i][0]);
mat4 lightTrans = glm::translate(mv,vec3(light_position[i]));
glLoadMatrixf(&lightTrans[0][0]);
glutSolidSphere(.4,20,20);
}
}
glUniform4fv(emission,1,&emis[0]);
glUniform4fv(lightPosn, MAXLIGHTS, (GLfloat*)&light[0]);
if (animate) {
int newTime = glutGet(GLUT_ELAPSED_TIME);
yaw += (newTime-lastTime)/100.0f;
lastTime=newTime;
}
mv = glm::rotate(mv,yaw,UP);
mv = glm::rotate(mv,pitch,LEFT);
glLoadMatrixf(&mv[0][0]);
mesh->draw();
glutSwapBuffers();
}
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "You need a mesh file as an argument\n";
exit(1);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutCreateWindow("Mesh Viewer");
init(argv[1]);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialKey);
glutReshapeFunc(reshape);
glutReshapeWindow(width,height);
glutIdleFunc(display);
glutMotionFunc(mouse);
glutMouseFunc(mouseClick) ;
printHelp();
glutMainLoop();
return 0;
}