rev |
line source |
nuclear@54
|
1 #include <string.h>
|
nuclear@54
|
2 #include <assert.h>
|
nuclear@54
|
3 #include "rt.h"
|
nuclear@54
|
4 #include "ogl.h"
|
nuclear@54
|
5 #include "vector.h"
|
nuclear@54
|
6 #include "timer.h"
|
nuclear@54
|
7
|
nuclear@54
|
8 struct SurfPoint {
|
nuclear@54
|
9 float t;
|
nuclear@54
|
10 Vector3 pos, norm;
|
nuclear@54
|
11 const Face *face;
|
nuclear@54
|
12 };
|
nuclear@54
|
13
|
nuclear@54
|
14 static void trace_ray(float *pixel, const Ray &ray, int iter, float energy = 1.0f);
|
nuclear@54
|
15 static void shade(float *pixel, const Ray &ray, const SurfPoint &sp, int iter, float energy = 1.0f);
|
nuclear@54
|
16 static bool find_intersection(const Ray &ray, const Scene *scn, const KDNode *kd, SurfPoint *spret);
|
nuclear@54
|
17 static bool ray_aabb_test(const Ray &ray, const AABBox &aabb);
|
nuclear@54
|
18 static bool ray_triangle_test(const Ray &ray, const Face *face, SurfPoint *sp);
|
nuclear@54
|
19 static Vector3 calc_bary(const Vector3 &pt, const Face *face, const Vector3 &norm);
|
nuclear@54
|
20 static void transform(float *res, const float *v, const float *xform);
|
nuclear@54
|
21 static void transform_ray(Ray *ray, const float *xform, const float *invtrans_xform);
|
nuclear@54
|
22
|
nuclear@54
|
23 static int xsz, ysz;
|
nuclear@54
|
24 static float *fb;
|
nuclear@54
|
25 static unsigned int tex;
|
nuclear@54
|
26 static Scene *scn;
|
nuclear@54
|
27 static const Ray *prim_rays;
|
nuclear@54
|
28
|
nuclear@54
|
29
|
nuclear@54
|
30 bool init_dbg_renderer(int width, int height, Scene *scene, unsigned int texid)
|
nuclear@54
|
31 {
|
nuclear@54
|
32 try {
|
nuclear@54
|
33 fb = new float[3 * width * height];
|
nuclear@54
|
34 }
|
nuclear@54
|
35 catch(...) {
|
nuclear@54
|
36 return false;
|
nuclear@54
|
37 }
|
nuclear@54
|
38
|
nuclear@54
|
39 xsz = width;
|
nuclear@54
|
40 ysz = height;
|
nuclear@54
|
41 tex = texid;
|
nuclear@54
|
42 scn = scene;
|
nuclear@54
|
43 return true;
|
nuclear@54
|
44 }
|
nuclear@54
|
45
|
nuclear@54
|
46 void destroy_dbg_renderer()
|
nuclear@54
|
47 {
|
nuclear@54
|
48 delete [] fb;
|
nuclear@54
|
49 delete [] prim_rays;
|
nuclear@54
|
50 }
|
nuclear@54
|
51
|
nuclear@54
|
52 void dbg_set_primary_rays(const Ray *rays)
|
nuclear@54
|
53 {
|
nuclear@54
|
54 prim_rays = rays;
|
nuclear@54
|
55 }
|
nuclear@54
|
56
|
nuclear@54
|
57 void dbg_render(const float *xform, const float *invtrans_xform, int num_threads)
|
nuclear@54
|
58 {
|
nuclear@54
|
59 unsigned long t0 = get_msec();
|
nuclear@54
|
60
|
nuclear@54
|
61 int iter = get_render_option_int(ROPT_ITER);
|
nuclear@54
|
62
|
nuclear@54
|
63 int offs = 0;
|
nuclear@54
|
64 for(int i=0; i<ysz; i++) {
|
nuclear@54
|
65 for(int j=0; j<xsz; j++) {
|
nuclear@54
|
66 Ray ray = prim_rays[offs];
|
nuclear@54
|
67 transform_ray(&ray, xform, invtrans_xform);
|
nuclear@54
|
68
|
nuclear@54
|
69 trace_ray(fb + offs * 3, ray, iter, 1.0);
|
nuclear@54
|
70 offs++;
|
nuclear@54
|
71 }
|
nuclear@54
|
72 }
|
nuclear@54
|
73
|
nuclear@54
|
74 glPushAttrib(GL_TEXTURE_BIT);
|
nuclear@54
|
75 glBindTexture(GL_TEXTURE_2D, tex);
|
nuclear@54
|
76 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, fb);
|
nuclear@54
|
77 glPopAttrib();
|
nuclear@54
|
78
|
nuclear@54
|
79 printf("rendered in %lu msec\n", get_msec() - t0);
|
nuclear@54
|
80 }
|
nuclear@54
|
81
|
nuclear@54
|
82 static void trace_ray(float *pixel, const Ray &ray, int iter, float energy)
|
nuclear@54
|
83 {
|
nuclear@54
|
84 SurfPoint sp;
|
nuclear@54
|
85
|
nuclear@54
|
86 if(find_intersection(ray, scn, scn->kdtree, &sp)) {
|
nuclear@54
|
87 shade(pixel, ray, sp, iter, energy);
|
nuclear@54
|
88 } else {
|
nuclear@54
|
89 pixel[0] = pixel[1] = pixel[2] = 0.05f;
|
nuclear@54
|
90 }
|
nuclear@54
|
91 }
|
nuclear@54
|
92
|
nuclear@54
|
93 #define MAX(a, b) ((a) > (b) ? (a) : (b))
|
nuclear@54
|
94
|
nuclear@54
|
95 static void shade(float *pixel, const Ray &ray, const SurfPoint &sp, int iter, float energy)
|
nuclear@54
|
96 {
|
nuclear@54
|
97 const Material *mat = scn->get_materials() + sp.face->matid;
|
nuclear@54
|
98
|
nuclear@54
|
99 bool cast_shadows = get_render_option_bool(ROPT_SHAD);
|
nuclear@54
|
100 Vector3 raydir(ray.dir);
|
nuclear@54
|
101 Vector3 norm = sp.norm;
|
nuclear@54
|
102
|
nuclear@54
|
103 if(dot(raydir, norm) >= 0.0) {
|
nuclear@54
|
104 norm = -norm;
|
nuclear@54
|
105 }
|
nuclear@54
|
106
|
nuclear@54
|
107 float dcol[3] = {0, 0, 0};
|
nuclear@54
|
108 float scol[3] = {0, 0, 0};
|
nuclear@54
|
109
|
nuclear@54
|
110 for(int i=0; i<scn->get_num_lights(); i++) {
|
nuclear@54
|
111 Vector3 lpos(scn->lights[i].pos);
|
nuclear@54
|
112 Vector3 ldir = lpos - sp.pos;
|
nuclear@54
|
113
|
nuclear@54
|
114 Ray shadowray;
|
nuclear@54
|
115 shadowray.origin[0] = sp.pos.x;
|
nuclear@54
|
116 shadowray.origin[1] = sp.pos.y;
|
nuclear@54
|
117 shadowray.origin[2] = sp.pos.z;
|
nuclear@54
|
118 shadowray.dir[0] = ldir.x;
|
nuclear@54
|
119 shadowray.dir[1] = ldir.y;
|
nuclear@54
|
120 shadowray.dir[2] = ldir.z;
|
nuclear@54
|
121
|
nuclear@54
|
122 if(!cast_shadows || !find_intersection(shadowray, scn, scn->kdtree, 0)) {
|
nuclear@54
|
123
|
nuclear@54
|
124 ldir.normalize();
|
nuclear@54
|
125
|
nuclear@54
|
126 Vector3 vdir = -raydir / RAY_MAG;
|
nuclear@54
|
127 Vector3 vref = reflect(vdir, norm);
|
nuclear@54
|
128
|
nuclear@54
|
129 float ndotl = dot(ldir, norm);
|
nuclear@54
|
130 float diff = MAX(ndotl, 0.0f);
|
nuclear@54
|
131
|
nuclear@54
|
132 dcol[0] += mat->kd[0] * diff;
|
nuclear@54
|
133 dcol[1] += mat->kd[1] * diff;
|
nuclear@54
|
134 dcol[2] += mat->kd[2] * diff;
|
nuclear@54
|
135
|
nuclear@54
|
136 float ldotvr = dot(ldir, vref);
|
nuclear@54
|
137 float spec = pow(MAX(ldotvr, 0.0f), mat->spow);
|
nuclear@54
|
138
|
nuclear@54
|
139 scol[0] += mat->ks[0] * spec;
|
nuclear@54
|
140 scol[1] += mat->ks[1] * spec;
|
nuclear@54
|
141 scol[2] += mat->ks[2] * spec;
|
nuclear@54
|
142 }
|
nuclear@54
|
143 }
|
nuclear@54
|
144
|
nuclear@54
|
145 float refl_color[3];
|
nuclear@54
|
146 refl_color[0] = mat->ks[0] * mat->kr;
|
nuclear@54
|
147 refl_color[1] = mat->ks[1] * mat->kr;
|
nuclear@54
|
148 refl_color[2] = mat->ks[2] * mat->kr;
|
nuclear@54
|
149
|
nuclear@54
|
150 energy *= (refl_color[0] + refl_color[1] + refl_color[2]) / 3.0;
|
nuclear@54
|
151 if(iter >= 0 && energy > MIN_ENERGY) {
|
nuclear@54
|
152 Vector3 rdir = reflect(-raydir, norm);
|
nuclear@54
|
153
|
nuclear@54
|
154 Ray refl;
|
nuclear@54
|
155 refl.origin[0] = sp.pos.x;
|
nuclear@54
|
156 refl.origin[1] = sp.pos.y;
|
nuclear@54
|
157 refl.origin[2] = sp.pos.z;
|
nuclear@54
|
158 refl.dir[0] = rdir.x;
|
nuclear@54
|
159 refl.dir[1] = rdir.y;
|
nuclear@54
|
160 refl.dir[2] = rdir.z;
|
nuclear@54
|
161
|
nuclear@54
|
162 float rcol[3];
|
nuclear@54
|
163 trace_ray(rcol, refl, iter - 1, energy);
|
nuclear@54
|
164 scol[0] += rcol[0] * mat->ks[0] * mat->kr;
|
nuclear@54
|
165 scol[1] += rcol[1] * mat->ks[1] * mat->kr;
|
nuclear@54
|
166 scol[2] += rcol[2] * mat->ks[2] * mat->kr;
|
nuclear@54
|
167 }
|
nuclear@54
|
168
|
nuclear@54
|
169 pixel[0] = dcol[0] + scol[0];
|
nuclear@54
|
170 pixel[1] = dcol[1] + scol[1];
|
nuclear@54
|
171 pixel[2] = dcol[2] + scol[2];
|
nuclear@54
|
172 }
|
nuclear@54
|
173
|
nuclear@54
|
174 static bool find_intersection(const Ray &ray, const Scene *scn, const KDNode *kd, SurfPoint *spret)
|
nuclear@54
|
175 {
|
nuclear@54
|
176 if(!ray_aabb_test(ray, kd->aabb)) {
|
nuclear@54
|
177 return false;
|
nuclear@54
|
178 }
|
nuclear@54
|
179
|
nuclear@54
|
180 SurfPoint sp, sptmp;
|
nuclear@54
|
181 if(!spret) {
|
nuclear@54
|
182 spret = &sptmp;
|
nuclear@54
|
183 }
|
nuclear@54
|
184
|
nuclear@54
|
185 spret->t = RAY_MAG;
|
nuclear@54
|
186 spret->face = 0;
|
nuclear@54
|
187
|
nuclear@54
|
188 if(kd->left) {
|
nuclear@54
|
189 assert(kd->right);
|
nuclear@54
|
190
|
nuclear@54
|
191 bool found = find_intersection(ray, scn, kd->left, spret);
|
nuclear@54
|
192 if(find_intersection(ray, scn, kd->right, &sp)) {
|
nuclear@54
|
193 if(!found || sp.t < spret->t) {
|
nuclear@54
|
194 *spret = sp;
|
nuclear@54
|
195 }
|
nuclear@54
|
196 found = true;
|
nuclear@54
|
197 }
|
nuclear@54
|
198 return found;
|
nuclear@54
|
199 }
|
nuclear@54
|
200
|
nuclear@54
|
201 const Face *faces = scn->get_face_buffer();
|
nuclear@54
|
202
|
nuclear@54
|
203 for(size_t i=0; i<kd->face_idx.size(); i++) {
|
nuclear@54
|
204 if(ray_triangle_test(ray, faces + kd->face_idx[i], &sp) && sp.t < spret->t) {
|
nuclear@54
|
205 *spret = sp;
|
nuclear@54
|
206 }
|
nuclear@54
|
207 }
|
nuclear@54
|
208 return spret->face != 0;
|
nuclear@54
|
209 }
|
nuclear@54
|
210
|
nuclear@54
|
211 static bool ray_aabb_test(const Ray &ray, const AABBox &aabb)
|
nuclear@54
|
212 {
|
nuclear@54
|
213 if(ray.origin[0] >= aabb.min[0] && ray.origin[1] >= aabb.min[1] && ray.origin[2] >= aabb.min[2] &&
|
nuclear@54
|
214 ray.origin[0] < aabb.max[0] && ray.origin[1] < aabb.max[1] && ray.origin[2] < aabb.max[2]) {
|
nuclear@54
|
215 return true;
|
nuclear@54
|
216 }
|
nuclear@54
|
217
|
nuclear@54
|
218 float bbox[][3] = {
|
nuclear@54
|
219 {aabb.min[0], aabb.min[1], aabb.min[2]},
|
nuclear@54
|
220 {aabb.max[0], aabb.max[1], aabb.max[2]}
|
nuclear@54
|
221 };
|
nuclear@54
|
222
|
nuclear@54
|
223 int xsign = (int)(ray.dir[0] < 0.0);
|
nuclear@54
|
224 float invdirx = 1.0 / ray.dir[0];
|
nuclear@54
|
225 float tmin = (bbox[xsign][0] - ray.origin[0]) * invdirx;
|
nuclear@54
|
226 float tmax = (bbox[1 - xsign][0] - ray.origin[0]) * invdirx;
|
nuclear@54
|
227
|
nuclear@54
|
228 int ysign = (int)(ray.dir[1] < 0.0);
|
nuclear@54
|
229 float invdiry = 1.0 / ray.dir[1];
|
nuclear@54
|
230 float tymin = (bbox[ysign][1] - ray.origin[1]) * invdiry;
|
nuclear@54
|
231 float tymax = (bbox[1 - ysign][1] - ray.origin[1]) * invdiry;
|
nuclear@54
|
232
|
nuclear@54
|
233 if(tmin > tymax || tymin > tmax) {
|
nuclear@54
|
234 return false;
|
nuclear@54
|
235 }
|
nuclear@54
|
236
|
nuclear@54
|
237 if(tymin > tmin) tmin = tymin;
|
nuclear@54
|
238 if(tymax < tmax) tmax = tymax;
|
nuclear@54
|
239
|
nuclear@54
|
240 int zsign = (int)(ray.dir[2] < 0.0);
|
nuclear@54
|
241 float invdirz = 1.0 / ray.dir[2];
|
nuclear@54
|
242 float tzmin = (bbox[zsign][2] - ray.origin[2]) * invdirz;
|
nuclear@54
|
243 float tzmax = (bbox[1 - zsign][2] - ray.origin[2]) * invdirz;
|
nuclear@54
|
244
|
nuclear@54
|
245 if(tmin > tzmax || tzmin > tmax) {
|
nuclear@54
|
246 return false;
|
nuclear@54
|
247 }
|
nuclear@54
|
248
|
nuclear@54
|
249 return tmin < 1.0 && tmax > 0.0;
|
nuclear@54
|
250
|
nuclear@54
|
251 }
|
nuclear@54
|
252
|
nuclear@54
|
253 static bool ray_triangle_test(const Ray &ray, const Face *face, SurfPoint *sp)
|
nuclear@54
|
254 {
|
nuclear@54
|
255 Vector3 origin = ray.origin;
|
nuclear@54
|
256 Vector3 dir = ray.dir;
|
nuclear@54
|
257 Vector3 norm = face->normal;
|
nuclear@54
|
258
|
nuclear@54
|
259 float ndotdir = dot(dir, norm);
|
nuclear@54
|
260
|
nuclear@54
|
261 if(fabs(ndotdir) <= EPSILON) {
|
nuclear@54
|
262 return false;
|
nuclear@54
|
263 }
|
nuclear@54
|
264
|
nuclear@54
|
265 Vector3 pt = face->v[0].pos;
|
nuclear@54
|
266 Vector3 vec = pt - origin;
|
nuclear@54
|
267
|
nuclear@54
|
268 float ndotvec = dot(norm, vec);
|
nuclear@54
|
269 float t = ndotvec / ndotdir;
|
nuclear@54
|
270
|
nuclear@54
|
271 if(t < EPSILON || t > 1.0) {
|
nuclear@54
|
272 return false;
|
nuclear@54
|
273 }
|
nuclear@54
|
274 pt = origin + dir * t;
|
nuclear@54
|
275
|
nuclear@54
|
276
|
nuclear@54
|
277 Vector3 bc = calc_bary(pt, face, norm);
|
nuclear@54
|
278 float bc_sum = bc.x + bc.y + bc.z;
|
nuclear@54
|
279
|
nuclear@54
|
280 if(bc_sum < 1.0 - EPSILON || bc_sum > 1.0 + EPSILON) {
|
nuclear@54
|
281 return false;
|
nuclear@54
|
282 }
|
nuclear@54
|
283
|
nuclear@54
|
284 Vector3 n0(face->v[0].normal);
|
nuclear@54
|
285 Vector3 n1(face->v[1].normal);
|
nuclear@54
|
286 Vector3 n2(face->v[2].normal);
|
nuclear@54
|
287
|
nuclear@54
|
288 sp->t = t;
|
nuclear@54
|
289 sp->pos = pt;
|
nuclear@54
|
290 sp->norm = n0 * bc.x + n1 * bc.y + n2 * bc.z;
|
nuclear@54
|
291 sp->norm.normalize();
|
nuclear@54
|
292 sp->face = face;
|
nuclear@54
|
293 return true;
|
nuclear@54
|
294 }
|
nuclear@54
|
295
|
nuclear@54
|
296 static Vector3 calc_bary(const Vector3 &pt, const Face *face, const Vector3 &norm)
|
nuclear@54
|
297 {
|
nuclear@54
|
298 Vector3 bc(0.0f, 0.0f, 0.0f);
|
nuclear@54
|
299
|
nuclear@54
|
300 Vector3 v1 = Vector3(face->v[1].pos) - Vector3(face->v[0].pos);
|
nuclear@54
|
301 Vector3 v2 = Vector3(face->v[2].pos) - Vector3(face->v[0].pos);
|
nuclear@54
|
302 Vector3 xv1v2 = cross(v1, v2);
|
nuclear@54
|
303
|
nuclear@54
|
304 float area = fabs(dot(xv1v2, norm)) * 0.5;
|
nuclear@54
|
305 if(area < EPSILON) {
|
nuclear@54
|
306 return bc;
|
nuclear@54
|
307 }
|
nuclear@54
|
308
|
nuclear@54
|
309 Vector3 pv0 = face->v[0].pos - pt;
|
nuclear@54
|
310 Vector3 pv1 = face->v[1].pos - pt;
|
nuclear@54
|
311 Vector3 pv2 = face->v[2].pos - pt;
|
nuclear@54
|
312
|
nuclear@54
|
313 // calculate the area of each sub-triangle
|
nuclear@54
|
314 Vector3 x12 = cross(pv1, pv2);
|
nuclear@54
|
315 Vector3 x20 = cross(pv2, pv0);
|
nuclear@54
|
316 Vector3 x01 = cross(pv0, pv1);
|
nuclear@54
|
317
|
nuclear@54
|
318 float a0 = fabs(dot(x12, norm)) * 0.5;
|
nuclear@54
|
319 float a1 = fabs(dot(x20, norm)) * 0.5;
|
nuclear@54
|
320 float a2 = fabs(dot(x01, norm)) * 0.5;
|
nuclear@54
|
321
|
nuclear@54
|
322 bc.x = a0 / area;
|
nuclear@54
|
323 bc.y = a1 / area;
|
nuclear@54
|
324 bc.z = a2 / area;
|
nuclear@54
|
325 return bc;
|
nuclear@54
|
326
|
nuclear@54
|
327 }
|
nuclear@54
|
328
|
nuclear@54
|
329 static void transform(float *res, const float *v, const float *xform)
|
nuclear@54
|
330 {
|
nuclear@54
|
331 float tmp[3];
|
nuclear@54
|
332 tmp[0] = v[0] * xform[0] + v[1] * xform[4] + v[2] * xform[8] + xform[12];
|
nuclear@54
|
333 tmp[1] = v[0] * xform[1] + v[1] * xform[5] + v[2] * xform[9] + xform[13];
|
nuclear@54
|
334 tmp[2] = v[0] * xform[2] + v[1] * xform[6] + v[2] * xform[10] + xform[14];
|
nuclear@54
|
335 memcpy(res, tmp, sizeof tmp);
|
nuclear@54
|
336 }
|
nuclear@54
|
337
|
nuclear@54
|
338 static void transform_ray(Ray *ray, const float *xform, const float *invtrans_xform)
|
nuclear@54
|
339 {
|
nuclear@54
|
340 transform(ray->origin, ray->origin, xform);
|
nuclear@54
|
341 transform(ray->dir, ray->dir, invtrans_xform);
|
nuclear@54
|
342 }
|