-
Notifications
You must be signed in to change notification settings - Fork 0
/
main01.cpp
executable file
·418 lines (345 loc) · 10.4 KB
/
main01.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/************************************************************************/
/* Graphics 317 coursework exercise 01 */
/* Author: Bernhard Kainz */
/* This file has to be altered for this exercise */
/************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <GL/glew.h>
#ifdef __linux__
#include <pthread.h>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#endif
#ifdef __APPLE__
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
///////////////////////////////////////////////////////////////////////
//helper for submission
#include "inc/lodepng.h"
///////////////////////////////////////////////////////////////////////
//declarations
void captureFrame();
void CheckOpenGLError(const char* stmt, const char* fname, int line);
#define _DEBUG 1
#if _DEBUG
#define GL_CHECK(stmt) do { \
stmt; \
CheckOpenGLError(#stmt, __FILE__, __LINE__); \
} while (0)
#else
#define GL_CHECK(stmt) stmt
#endif
///////////////////////////////////////////////////////////////////////
//shaders and light pos variables
GLuint v, f, p, g;
float lpos[4] = { 15.0, 0.5, 15.0, 0.0 };
int subdivLevel;
GLuint tex;
// mouse controls
/////////////////////////////////////////////////
//scene interaction variables
int mouse_old_x, mouse_old_y;
int mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0;
float move_x = 0.0, move_y = 0.0;
unsigned int win_width = 128, win_height = 128;
float translate_z = 0.0;
unsigned int frameCaptured = 0;
/////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// TODO finish the following three functions to complete Exercise 1
//////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//the actual render function, which is called for each frame
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
gluLookAt(0.0, 0.0, 1.0, 0.0, 0.0, -1.0, 0.0f, 1.0f, 0.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
/////////////////////////////////////////////////
//TODO add scene interaction code here
//Middle button translation
glTranslatef(move_x, move_y, 0.0);
//Left button rotation
glRotatef(rotate_y, 1.0, 0.0, 0.0);
glRotatef(rotate_x, 0.0, 1.0, 0.0);
//Right button zoom
glTranslatef(0.0, 0.0, translate_z);
/////////////////////////////////////////////////
GL_CHECK(glUseProgram(p));
glutSolidTeapot(0.5);
GL_CHECK(glUseProgram(0));
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
//helper function for submission. Will capture 2nd frame of task
frameCaptured++;
if (frameCaptured == 2)
{
renderID(win_width, win_height);
captureFrame();
}
GL_CHECK(glutSwapBuffers());
}
///////////////////////////////////////////////////////////////////////
// mouse interaction functions
void mouseClick(int button, int state, int x, int y)
{
/////////////////////////////////////////////////
// TODO add code to handle mouse click events
// use GLUT_UP and GLUT_DOWN to evaluate the current
// "state" of the mouse.
/////////////////////////////////////////////////
if (state == GLUT_DOWN)
{
mouse_buttons = button;
}
if (state == GLUT_UP)
{
mouse_buttons = 666; //arbitrary high number
}
//store mouse position coordinates
mouse_old_x = x;
mouse_old_y = y;
/////////////////////////////////////////////////
}
void mouseMotion(int x, int y)
{
/////////////////////////////////////////////////
// TODO add code to handle mouse move events
// and calculate reasonable values for object
// rotations
/////////////////////////////////////////////////
float diffx, diffy;
//How much the mouse has moved by
diffx = x - mouse_old_x;
diffy = y - mouse_old_y;
//Move
if (mouse_buttons == GLUT_MIDDLE_BUTTON)
{
move_x += diffx * f/win_width;
move_y += -1*(diffy * f/win_height);
}
//Rotate
if (mouse_buttons == GLUT_LEFT_BUTTON)
{
rotate_x += diffx * 0.5f;
rotate_y += diffy * 0.5f;
}
//Zoom
if (mouse_buttons == GLUT_RIGHT_BUTTON)
{
translate_z += diffy * f/win_height;
}
mouse_old_x = x;
mouse_old_y = y;
/////////////////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////
//nothing to do from here on
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//Text file IO for shader files
char *textFileRead(char *fn)
{
FILE *fp;
char *content = NULL;
int count = 0;
if (fn != NULL) {
fp = fopen(fn, "rt");
if (fp != NULL) {
fseek(fp, 0, SEEK_END);
count = ftell(fp);
rewind(fp);
if (count > 0) {
content = (char *)malloc(sizeof(char) * (count + 1));
count = fread(content, sizeof(char), count, fp);
content[count] = '\0';
}
fclose(fp);
}
}
if (content == NULL)
{
fprintf(stderr, "ERROR: could not load in file %s\n", fn);
exit(1);
}
return content;
}
///////////////////////////////////////////////////////////////////////
//helper for submission
void captureFrame()
{
unsigned char* pixels = new unsigned char[3 * win_width * win_height];
glReadPixels(0, 0, win_width, win_height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
encodeOneStep(pixels, win_width, win_height, std::string("01"));
delete[] pixels;
}
///////////////////////////////////////////////////////////////////////
//keyboard functions
void processNormalKeys(unsigned char key, int x, int y) {
if (key == 27)
exit(0);
}
///////////////////////////////////////////////////////////////////////
//adapt viewport when window size changes
void changeSize(int w, int h)
{
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (h == 0)
h = 1;
float ratio = 1.0* w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45, ratio, 0.1, 1000);
glMatrixMode(GL_MODELVIEW);
win_width = w;
win_height = h;
}
///////////////////////////////////////////////////////////////////////
//load, compile and set the shaders
void setShaders()
{
char *vs, *fs, *gs;
v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
vs = textFileRead(const_cast<char *>("./shader01.vert"));
fs = textFileRead(const_cast<char *>("./shader01.frag"));
const char * ff = fs;
const char * vv = vs;
GL_CHECK(glShaderSource(v, 1, &vv, NULL));
GL_CHECK(glShaderSource(f, 1, &ff, NULL));
free(vs); free(fs);
GL_CHECK(glCompileShader(v));
GL_CHECK(glCompileShader(f));
GLint blen = 0;
GLsizei slen = 0;
glGetShaderiv(v, GL_INFO_LOG_LENGTH, &blen);
if (blen > 1)
{
GLchar* compiler_log = (GLchar*)malloc(blen);
glGetShaderInfoLog(v, blen, &slen, compiler_log);
std::cout << "compiler_log vertex shader:\n" << compiler_log << std::endl;
free(compiler_log);
}
blen = 0;
slen = 0;
glGetShaderiv(f, GL_INFO_LOG_LENGTH, &blen);
if (blen > 1)
{
GLchar* compiler_log = (GLchar*)malloc(blen);
glGetShaderInfoLog(f, blen, &slen, compiler_log);
std::cout << "compiler_log fragment shader:\n" << compiler_log << std::endl;
free(compiler_log);
}
p = glCreateProgram();
GL_CHECK(glAttachShader(p, f));
GL_CHECK(glAttachShader(p, v));
GL_CHECK(glLinkProgram(p));
//comment out this line to not use the shaders at all
GL_CHECK(glUseProgram(p));
}
void initialize()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat aspect = (GLfloat)320 / 320;
gluPerspective(45, aspect, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
GLfloat amb_light[] = { 0.1, 0.1, 0.1, 1.0 };
GLfloat diffuse[] = { 0.6, 0.6, 0.6, 1 };
GLfloat specular[] = { 0.7, 0.7, 0.3, 1 };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb_light);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_SMOOTH);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glTranslatef(0.0, 0.0, -1.0);
}
///////////////////////////////////////////////////////////////////////
//main, setup and execution of environment
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(320, 320);
glutCreateWindow("Computer Graphics");
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);
glutKeyboardFunc(processNormalKeys);
//Here!
glutMouseFunc(mouseClick);
glutMotionFunc(mouseMotion);
glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 1.0);
glewInit();
if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)
printf("Ready for GLSL\n");
else {
printf("No GLSL support\n");
exit(1);
}
if (glewIsSupported("GL_VERSION_3_1"))
printf("Ready for OpenGL 3.1\n");
else {
printf("OpenGL 3.1 not supported\n");
exit(1);
}
if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader && GL_EXT_geometry_shader4)
printf("Ready for GLSL - vertex, fragment, and geometry units\n");
else {
printf("Not totally ready :( \n");
exit(1);
}
initialize();
#ifdef __linux__
int i=pthread_getconcurrency();
#endif
setShaders();
glutMainLoop();
return EXIT_SUCCESS;
}
//////////////////////////////////////////////////////////////////////
//Error checking functions
void CheckOpenGLError(const char* stmt, const char* fname, int line)
{
GLenum err = glGetError();
if (err != GL_NO_ERROR)
{
printf("OpenGL error %08x, at %s:%i - for %s\n", err, fname, line, stmt);
abort();
}
}