-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpathtracer.cpp
66 lines (54 loc) · 2.06 KB
/
pathtracer.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
#include "pathtracer.h"
#include <iostream>
#include <Eigen/Dense>
#include <util/CS123Common.h>
using namespace Eigen;
PathTracer::PathTracer(int width, int height)
: m_width(width), m_height(height)
{
}
void PathTracer::traceScene(QRgb *imageData, const Scene& scene)
{
std::vector<Vector3f> intensityValues(m_width * m_height);
Matrix4f invViewMat = (scene.getCamera().getScaleMatrix() * scene.getCamera().getViewMatrix()).inverse();
for(int y = 0; y < m_height; ++y) {
//#pragma omp parallel for
for(int x = 0; x < m_width; ++x) {
int offset = x + (y * m_width);
intensityValues[offset] = tracePixel(x, y, scene, invViewMat);
}
}
toneMap(imageData, intensityValues);
}
Vector3f PathTracer::tracePixel(int x, int y, const Scene& scene, const Matrix4f &invViewMatrix)
{
Vector3f p(0, 0, 0);
Vector3f d((2.f * x / m_width) - 1, 1 - (2.f * y / m_height), -1);
d.normalize();
Ray r(p, d);
r = r.transform(invViewMatrix);
return traceRay(r, scene);
}
Vector3f PathTracer::traceRay(const Ray& r, const Scene& scene)
{
IntersectionInfo i;
Ray ray(r);
if(scene.getIntersection(ray, &i)) {
//** Example code for accessing materials provided by a .mtl file **
// const Triangle *t = static_cast<const Triangle *>(i.data);//Get the triangle in the mesh that was intersected
// const tinyobj::material_t& mat = t->getMaterial();//Get the material of the triangle from the mesh
// const tinyobj::real_t *d = mat.diffuse;//Diffuse color as array of floats
// const std::string diffuseTex = mat.diffuse_texname;//Diffuse texture name
return Vector3f(1, 1, 1);
} else {
return Vector3f(0, 0, 0);
}
}
void PathTracer::toneMap(QRgb *imageData, std::vector<Vector3f> &intensityValues) {
for(int y = 0; y < m_height; ++y) {
for(int x = 0; x < m_width; ++x) {
int offset = x + (y * m_width);
imageData[offset] = intensityValues[offset].norm() > 0 ? qRgb(255, 255, 255) : qRgb(40, 40, 40);
}
}
}