Browse Source

Directional work

Josh Engebretson 8 years ago
parent
commit
733e5d3d58

+ 19 - 10
Source/AtomicGlow/Kernel/BakeLight.cpp

@@ -297,11 +297,17 @@ float DirectionalLightInfluence::Calculate(LightRay* lightRay, const Vector3& li
         return 0.0f;
     }
 
-    // ** Cast shadow to point
-    if( light_->GetCastsShadow() )
+    // clean this mess up
+    RTCScene scene = source.bakeMesh->GetSceneBaker()->GetEmbreeScene()->GetRTCScene();
+
+    lightRay->SetupRay(source.position, -direction_, .001f, LIGHT_LARGE_DISTANCE);
+
+    rtcOccluded(scene, lightRay->rtcRay_);
+
+    // obstructed?  TODO: glass, etc
+    if (lightRay->rtcRay_.geomID != RTC_INVALID_GEOMETRY_ID)
     {
-        // FIXME: tracer
-        // intensity *= tracer->traceSegment( point, point - m_direction * 1000, rt::HitUseAlpha ) ? 0.0f : 1.0f;
+        return 0.0f;
     }
 
     return intensity;
@@ -504,15 +510,18 @@ DirectionalPhotonEmitter::DirectionalPhotonEmitter( const BakeLight* light, cons
     plane_.Define(direction.Normalized(), light->GetPosition()); //  = Plane::calculate( direction, light->position() );
 }
 
-/*
-void DirectionalPhotonEmitter::Emit( const Scene* scene, Vector3& position, Vector3& direction ) const
+void DirectionalPhotonEmitter::Emit( SceneBaker* sceneBaker, Vector3& position, Vector3& direction ) const
 {
-    const Bounds& bounds = scene->bounds();
+    const BoundingBox& bounds = sceneBaker->GetSceneBounds();
 
-    position  = m_plane * bounds.randomPointInside() - m_direction * 5;
-    direction = m_direction;
+    Vector3 randomPoint(Lerp<float>(bounds.min_.x_, bounds.max_.x_, RandZeroOne()),
+                        Lerp<float>(bounds.min_.y_, bounds.max_.y_, RandZeroOne()),
+                        Lerp<float>(bounds.min_.z_, bounds.max_.z_, RandZeroOne()));
+
+
+    position  = plane_.Project(randomPoint) - direction_ * ( LIGHT_LARGE_DISTANCE * 0.5f);
+    direction = direction_;
 }
-*/
 
 
 }

+ 1 - 1
Source/AtomicGlow/Kernel/BakeLight.h

@@ -244,7 +244,7 @@ public:
     DirectionalPhotonEmitter( const BakeLight* light, const Vector3& direction );
 
     /// Emits a new photon.
-    // virtual void Emit( const Scene* scene, Vector3& position, Vector3& direction ) const;
+    virtual void Emit( SceneBaker* scene, Vector3& position, Vector3& direction ) const;
 
 private:
 

+ 1 - 0
Source/AtomicGlow/Kernel/GlowTypes.h

@@ -37,6 +37,7 @@ enum GlowLightMode
     GLOW_LIGHTMODE_COMPLETE
 };
 
+const float LIGHT_LARGE_DISTANCE = 65535.0f;
 const float LIGHT_ANGLE_EPSILON = 0.001f;
 
 }

+ 3 - 1
Source/AtomicGlow/Kernel/Photons.cpp

@@ -256,7 +256,9 @@ void Photons::Trace(const LightAttenuation* attenuation, const Vector3& position
     // clean this mess up
     RTCScene scene = sceneBaker_->GetEmbreeScene()->GetRTCScene();
 
-    lightRay_.SetupRay(position, direction, .001f, maxDistance_);
+    float maxDist = attenuation ? maxDistance_ : LIGHT_LARGE_DISTANCE;
+
+    lightRay_.SetupRay(position, direction, .001f, maxDist);
 
     RTCRay& ray = lightRay_.rtcRay_;
 

+ 2 - 1
Source/AtomicGlow/Kernel/Photons.h

@@ -163,7 +163,8 @@ public:
             {
                 if (photon->tris_[i] == PHOTON_TRI_INVALID)
                 {
-                    idx = photon->tris_[i] = triIndex;
+                    idx = i;
+                    photon->tris_[i] = triIndex;
                     break;
                 }
             }

+ 23 - 8
Source/AtomicGlow/Kernel/SceneBaker.cpp

@@ -368,6 +368,7 @@ bool SceneBaker::LoadScene(const String& filename)
 
     for (unsigned i = 0; i < lightNodes.Size(); i++)
     {
+        BakeLight* bakeLight = 0;
         Node* lightNode = lightNodes[i];
         Atomic::Light* light = lightNode->GetComponent<Atomic::Light>();
 
@@ -376,15 +377,15 @@ bool SceneBaker::LoadScene(const String& filename)
 
         if (light->GetLightType() == LIGHT_DIRECTIONAL)
         {
-            /*
-            SharedPtr<DirectionalBakeLight> dlight(new DirectionalBakeLight(context_, this));
-            dlight->SetLight(light);
-            bakeLights_.Push(dlight);
-            */
+            bakeLight = BakeLight::CreateDirectionalLight(this, lightNode->GetDirection(), light->GetColor(), 1.0f, light->GetCastShadows());
         }
         else if (light->GetLightType() == LIGHT_POINT)
         {
-            BakeLight* bakeLight = BakeLight::CreatePointLight(this, lightNode->GetWorldPosition(), light->GetRange(), light->GetColor(), 1.0f, light->GetCastShadows());
+            bakeLight = BakeLight::CreatePointLight(this, lightNode->GetWorldPosition(), light->GetRange(), light->GetColor(), 1.0f, light->GetCastShadows());
+        }
+
+        if (bakeLight)
+        {
             bakeLights_.Push(SharedPtr<BakeLight>(bakeLight));
         }
 
@@ -401,7 +402,11 @@ bool SceneBaker::LoadScene(const String& filename)
         if (!staticModel->GetNode()->IsEnabled() || !staticModel->IsEnabled())
             continue;
 
-        Vector3 center = staticModel->GetWorldBoundingBox().Center();
+        const BoundingBox& modelWBox = staticModel->GetWorldBoundingBox();
+
+        sceneBounds_.Merge(modelWBox);
+
+        Vector3 center = modelWBox.Center();
         int bestPriority = M_MIN_INT;
         Zone* newZone = 0;
 
@@ -585,6 +590,7 @@ void SceneBaker::IndirectLight( LightRay* lightRay)
     int nsamples = GlobalGlowSettings.finalGatherSamples_;
     float maxDistance = GlobalGlowSettings.finalGatherDistance_; // , settings.m_finalGatherRadius, settings.m_skyColor, settings.m_ambientColor
 
+    int hits = 0;
     for( int k = 0; k <nsamples; k++ )
     {
         Vector3 dir;
@@ -604,7 +610,12 @@ void SceneBaker::IndirectLight( LightRay* lightRay)
 
         if (ray.geomID == RTC_INVALID_GEOMETRY_ID)
         {
-            // gathered += skyColor * influence + ambientColor;
+            Color skyColor(130.0f/255.0f, 209.0f/255.0f, 207.0f/255.0f);
+
+            skyColor = skyColor * 0.30f;
+
+            gathered += (skyColor * influence).ToVector3();// + ambientColor;
+            hits++;
             continue;
         }               
 
@@ -649,10 +660,14 @@ void SceneBaker::IndirectLight( LightRay* lightRay)
             continue;
         }
 
+        hits++;
         gathered += gcolor.ToVector3() * influence;// + ambientColor;
 
     }
 
+    if (!hits)
+        return;
+
     gathered /= static_cast<float>( nsamples );
 
     if (gathered.x_ >= 0.01f || gathered.y_ >= 0.01f || gathered.z_ >= 0.01f )

+ 5 - 1
Source/AtomicGlow/Kernel/SceneBaker.h

@@ -68,7 +68,9 @@ class SceneBaker : public Object
 
     bool GetStandaloneMode() const { return standaloneMode_; }
 
-    void SetStandaloneMode(bool standaloneMode) { standaloneMode_ = standaloneMode; }    
+    void SetStandaloneMode(bool standaloneMode) { standaloneMode_ = standaloneMode; }
+
+    const BoundingBox& GetSceneBounds() const { return sceneBounds_; }
 
 private:
 
@@ -116,6 +118,8 @@ private:
     String projectPath_;
     bool standaloneMode_;
 
+    BoundingBox sceneBounds_;
+
 };
 
 }