-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.h
201 lines (162 loc) · 4.65 KB
/
scene.h
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
#include "bvh.h"
#define EYE vecF(0.,0.,0.)
#define LOOK vecF(0.,0.,1.)
#define UP vecF(0.,1.,0.)
#define HA 90.f
#define AR 1.f
#define IM_WIDTH 500
#define IM_HEIGHT 500
#define SAMPLE_NUM 100
#define p_RR .9f
#define BLOCKSIZE 256
#define NBLOCKS IM_WIDTH * IM_HEIGHT * SAMPLE_NUM / BLOCKSIZE + 1
struct CameraParams_t {
float eye[3];
float look[3];
float up[3];
float heightAngle;
float aspectRatio;
CameraParams_t(bool isDefault=false) {
if (isDefault) {
for (int i = 0; i < 3; i++) {
eye[i] = EYE[i];
look[i] = LOOK[i];
up[i] = UP[i];
}
heightAngle = HA;
aspectRatio = AR;
}
}
};
class Camera {
public:
Camera(CameraParams_t camera) {
m_position = vecF(camera.eye[0],camera.eye[1],camera.eye[2]);
m_direction = vecF(camera.look[0],camera.look[1],camera.look[2]);
m_direction.normalize();
m_up = vecF(camera.up[0],camera.up[1],camera.up[2]);
m_up.normalize();
m_heightAngle = M_PI * camera.heightAngle / 360.f;
m_aspectRatio = camera.aspectRatio;
}
Camera() {}
__host__ __device__ mat4F getViewMatrix() {
vecF f = m_direction;
f.normalize();
vecF s = f.cross(m_up);
s.normalize();
vecF u = s.cross(f);
u.normalize();
mat4F vM;
vM << s[0],s[1],s[2],-s.dot(m_position),
u[0],u[1],u[2],-u.dot(m_position),
f[0],f[1],f[2],-f.dot(m_position),
0,0,0,1;
return vM;
}
__host__ __device__ mat4F getScaleMatrix() {
mat4F sM = mat4F::Identity();
sM(0,0) = tan(m_heightAngle);
sM(1,1) = tan(m_heightAngle*m_aspectRatio);
return sM;
}
__host__ __device__ mat4F getInverseViewMatrix() {
return getScaleMatrix() * getViewMatrix().transpose();
}
private:
vecF m_position;
vecF m_direction;
vecF m_up;
float m_heightAngle;
float m_aspectRatio;
};
class Scene {
public:
Scene(CameraParams_t camera, std::vector<ObjParams_t> objects) : m_camera(camera) {
m_nO = objects.size();
cudaMallocManaged(&m_objects,sizeof(Object)*m_nO);
for (int i = 0; i < m_nO; i++) {
new(&(m_objects[i])) Object(objects[i]);
}
m_nT = 0;
m_nE = 0;
std::vector<Triangle *> emissives;
for (int i = 0; i < m_nO; i++) {
Object *o = m_objects+i;
o->setOffsets(m_nT,m_nE);
Triangle **o_emissives = o->getEmissives();
for (int j = 0; j < o->nEmissives(); j++) {
emissives.push_back(o_emissives[j]);
}
m_nT += o->nTriangles();
m_nE += o->nEmissives();
}
cudaMalloc(&m_emissives,sizeof(Triangle *)*emissives.size());
cudaMemcpy(m_emissives,emissives.data(),sizeof(Triangle *)*emissives.size(),cudaMemcpyHostToDevice);
m_bvh.build(m_objects,m_nO);
}
~Scene() {
for (int i = 0; i < m_nO; i++) {
(m_objects+i)->~Object();
}
cudaFree(m_objects);
cudaFree(m_emissives);
}
__host__ __device__ void getIntersection(Ray ray, intersection_t &intersect, bool occlusion=false) {
return m_bvh.getIntersection(ray,intersect,occlusion);
}
__host__ __device__ mat4F getInverseViewMatrix() {
return m_camera.getInverseViewMatrix();
}
__host__ __device__ void emissives(Triangle ** &emissives) {
emissives = m_emissives;
}
__host__ __device__ int nTriangles() {return m_nT;}
__host__ __device__ int nObjects() {return m_nO;}
__host__ __device__ int nEmissives() {return m_nE;}
std::vector<float> getMaterials() {
std::vector<float> materials;
for (int i = 0; i < m_nO; i++) {
std::vector<float> curr = (m_objects+i)->getMaterials();
materials.insert(materials.end(), curr.begin(), curr.end());
}
return materials;
}
void setMaterials(std::vector<float> materials) {
int idx = 0;
for (int i = 0; i < m_nO; i++) {
int len = (m_objects+i)->nTriangles() * 3;
std::vector<float> curr(materials.begin() + idx, materials.begin() + idx + len);
(m_objects+i)->setMaterials(curr);
idx += len;
}
}
private:
Triangle **m_emissives;
Camera m_camera;
Object *m_objects;
BVH m_bvh;
int m_nO;
int m_nE;
int m_nT;
};
extern "C" {
int loadScene(float **poss, float **oris, float **scls, char **obj_fs, char **mtl_fs, int n, void **scenePtr) {
CameraParams_t camParams(true);
std::vector<ObjParams_t> objParams(n);
for (int i = 0; i < n; i++) {
ObjParams_t op(poss[i],oris[i],scls[i],obj_fs[i],mtl_fs[i]);
objParams[i] = op;
}
Scene *scene;
cudaMallocManaged(&scene,sizeof(Scene));
new(scene) Scene(camParams,objParams);
*scenePtr = scene;
return scene->nTriangles();
}
void freeScene(void *scenePtr) {
Scene *scene = (Scene *) scenePtr;
scene->~Scene();
cudaFree(scene);
}
};