-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
215 lines (186 loc) · 5.71 KB
/
Copy pathmain.cpp
File metadata and controls
215 lines (186 loc) · 5.71 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
#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 "Image.h"
#include "Ray.h"
//static int width = 800;
//static int height = 600;
static int width = 200;
static int height = 150;
static const char* title = "Scene viewer";
static const glm::vec4 background(0.1f, 0.2f, 0.3f, 1.0f);
static Scene scene;
//added variables
static Image image(width,height);
static RTScene RTscene;
static bool RT_mode = false; //ray tracing mode vs hw3 view mode
#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 'I' to toggle ray tracing/show image.
press Spacebar to generate images for hw3 submission.
)";
}
//helper to put dummy colors into image.pixels
void dummyColors(std::vector<glm::vec3> &pixels) {
int size = width*height;
for (int i=0; i<size/2; i++) {
pixels[i] = glm::vec3(0.2f, 0.375f, 0.35f);
}
for (int i=size/2; i<size; i++) {
pixels[i] = glm::vec3(0.1f, 0.25f, 0.7f);
}
}
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 RTscene
RTscene.init();
//initialize image
image.init();
std::cout << "RTScene and Image initialized." << std::endl;
// Enable depth test
glEnable(GL_DEPTH_TEST);
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//check if on ray tracing mode
if (RT_mode) {
//build the scene full of triangles
RTscene.buildTriangleSoup();
//ray tracing algorithm
RayTracer::Raytrace(RTscene.camera, RTscene, image);
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();
RTscene.camera -> aspect_default = float(glutGet(GLUT_WINDOW_WIDTH))/float(glutGet(GLUT_WINDOW_HEIGHT));
RTscene.camera -> reset();
glutPostRedisplay();
break;
case 'a':
scene.camera -> zoom(0.9f);
RTscene.camera -> zoom(0.9f);
glutPostRedisplay();
break;
case 'z':
scene.camera -> zoom(1.1f);
RTscene.camera -> zoom(1.1f);
glutPostRedisplay();
break;
case 'l':
scene.shader -> enablelighting = !(scene.shader -> enablelighting);
glutPostRedisplay();
break;
case ' ':
hw3AutoScreenshots();
glutPostRedisplay();
break;
case 'i':
//toggle ray tracing mode
RT_mode = !RT_mode;
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);
RTscene.camera -> rotateUp(-10.0f);
glutPostRedisplay();
break;
case GLUT_KEY_DOWN: // down
scene.camera -> rotateUp(10.0f);
RTscene.camera -> rotateUp(10.0f);
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT: // right
scene.camera -> rotateRight(-10.0f);
RTscene.camera -> rotateRight(-10.0f);
glutPostRedisplay();
break;
case GLUT_KEY_LEFT: // left
scene.camera -> rotateRight(10.0f);
RTscene.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. */
}