-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scene.cpp
198 lines (162 loc) · 6.11 KB
/
Scene.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
#include "Scene.h"
#include "Primitive.h"
#include "Light.h"
#include "ImageWriter.hpp"
#define GLFW_INCLUDE_GLCOREARB
#define GL_GLEXT_PROTOTYPES
#include <GLFW/glfw3.h>
#include <iostream>
#include <thread>
#include <map>
enum KeyState {UP, DOWN, HOLD};
static std::map<int, KeyState> input;
static const float CAMVELOCITY = 1.0f;
static void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS) {
input[key] = KeyState::DOWN;
} else if (action == GLFW_RELEASE) {
input[key] = KeyState::UP;
}
}
void Scene::createGeometry() {
GLfloat vertices[][2] = {
{-1.0f, -1.0f},
{-1.0f, 1.0f},
{1.0f, 1.0f},
{1.0f, -1.0f}
};
GLuint vertexBuffer = 0;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glGenVertexArrays(1, &m_vertexArray);
glBindVertexArray(m_vertexArray);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
Scene::Scene(const Camera &cam, const int width, const int height)
: m_camera(cam),
m_width(width),
m_height(height),
m_shader(Shader("passthrough.vert", "raytracer.frag"))
{
createGeometry();
glfwSetKeyCallback(m_window.window(), keyCallback);
}
Scene::Scene()
: m_shader(Shader("passthrough.vert", "raytracer.frag"))
{
createGeometry();
glfwSetKeyCallback(m_window.window(), keyCallback);
}
void Scene::addSphere(Sphere sphere)
{
m_spheres.push_back(sphere);
m_dirty = true;
}
void Scene::addTriangle(Triangle tri)
{
m_triangles.push_back(tri);
m_dirty = true;
}
void Scene::addPlane(Plane plane)
{
m_planes.push_back(plane);
m_dirty = true;
}
void Scene::addLight(Light light)
{
m_lights.push_back(light);
m_dirty = true;
}
static void updateCameraTransform(Camera &cam, float dt) {
if (input[GLFW_KEY_UP] == HOLD) {
cam.pos += cam.w * CAMVELOCITY * dt;
} else if (input[GLFW_KEY_DOWN] == HOLD) {
cam.pos -= cam.w * CAMVELOCITY * dt;
} else if (input[GLFW_KEY_RIGHT] == HOLD) {
cam.pos += cam.u * CAMVELOCITY * dt;
} else if (input[GLFW_KEY_LEFT] == HOLD) {
cam.pos -= cam.u * CAMVELOCITY * dt;
}
}
static void updateCameraDepth(Camera &cam, float dt) {
if (input[GLFW_KEY_UP] == HOLD) {
cam.focallength += CAMVELOCITY * dt;
} else if (input[GLFW_KEY_DOWN] == HOLD) {
cam.focallength -= CAMVELOCITY * dt;
} else if (input[GLFW_KEY_RIGHT] == HOLD) {
cam.aperture += CAMVELOCITY / 10 * dt;
} else if (input[GLFW_KEY_LEFT] == HOLD) {
cam.aperture -= CAMVELOCITY / 10 * dt;
}
}
void Scene::save()
{
static unsigned int nfiles = 0;
std::string filename("screenshot");
filename += std::to_string(nfiles++) + ".jpg";
m_camera.ndepthrays = 64;
render();
ImageWriter writer(512, 512);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, 512, 512, GL_RGBA, GL_UNSIGNED_BYTE, writer.get_data());
writer.save_opengl(filename.c_str());
m_camera.ndepthrays = 4;
}
void Scene::update(float dt)
{
if (input[GLFW_KEY_2] == DOWN) {
m_camera.dof = true;
} else if (input[GLFW_KEY_1] == DOWN) {
m_camera.dof = false;
} else if (input[GLFW_KEY_3] == DOWN) {
save();
}
if (m_camera.dof) {
updateCameraDepth(m_camera, dt);
} else {
updateCameraTransform(m_camera, dt);
}
for (auto &i: input) {
if (i.second == KeyState::DOWN) i.second = KeyState::HOLD;
}
}
void Scene::render()
{
glUseProgram(m_shader.program());
if (m_dirty) {
GLuint buffers[4] = {0, 0, 0};
GLuint lengths[4] = {
static_cast<GLuint>(sizeof(Light) * m_lights.size()),
static_cast<GLuint>(sizeof(Sphere) * m_spheres.size()),
static_cast<GLuint>(sizeof(Triangle) * m_triangles.size()),
static_cast<GLuint>(sizeof(Plane) * m_planes.size())
};
glGenBuffers(4, buffers);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, buffers[0]);
glBufferData(GL_SHADER_STORAGE_BUFFER, lengths[0], m_lights.data(), GL_DYNAMIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, buffers[1]);
glBufferData(GL_SHADER_STORAGE_BUFFER, lengths[1], m_spheres.data(), GL_DYNAMIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, buffers[2]);
glBufferData(GL_SHADER_STORAGE_BUFFER, lengths[2], m_triangles.data(), GL_DYNAMIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, buffers[3]);
glBufferData(GL_SHADER_STORAGE_BUFFER, lengths[3], m_planes.data(), GL_DYNAMIC_DRAW);
}
glUniform3f(glGetUniformLocation(m_shader.program(), "camera.position"), m_camera.pos.x, m_camera.pos.y, m_camera.pos.z);
glUniform3f(glGetUniformLocation(m_shader.program(), "camera.u"), m_camera.u.x, m_camera.u.y, m_camera.u.z);
glUniform3f(glGetUniformLocation(m_shader.program(), "camera.v"), m_camera.v.x, m_camera.v.y, m_camera.v.z);
glUniform3f(glGetUniformLocation(m_shader.program(), "camera.w"), m_camera.w.x, m_camera.w.y, m_camera.w.z);
glUniform1f(glGetUniformLocation(m_shader.program(), "camera.fov"), m_camera.fovy);
glUniform1f(glGetUniformLocation(m_shader.program(), "camera.aperture"), m_camera.aperture);
glUniform1f(glGetUniformLocation(m_shader.program(), "camera.focallength"), m_camera.focallength);
glUniform1i(glGetUniformLocation(m_shader.program(), "camera.dof"), m_camera.dof);
glUniform1ui(glGetUniformLocation(m_shader.program(), "camera.ndepthrays"), m_camera.ndepthrays);
glUniform1ui(glGetUniformLocation(m_shader.program(), "maxdepth"), 5);
glBindVertexArray(m_vertexArray);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glfwSwapBuffers(m_window.window());
}