-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
186 lines (165 loc) · 4.77 KB
/
main.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
#include <stdlib.h>
#include <iostream>
// OSX systems need their own headers
#ifdef __APPLE__
#include <OpenGL/gl3.h>
#include <OpenGL/glext.h>
#include <GLUT/glut.h>
#else
#include <GL/glew.h>
#include <GL/glut.h>
#endif
// Use of degrees is deprecated. Use radians for GLM functions
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include "Screenshot.h"
#include "Scene.h"
#include "RTVersion/Image.h"
#include "RTVersion/RTScene.h"
#include "RTVersion/RayTracer.h"
using namespace RayTracer;
static const int width = 800;
static const int height = 600;
static const char* title = "Scene viewer";
static bool rayTrace = false;
static const glm::vec4 background(0.1f, 0.2f, 0.3f, 1.0f);
static Scene scene;
static RTScene rtscene;
static Image image(width, height);
#include "hw3AutoScreenshots.h"
void printHelp(){
std::cout << R"(
Available commands:
press 'H' to print this message again.
press Esc to quit.
press 'O' to save a screenshot.
press the arrow keys to rotate camera.
press 'A'/'Z' to zoom.
press 'R' to reset camera.
press 'L' to turn on/off the lighting.
press Spacebar to generate images for hw3 submission.
)";
}
void initialize(void){
printHelp();
glClearColor(background[0], background[1], background[2], background[3]); // background color
glViewport(0,0,width,height);
// Initialize scene
scene.init();
// Initialize image
image.init();
// Initialize ray tracing scene
rtscene.init();
rtscene.camera -> zoom(0.9f);
rtscene.camera -> rotateUp(15.0f);
rtscene.camera -> rotateRight(30.0f);
rtscene.buildTriangleSoup();
Raytrace(*(rtscene.camera), rtscene, image);
// Enable depth test
glEnable(GL_DEPTH_TEST);
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
if(rayTrace){
image.draw();
} else {
scene.draw();
}
glutSwapBuffers();
glFlush();
}
void saveScreenShot(const char* filename = "test.png"){
int currentwidth = glutGet(GLUT_WINDOW_WIDTH);
int currentheight = glutGet(GLUT_WINDOW_HEIGHT);
Screenshot imag = Screenshot(currentwidth,currentheight);
imag.save(filename);
}
void keyboard(unsigned char key, int x, int y){
switch(key){
case 27: // Escape to quit
exit(0);
break;
case 'h': // print help
printHelp();
break;
case 'o': // save screenshot
saveScreenShot();
break;
case 'r':
scene.camera -> aspect_default = float(glutGet(GLUT_WINDOW_WIDTH))/float(glutGet(GLUT_WINDOW_HEIGHT));
scene.camera -> reset();
glutPostRedisplay();
break;
case 'a':
scene.camera -> zoom(0.9f);
glutPostRedisplay();
break;
case 'z':
scene.camera -> zoom(1.1f);
glutPostRedisplay();
break;
case 'l':
scene.shader -> enablelighting = !(scene.shader -> enablelighting);
glutPostRedisplay();
break;
case 'i':
rayTrace = !rayTrace;
glutPostRedisplay();
break;
case ' ':
hw3AutoScreenshots();
glutPostRedisplay();
break;
default:
glutPostRedisplay();
break;
}
}
void specialKey(int key, int x, int y){
switch (key) {
case GLUT_KEY_UP: // up
scene.camera -> rotateUp(-10.0f);
glutPostRedisplay();
break;
case GLUT_KEY_DOWN: // down
scene.camera -> rotateUp(10.0f);
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT: // right
scene.camera -> rotateRight(-10.0f);
glutPostRedisplay();
break;
case GLUT_KEY_LEFT: // left
scene.camera -> rotateRight(10.0f);
glutPostRedisplay();
break;
}
}
int main(int argc, char** argv)
{
// BEGIN CREATE WINDOW
glutInit(&argc, argv);
#ifdef __APPLE__
glutInitDisplayMode( GLUT_3_2_CORE_PROFILE | GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
#else
glutInitContextVersion(3,1);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );
#endif
glutInitWindowSize(width, height);
glutCreateWindow(title);
#ifndef __APPLE__
glewExperimental = GL_TRUE;
GLenum err = glewInit() ;
if (GLEW_OK != err) {
std::cerr << "Error: " << glewGetErrorString(err) << std::endl;
}
#endif
std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << std::endl;
// END CREATE WINDOW
initialize();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialKey);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}