-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
375 lines (375 loc) · 15.9 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
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
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "RayTracer.h"
using namespace std;
// Global Ray Tracer Settings
float highQualitySize = 256;
float lowQualitySize = 32;
float highQualityProjDistance = 144.0f;
float lowQualityProjDistance = 18.0f;
float stepSize = 4;
float thetaSize = 10;
float cursorSens = 0.1f;
bool printLocation = false;
// Global Ray Tracer Variables
RayTracer rayTracer;
vector<float> camera = {100.0f, 100.0f, 0.0f};
vector<float> lookAtVec = {0.0f, 0.0f, -1.0f};
vector<float> upVec = {0.0f, 1.0f, 0.0f};
float yaw = 0.0f;
float pitch = 0.0f;
float roll = 0.0f;
double lastCursorPosX;
double lastCursorPosY;
bool render = true;
bool cameraControlsEnabled = false;
bool record = false;
vector<vector<vector<float>>> movements;
// Function that processes keyboard inputs
void processInput(GLFWwindow *window)
{
// Escape closes window
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
// Switch to Orthogonal with O
if (glfwGetKey(window, GLFW_KEY_O) == GLFW_PRESS) {
rayTracer.orthogonal = true;
render = true;
}
// Switch to Perspective with P
if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
rayTracer.orthogonal = false;
render = true;
}
if (cameraControlsEnabled) {
// Mouse Camera controls
double cursorX, cursorY;
glfwGetCursorPos(window, &cursorX, &cursorY);
if (cursorX != lastCursorPosX || cursorY != lastCursorPosY) {
yaw += (float)(cursorX - lastCursorPosX) * cursorSens;
pitch += (float)(cursorY - lastCursorPosY) * cursorSens;
lastCursorPosY = cursorY;
lastCursorPosX = cursorX;
render = true;
}
// Move camera left with A
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
// Moves camera in direction of -u
camera = RayTracer::addVec(camera, RayTracer::scalarVec(-1 * stepSize,
RayTracer::normalizeVec(RayTracer::crossVec(upVec, RayTracer::scalarVec(-1,
lookAtVec)))));
render = true;
}
// Move camera right with D
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
// Moves camera in direction of u
camera = RayTracer::addVec(camera, RayTracer::scalarVec(stepSize,
RayTracer::normalizeVec(RayTracer::crossVec(upVec, RayTracer::scalarVec(-1,
lookAtVec)))));
render = true;
}
// Move camera forwards with W
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
// Moves camera in direction of -w
camera = RayTracer::addVec(camera, RayTracer::scalarVec(stepSize,
RayTracer::normalizeVec(lookAtVec)));
render = true;
}
// Move camera backwards with S
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
// Moves camera in direction of w
camera = RayTracer::addVec(camera, RayTracer::scalarVec(-1 * stepSize,
RayTracer::normalizeVec(lookAtVec)));
render = true;
}
// Move camera up with Space
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
// Moves camera in direction of v
camera = RayTracer::addVec(camera, RayTracer::scalarVec(stepSize,
RayTracer::normalizeVec(upVec)));
render = true;
}
// Move camera up with Left Control
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) {
// Moves camera in direction of -v
camera = RayTracer::addVec(camera, RayTracer::scalarVec(-1 * stepSize,
RayTracer::normalizeVec(upVec)));
render = true;
}
// Turn camera left with Left Arrow
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
yaw -= thetaSize;
render = true;
}
// Turn camera right with Right Arrow
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
yaw += thetaSize;
render = true;
}
// Turn camera up with Up Arrow
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
pitch -= thetaSize;
render = true;
}
// Turn camera down with Down Arrow
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
pitch += thetaSize;
render = true;
}
// Not implemented: Roll camera counter clockwise with Q
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
roll -= thetaSize;
render = true;
}
// Not implemented: Roll camera clockwise with E
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) {
roll += thetaSize;
render = true;
}
}
// Press H to switch to high quality resolution
if (glfwGetKey(window, GLFW_KEY_H)) {
rayTracer.imgSizeX = highQualitySize;
rayTracer.imgSizeY = highQualitySize;
rayTracer.projectionDistance = highQualityProjDistance;
render = true;
cameraControlsEnabled = false;
}
// Press L to switch to low quality resolution
if (glfwGetKey(window, GLFW_KEY_L)) {
rayTracer.imgSizeX = lowQualitySize;
rayTracer.imgSizeY = lowQualitySize;
rayTracer.projectionDistance = lowQualityProjDistance;
render = true;
cameraControlsEnabled = true;
glfwGetCursorPos(window, &lastCursorPosX, &lastCursorPosY);
}
// Press U to turn off light visualization
if (glfwGetKey(window, GLFW_KEY_U)) {
render = true;
rayTracer.lightVisualization = false;
}
// Press V to turn on light visualization
if (glfwGetKey(window, GLFW_KEY_I)) {
render = true;
rayTracer.lightVisualization = true;
}
// Press R to start recording movements
if (glfwGetKey(window, GLFW_KEY_R)) {
render = true;
record = true;
}
// Press T to save all movements as PPM files
if (glfwGetKey(window, GLFW_KEY_T)) {
record = false;
rayTracer.imgSizeX = highQualitySize;
rayTracer.imgSizeY = highQualitySize;
rayTracer.projectionDistance = highQualityProjDistance;
cameraControlsEnabled = false;
// Renders each frame (program will say not responding while this occurs)
for (int i = 0; i < movements.size(); i++) {
unsigned char* move = rayTracer.produceImage(movements.at(i)[0],
movements.at(i)[1], movements.at(i)[2]);
// To render: ffmpeg -framerate 20 -i rayTrace%d.ppm -c:v libx264 -crf 25 -vf "scale=256:256,format=yuv420p" -movflags +faststart rayTrace.mp4
rayTracer.takePicture("rayTrace" + to_string(i) + ".ppm", move);
}
movements = vector<vector<vector<float>>>();
render = true;
}
// If we rendered, transform our vectors based on pitch yaw and roll
if (render) {
lookAtVec = RayTracer::transformVector({0.0f, 0.0f, -1.0f}, pitch, yaw,
roll);
upVec = RayTracer::transformVector({0.0f, 1.0f, 0.0f}, pitch, yaw, roll);
}
}
// Given by professor
// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 800;
const char *vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec3 aColor;\n"
"layout (location = 2) in vec2 aTexCoord;\n"
"out vec3 ourColor;\n"
"out vec2 TexCoord;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(aPos, 1.0);\n"
"ourColor = aColor;\n"
"TexCoord = vec2(aTexCoord.x, aTexCoord.y);\n"
"}\0";
const char *fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec3 ourColor;\n"
"in vec2 TexCoord;\n"
"uniform sampler2D texture1;\n"
"void main()\n"
"{\n"
" FragColor = texture(texture1, TexCoord);\n"
"}\n\0";
// Callback for if errors occur
void error_callback(int error, const char* log) {
cout << "Error: " << error << " " << log << endl;
}
// Function provided by professor:
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
// Main Function
int main() {
// Code for GLFW/GLEW Setup and 2D Array Display given by professor
if (!glfwInit()) {
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwSetErrorCallback(error_callback);
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Ray Tracer",
nullptr, nullptr);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
int glewErr = glewInit();
if (glewErr != GLEW_OK) {
glfwTerminate();
return -1;
}
// build and compile the shaders
// ------------------------------------
// vertex shader
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// check for shader compile errors
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog <<
std::endl;
}
// fragment shader
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// check for shader compile errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog <<
std::endl;
}
// link shaders
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog <<
std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 *
sizeof(float)));
glEnableVertexAttribArray(1);
// texture coord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 *
sizeof(float)));
glEnableVertexAttribArray(2);
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Hide cursor for mouse support
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwGetCursorPos(window, &lastCursorPosX, &lastCursorPosY);
rayTracer.imgSizeX = highQualitySize;
rayTracer.imgSizeY = highQualitySize;
rayTracer.projectionDistance = highQualityProjDistance;
// Render Loop
while(!glfwWindowShouldClose(window)) {
processInput(window);
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
if (render) {
if (record) {
movements.push_back({camera, lookAtVec, upVec});
}
if (printLocation) {
cout << "Camera: " << camera[0] << " " << camera[1] << " " <<
camera[2] << endl;
cout << "LookAtVec: " << lookAtVec[0] << " " << lookAtVec[1] << " "
<< lookAtVec[2] << endl;
cout << "UpVec: " << upVec[0] << " " << upVec[1] << " " << upVec[2]
<< endl;
}
unsigned char* currImage = rayTracer.produceImage(camera, lookAtVec,
upVec);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, rayTracer.imgSizeX,
rayTracer.imgSizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, currImage);
glGenerateMipmap(GL_TEXTURE_2D);
render = false;
}
// render container
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}