-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.cpp
More file actions
291 lines (221 loc) · 6.72 KB
/
renderer.cpp
File metadata and controls
291 lines (221 loc) · 6.72 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "framework.h"
#include <execution>
#include <algorithm>
#include <glm/gtx/string_cast.hpp>
Renderer::Renderer(int width, int height)
{
imageData = nullptr;
scene.create();
camera = new Camera(width, height);
resize(width, height);
}
Renderer::~Renderer()
{
if (imageData != nullptr)
delete[] imageData;
}
void Renderer::createScene()
{
}
void Renderer::resize(int width, int height)
{
this->width = width;
this->height = height;
if (imageData != nullptr)
delete[] imageData;
imageData = new GLuint[width * height + 1];
camera->onResize(width, height);
}
void Renderer::render(float deltaTime)
{
camera->calculateRayDirections();
srand(time(NULL));
std::vector<GLuint> horizontalIter;
std::vector<GLuint> verticalIter;
horizontalIter.resize(width);
verticalIter.resize(height);
for (uint32_t i = 0; i < width; i++)
horizontalIter[i] = i;
for (uint32_t i = 0; i < height; i++)
verticalIter[i] = i;
static float time = glfwGetTime();
scene.lights[0].position = glm::vec3(
//(glfwGetTime() - time), 0.0f, 0.0f
2.5f * glm::sin(glfwGetTime()),
2.5f * glm::cos(glfwGetTime()),
1.5f * glm::sin(glfwGetTime())
);//light.position;
std::for_each(std::execution::par, verticalIter.begin(), verticalIter.end(),
[this, deltaTime, horizontalIter](uint32_t i)
{
std::for_each(std::execution::par, horizontalIter.begin(), horizontalIter.end(),
[this, i, deltaTime](uint32_t j)
{
imageData[i * width + j] = toRGBA(rayGen(i, j, deltaTime));
});
});
}
GLuint* Renderer::getImage()
{
return imageData;
}
GLuint Renderer::toRGBA(glm::vec4& color)
{
unsigned char r = color.r * 255.0f;
unsigned char g = color.g * 255.0f;
unsigned char b = color.b * 255.0f;
unsigned char a = color.a * 255.0f;
return (r << 24) | (g << 16) | (b << 8) | a;
}
glm::vec4 Renderer::rayGen(int i, int j, float deltaTime)
{
Ray ray;
ray.origin = camera->getRayOrigin(); //camera->getOrthographicRayOrigins()[i * width + j];
ray.direction = camera->getRayDirections()[i * width + j];
HitPayload payload = traceRayFromPixel(ray);
// no sphere detected
if (payload.hitDistance < 0)
return glm::vec4(skyColor, 1.0f);
// light source hit
if (payload.hitDistance == 0)
return glm::vec4(scene.lights[payload.sphereIndex].color, 1.0f);
const Sphere& sphere = scene.spheres[payload.sphereIndex];
glm::vec4 color = glm::vec4(kAmbient * ambientColor * sphere.albedo, 1.0f);
for (int i = 0; i < scene.lights.size(); i++)
{
const Light& light = scene.lights[i];
Ray rayToLight;
// cast ray a bit away from the sphere so that the ray doesn't hit it
rayToLight.origin = payload.hitPoint + payload.normal * 1e-4f;
float distanceToLight = glm::length(light.position - payload.hitPoint);
rayToLight.direction = glm::normalize(light.position - payload.hitPoint);
HitPayload payloadToLight = traceRayFromHitpoint(rayToLight, distanceToLight);
// no sphere hit on path to light
if (payloadToLight.hitDistance < 0)
color += phong(payload, light);
}
return glm::clamp(color, 0.0f, 1.0f);
}
glm::vec4 Renderer::phong(HitPayload payload, Light light)
{
glm::vec3 lightDir = glm::normalize(light.position - payload.hitPoint);
glm::vec3 lightColor = light.color;
float cosNL = glm::max(0.0f, glm::dot(lightDir, payload.normal));
glm::vec3 reflectionVector = glm::reflect(-lightDir, payload.normal);
glm::vec3 eyeVector = glm::normalize(camera->position - payload.hitPoint);
float cosVR = glm::max(0.0f, glm::dot(reflectionVector, eyeVector));
glm::vec3 color =
kDiffuse * cosNL * lightColor +
kSpecular * glm::pow(cosVR, kShininess) * lightColor;
Sphere& sphere = scene.spheres[payload.sphereIndex];
color *= sphere.albedo;
return glm::vec4(color, 1.0f);
}
Renderer::HitPayload Renderer::traceRayFromPixel(const Ray& ray)
{
int hitSphereIndex = -1;
int hitLightIndex = -1;
float hitDistance = FLT_MAX;
for (int k = 0; k < scene.spheres.size(); k++)
{
Sphere& sphere = scene.spheres[k];
glm::vec3 origin = ray.origin - sphere.center;
glm::vec3 direction = ray.direction;
float a = glm::dot(direction, direction);
float b = 2.0f * glm::dot(origin, direction);
float c = glm::dot(origin, origin)
- sphere.radius * sphere.radius;
float delta = b * b - 4.0f * a * c;
if (delta < 0)
continue;
float t = (-b - glm::sqrt(delta)) / (2.0f * a);
if (t > 0 && t < hitDistance)
{
hitDistance = t;
hitSphereIndex = k;
}
}
for (int k = 0; k < scene.lights.size(); k++)
{
Light& light = scene.lights[k];
glm::vec3 origin = ray.origin - light.position;
glm::vec3 direction = ray.direction;
float a = glm::dot(direction, direction);
float b = 2.0f * glm::dot(origin, direction);
float c = glm::dot(origin, origin)
- 0.1f * 0.1f;
float delta = b * b - 4.0f * a * c;
if (delta < 0)
continue;
float t = (-b - glm::sqrt(delta)) / (2.0f * a);
if (t > 0 && t < hitDistance)
{
hitDistance = t;
hitSphereIndex = -2;
hitLightIndex = k;
}
}
if (hitSphereIndex == -1)
return miss(ray);
if (hitSphereIndex == -2)
return lightHit(ray, hitLightIndex);
return closestHit(ray, hitSphereIndex, hitDistance);
}
Renderer::HitPayload Renderer::traceRayFromHitpoint(const Ray& ray, float diff)
{
int hitSphereIndex = -1;
float hitDistance = FLT_MAX;
for (int k = 0; k < scene.spheres.size(); k++)
{
Sphere& sphere = scene.spheres[k];
glm::vec3 origin = ray.origin - sphere.center;
glm::vec3 direction = ray.direction;
float a = glm::dot(direction, direction);
float b = 2.0f * glm::dot(origin, direction);
float c = glm::dot(origin, origin)
- sphere.radius * sphere.radius;
float delta = b * b - 4.0f * a * c;
if (delta < 0)
continue;
float t = (-b - glm::sqrt(delta)) / (2.0f * a);
if (t > 0 && t < diff && t < hitDistance)
{
hitDistance = t;
hitSphereIndex = k;
}
}
if (hitSphereIndex == -1)
return miss(ray);
return closestHit(ray, hitSphereIndex, hitDistance);
}
Renderer::HitPayload Renderer::miss(const Ray& ray)
{
HitPayload payload;
payload.hitDistance = -1.0f;
return payload;
}
Renderer::HitPayload Renderer::lightHit(const Ray& ray, int lightIndex)
{
HitPayload payload;
payload.hitDistance = 0.0f;
payload.sphereIndex = lightIndex;
return payload;
}
Renderer::HitPayload Renderer::closestHit(const Ray& ray, int sphereIndex, float hitDistance)
{
HitPayload payload;
payload.hitDistance = hitDistance;
payload.sphereIndex = sphereIndex;
Sphere& sphere = scene.spheres[sphereIndex];
payload.hitPoint = ray.origin + ray.direction * hitDistance;
payload.normal = glm::normalize(payload.hitPoint - sphere.center);
return payload;
}
void Renderer::processKeyboard(int key, float deltaTime)
{
camera->onUpdate(key, deltaTime);
}
void Renderer::processMouse(glm::vec2 offset, float deltaTime)
{
camera->onMouseUpdate(offset, deltaTime);
}