949b7afb5a037beda81f475d45de2ca7806cddef
[blender-staging.git] / source / blender / render / intern / source / rayobject.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): AndrĂ© Pinto.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29 #include <assert.h>
30 #include <stdio.h>
31
32 #include "BKE_utildefines.h"
33 #include "BLI_arithb.h"
34
35 #include "RE_raytrace.h"
36 #include "render_types.h"
37 #include "rayobject.h"
38
39
40 /*
41  * Determines the distance that the ray must travel to hit the bounding volume of the given node
42  * Based on Tactical Optimization of Ray/Box Intersection, by Graham Fyffe
43  *  [http://tog.acm.org/resources/RTNews/html/rtnv21n1.html#art9]
44  */
45 float RE_rayobject_bb_intersect(const Isect *isec, const float *_bb)
46 {
47         const float *bb = _bb;
48         float dist;
49         
50         float t1x = (bb[isec->bv_index[0]] - isec->start[0]) * isec->idot_axis[0];
51         float t2x = (bb[isec->bv_index[1]] - isec->start[0]) * isec->idot_axis[0];
52         float t1y = (bb[isec->bv_index[2]] - isec->start[1]) * isec->idot_axis[1];
53         float t2y = (bb[isec->bv_index[3]] - isec->start[1]) * isec->idot_axis[1];
54         float t1z = (bb[isec->bv_index[4]] - isec->start[2]) * isec->idot_axis[2];
55         float t2z = (bb[isec->bv_index[5]] - isec->start[2]) * isec->idot_axis[2];
56
57         RE_RC_COUNT(isec->raycounter->bb.test);
58
59         if(t1x > t2y || t2x < t1y || t1x > t2z || t2x < t1z || t1y > t2z || t2y < t1z) return FLT_MAX;
60         if(t2x < 0.0 || t2y < 0.0 || t2z < 0.0) return FLT_MAX;
61         if(t1x > isec->labda || t1y > isec->labda || t1z > isec->labda) return FLT_MAX;
62
63         RE_RC_COUNT(isec->raycounter->bb.hit);
64
65         dist = t1x;
66         if (t1y > dist) dist = t1y;
67     if (t1z > dist) dist = t1z;
68         return dist;
69 }
70
71
72 /* only for self-intersecting test with current render face (where ray left) */
73 static int intersection2(VlakRen *face, float r0, float r1, float r2, float rx1, float ry1, float rz1)
74 {
75         float co1[3], co2[3], co3[3], co4[3];
76         float x0,x1,x2,t00,t01,t02,t10,t11,t12,t20,t21,t22;
77         float m0, m1, m2, divdet, det, det1;
78         float u1, v, u2;
79
80         VECCOPY(co1, face->v1->co);
81         VECCOPY(co2, face->v2->co);
82         if(face->v4)
83         {
84                 VECCOPY(co3, face->v4->co);
85                 VECCOPY(co4, face->v3->co);
86         }
87         else
88         {
89                 VECCOPY(co3, face->v3->co);
90         }
91
92         t00= co3[0]-co1[0];
93         t01= co3[1]-co1[1];
94         t02= co3[2]-co1[2];
95         t10= co3[0]-co2[0];
96         t11= co3[1]-co2[1];
97         t12= co3[2]-co2[2];
98         
99         x0= t11*r2-t12*r1;
100         x1= t12*r0-t10*r2;
101         x2= t10*r1-t11*r0;
102
103         divdet= t00*x0+t01*x1+t02*x2;
104
105         m0= rx1-co3[0];
106         m1= ry1-co3[1];
107         m2= rz1-co3[2];
108         det1= m0*x0+m1*x1+m2*x2;
109         
110         if(divdet!=0.0f) {
111                 u1= det1/divdet;
112
113                 if(u1<ISECT_EPSILON) {
114                         det= t00*(m1*r2-m2*r1);
115                         det+= t01*(m2*r0-m0*r2);
116                         det+= t02*(m0*r1-m1*r0);
117                         v= det/divdet;
118
119                         if(v<ISECT_EPSILON && (u1 + v) > -(1.0f+ISECT_EPSILON)) {
120                                 return 1;
121                         }
122                 }
123         }
124
125         if(face->v4) {
126
127                 t20= co3[0]-co4[0];
128                 t21= co3[1]-co4[1];
129                 t22= co3[2]-co4[2];
130
131                 divdet= t20*x0+t21*x1+t22*x2;
132                 if(divdet!=0.0f) {
133                         u2= det1/divdet;
134                 
135                         if(u2<ISECT_EPSILON) {
136                                 det= t20*(m1*r2-m2*r1);
137                                 det+= t21*(m2*r0-m0*r2);
138                                 det+= t22*(m0*r1-m1*r0);
139                                 v= det/divdet;
140         
141                                 if(v<ISECT_EPSILON && (u2 + v) >= -(1.0f+ISECT_EPSILON)) {
142                                         return 2;
143                                 }
144                         }
145                 }
146         }
147         return 0;
148 }
149
150
151 /* ray - triangle or quad intersection */
152 /* this function shall only modify Isect if it detects an hit */
153 static int intersect_rayface(RayFace *face, Isect *is)
154 {
155         float co1[3],co2[3],co3[3],co4[3];
156         float x0,x1,x2,t00,t01,t02,t10,t11,t12,t20,t21,t22,r0,r1,r2;
157         float m0, m1, m2, divdet, det1;
158         float labda, u, v;
159         short ok=0;
160         
161         if(is->orig.ob == face->ob && is->orig.face == face->face)
162                 return 0;
163
164         RE_RC_COUNT(is->raycounter->faces.test);
165
166         VECCOPY(co1, face->v1);
167         VECCOPY(co2, face->v2);
168         if(face->v4)
169         {
170                 VECCOPY(co3, face->v4);
171                 VECCOPY(co4, face->v3);
172         }
173         else
174         {
175                 VECCOPY(co3, face->v3);
176         }
177
178         t00= co3[0]-co1[0];
179         t01= co3[1]-co1[1];
180         t02= co3[2]-co1[2];
181         t10= co3[0]-co2[0];
182         t11= co3[1]-co2[1];
183         t12= co3[2]-co2[2];
184         
185         r0= is->vec[0];
186         r1= is->vec[1];
187         r2= is->vec[2];
188         
189         x0= t12*r1-t11*r2;
190         x1= t10*r2-t12*r0;
191         x2= t11*r0-t10*r1;
192
193         divdet= t00*x0+t01*x1+t02*x2;
194
195         m0= is->start[0]-co3[0];
196         m1= is->start[1]-co3[1];
197         m2= is->start[2]-co3[2];
198         det1= m0*x0+m1*x1+m2*x2;
199         
200         if(divdet!=0.0f) {
201
202                 divdet= 1.0f/divdet;
203                 u= det1*divdet;
204                 if(u<ISECT_EPSILON && u>-(1.0f+ISECT_EPSILON)) {
205                         float cros0, cros1, cros2;
206                         
207                         cros0= m1*t02-m2*t01;
208                         cros1= m2*t00-m0*t02;
209                         cros2= m0*t01-m1*t00;
210                         v= divdet*(cros0*r0 + cros1*r1 + cros2*r2);
211
212                         if(v<ISECT_EPSILON && (u + v) > -(1.0f+ISECT_EPSILON)) {
213                                 labda= divdet*(cros0*t10 + cros1*t11 + cros2*t12);
214
215                                 if(labda>-ISECT_EPSILON && labda<is->labda) {
216                                         ok= 1;
217                                 }
218                         }
219                 }
220         }
221
222         if(ok==0 && face->v4) {
223
224                 t20= co3[0]-co4[0];
225                 t21= co3[1]-co4[1];
226                 t22= co3[2]-co4[2];
227
228                 divdet= t20*x0+t21*x1+t22*x2;
229                 if(divdet!=0.0f) {
230                         divdet= 1.0f/divdet;
231                         u = det1*divdet;
232                         
233                         if(u<ISECT_EPSILON && u>-(1.0f+ISECT_EPSILON)) {
234                                 float cros0, cros1, cros2;
235                                 cros0= m1*t22-m2*t21;
236                                 cros1= m2*t20-m0*t22;
237                                 cros2= m0*t21-m1*t20;
238                                 v= divdet*(cros0*r0 + cros1*r1 + cros2*r2);
239         
240                                 if(v<ISECT_EPSILON && (u + v) >-(1.0f+ISECT_EPSILON)) {
241                                         labda= divdet*(cros0*t10 + cros1*t11 + cros2*t12);
242                                         
243                                         if(labda>-ISECT_EPSILON && labda<is->labda) {
244                                                 ok= 2;
245                                         }
246                                 }
247                         }
248                 }
249         }
250
251         if(ok) {
252         
253                 /* when a shadow ray leaves a face, it can be little outside the edges of it, causing
254                 intersection to be detected in its neighbour face */
255                 if(is->skip & RE_SKIP_VLR_NEIGHBOUR)
256                 {
257                         if(labda < 0.1f && is->orig.ob == face->ob)
258                         {
259                                 VlakRen * a = is->orig.face;
260                                 VlakRen * b = face->face;
261
262                                 /* so there's a shared edge or vertex, let's intersect ray with face
263                                 itself, if that's true we can safely return 1, otherwise we assume
264                                 the intersection is invalid, 0 */
265                                 if(a->v1==b->v1 || a->v2==b->v1 || a->v3==b->v1 || a->v4==b->v1
266                                 || a->v1==b->v2 || a->v2==b->v2 || a->v3==b->v2 || a->v4==b->v2
267                                 || a->v1==b->v3 || a->v2==b->v3 || a->v3==b->v3 || a->v4==b->v3
268                                 || (b->v4 && (a->v1==b->v4 || a->v2==b->v4 || a->v3==b->v4 || a->v4==b->v4)))
269                                 if(intersection2((VlakRen*)b, -r0, -r1, -r2, is->start[0], is->start[1], is->start[2]))
270                                 {
271                                         return 0;
272                                 }
273                         }
274                 }
275 #if 0
276                 else if(labda < ISECT_EPSILON)
277                 {
278                         /* too close to origin */
279                         return 0;
280                 }
281 #endif
282
283                 RE_RC_COUNT(is->raycounter->faces.hit);
284
285                 is->isect= ok;  // wich half of the quad
286                 is->labda= labda;
287                 is->u= u; is->v= v;
288
289                 is->hit.ob   = face->ob;
290                 is->hit.face = face->face;
291 #ifdef RT_USE_LAST_HIT
292                 is->last_hit = (RayObject*) RayObject_unalignRayFace(face);
293 #endif
294                 return 1;
295         }
296
297         return 0;
298 }
299
300 int RE_rayobject_raycast(RayObject *r, Isect *isec)
301 {
302         int i;
303         RE_RC_COUNT(isec->raycounter->raycast.test);
304
305         /* Setup vars used on raycast */
306         isec->labda *= Normalize(isec->vec);
307         isec->dist = VecLength(isec->vec);
308         
309         for(i=0; i<3; i++)
310         {
311                 isec->idot_axis[i]              = 1.0f / isec->vec[i];
312                 
313                 isec->bv_index[2*i]             = isec->idot_axis[i] < 0.0 ? 1 : 0;
314                 isec->bv_index[2*i+1]   = 1 - isec->bv_index[2*i];
315                 
316                 isec->bv_index[2*i]             = i+3*isec->bv_index[2*i];
317                 isec->bv_index[2*i+1]   = i+3*isec->bv_index[2*i+1];
318         }
319
320 #ifdef RT_USE_LAST_HIT  
321         /* Last hit heuristic */
322         if(isec->mode==RE_RAY_SHADOW && isec->last_hit)
323         {
324                 RE_RC_COUNT(isec->raycounter->rayshadow_last_hit.test);
325                 
326                 if(RE_rayobject_intersect(isec->last_hit, isec))
327                 {
328                         RE_RC_COUNT(isec->raycounter->raycast.hit);
329                         RE_RC_COUNT(isec->raycounter->rayshadow_last_hit.hit);
330                         return 1;
331                 }
332         }
333 #endif
334
335 #ifdef RT_USE_HINT
336         isec->hit_hint = 0;
337 #endif
338
339         if(RE_rayobject_intersect(r, isec))
340         {
341 #ifdef RE_RAYCOUNTER
342                 RE_RC_COUNT(isec->raycounter->raycast.hit);
343 #endif
344
345 #ifdef RT_USE_HINT
346                 isec->hint = isec->hit_hint;
347 #endif
348                 return 1;
349         }
350         return 0;
351 }
352
353 int RE_rayobject_intersect(RayObject *r, Isect *i)
354 {
355         if(RayObject_isRayFace(r))
356         {
357                 return intersect_rayface( (RayFace*) RayObject_align(r), i);
358         }
359         else if(RayObject_isRayAPI(r))
360         {
361                 r = RayObject_align( r );
362                 return r->api->raycast( r, i );
363         }
364         else assert(0);
365 }
366
367 void RE_rayobject_add(RayObject *r, RayObject *o)
368 {
369         r = RayObject_align( r );
370         return r->api->add( r, o );
371 }
372
373 void RE_rayobject_done(RayObject *r)
374 {
375         r = RayObject_align( r );
376         r->api->done( r );
377 }
378
379 void RE_rayobject_free(RayObject *r)
380 {
381         r = RayObject_align( r );
382         r->api->free( r );
383 }
384
385 void RE_rayobject_merge_bb(RayObject *r, float *min, float *max)
386 {
387         if(RayObject_isRayFace(r))
388         {
389                 RayFace *face = (RayFace*) RayObject_align(r);
390                 DO_MINMAX( face->v1, min, max );
391                 DO_MINMAX( face->v2, min, max );
392                 DO_MINMAX( face->v3, min, max );
393                 if(face->v4) DO_MINMAX( face->v4, min, max );
394         }
395         else if(RayObject_isRayAPI(r))
396         {
397                 r = RayObject_align( r );
398                 r->api->bb( r, min, max );
399         }
400         else assert(0);
401 }
402
403 float RE_rayobject_cost(RayObject *r)
404 {
405         if(RayObject_isRayFace(r))
406         {
407                 return 1.0;
408         }
409         else if(RayObject_isRayAPI(r))
410         {
411                 r = RayObject_align( r );
412                 return r->api->cost( r );
413         }
414         else assert(0);
415 }
416
417 #ifdef RE_RAYCOUNTER
418 void RE_RC_INFO(RayCounter *info)
419 {
420         printf("----------- Raycast counter --------\n");
421         printf("Rays total: %llu\n", info->raycast.test );
422         printf("Rays hit: %llu\n",   info->raycast.hit  );
423         printf("\n");
424         printf("BB tests: %llu\n", info->bb.test );
425         printf("BB hits: %llu\n", info->bb.hit );
426         printf("\n");   
427         printf("Primitives tests: %llu\n", info->faces.test );
428         printf("Primitives hits: %llu\n", info->faces.hit );
429         printf("------------------------------------\n");
430         printf("Shadow last-hit tests per ray: %f\n", info->rayshadow_last_hit.test / ((float)info->raycast.test) );
431         printf("Shadow last-hit hits per ray: %f\n",  info->rayshadow_last_hit.hit  / ((float)info->raycast.test) );
432         printf("\n");
433         printf("Hint tests per ray: %f\n", info->raytrace_hint.test / ((float)info->raycast.test) );
434         printf("Hint hits per ray: %f\n",  info->raytrace_hint.hit  / ((float)info->raycast.test) );
435         printf("\n");
436         printf("BB tests per ray: %f\n", info->bb.test / ((float)info->raycast.test) );
437         printf("BB hits per ray: %f\n", info->bb.hit / ((float)info->raycast.test) );
438         printf("\n");
439         printf("Primitives tests per ray: %f\n", info->faces.test / ((float)info->raycast.test) );
440         printf("Primitives hits per ray: %f\n", info->faces.hit / ((float)info->raycast.test) );
441         printf("------------------------------------\n");
442 }
443
444 void RE_RC_MERGE(RayCounter *dest, RayCounter *tmp)
445 {
446         dest->faces.test += tmp->faces.test;
447         dest->faces.hit  += tmp->faces.hit;
448
449         dest->bb.test += tmp->bb.test;
450         dest->bb.hit  += tmp->bb.hit;
451
452         dest->raycast.test += tmp->raycast.test;
453         dest->raycast.hit  += tmp->raycast.hit;
454         
455         dest->rayshadow_last_hit.test += tmp->rayshadow_last_hit.test;
456         dest->rayshadow_last_hit.hit  += tmp->rayshadow_last_hit.hit;
457
458         dest->raytrace_hint.test += tmp->raytrace_hint.test;
459         dest->raytrace_hint.hit  += tmp->raytrace_hint.hit;
460 }
461
462 #endif