Revision: 21492
          
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=21492
Author:   jaguarandi
Date:     2009-07-10 18:42:51 +0200 (Fri, 10 Jul 2009)

Log Message:
-----------
*rtbuild now stores BB
*fix in ray/bb hit tests inside instances

Modified Paths:
--------------
    
branches/soc-2009-jaguarandi/source/blender/render/intern/include/rayobject_rtbuild.h
    
branches/soc-2009-jaguarandi/source/blender/render/intern/source/rayobject_instance.c
    
branches/soc-2009-jaguarandi/source/blender/render/intern/source/rayobject_rtbuild.c

Modified: 
branches/soc-2009-jaguarandi/source/blender/render/intern/include/rayobject_rtbuild.h
===================================================================
--- 
branches/soc-2009-jaguarandi/source/blender/render/intern/include/rayobject_rtbuild.h
       2009-07-10 15:43:59 UTC (rev 21491)
+++ 
branches/soc-2009-jaguarandi/source/blender/render/intern/include/rayobject_rtbuild.h
       2009-07-10 16:42:51 UTC (rev 21492)
@@ -54,6 +54,8 @@
        int child_offset[RTBUILD_MAX_CHILDS+1];
        
        int child_sorted_axis; /* -1 if not sorted */
+       
+       float bb[6];
 
 } RTBuilder;
 
@@ -61,6 +63,7 @@
 RTBuilder* rtbuild_create(int size);
 void rtbuild_free(RTBuilder *b);
 void rtbuild_add(RTBuilder *b, RayObject *o);
+void rtbuild_merge_bb(RTBuilder *b, float *min, float *max);
 int rtbuild_size(RTBuilder *b);
 
 /* used during tree reorganization */
@@ -82,5 +85,7 @@
 
 /* bb utils */
 float bb_area(float *min, float *max);
+float bb_volume(float *min, float *max);
+int bb_largest_axis(float *min, float *max);
 
 #endif

Modified: 
branches/soc-2009-jaguarandi/source/blender/render/intern/source/rayobject_instance.c
===================================================================
--- 
branches/soc-2009-jaguarandi/source/blender/render/intern/source/rayobject_instance.c
       2009-07-10 15:43:59 UTC (rev 21491)
+++ 
branches/soc-2009-jaguarandi/source/blender/render/intern/source/rayobject_instance.c
       2009-07-10 16:42:51 UTC (rev 21492)
@@ -34,9 +34,12 @@
 #include "RE_raytrace.h"
 #include "rayobject.h"
 
+#define RE_COST_INSTANCE (1.0f)
+
 static int  RayObject_instance_intersect(RayObject *o, Isect *isec);
 static void RayObject_instance_free(RayObject *o);
 static void RayObject_instance_bb(RayObject *o, float *min, float *max);
+static float RayObject_instance_cost(RayObject *o);
 
 static RayObjectAPI instance_api =
 {
@@ -44,7 +47,8 @@
        NULL, //static void RayObject_instance_add(RayObject *o, RayObject *ob);
        NULL, //static void RayObject_instance_done(RayObject *o);
        RayObject_instance_free,
-       RayObject_instance_bb
+       RayObject_instance_bb,
+       RayObject_instance_cost
 };
 
 typedef struct InstanceRayObject
@@ -85,7 +89,7 @@
        InstanceRayObject *obj = (InstanceRayObject*)o;
        int res;
        float start[3], vec[3], labda, dist;
-       int changed = 0;
+       int changed = 0, i;
        
        //TODO - this is disabling self intersection on instances
        if(isec->orig.ob == obj->ob && obj->ob)
@@ -111,6 +115,18 @@
        
        isec->labda *= isec->dist / dist;
        
+       //Update idot_axis and bv_index
+       for(i=0; i<3; i++)
+       {
+               isec->idot_axis[i]              = 1.0f / isec->vec[i];
+               
+               isec->bv_index[2*i]             = isec->idot_axis[i] < 0.0 ? 1 
: 0;
+               isec->bv_index[2*i+1]   = 1 - isec->bv_index[2*i];
+               
+               isec->bv_index[2*i]             = i+3*isec->bv_index[2*i];
+               isec->bv_index[2*i+1]   = i+3*isec->bv_index[2*i+1];
+       }
+
        //Raycast
        res = RE_rayobject_intersect(obj->target, isec);
 
@@ -118,7 +134,6 @@
        if(res == 0)
        {
                isec->labda = labda;
-               
        }
        else
        {
@@ -131,7 +146,19 @@
        
        if(changed)
                isec->orig.ob = obj->ob;
+
+       //Update idot_axis and bv_index
+       for(i=0; i<3; i++)
+       {
+               isec->idot_axis[i]              = 1.0f / isec->vec[i];
                
+               isec->bv_index[2*i]             = isec->idot_axis[i] < 0.0 ? 1 
: 0;
+               isec->bv_index[2*i+1]   = 1 - isec->bv_index[2*i];
+               
+               isec->bv_index[2*i]             = i+3*isec->bv_index[2*i];
+               isec->bv_index[2*i+1]   = i+3*isec->bv_index[2*i+1];
+       }
+               
        return res;
 }
 
@@ -141,6 +168,12 @@
        MEM_freeN(obj);
 }
 
+static float RayObject_instance_cost(RayObject *o)
+{
+       InstanceRayObject *obj = (InstanceRayObject*)o;
+       return RE_rayobject_cost(obj->target) + RE_COST_INSTANCE;
+}
+
 static void RayObject_instance_bb(RayObject *o, float *min, float *max)
 {
        //TODO:

Modified: 
branches/soc-2009-jaguarandi/source/blender/render/intern/source/rayobject_rtbuild.c
===================================================================
--- 
branches/soc-2009-jaguarandi/source/blender/render/intern/source/rayobject_rtbuild.c
        2009-07-10 15:43:59 UTC (rev 21491)
+++ 
branches/soc-2009-jaguarandi/source/blender/render/intern/source/rayobject_rtbuild.c
        2009-07-10 16:42:51 UTC (rev 21492)
@@ -11,7 +11,6 @@
 static void split_leafs(RTBuilder *b, int *nth, int partitions, int 
split_axis);
 static int split_leafs_by_plane(RTBuilder *b, int begin, int end, float plane);
 
-
 static void rtbuild_init(RTBuilder *b, RayObject **begin, RayObject **end)
 {
        int i;
@@ -23,6 +22,8 @@
        
        for(i=0; i<RTBUILD_MAX_CHILDS; i++)
                b->child_offset[i] = 0;
+               
+       INIT_MINMAX(b->bb, b->bb+3);
 }
 
 RTBuilder* rtbuild_create(int size)
@@ -44,66 +45,45 @@
        *(b->end++) = o;
 }
 
-RTBuilder* rtbuild_get_child(RTBuilder *b, int child, RTBuilder *tmp)
+void rtbuild_calc_bb(RTBuilder *b)
 {
-       rtbuild_init( tmp, b->begin + b->child_offset[child], b->begin + 
b->child_offset[child+1] );
-       tmp->child_sorted_axis = b->child_sorted_axis;
-       return tmp;
+       if(b->bb[0] == 1.0e30f)
+       {
+               RayObject **index = b->begin;
+               for(; index != b->end; index++)
+                       RE_rayobject_merge_bb(*index, b->bb, b->bb+3);
+       }
 }
 
-int rtbuild_size(RTBuilder *b)
+void rtbuild_merge_bb(RTBuilder *b, float *min, float *max)
 {
-       return b->end - b->begin;
+       rtbuild_calc_bb(b);
+       DO_MIN(b->bb, min);
+       DO_MAX(b->bb+3, max);
 }
 
-/* Split methods */
-static void merge_bb(RTBuilder *b, float *min, float *max)
+int rtbuild_get_largest_axis(RTBuilder *b)
 {
-       RayObject **index = b->begin;
-
-       for(; index != b->end; index++)
-               RE_rayobject_merge_bb(*index, min, max);
+       rtbuild_calc_bb(b);
+       return bb_largest_axis(b->bb, b->bb+3);
 }
 
-static int largest_axis(float *min, float *max)
-{
-       float sub[3];
-       
-       sub[0] = max[0]-min[0];
-       sub[1] = max[1]-min[1];
-       sub[2] = max[2]-min[2];
-       if(sub[0] > sub[1])
-       {
-               if(sub[0] > sub[2])
-                       return 0;
-               else
-                       return 2;
-       }
-       else
-       {
-               if(sub[1] > sub[2])
-                       return 1;
-               else
-                       return 2;
-       }       
-}
 
-int rtbuild_get_largest_axis(RTBuilder *b)
+int rtbuild_size(RTBuilder *b)
 {
-       float min[3], max[3];
-
-       INIT_MINMAX(min, max);
-       merge_bb( b, min, max);
-
-       return largest_axis(min,max);
+       return b->end - b->begin;
 }
 
 
-/*
-int rtbuild_median_split(RTBuilder *b, int nchilds, int axis)
+RTBuilder* rtbuild_get_child(RTBuilder *b, int child, RTBuilder *tmp)
 {
+       rtbuild_init( tmp, b->begin + b->child_offset[child], b->begin + 
b->child_offset[child+1] );
+       tmp->child_sorted_axis = b->child_sorted_axis;
+       return tmp;
 }
-*/
+
+
+
 //Left balanced tree
 int rtbuild_mean_split(RTBuilder *b, int nchilds, int axis)
 {
@@ -199,14 +179,12 @@
 {
        int la, i;
        float separators[RTBUILD_MAX_CHILDS];
-       float min[3], max[3];
+       
+       rtbuild_calc_bb(b);
 
-       INIT_MINMAX(min, max);
-       merge_bb( b, min, max);
-
-       la = largest_axis(min,max);
+       la = bb_largest_axis(b->bb,b->bb+3);
        for(i=1; i<nchilds; i++)
-               separators[i-1] = (max[la]-min[la])*i / nchilds;
+               separators[i-1] = (b->bb[la+3]-b->bb[la])*i / nchilds;
                
        return rtbuild_median_split(b, separators, nchilds, la);
 }
@@ -250,23 +228,9 @@
        else if(axis == 5) qsort(begin, end-begin, sizeof(*begin), 
(int(*)(const void *, const void *)) costobject_cmp(5));
 }
 
-float bb_volume(float *min, float *max)
-{
-       return (max[0]-min[0])*(max[1]-min[1])*(max[2]-min[2]);
-}
 
-float bb_area(float *min, float *max)
-{
-       float sub[3], a;
-       sub[0] = max[0]-min[0];
-       sub[1] = max[1]-min[1];
-       sub[2] = max[2]-min[2];
 
-       a = (sub[0]*sub[1] + sub[0]*sub[2] + sub[1]*sub[2])*2;
-       assert(a >= 0.0);
-       return a;
-}
-
+/* Object Surface Area Heuristic splitter */
 int rtbuild_heuristic_object_split(RTBuilder *b, int nchilds)
 {
        int size = rtbuild_size(b);             
@@ -387,78 +351,7 @@
        }
 }
 
-//Heuristic Area Splitter
-typedef struct CostEvent CostEvent;
-
-struct CostEvent
-{
-       float key;
-       float value;
-};
-
-int costevent_cmp(const CostEvent *a, const CostEvent *b)
-{
-       if(a->key < b->key) return -1;
-       if(a->key > b->key) return  1;
-       if(a->value < b->value) return -1;
-       if(a->value > b->value) return  1;
-       return 0;
-}
-
-void costevent_sort(CostEvent *begin, CostEvent *end)
-{
-       //TODO introsort
-       qsort(begin, sizeof(*begin), end-begin, (int(*)(const void *, const 
void *)) costevent_cmp);
-}
 /*
-int rtbuild_heuristic_split(RTBuilder *b, int nchilds)
-{
-       int size = rtbuild_size(b);             
-       
-       assert(nchilds == 2);
-       
-       if(size <= nchilds)
-       {
-               return rtbuild_mean_split_largest_axis(b, nchilds);
-       }
-       else
-       {
-               CostEvent *events, *ev;
-               RayObject *index;
-               int a = 0;
-
-               events = MEM_malloc( sizeof(CostEvent)*2*size, 
"RTBuilder.SweepSplitCostEvent" );
-               for(a = 0; a<3; a++)
-               
-
-               for(index = b->begin; b != b->end; b++)
-               {
-                       float min[3], max[3];
-                       INIT_MINMAX(min, max);
-                       RE_rayobject_merge_bb(index, min, max);
-                       for(a = 0; a<3; a++)
-                       {
-                               ev[a]->key = min[a];
-                               ev[a]->value = 1;
-                               ev[a]++;
-               
-                               ev[a]->key = max[a];
-                               ev[a]->value = -1;
-                               ev[a]++;
-                       }
-               }
-               for(a = 0; a<3; a++)
-                       costevent_sort(events[a], ev[a]);
-                       
-               
-               
-               for(a = 0; a<3; a++)
-                       MEM_freeN(ev[a]);
-       }
-}
-*/
-
-/*
  * Helper code
  * PARTITION code / used on mean-split
  * basicly this a std::nth_element (like on C++ STL algorithm)
@@ -593,3 +486,46 @@
        }
        return begin;
 }
+
+/*
+ * Bounding Box utils
+ */
+float bb_volume(float *min, float *max)
+{
+       return (max[0]-min[0])*(max[1]-min[1])*(max[2]-min[2]);
+}
+
+float bb_area(float *min, float *max)
+{
+       float sub[3], a;
+       sub[0] = max[0]-min[0];
+       sub[1] = max[1]-min[1];
+       sub[2] = max[2]-min[2];
+
+       a = (sub[0]*sub[1] + sub[0]*sub[2] + sub[1]*sub[2])*2;
+       assert(a >= 0.0);
+       return a;
+}
+
+int bb_largest_axis(float *min, float *max)
+{
+       float sub[3];
+       
+       sub[0] = max[0]-min[0];
+       sub[1] = max[1]-min[1];
+       sub[2] = max[2]-min[2];
+       if(sub[0] > sub[1])
+       {
+               if(sub[0] > sub[2])
+                       return 0;
+               else
+                       return 2;
+       }
+       else
+       {
+               if(sub[1] > sub[2])
+                       return 1;
+               else
+                       return 2;
+       }       
+}


_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to