Warning fixes, one actual bug found in sequencer sound wave drawing. Also
[blender-staging.git] / source / blender / render / intern / raytrace / rayobject.cpp
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
31 #include "BKE_utildefines.h"
32 #include "BLI_math.h"
33 #include "DNA_material_types.h"
34
35 #include "RE_raytrace.h"
36 #include "render_types.h"
37 #include "rayobject.h"
38 #include "raycounter.h"
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 int RE_rayobject_bb_intersect_test(const Isect *isec, const float *_bb)
46 {
47         const float *bb = _bb;
48         
49         float t1x = (bb[isec->bv_index[0]] - isec->start[0]) * isec->idot_axis[0];
50         float t2x = (bb[isec->bv_index[1]] - isec->start[0]) * isec->idot_axis[0];
51         float t1y = (bb[isec->bv_index[2]] - isec->start[1]) * isec->idot_axis[1];
52         float t2y = (bb[isec->bv_index[3]] - isec->start[1]) * isec->idot_axis[1];
53         float t1z = (bb[isec->bv_index[4]] - isec->start[2]) * isec->idot_axis[2];
54         float t2z = (bb[isec->bv_index[5]] - isec->start[2]) * isec->idot_axis[2];
55
56         RE_RC_COUNT(isec->raycounter->bb.test);
57         
58         if(t1x > t2y || t2x < t1y || t1x > t2z || t2x < t1z || t1y > t2z || t2y < t1z) return 0;
59         if(t2x < 0.0 || t2y < 0.0 || t2z < 0.0) return 0;
60         if(t1x > isec->labda || t1y > isec->labda || t1z > isec->labda) return 0;
61         RE_RC_COUNT(isec->raycounter->bb.hit);  
62
63         return 1;
64 }
65
66
67 /* only for self-intersecting test with current render face (where ray left) */
68 static int intersection2(VlakRen *face, float r0, float r1, float r2, float rx1, float ry1, float rz1)
69 {
70         float co1[3], co2[3], co3[3], co4[3];
71         float x0,x1,x2,t00,t01,t02,t10,t11,t12,t20,t21,t22;
72         float m0, m1, m2, divdet, det, det1;
73         float u1, v, u2;
74
75         VECCOPY(co1, face->v1->co);
76         VECCOPY(co2, face->v2->co);
77         if(face->v4)
78         {
79                 VECCOPY(co3, face->v4->co);
80                 VECCOPY(co4, face->v3->co);
81         }
82         else
83         {
84                 VECCOPY(co3, face->v3->co);
85         }
86
87         t00= co3[0]-co1[0];
88         t01= co3[1]-co1[1];
89         t02= co3[2]-co1[2];
90         t10= co3[0]-co2[0];
91         t11= co3[1]-co2[1];
92         t12= co3[2]-co2[2];
93         
94         x0= t11*r2-t12*r1;
95         x1= t12*r0-t10*r2;
96         x2= t10*r1-t11*r0;
97
98         divdet= t00*x0+t01*x1+t02*x2;
99
100         m0= rx1-co3[0];
101         m1= ry1-co3[1];
102         m2= rz1-co3[2];
103         det1= m0*x0+m1*x1+m2*x2;
104         
105         if(divdet!=0.0f) {
106                 u1= det1/divdet;
107
108                 if(u1<ISECT_EPSILON) {
109                         det= t00*(m1*r2-m2*r1);
110                         det+= t01*(m2*r0-m0*r2);
111                         det+= t02*(m0*r1-m1*r0);
112                         v= det/divdet;
113
114                         if(v<ISECT_EPSILON && (u1 + v) > -(1.0f+ISECT_EPSILON)) {
115                                 return 1;
116                         }
117                 }
118         }
119
120         if(face->v4) {
121
122                 t20= co3[0]-co4[0];
123                 t21= co3[1]-co4[1];
124                 t22= co3[2]-co4[2];
125
126                 divdet= t20*x0+t21*x1+t22*x2;
127                 if(divdet!=0.0f) {
128                         u2= det1/divdet;
129                 
130                         if(u2<ISECT_EPSILON) {
131                                 det= t20*(m1*r2-m2*r1);
132                                 det+= t21*(m2*r0-m0*r2);
133                                 det+= t22*(m0*r1-m1*r0);
134                                 v= det/divdet;
135         
136                                 if(v<ISECT_EPSILON && (u2 + v) >= -(1.0f+ISECT_EPSILON)) {
137                                         return 2;
138                                 }
139                         }
140                 }
141         }
142         return 0;
143 }
144
145 static inline int vlr_check_intersect(Isect *is, ObjectInstanceRen *obi, VlakRen *vlr)
146 {
147         /* for baking selected to active non-traceable materials might still
148          * be in the raytree */
149         if(!(vlr->mat->mode & MA_TRACEBLE))
150                 return 0;
151
152         /* I know... cpu cycle waste, might do smarter once */
153         if(is->mode==RE_RAY_MIRROR)
154                 return !(vlr->mat->mode & MA_ONLYCAST);
155         else
156                 return (is->lay & obi->lay);
157 }
158
159 static inline int vlr_check_intersect_solid(Isect *is, ObjectInstanceRen* obi, VlakRen *vlr)
160 {
161         /* solid material types only */
162         if (vlr->mat->material_type == MA_TYPE_SURFACE)
163                 return 1;
164         else
165                 return 0;
166 }
167
168 static inline int rayface_check_cullface(RayFace *face, Isect *is)
169 {
170         float nor[3];
171         
172         /* don't intersect if the ray faces along the face normal */
173         if(face->quad) normal_quad_v3( nor,face->v1, face->v2, face->v3, face->v4);
174         else normal_tri_v3( nor,face->v1, face->v2, face->v3);
175
176         return (INPR(nor, is->vec) < 0);
177 }
178
179 /* ray - triangle or quad intersection */
180 /* this function shall only modify Isect if it detects an hit */
181 static int intersect_rayface(RayObject *hit_obj, RayFace *face, Isect *is)
182 {
183         float co1[3],co2[3],co3[3],co4[3]={0};
184         float x0,x1,x2,t00,t01,t02,t10,t11,t12,t20,t21,t22,r0,r1,r2;
185         float m0, m1, m2, divdet, det1;
186         float labda, u, v;
187         short ok=0;
188         
189         if(is->orig.ob == face->ob && is->orig.face == face->face)
190                 return 0;
191                 
192
193         if(is->skip & RE_SKIP_VLR_RENDER_CHECK)
194         {
195                 if(vlr_check_intersect(is, (ObjectInstanceRen*)face->ob, (VlakRen*)face->face ) == 0)
196                         return 0;
197         }
198         if(is->skip & RE_SKIP_VLR_NON_SOLID_MATERIAL)
199         {
200                 if(vlr_check_intersect_solid(is, (ObjectInstanceRen*)face->ob, (VlakRen*)face->face) == 0)
201                         return 0;
202         }
203         if(is->skip & RE_SKIP_CULLFACE)
204         {
205                 if(rayface_check_cullface(face, is) == 0)
206                         return 0;
207         }
208
209         RE_RC_COUNT(is->raycounter->faces.test);
210
211         //Load coords
212         VECCOPY(co1, face->v1);
213         VECCOPY(co2, face->v2);
214         if(RE_rayface_isQuad(face))
215         {
216                 VECCOPY(co3, face->v4);
217                 VECCOPY(co4, face->v3);
218         }
219         else
220         {
221                 VECCOPY(co3, face->v3);
222         }
223
224         t00= co3[0]-co1[0];
225         t01= co3[1]-co1[1];
226         t02= co3[2]-co1[2];
227         t10= co3[0]-co2[0];
228         t11= co3[1]-co2[1];
229         t12= co3[2]-co2[2];
230         
231         r0= is->vec[0];
232         r1= is->vec[1];
233         r2= is->vec[2];
234         
235         x0= t12*r1-t11*r2;
236         x1= t10*r2-t12*r0;
237         x2= t11*r0-t10*r1;
238
239         divdet= t00*x0+t01*x1+t02*x2;
240
241         m0= is->start[0]-co3[0];
242         m1= is->start[1]-co3[1];
243         m2= is->start[2]-co3[2];
244         det1= m0*x0+m1*x1+m2*x2;
245         
246         if(divdet!=0.0f) {
247
248                 divdet= 1.0f/divdet;
249                 u= det1*divdet;
250                 if(u<ISECT_EPSILON && u>-(1.0f+ISECT_EPSILON)) {
251                         float cros0, cros1, cros2;
252                         
253                         cros0= m1*t02-m2*t01;
254                         cros1= m2*t00-m0*t02;
255                         cros2= m0*t01-m1*t00;
256                         v= divdet*(cros0*r0 + cros1*r1 + cros2*r2);
257
258                         if(v<ISECT_EPSILON && (u + v) > -(1.0f+ISECT_EPSILON)) {
259                                 labda= divdet*(cros0*t10 + cros1*t11 + cros2*t12);
260
261                                 if(labda>-ISECT_EPSILON && labda<is->labda) {
262                                         ok= 1;
263                                 }
264                         }
265                 }
266         }
267
268         if(ok==0 && RE_rayface_isQuad(face)) {
269
270                 t20= co3[0]-co4[0];
271                 t21= co3[1]-co4[1];
272                 t22= co3[2]-co4[2];
273
274                 divdet= t20*x0+t21*x1+t22*x2;
275                 if(divdet!=0.0f) {
276                         divdet= 1.0f/divdet;
277                         u = det1*divdet;
278                         
279                         if(u<ISECT_EPSILON && u>-(1.0f+ISECT_EPSILON)) {
280                                 float cros0, cros1, cros2;
281                                 cros0= m1*t22-m2*t21;
282                                 cros1= m2*t20-m0*t22;
283                                 cros2= m0*t21-m1*t20;
284                                 v= divdet*(cros0*r0 + cros1*r1 + cros2*r2);
285         
286                                 if(v<ISECT_EPSILON && (u + v) >-(1.0f+ISECT_EPSILON)) {
287                                         labda= divdet*(cros0*t10 + cros1*t11 + cros2*t12);
288                                         
289                                         if(labda>-ISECT_EPSILON && labda<is->labda) {
290                                                 ok= 2;
291                                         }
292                                 }
293                         }
294                 }
295         }
296
297         if(ok) {
298         
299                 /* when a shadow ray leaves a face, it can be little outside the edges of it, causing
300                 intersection to be detected in its neighbour face */
301                 if(is->skip & RE_SKIP_VLR_NEIGHBOUR)
302                 {
303                         if(labda < 0.1f && is->orig.ob == face->ob)
304                         {
305                                 VlakRen * a = (VlakRen*)is->orig.face;
306                                 VlakRen * b = (VlakRen*)face->face;
307
308                                 /* so there's a shared edge or vertex, let's intersect ray with face
309                                 itself, if that's true we can safely return 1, otherwise we assume
310                                 the intersection is invalid, 0 */
311                                 if(a->v1==b->v1 || a->v2==b->v1 || a->v3==b->v1 || a->v4==b->v1
312                                 || a->v1==b->v2 || a->v2==b->v2 || a->v3==b->v2 || a->v4==b->v2
313                                 || a->v1==b->v3 || a->v2==b->v3 || a->v3==b->v3 || a->v4==b->v3
314                                 || (b->v4 && (a->v1==b->v4 || a->v2==b->v4 || a->v3==b->v4 || a->v4==b->v4)))
315                                 if(!intersection2((VlakRen*)a, -r0, -r1, -r2, is->start[0], is->start[1], is->start[2]))
316                                 {
317                                         return 0;
318                                 }
319                         }
320                 }
321
322                 RE_RC_COUNT(is->raycounter->faces.hit);
323
324                 is->isect= ok;  // wich half of the quad
325                 is->labda= labda;
326                 is->u= u; is->v= v;
327
328                 is->hit.ob   = face->ob;
329                 is->hit.face = face->face;
330 #ifdef RT_USE_LAST_HIT
331                 is->last_hit = hit_obj;
332 #endif
333                 return 1;
334         }
335
336         return 0;
337 }
338
339 RayObject* RE_rayface_from_vlak(RayFace *rayface, ObjectInstanceRen *obi, VlakRen *vlr)
340 {
341         return RE_rayface_from_coords(rayface, obi, vlr, vlr->v1->co, vlr->v2->co, vlr->v3->co, vlr->v4 ? vlr->v4->co : 0 );
342 }
343
344 RayObject* RE_rayface_from_coords(RayFace *rayface, void *ob, void *face, float *v1, float *v2, float *v3, float *v4)
345 {
346         rayface->ob = ob;
347         rayface->face = face;
348
349         VECCOPY(rayface->v1, v1);
350         VECCOPY(rayface->v2, v2);
351         VECCOPY(rayface->v3, v3);
352         if(v4)
353         {
354                 VECCOPY(rayface->v4, v4);
355                 rayface->quad = 1;
356         }
357         else
358         {
359                 rayface->quad = 0;
360         }
361
362         return RE_rayobject_unalignRayFace(rayface);
363 }
364
365 RayObject* RE_vlakprimitive_from_vlak(VlakPrimitive *face, struct ObjectInstanceRen *obi, struct VlakRen *vlr)
366 {
367         face->ob = obi;
368         face->face = vlr;
369         return RE_rayobject_unalignVlakPrimitive(face);
370 }
371
372
373 int RE_rayobject_raycast(RayObject *r, Isect *isec)
374 {
375         int i;
376         RE_RC_COUNT(isec->raycounter->raycast.test);
377
378         /* Setup vars used on raycast */
379         isec->dist = len_v3(isec->vec);
380         
381         for(i=0; i<3; i++)
382         {
383                 isec->idot_axis[i]              = 1.0f / isec->vec[i];
384                 
385                 isec->bv_index[2*i]             = isec->idot_axis[i] < 0.0 ? 1 : 0;
386                 isec->bv_index[2*i+1]   = 1 - isec->bv_index[2*i];
387                 
388                 isec->bv_index[2*i]             = i+3*isec->bv_index[2*i];
389                 isec->bv_index[2*i+1]   = i+3*isec->bv_index[2*i+1];
390         }
391
392 #ifdef RT_USE_LAST_HIT  
393         /* Last hit heuristic */
394         if(isec->mode==RE_RAY_SHADOW && isec->last_hit)
395         {
396                 RE_RC_COUNT(isec->raycounter->rayshadow_last_hit.test);
397                 
398                 if(RE_rayobject_intersect(isec->last_hit, isec))
399                 {
400                         RE_RC_COUNT(isec->raycounter->raycast.hit);
401                         RE_RC_COUNT(isec->raycounter->rayshadow_last_hit.hit);
402                         return 1;
403                 }
404         }
405 #endif
406
407 #ifdef RT_USE_HINT
408         isec->hit_hint = 0;
409 #endif
410
411         if(RE_rayobject_intersect(r, isec))
412         {
413                 RE_RC_COUNT(isec->raycounter->raycast.hit);
414
415 #ifdef RT_USE_HINT
416                 isec->hint = isec->hit_hint;
417 #endif
418                 return 1;
419         }
420         return 0;
421 }
422
423 int RE_rayobject_intersect(RayObject *r, Isect *i)
424 {
425         if(RE_rayobject_isRayFace(r))
426         {
427                 return intersect_rayface(r, (RayFace*) RE_rayobject_align(r), i);
428         }
429         else if(RE_rayobject_isVlakPrimitive(r))
430         {
431                 //TODO optimize (useless copy to RayFace to avoid duplicate code)
432                 VlakPrimitive *face = (VlakPrimitive*) RE_rayobject_align(r);
433                 RayFace nface;
434                 RE_rayface_from_vlak(&nface, face->ob, face->face);
435
436                 if(face->ob->transform_primitives)
437                 {
438                         mul_m4_v3(face->ob->mat, nface.v1);
439                         mul_m4_v3(face->ob->mat, nface.v2);
440                         mul_m4_v3(face->ob->mat, nface.v3);
441                         if(RE_rayface_isQuad(&nface))
442                                 mul_m4_v3(face->ob->mat, nface.v4);
443                 }
444
445                 return intersect_rayface(r, &nface, i);
446         }
447         else if(RE_rayobject_isRayAPI(r))
448         {
449                 r = RE_rayobject_align( r );
450                 return r->api->raycast( r, i );
451         }
452         else assert(0);
453 }
454
455 void RE_rayobject_add(RayObject *r, RayObject *o)
456 {
457         r = RE_rayobject_align( r );
458         return r->api->add( r, o );
459 }
460
461 void RE_rayobject_done(RayObject *r)
462 {
463         r = RE_rayobject_align( r );
464         r->api->done( r );
465 }
466
467 void RE_rayobject_free(RayObject *r)
468 {
469         r = RE_rayobject_align( r );
470         r->api->free( r );
471 }
472
473 void RE_rayobject_merge_bb(RayObject *r, float *min, float *max)
474 {
475         if(RE_rayobject_isRayFace(r))
476         {
477                 RayFace *face = (RayFace*) RE_rayobject_align(r);
478                 
479                 DO_MINMAX( face->v1, min, max );
480                 DO_MINMAX( face->v2, min, max );
481                 DO_MINMAX( face->v3, min, max );
482                 if(RE_rayface_isQuad(face)) DO_MINMAX( face->v4, min, max );
483         }
484         else if(RE_rayobject_isVlakPrimitive(r))
485         {
486                 VlakPrimitive *face = (VlakPrimitive*) RE_rayobject_align(r);
487                 VlakRen *vlr = face->face;
488
489                 DO_MINMAX( vlr->v1->co, min, max );
490                 DO_MINMAX( vlr->v2->co, min, max );
491                 DO_MINMAX( vlr->v3->co, min, max );
492                 if(vlr->v4) DO_MINMAX( vlr->v4->co, min, max );
493         }
494         else if(RE_rayobject_isRayAPI(r))
495         {
496                 r = RE_rayobject_align( r );
497                 r->api->bb( r, min, max );
498         }
499         else assert(0);
500 }
501
502 float RE_rayobject_cost(RayObject *r)
503 {
504         if(RE_rayobject_isRayFace(r) || RE_rayobject_isVlakPrimitive(r))
505         {
506                 return 1.0;
507         }
508         else if(RE_rayobject_isRayAPI(r))
509         {
510                 r = RE_rayobject_align( r );
511                 return r->api->cost( r );
512         }
513         else assert(0);
514 }
515
516 void RE_rayobject_hint_bb(RayObject *r, RayHint *hint, float *min, float *max)
517 {
518         if(RE_rayobject_isRayFace(r) || RE_rayobject_isVlakPrimitive(r))
519         {
520                 return;
521         }
522         else if(RE_rayobject_isRayAPI(r))
523         {
524                 r = RE_rayobject_align( r );
525                 return r->api->hint_bb( r, hint, min, max );
526         }
527         else assert(0);
528 }
529
530 int RE_rayobjectcontrol_test_break(RayObjectControl *control)
531 {
532         if(control->test_break)
533                 return control->test_break( control->data );
534
535         return 0;
536 }
537
538
539 /*
540  * Empty raytree
541  */
542 static int RE_rayobject_empty_intersect(RayObject *o, Isect *is)
543 {
544         return 0;
545 }
546
547 static void RE_rayobject_empty_free(RayObject *o)
548 {
549 }
550
551 static void RE_rayobject_empty_bb(RayObject *o, float *min, float *max)
552 {
553         return;
554 }
555
556 static float RE_rayobject_empty_cost(RayObject *o)
557 {
558         return 0.0;
559 }
560
561 static void RE_rayobject_empty_hint_bb(RayObject *o, RayHint *hint, float *min, float *max)
562 {}
563
564 static RayObjectAPI empty_api =
565 {
566         RE_rayobject_empty_intersect,
567         NULL, //static void RE_rayobject_instance_add(RayObject *o, RayObject *ob);
568         NULL, //static void RE_rayobject_instance_done(RayObject *o);
569         RE_rayobject_empty_free,
570         RE_rayobject_empty_bb,
571         RE_rayobject_empty_cost,
572         RE_rayobject_empty_hint_bb
573 };
574
575 static RayObject empty_raytree = { &empty_api, {0, 0} };
576
577 RayObject *RE_rayobject_empty_create()
578 {
579         return RE_rayobject_unalignRayAPI( &empty_raytree );
580 }