-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
267 lines (229 loc) · 7.31 KB
/
main.cpp
File metadata and controls
267 lines (229 loc) · 7.31 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
#include <GL/glut.h>
#include <cmath>
#include "Camera/Camera.hpp"
#include <fstream>
#include "Object/Object.hpp"
#include "Object/Sphere/Sphere.hpp"
#include "Object/CheckerBoard/CheckerBoard.hpp"
#include "PointLight/PointLight.hpp"
#include "SpotLight/SpotLight.hpp"
#include "Config.hpp"
#include "decompose.hpp"
#define DEG2RAD(deg) ((deg) * M_PI / 180.0)
// globals
Config cfg;
const double AXES_LEN = 100;
// camera variables
Camera camera(cfg);
// callbacks
void display();
void idle();
void specialKeyListener(int, int, int);
void keyboardListener(unsigned char, int, int);
// utils
void drawAxes();
void printObjects();
// initialize values
void initGL() {
glClearColor(0, 0, 0, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(cfg.fov_y, cfg.asr, cfg.near, cfg.far);
}
void loadConfigData(std::string filename) {
std::fstream input(filename);
input >> cfg.near >> cfg.far >> cfg.fov_y >> cfg.asr;
// window height and width in coord sys units
cfg.win_height = 2 * cfg.near * tan(DEG2RAD(cfg.fov_y / 2));
cfg.win_width = 2 * cfg.near * tan(DEG2RAD(cfg.fov_y * cfg.asr / 2));
input >> cfg.recur_level >> cfg.n_pix;
double checker_w, checker_ambient, checker_diffuse, checker_reflection;
input >> checker_w >> checker_ambient >> checker_diffuse >> checker_reflection;
// first object is checkerboard
cfg.objects.push_back(std::make_shared<CheckerBoard>(
checker_w, checker_ambient, checker_diffuse, checker_reflection));
size_t n_obj;
input >> n_obj;
std::string objtype;
Point ref_point;
Color color;
double ambient, diffuse, specular, reflection;
int shine;
for (size_t i = 0; i < n_obj; i++) {
input >> objtype;
input >> ref_point.x >> ref_point.y >> ref_point.z;
if (objtype == "sphere") {
double rad;
input >> rad;
input >> color.r >> color.g >> color.b;
input >> ambient >> diffuse >> specular >> reflection >> shine;
cfg.objects.push_back(std::make_shared<Sphere>(
ref_point, rad, color, ambient, diffuse, specular, reflection, shine));
} else if (objtype == "pyramid") {
double w, h;
input >> w >> h;
input >> color.r >> color.g >> color.b;
input >> ambient >> diffuse >> specular >> reflection >> shine;
auto triangle_ptrs = decomponse_pyramid(ref_point, w, h, color, ambient, diffuse, specular, reflection, shine);
cfg.objects.insert(cfg.objects.end(), triangle_ptrs.begin(), triangle_ptrs.end());
} else if (objtype == "cube") {
double side;
input >> side;
input >> color.r >> color.g >> color.b;
input >> ambient >> diffuse >> specular >> reflection >> shine;
auto triangle_ptrs = decompose_cube(ref_point, side, color, ambient, diffuse, specular, reflection, shine);
cfg.objects.insert(cfg.objects.end(), triangle_ptrs.begin(), triangle_ptrs.end());
}
}
size_t n_pointlights;
input >> n_pointlights;
Point src;
double falloff;
for (size_t i = 0; i < n_pointlights; i++) {
input >> src.x >> src.y >> src.z >> falloff;
cfg.pointlights.push_back({ src, falloff });
}
size_t n_spotlights;
input >> n_spotlights;
Point direction;
double cutoff_angle;
for (size_t i = 0; i < n_spotlights; i++) {
input >> src.x >> src.y >> src.z >> falloff;
input >> direction.x >> direction.y >> direction.z;
input >> cutoff_angle;
cfg.spotlights.push_back({ src, falloff, direction, cutoff_angle });
}
}
int main(int argc, char* argv[]) {
loadConfigData(argv[1]);
glutInit(&argc, argv);
// window width and height are being set in coord sys units
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("RTX");
initGL();
glEnable(GL_DEPTH_TEST); // enable Depth Testing
glutDisplayFunc(display); // display callback function
glutIdleFunc(idle); // what you want to do in the idle time (when no drawing is occuring)
glutKeyboardFunc(keyboardListener);
glutSpecialFunc(specialKeyListener);
glutMainLoop(); // The main loop of OpenGL
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(camera.eye.x, camera.eye.y, camera.eye.z,
camera.eye.x + camera.look_dir.x, camera.eye.y + camera.look_dir.y, camera.eye.z + camera.look_dir.z,
camera.up_dir.x, camera.up_dir.y, camera.up_dir.z);
// draw
drawAxes();
for (auto obj : cfg.objects) {
obj->draw();
}
for (auto pointlight : cfg.pointlights) {
pointlight.draw();
}
for (auto spotlight : cfg.spotlights) {
spotlight.draw();
}
glutSwapBuffers(); // Render now
}
void idle() {
glutPostRedisplay();
}
void drawAxes() {
glLineWidth(3);
glBegin(GL_LINES); {
glColor3f(1, 0, 0); // Red
glVertex3f(0, 0, 0);
glVertex3f(AXES_LEN, 0, 0);
glColor3f(0, 1, 0); // Green
glVertex3f(0, 0, 0);
glVertex3f(0, AXES_LEN, 0);
glColor3f(0, 0, 1); // Blue
glVertex3f(0, 0, 0);
glVertex3f(0, 0, AXES_LEN);
} glEnd();
}
void keyboardListener(unsigned char key, int mouse_x, int mouse_y) {
switch (key) {
case '1':
case 'a':
// rotate left
camera.rotate(Camera::ANG_VELOCITY);
break;
case '2':
case 'd':
// rotate right
camera.rotate(-Camera::ANG_VELOCITY);
break;
case '3':
case 'w':
// rotate up
camera.rotate(0, Camera::ANG_VELOCITY);
break;
case '4':
case 's':
// rotate down
camera.rotate(0, -Camera::ANG_VELOCITY);
break;
case '5':
case 'e':
// tilt clockwise
camera.rotate(0, 0, Camera::ANG_VELOCITY);
break;
case '6':
case 'q':
// tilt counter clockwise
camera.rotate(0, 0, -Camera::ANG_VELOCITY);
break;
case '0':
case 'c':
camera.capture();
break;
case ' ':
cfg.is_texture = !cfg.is_texture;
std::cout << "texture enabled: " << cfg.is_texture << std::endl;
default:
break;
}
glutPostRedisplay();
}
void specialKeyListener(int key, int x, int y) {
switch (key) {
case GLUT_KEY_UP:
// move forward
camera.translate(camera.look_dir * Camera::VELOCITY);
break;
case GLUT_KEY_DOWN:
// move backward
camera.translate(camera.look_dir * -Camera::VELOCITY);
break;
case GLUT_KEY_RIGHT:
// move right
camera.translate(camera.right_dir * Camera::VELOCITY);
break;
case GLUT_KEY_LEFT:
// move left
camera.translate(camera.right_dir * -Camera::VELOCITY);
break;
case GLUT_KEY_PAGE_UP:
// move up
camera.translate(camera.up_dir * Camera::VELOCITY);
break;
case GLUT_KEY_PAGE_DOWN:
// move down
camera.translate(camera.up_dir * -Camera::VELOCITY);
break;
default:
break;
}
glutPostRedisplay();
}
void printObjects() {
for (auto objptr : cfg.objects) {
std::cout << *objptr << std::endl;
}
}