Revision: 45730
          http://brlcad.svn.sourceforge.net/brlcad/?rev=45730&view=rev
Author:   kunigami
Date:     2011-07-29 01:08:44 +0000 (Fri, 29 Jul 2011)

Log Message:
-----------
started coding the (supposely) raytracer using OSL. the results are not correct

Modified Paths:
--------------
    brlcad/trunk/src/liboptical/liboslrend.cpp
    brlcad/trunk/src/liboptical/liboslrend.h
    brlcad/trunk/src/liboptical/sh_osl.cpp

Modified: brlcad/trunk/src/liboptical/liboslrend.cpp
===================================================================
--- brlcad/trunk/src/liboptical/liboslrend.cpp  2011-07-29 00:25:33 UTC (rev 
45729)
+++ brlcad/trunk/src/liboptical/liboslrend.cpp  2011-07-29 01:08:44 UTC (rev 
45730)
@@ -76,10 +76,8 @@
            std::pair< TypeDesc, Vec3 > &vec_type = sh_info.vparam[i].second;
            shadingsys->Parameter(sh_info.vparam[i].first.c_str(), 
vec_type.first, &(vec_type.second));
        }
-       /*
        for(size_t i = 0; i < sh_info.mparam.size(); i++)
            shadingsys->Parameter(sh_info.mparam[i].first.c_str(), 
TypeDesc::TypeMatrix, &(sh_info.mparam[i].second));
-       */
 
        if(sh_info.layername == "")
            shadingsys->Shader("surface", sh_info.shadername.c_str(), NULL);
@@ -132,6 +130,34 @@
     // sample primitive from closure tree
     const ClosurePrimitive *prim = SamplePrimitive(weight, closure, 0.5);
 
+// Ray-tracing (local illumination)
+#if 1
+
+    if(prim){
+       if(prim->category() == OSL::ClosurePrimitive::BSDF) {
+           // evaluate bsdf closure
+           BSDFClosure *bsdf = (BSDFClosure*)prim;
+           
+           // Eval the reflection weight from each light source
+           size_t nlights = info->light_dirs.size();
+           float pdf;
+           for(size_t li = 0; li < nlights; li++){
+               info->reflect_weight += bsdf->eval_reflect(globals.I, 
info->light_dirs[li], pdf);
+           }
+           info->reflect_weight *= 1.0/nlights;
+       }
+       else if(prim->category() == OSL::ClosurePrimitive::Emissive) {
+           // evaluate emissive closure
+           EmissiveClosure *emissive = (EmissiveClosure*)prim;
+           Color3 l = weight*emissive->eval(globals.Ng, globals.I);
+           return l;
+       }
+    }
+    return Color3(0.0);
+
+// Path-tracing (global illumination)
+#else
+
     if(prim) {
        if(prim->category() == OSL::ClosurePrimitive::BSDF) {
            // sample BSDF closure
@@ -166,6 +192,9 @@
        }
     }
     return Color3(0.0f);
+
+#endif
+
 }
 /* Return thread specific information */
 void* OSLRenderer::CreateThreadInfo(){

Modified: brlcad/trunk/src/liboptical/liboslrend.h
===================================================================
--- brlcad/trunk/src/liboptical/liboslrend.h    2011-07-29 00:25:33 UTC (rev 
45729)
+++ brlcad/trunk/src/liboptical/liboslrend.h    2011-07-29 01:08:44 UTC (rev 
45730)
@@ -64,11 +64,19 @@
     int depth;              /* How many times the ray hit an object */
     fastf_t surfacearea;    /* FIXME */
     ShadingAttribStateRef shader_ref;   /* Reference for the shader we're 
querying */
- 
+    std::vector< Vec3 > light_dirs;     /* List of directions of lights that 
are visible from 
+                                          this query point */
+    
     /* -- output -- */
     point_t pc;           /* Color of the point (or multiplier) */
     int doreflection;     /* 1 if there will be reflection 0, otherwise */
     Ray out_ray;          /* output ray (in case of reflection) */
+
+    /* Experimental! Don't use yet */
+    Color3 reflect_weight;            /* Color that will be multiplied by the
+                                        color returned by the reflected ray */
+    Color3 transmit_weight;           /* Color that will be multiplied by the 
+                                        color returned by the transmited ray */
 };
 
 /* Required structure to initialize an OSL shader */

Modified: brlcad/trunk/src/liboptical/sh_osl.cpp
===================================================================
--- brlcad/trunk/src/liboptical/sh_osl.cpp      2011-07-29 00:25:33 UTC (rev 
45729)
+++ brlcad/trunk/src/liboptical/sh_osl.cpp      2011-07-29 01:08:44 UTC (rev 
45730)
@@ -38,8 +38,8 @@
 #include "vmath.h"
 #include "raytrace.h"
 #include "optical.h"
+#include "light.h"
 
-
 #define OSL_MAGIC 0x1837    /* make this a unique number for each shader */
 #define CK_OSL_SP(_p) BU_CKMAG(_p, OSL_MAGIC, "osl_specific")
 
@@ -618,7 +618,75 @@
     /* We assume that the only information that will be written is thread_info,
        so that oslr->QueryColor is thread safe */
     info.thread_info = thread_info;
+
+
+// Ray-tracing (local illumination)
+#if 1
+
+    /* -----------------------------------
+     * Get a list of all visible lights from this point
+     * -----------------------------------
+     */
+    light_obs(ap, swp, MFI_NORMAL|MFI_HIT|MFI_UV);
     
+    for (int i = ap->a_rt_i->rti_nlights-1; i >= 0; i--) {
+
+       struct light_specific *lp;
+
+       /* Light is not visible */
+       if ((lp = (struct light_specific *)swp->sw_visible[i]) == LIGHT_NULL)
+           continue;
+       /* Get the direction of this light */
+       Vec3 to_light;
+       VMOVE(to_light, swp->sw_tolight+3*i);
+       info.light_dirs.push_back(to_light);
+    }
+
+    info.reflect_weight = Color3(0.0);
+    info.transmit_weight = Color3(0.0);
+    Color3 weight = oslr->QueryColor(&info);
+    
+    /* If the weight of reflection is greater than zero, we shoot another ray 
*/
+    fastf_t reflect_W = 0;
+    for(size_t i = 0; i < 3; i++)
+       reflect_W += info.reflect_weight[i];
+
+    // Do reflection
+    if(reflect_W > 0.0f){
+
+       /* Find the direction of the reflected ray */
+       Vec3 I, N;
+       VMOVE(I, info.I); // incidence ray
+       VMOVE(N, info.N); // normal
+
+       float proj = N.dot(I);
+       Vec3 R = (2 * proj) * N - I;
+
+       struct application new_ap;
+       RT_APPLICATION_INIT(&new_ap);
+       
+       new_ap = *ap;                     /* struct copy */
+       new_ap.a_onehit = 1;
+       new_ap.a_hit = default_a_hit;
+       new_ap.a_level = info.depth + 1;
+       new_ap.a_flag = 0;
+       
+       VMOVE(new_ap.a_ray.r_dir, R);
+       VMOVE(new_ap.a_ray.r_pt, info.P);
+
+       //(void)rt_shootray(&new_ap);
+       //Color3 rec;
+       //VMOVE(rec, new_ap.a_color);
+       //Color3 res = rec*info.reflect_weight;
+       VMOVE(swp->sw_color, info.reflect_weight);
+    }
+    else {
+       VMOVE(swp->sw_color, weight);
+    }
+    
+
+// Path-tracing (global illumination)
+#else    
     for(int i = 0; i < nsamples; i++){
 
        /* We only perform reflection if application decides to */
@@ -671,6 +739,7 @@
     }
 
     VSCALE(swp->sw_color, acc_color, 1.0/nsamples);
+#endif
 
     return 1;
 }


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Got Input?   Slashdot Needs You.
Take our quick survey online.  Come on, we don't ask for help often.
Plus, you'll get a chance to win $100 to spend on ThinkGeek.
http://p.sf.net/sfu/slashdot-survey
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to