|
@@ -1,4 +1,5 @@
|
|
|
// Copyright (c) 2014-2017, THUNDERBEAST GAMES LLC All rights reserved
|
|
// Copyright (c) 2014-2017, THUNDERBEAST GAMES LLC All rights reserved
|
|
|
|
|
+// Portions Copyright (c) 2015 Dmitry Sovetov
|
|
|
// Copyright 2009-2017 Intel Corporation
|
|
// Copyright 2009-2017 Intel Corporation
|
|
|
//
|
|
//
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
@@ -35,443 +36,483 @@
|
|
|
namespace AtomicGlow
|
|
namespace AtomicGlow
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
-const float LIGHT_ANGLE_EPSILON = 0.001f;
|
|
|
|
|
-
|
|
|
|
|
-// http://www.altdevblogaday.com/2012/05/03/generating-uniformly-distributed-points-on-sphere/
|
|
|
|
|
-static inline void GetRandomDirection(Vector3& result)
|
|
|
|
|
|
|
+BakeLight::BakeLight(Context* context, SceneBaker* sceneBaker) : BakeNode(context, sceneBaker),
|
|
|
|
|
+ color_(Color::WHITE),
|
|
|
|
|
+ intensity_( 0.0f ),
|
|
|
|
|
+ castsShadow_( false )
|
|
|
{
|
|
{
|
|
|
- float z = 2.0f * rand() / RAND_MAX - 1.0f;
|
|
|
|
|
- float t = 2.0f * rand() / RAND_MAX * 3.14f;
|
|
|
|
|
- float r = sqrt(1.0f - z * z);
|
|
|
|
|
- result.x_ = r * (float) cos(t);
|
|
|
|
|
- result.y_ = r * (float) sin(t);
|
|
|
|
|
- result.z_ = z;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-BakeLight::BakeLight(Context* context, SceneBaker* sceneBaker) : BakeNode(context, sceneBaker),
|
|
|
|
|
- range_(0.0f)
|
|
|
|
|
|
|
+BakeLight::~BakeLight()
|
|
|
{
|
|
{
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-BakeLight::~BakeLight()
|
|
|
|
|
|
|
+LightCutoff* BakeLight::GetCutoffModel( void ) const
|
|
|
{
|
|
{
|
|
|
|
|
+ return cutoffModel_;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
+void BakeLight::SetCutoffModel( LightCutoff* value )
|
|
|
|
|
+{
|
|
|
|
|
+ cutoffModel_ = value;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Zone Lights
|
|
|
|
|
|
|
+LightInfluence* BakeLight::GetInfluenceModel( void ) const
|
|
|
|
|
+{
|
|
|
|
|
+ return influenceModel_;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
-ZoneBakeLight::ZoneBakeLight(Context* context, SceneBaker* sceneBaker) : BakeLight(context, sceneBaker)
|
|
|
|
|
|
|
+void BakeLight::SetInfluenceModel( LightInfluence* value )
|
|
|
{
|
|
{
|
|
|
|
|
+ influenceModel_ = value;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-ZoneBakeLight::~ZoneBakeLight()
|
|
|
|
|
|
|
+/*
|
|
|
|
|
+LightVertexGenerator* BakeLight::vertexGenerator( void ) const
|
|
|
{
|
|
{
|
|
|
|
|
+ return vertexGenerator_;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
+// ** BakeLight::setVertexGenerator
|
|
|
|
|
+void BakeLight::setVertexGenerator( LightVertexGenerator* value )
|
|
|
|
|
+{
|
|
|
|
|
+ delete m_vertexGenerator;
|
|
|
|
|
+ m_vertexGenerator = value;
|
|
|
}
|
|
}
|
|
|
|
|
+*/
|
|
|
|
|
|
|
|
-void ZoneBakeLight::Light(LightRay* lightRay)
|
|
|
|
|
|
|
+LightAttenuation* BakeLight::GetAttenuationModel( void ) const
|
|
|
{
|
|
{
|
|
|
- LightRay::SamplePoint& source = lightRay->samplePoint_;
|
|
|
|
|
|
|
+ return attenuationModel_;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- if (source.normal == Vector3::ZERO)
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+void BakeLight::SetAttenuationModel( LightAttenuation* value )
|
|
|
|
|
+{
|
|
|
|
|
+ attenuationModel_ = value;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- RTCScene scene = sceneBaker_->GetEmbreeScene()->GetRTCScene();
|
|
|
|
|
|
|
+PhotonEmitter* BakeLight::GetPhotonEmitter( void ) const
|
|
|
|
|
+{
|
|
|
|
|
+ return photonEmitter_;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- const Color& color = zone_->GetAmbientColor();
|
|
|
|
|
|
|
+void BakeLight::SetPhotonEmitter( PhotonEmitter* value )
|
|
|
|
|
+{
|
|
|
|
|
+ photonEmitter_ = value;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- Vector3 rad(color.r_, color.g_, color.b_);
|
|
|
|
|
|
|
+const Vector3& BakeLight::GetPosition( void ) const
|
|
|
|
|
+{
|
|
|
|
|
+ return position_;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- if (!GlobalGlowSettings.aoEnabled_)
|
|
|
|
|
- {
|
|
|
|
|
- source.bakeMesh->ContributeRadiance(lightRay, rad, GLOW_LIGHTMODE_AMBIENT);
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+void BakeLight::SetPosition( const Vector3& value )
|
|
|
|
|
+{
|
|
|
|
|
+ position_ = value;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
+const Color& BakeLight::GetColor( void ) const
|
|
|
|
|
+{
|
|
|
|
|
+ return color_;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- // TODO: AO using ray packets/streams
|
|
|
|
|
|
|
+void BakeLight::SetColor( const Color& value )
|
|
|
|
|
+{
|
|
|
|
|
+ color_ = value;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- RTCRay& ray = lightRay->rtcRay_;
|
|
|
|
|
|
|
+float BakeLight::GetIntensity( void ) const
|
|
|
|
|
+{
|
|
|
|
|
+ return intensity_;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- unsigned nsamples = GlobalGlowSettings.nsamples_;
|
|
|
|
|
|
|
+void BakeLight::SetIntensity( float value )
|
|
|
|
|
+{
|
|
|
|
|
+ intensity_ = value;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- // this needs to be based on model/scale likely?
|
|
|
|
|
- float aoDepth = GlobalGlowSettings.aoDepth_;
|
|
|
|
|
|
|
+bool BakeLight::GetCastsShadow( void ) const
|
|
|
|
|
+{
|
|
|
|
|
+ return castsShadow_;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- // smallest percent of ao value to use
|
|
|
|
|
- float aoMin = GlobalGlowSettings.aoMin_;
|
|
|
|
|
|
|
+void BakeLight::SetCastsShadow( bool value )
|
|
|
|
|
+{
|
|
|
|
|
+ castsShadow_ = value;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- // brightness control
|
|
|
|
|
- float multiply = GlobalGlowSettings.aoMultiply_;
|
|
|
|
|
|
|
+BakeLight* BakeLight::CreateZoneLight( SceneBaker* baker, const Color& color)
|
|
|
|
|
+{
|
|
|
|
|
+ BakeLight* light = new BakeLight(baker->GetContext(), baker);
|
|
|
|
|
+ light->SetCastsShadow( false );
|
|
|
|
|
+ light->SetColor( color );
|
|
|
|
|
+ light->SetIntensity(1.0f);
|
|
|
|
|
|
|
|
- // Shoot rays through the differential hemisphere.
|
|
|
|
|
- int nhits = 0;
|
|
|
|
|
- float avgDepth = 0.0f;
|
|
|
|
|
- for (unsigned nsamp = 0; nsamp < nsamples; nsamp++)
|
|
|
|
|
|
|
+ if (GlobalGlowSettings.aoEnabled_)
|
|
|
{
|
|
{
|
|
|
- Vector3 rayDir;
|
|
|
|
|
- GetRandomDirection(rayDir);
|
|
|
|
|
-
|
|
|
|
|
- float dotp = source.normal.x_ * rayDir.x_ +
|
|
|
|
|
- source.normal.y_ * rayDir.y_ +
|
|
|
|
|
- source.normal.z_ * rayDir.z_;
|
|
|
|
|
-
|
|
|
|
|
- if (dotp < 0.1f)
|
|
|
|
|
- {
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- float variance = 0.0f;//nsamples <= 32 ? 0.0f : aoDepth * ((float) rand() / (float) RAND_MAX) * 0.25f;
|
|
|
|
|
|
|
+ light->SetInfluenceModel( new AmbientOcclusionInfluence( light ) );
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- float depth = aoDepth + variance;
|
|
|
|
|
|
|
+ return light;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- lightRay->SetupRay(source.position, rayDir, .001f, depth);
|
|
|
|
|
|
|
+BakeLight* BakeLight::CreatePointLight( SceneBaker* baker, const Vector3& position, float radius, const Color& color, float intensity, bool castsShadow )
|
|
|
|
|
+{
|
|
|
|
|
+ BakeLight* light = new BakeLight(baker->GetContext(), baker);
|
|
|
|
|
|
|
|
- rtcOccluded(scene, ray);
|
|
|
|
|
|
|
+ light->SetInfluenceModel( new LightInfluence( light ) );
|
|
|
|
|
+ light->SetAttenuationModel( new LinearLightAttenuation( light, radius ) );
|
|
|
|
|
+ light->SetCutoffModel( new LightCutoff( light ) );
|
|
|
|
|
+ light->SetPhotonEmitter( new PhotonEmitter( light ) );
|
|
|
|
|
|
|
|
- if (ray.geomID != RTC_INVALID_GEOMETRY_ID)
|
|
|
|
|
- {
|
|
|
|
|
- avgDepth += Min<float>(ray.tfar, aoDepth);
|
|
|
|
|
- nhits++;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ light->SetCastsShadow( castsShadow );
|
|
|
|
|
+ light->SetPosition( position );
|
|
|
|
|
+ light->SetColor( color );
|
|
|
|
|
+ light->SetIntensity( intensity );
|
|
|
|
|
|
|
|
- if (nhits)// && (nsamples <= 32 ? true : nhits > 4))
|
|
|
|
|
- {
|
|
|
|
|
- avgDepth /= float(nhits);
|
|
|
|
|
- avgDepth /= aoDepth;
|
|
|
|
|
|
|
+ return light;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- avgDepth = Clamp<float>(avgDepth, 0.1f, 1.0f) * 100.0f;
|
|
|
|
|
- avgDepth *= avgDepth;
|
|
|
|
|
- float ao = avgDepth / 10000.0f;
|
|
|
|
|
|
|
+BakeLight* BakeLight::CreateSpotLight( SceneBaker* baker, const Vector3& position, const Vector3& direction, float cutoff, float radius, const Color& color, float intensity, bool castsShadow )
|
|
|
|
|
+{
|
|
|
|
|
+ BakeLight* light = new BakeLight(baker->GetContext(), baker);
|
|
|
|
|
|
|
|
- ao = aoMin + ao/2.0f;
|
|
|
|
|
- ao *= multiply;
|
|
|
|
|
- ao = Clamp<float>(ao, aoMin, 1.0f);
|
|
|
|
|
|
|
+ light->SetInfluenceModel( new LightInfluence( light ) );
|
|
|
|
|
+ light->SetAttenuationModel( new LinearLightAttenuation( light, radius ) );
|
|
|
|
|
+ light->SetCutoffModel( new LightSpotCutoff( light, direction, cutoff, 1.0f ) );
|
|
|
|
|
+ light->SetPhotonEmitter( new PhotonEmitter( light ) );
|
|
|
|
|
|
|
|
- rad *= ao;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ light->SetCastsShadow( castsShadow );
|
|
|
|
|
+ light->SetPosition( position );
|
|
|
|
|
+ light->SetColor( color );
|
|
|
|
|
+ light->SetIntensity( intensity );
|
|
|
|
|
|
|
|
- source.bakeMesh->ContributeRadiance(lightRay, rad, GLOW_LIGHTMODE_AMBIENT);
|
|
|
|
|
|
|
+ return light;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void ZoneBakeLight::SetZone(Zone* zone)
|
|
|
|
|
|
|
+BakeLight* BakeLight::CreateDirectionalLight( SceneBaker* baker, const Vector3& direction, const Color& color, float intensity, bool castsShadow )
|
|
|
{
|
|
{
|
|
|
- node_ = zone->GetNode();
|
|
|
|
|
- zone_ = zone;
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ BakeLight* light = new BakeLight(baker->GetContext(), baker);
|
|
|
|
|
|
|
|
|
|
+ light->SetInfluenceModel( new DirectionalLightInfluence( light, direction ) );
|
|
|
|
|
+ light->SetPhotonEmitter( new DirectionalPhotonEmitter( light, direction ) );
|
|
|
|
|
+ light->SetCastsShadow( castsShadow );
|
|
|
|
|
+ light->SetColor( color );
|
|
|
|
|
+ light->SetIntensity( intensity );
|
|
|
|
|
|
|
|
-// Directional Lights
|
|
|
|
|
|
|
+ return light;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
-DirectionalBakeLight::DirectionalBakeLight(Context* context, SceneBaker* sceneBaker) : BakeLight(context, sceneBaker)
|
|
|
|
|
|
|
+/*
|
|
|
|
|
+BakeLight* BakeLight::CreateAreaLight( SceneBaker* baker, const Mesh* mesh, const Vector3& position, const Rgb& color, float intensity, bool castsShadow )
|
|
|
{
|
|
{
|
|
|
|
|
+ Light* light = new Light;
|
|
|
|
|
+
|
|
|
|
|
+ light->SetInfluence( new LightInfluence( light ) );
|
|
|
|
|
+ light->SetAttenuation( new LinearLightAttenuation( light, mesh->bounds().volume() ) );
|
|
|
|
|
+ light->SetPhotonEmitter( new PhotonEmitter( light ) );
|
|
|
|
|
+ light->SetCutoff( new LightCutoff( light ) );
|
|
|
|
|
+ //light->SetVertexGenerator( new FaceLightVertexGenerator( mesh, true, 3 ) );
|
|
|
|
|
+ //light->SetVertexGenerator( new FaceLightVertexGenerator( mesh, false, 0 ) );
|
|
|
|
|
+ light->SetVertexGenerator( new FaceLightVertexGenerator( mesh, true, 0 ) );
|
|
|
|
|
+ // light->SetVertexGenerator( new LightVertexGenerator( mesh ) );
|
|
|
|
|
+ light->SetCastsShadow( castsShadow );
|
|
|
|
|
+ light->SetPosition( position );
|
|
|
|
|
+ light->SetColor( color );
|
|
|
|
|
+ light->SetIntensity( intensity );
|
|
|
|
|
+
|
|
|
|
|
+ light->GetVertexGenerator()->Generate();
|
|
|
|
|
+
|
|
|
|
|
+ return light;
|
|
|
}
|
|
}
|
|
|
|
|
+*/
|
|
|
|
|
+
|
|
|
|
|
+// ------------------------------------------------------- LightInfluence --------------------------------------------------------- //
|
|
|
|
|
|
|
|
-DirectionalBakeLight::~DirectionalBakeLight()
|
|
|
|
|
|
|
+LightInfluence::LightInfluence( const BakeLight* light ) :
|
|
|
|
|
+ light_( light )
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void DirectionalBakeLight::Light(LightRay* lightRay)
|
|
|
|
|
|
|
+float LightInfluence::Calculate(LightRay* lightRay, const Vector3& light, float& distance ) const
|
|
|
{
|
|
{
|
|
|
- RTCScene scene = sceneBaker_->GetEmbreeScene()->GetRTCScene();
|
|
|
|
|
-
|
|
|
|
|
LightRay::SamplePoint& source = lightRay->samplePoint_;
|
|
LightRay::SamplePoint& source = lightRay->samplePoint_;
|
|
|
- RTCRay& ray = lightRay->rtcRay_;
|
|
|
|
|
|
|
|
|
|
- float angle = direction_.DotProduct(source.normal);
|
|
|
|
|
|
|
+ Vector3 direction = light - source.position;
|
|
|
|
|
+ distance = direction.Length();
|
|
|
|
|
+ direction.Normalize();
|
|
|
|
|
|
|
|
- if (angle < 0.0f)
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ // ** Calculate Lambert's cosine law intensity
|
|
|
|
|
+ float intensity = GetLambert( direction, source.normal );
|
|
|
|
|
|
|
|
- lightRay->SetupRay(source.position, direction_);
|
|
|
|
|
|
|
+ if( intensity <= 0.001f ) {
|
|
|
|
|
+ return 0.0f;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- rtcOccluded(scene, ray);
|
|
|
|
|
|
|
+ // ** Cast shadow to point
|
|
|
|
|
+ if( light_->GetCastsShadow() )
|
|
|
|
|
+ {
|
|
|
|
|
+ LightRay::SamplePoint& source = lightRay->samplePoint_;
|
|
|
|
|
|
|
|
- // obstructed? TODO: glass, etc
|
|
|
|
|
- if (ray.geomID != RTC_INVALID_GEOMETRY_ID)
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ // clean this mess up
|
|
|
|
|
+ RTCScene scene = source.bakeMesh->GetSceneBaker()->GetEmbreeScene()->GetRTCScene();
|
|
|
|
|
|
|
|
- Vector3 rad(color_.r_, color_.g_, color_.b_);
|
|
|
|
|
|
|
+ lightRay->SetupRay(source.position, direction, .001f, distance);
|
|
|
|
|
|
|
|
- rad*=angle;
|
|
|
|
|
|
|
+ rtcOccluded(scene, lightRay->rtcRay_);
|
|
|
|
|
|
|
|
- source.bakeMesh->ContributeRadiance(lightRay, rad);
|
|
|
|
|
|
|
+ // obstructed? TODO: glass, etc
|
|
|
|
|
+ if (lightRay->rtcRay_.geomID != RTC_INVALID_GEOMETRY_ID)
|
|
|
|
|
+ {
|
|
|
|
|
+ return 0.0f;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ return intensity;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void DirectionalBakeLight::SetLight(Atomic::Light* light)
|
|
|
|
|
|
|
+float LightInfluence::GetLambert( const Vector3& direction, const Vector3& normal )
|
|
|
{
|
|
{
|
|
|
- node_ = light->GetNode();
|
|
|
|
|
-
|
|
|
|
|
- color_ = light->GetColor();
|
|
|
|
|
-
|
|
|
|
|
- direction_ = -node_->GetWorldDirection();
|
|
|
|
|
- direction_.Normalize();
|
|
|
|
|
-
|
|
|
|
|
|
|
+ float dp = direction.DotProduct(normal);
|
|
|
|
|
+ return dp < 0.0f ? 0.0f : dp;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Point Lights
|
|
|
|
|
|
|
+// --------------------------------------------------- DirectionalLightInfluence -------------------------------------------------- //
|
|
|
|
|
|
|
|
-PointBakeLight::PointBakeLight(Context* context, SceneBaker* sceneBaker) : BakeLight(context, sceneBaker)
|
|
|
|
|
-{
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-PointBakeLight::~PointBakeLight()
|
|
|
|
|
|
|
+DirectionalLightInfluence::DirectionalLightInfluence( const BakeLight* light, const Vector3& direction )
|
|
|
|
|
+ : LightInfluence( light ), direction_( direction )
|
|
|
{
|
|
{
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void PointBakeLight::Light(LightRay* lightRay)
|
|
|
|
|
|
|
+float DirectionalLightInfluence::Calculate(LightRay* lightRay, const Vector3& light, float& distance ) const
|
|
|
{
|
|
{
|
|
|
- RTCScene scene = sceneBaker_->GetEmbreeScene()->GetRTCScene();
|
|
|
|
|
-
|
|
|
|
|
LightRay::SamplePoint& source = lightRay->samplePoint_;
|
|
LightRay::SamplePoint& source = lightRay->samplePoint_;
|
|
|
- RTCRay& ray = lightRay->rtcRay_;
|
|
|
|
|
-
|
|
|
|
|
- Vector3 dir = position_ - source.position;
|
|
|
|
|
|
|
|
|
|
- float dist = dir.Length();
|
|
|
|
|
|
|
+ float intensity = GetLambert( -direction_, source.normal );
|
|
|
|
|
|
|
|
- if (range_ <= 0.0f || dist >= range_)
|
|
|
|
|
- return;
|
|
|
|
|
-
|
|
|
|
|
- dir.Normalize();
|
|
|
|
|
-
|
|
|
|
|
- float dot = dir.DotProduct(source.normal);
|
|
|
|
|
-
|
|
|
|
|
- if (dot < 0.0f)
|
|
|
|
|
- return;
|
|
|
|
|
-
|
|
|
|
|
- lightRay->SetupRay(source.position, dir, .001f, dist);
|
|
|
|
|
-
|
|
|
|
|
- rtcOccluded(scene, ray);
|
|
|
|
|
-
|
|
|
|
|
- // obstructed? TODO: glass, etc
|
|
|
|
|
- if (ray.geomID != RTC_INVALID_GEOMETRY_ID)
|
|
|
|
|
- return;
|
|
|
|
|
-
|
|
|
|
|
- Vector3 rad(color_.r_, color_.g_, color_.b_);
|
|
|
|
|
-
|
|
|
|
|
- // lightOverBright 1.2 for example will pop light,
|
|
|
|
|
- // needs to be configurable per light, maybe with brightness modifer
|
|
|
|
|
- float lightOverBright = 1.0f;
|
|
|
|
|
-
|
|
|
|
|
- rad *= Max<float> (1.0f - ( dist * lightOverBright / range_), 0.0f);
|
|
|
|
|
- rad *= dot;
|
|
|
|
|
-
|
|
|
|
|
- // EXPERIMENTAL: if GI is enabled, dim point light a bit
|
|
|
|
|
|
|
+ if( intensity <= 0.001f )
|
|
|
|
|
+ {
|
|
|
|
|
+ return 0.0f;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (GlobalGlowSettings.giEnabled_)
|
|
|
|
|
|
|
+ // ** Cast shadow to point
|
|
|
|
|
+ if( light_->GetCastsShadow() )
|
|
|
{
|
|
{
|
|
|
- rad *= 0.75f;
|
|
|
|
|
|
|
+ // FIXME: tracer
|
|
|
|
|
+ // intensity *= tracer->traceSegment( point, point - m_direction * 1000, rt::HitUseAlpha ) ? 0.0f : 1.0f;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ return intensity;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
+// --------------------------------------------------- AmbientOcclusionInfluence -------------------------------------------------- //
|
|
|
|
|
|
|
|
- if (rad.Length() > M_EPSILON)
|
|
|
|
|
- source.bakeMesh->ContributeRadiance(lightRay, rad);
|
|
|
|
|
|
|
+AmbientOcclusionInfluence::AmbientOcclusionInfluence( const BakeLight* light )
|
|
|
|
|
+ : LightInfluence( light )
|
|
|
|
|
+{
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void PointBakeLight::SetLight(Atomic::Light* light)
|
|
|
|
|
|
|
+float AmbientOcclusionInfluence::Calculate(LightRay* lightRay, const Vector3& light, float& distance ) const
|
|
|
{
|
|
{
|
|
|
- node_ = light->GetNode();
|
|
|
|
|
|
|
|
|
|
- color_ = light->GetColor();
|
|
|
|
|
- position_ = node_->GetWorldPosition();
|
|
|
|
|
|
|
+ LightRay::SamplePoint& source = lightRay->samplePoint_;
|
|
|
|
|
|
|
|
- range_ = light->GetRange();
|
|
|
|
|
|
|
+ if (source.normal == Vector3::ZERO)
|
|
|
|
|
+ return 1.0f;
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ // clean this mess up
|
|
|
|
|
+ RTCScene scene = source.bakeMesh->GetSceneBaker()->GetEmbreeScene()->GetRTCScene();
|
|
|
|
|
|
|
|
-// Bounce Lights
|
|
|
|
|
|
|
+ const Color& color = light_->GetColor();
|
|
|
|
|
|
|
|
-Mutex BounceBakeLight::sortMutex_;
|
|
|
|
|
|
|
+ Vector3 rad(color.r_, color.g_, color.b_);
|
|
|
|
|
|
|
|
-BounceBakeLight::BounceBakeLight(Context* context, SceneBaker* sceneBaker) : BakeLight(context, sceneBaker)
|
|
|
|
|
-{
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ if (!GlobalGlowSettings.aoEnabled_)
|
|
|
|
|
+ {
|
|
|
|
|
+ return 1.0f;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
-BounceBakeLight::~BounceBakeLight()
|
|
|
|
|
-{
|
|
|
|
|
|
|
+ // TODO: AO using ray packets/streams
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ RTCRay& ray = lightRay->rtcRay_;
|
|
|
|
|
|
|
|
-static Vector3 compareBouncePoint;
|
|
|
|
|
-static inline bool CompareBounceSamples(const BounceSample* lhs, const BounceSample* rhs)
|
|
|
|
|
-{
|
|
|
|
|
- Vector3 v1 = lhs->position_ - compareBouncePoint;
|
|
|
|
|
- Vector3 v2 = rhs->position_ - compareBouncePoint;
|
|
|
|
|
- return v1.LengthSquared() < v2.LengthSquared();
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ unsigned nsamples = GlobalGlowSettings.nsamples_;
|
|
|
|
|
|
|
|
-void BounceBakeLight::Light(LightRay* lightRay)
|
|
|
|
|
-{
|
|
|
|
|
- RTCScene scene = sceneBaker_->GetEmbreeScene()->GetRTCScene();
|
|
|
|
|
- LightRay::SamplePoint& source = lightRay->samplePoint_;
|
|
|
|
|
- RTCRay& ray = lightRay->rtcRay_;
|
|
|
|
|
|
|
+ // this needs to be based on model/scale likely?
|
|
|
|
|
+ float aoDepth = GlobalGlowSettings.aoDepth_;
|
|
|
|
|
|
|
|
- const float maxDist = 5.0f;
|
|
|
|
|
- const float maxDistSq = maxDist * maxDist;
|
|
|
|
|
|
|
+ // smallest percent of ao value to use
|
|
|
|
|
+ float aoMin = GlobalGlowSettings.aoMin_;
|
|
|
|
|
|
|
|
- const BounceSample* b;
|
|
|
|
|
- PODVector<const BounceSample*> samples;
|
|
|
|
|
|
|
+ // brightness control
|
|
|
|
|
+ float multiply = GlobalGlowSettings.aoMultiply_;
|
|
|
|
|
|
|
|
- for (int i = 0; i < bounceSamples_.Size(); i++)
|
|
|
|
|
|
|
+ // Shoot rays through the differential hemisphere.
|
|
|
|
|
+ int nhits = 0;
|
|
|
|
|
+ float avgDepth = 0.0f;
|
|
|
|
|
+ for (unsigned nsamp = 0; nsamp < nsamples; nsamp++)
|
|
|
{
|
|
{
|
|
|
- b = &bounceSamples_[i];
|
|
|
|
|
|
|
+ Vector3 rayDir;
|
|
|
|
|
+ Vector3::GetRandomHemisphereDirection(rayDir, source.normal);
|
|
|
|
|
|
|
|
- // don't light self
|
|
|
|
|
- if (source.bakeMesh == bakeMesh_)
|
|
|
|
|
|
|
+ float dotp = source.normal.x_ * rayDir.x_ +
|
|
|
|
|
+ source.normal.y_ * rayDir.y_ +
|
|
|
|
|
+ source.normal.z_ * rayDir.z_;
|
|
|
|
|
+
|
|
|
|
|
+ if (dotp < 0.1f)
|
|
|
{
|
|
{
|
|
|
- for (int j = 0; j < GLOW_MAX_BOUNCE_SAMPLE_TRIANGLES; j++)
|
|
|
|
|
- {
|
|
|
|
|
- if (b->triIndex_[j] == -1)
|
|
|
|
|
- break;
|
|
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ float variance = 0.0f;//nsamples <= 32 ? 0.0f : aoDepth * ((float) rand() / (float) RAND_MAX) * 0.25f;
|
|
|
|
|
|
|
|
- if (b->triIndex_[j] == source.triangle)
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ float depth = aoDepth + variance;
|
|
|
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ lightRay->SetupRay(source.position, rayDir, .001f, depth);
|
|
|
|
|
|
|
|
- Vector3 dir = b->position_ - source.position;
|
|
|
|
|
|
|
+ rtcOccluded(scene, ray);
|
|
|
|
|
|
|
|
- if (dir.LengthSquared() > maxDistSq)
|
|
|
|
|
|
|
+ if (ray.geomID != RTC_INVALID_GEOMETRY_ID)
|
|
|
{
|
|
{
|
|
|
- continue;
|
|
|
|
|
|
|
+ avgDepth += Min<float>(ray.tfar, aoDepth);
|
|
|
|
|
+ nhits++;
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- dir.Normalize();
|
|
|
|
|
|
|
+ if (nhits)// && (nsamples <= 32 ? true : nhits > 4))
|
|
|
|
|
+ {
|
|
|
|
|
+ avgDepth /= float(nhits);
|
|
|
|
|
+ avgDepth /= aoDepth;
|
|
|
|
|
|
|
|
- if (dir.DotProduct(source.normal) < M_EPSILON)
|
|
|
|
|
- {
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ avgDepth = Clamp<float>(avgDepth, 0.1f, 1.0f) * 100.0f;
|
|
|
|
|
+ avgDepth *= avgDepth;
|
|
|
|
|
+ float ao = avgDepth / 10000.0f;
|
|
|
|
|
|
|
|
- samples.Push(b);
|
|
|
|
|
|
|
+ ao = aoMin + ao/2.0f;
|
|
|
|
|
+ ao *= multiply;
|
|
|
|
|
+ ao = Clamp<float>(ao, aoMin, 1.0f);
|
|
|
|
|
|
|
|
|
|
+ return ao;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (!samples.Size())
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ return 1.0f;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- sortMutex_.Acquire();
|
|
|
|
|
|
|
+// --------------------------------------------------------- LightCutoff ---------------------------------------------------------- //
|
|
|
|
|
|
|
|
- compareBouncePoint = source.position;
|
|
|
|
|
|
|
+LightCutoff::LightCutoff( const BakeLight* light ) : light_( light )
|
|
|
|
|
+{
|
|
|
|
|
|
|
|
- Sort(samples.Begin(), samples.End(), CompareBounceSamples);
|
|
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- sortMutex_.Release();
|
|
|
|
|
|
|
+float LightCutoff::Calculate( const Vector3& point ) const
|
|
|
|
|
+{
|
|
|
|
|
+ return 1.0f;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
+float LightCutoff::GetCutoffForDirection( const Vector3& direction ) const
|
|
|
|
|
+{
|
|
|
|
|
+ return 1.0f;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- int bestIndex = -1;
|
|
|
|
|
- float bestDist = M_INFINITY;
|
|
|
|
|
- for (unsigned i = 0; i < samples.Size(); i++)
|
|
|
|
|
- {
|
|
|
|
|
- b = samples[i];
|
|
|
|
|
|
|
+// ------------------------------------------------------- LightSpotCutoff -------------------------------------------------------- //
|
|
|
|
|
|
|
|
- Vector3 dir = b->position_ - source.position;
|
|
|
|
|
- float dist = dir.Length();
|
|
|
|
|
|
|
+LightSpotCutoff::LightSpotCutoff( const BakeLight* light, const Vector3& direction, float cutoff, float exponent ) :
|
|
|
|
|
+ LightCutoff( light ), direction_( direction ), cutoff_( cutoff ), exponent_( exponent )
|
|
|
|
|
+{
|
|
|
|
|
|
|
|
- if (dist < bestDist)
|
|
|
|
|
- {
|
|
|
|
|
- dir.Normalize();
|
|
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- lightRay->SetupRay(source.position, dir, 0.01f, dist * 1.01f);
|
|
|
|
|
|
|
+float LightSpotCutoff::Calculate( const Vector3& point ) const
|
|
|
|
|
+{
|
|
|
|
|
+ Vector3 dir = point - light_->GetPosition();
|
|
|
|
|
+ dir.Normalize();
|
|
|
|
|
+ return GetCutoffForDirection( dir );
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- rtcIntersect(scene, ray);
|
|
|
|
|
|
|
+float LightSpotCutoff::GetCutoffForDirection( const Vector3& direction ) const
|
|
|
|
|
+{
|
|
|
|
|
+ float value = direction.DotProduct(direction_);
|
|
|
|
|
|
|
|
- if (ray.geomID != bakeMesh_->GetGeomID())
|
|
|
|
|
- {
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if( value <= cutoff_ ) {
|
|
|
|
|
+ return 0.0f;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // backface
|
|
|
|
|
- if ( (ray.Ng[0] * ray.dir[0]) +
|
|
|
|
|
- (ray.Ng[1] * ray.dir[1]) +
|
|
|
|
|
- (ray.Ng[2] * ray.dir[2]) < 0.0f)
|
|
|
|
|
- {
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ value = (1.0f - (1.0f - value) * 1.0f / (1.0f - cutoff_));
|
|
|
|
|
|
|
|
- bool found = false;
|
|
|
|
|
- for (int j = 0; j < GLOW_MAX_BOUNCE_SAMPLE_TRIANGLES; j++)
|
|
|
|
|
- {
|
|
|
|
|
- if (b->triIndex_[j] == -1)
|
|
|
|
|
- break;
|
|
|
|
|
|
|
+ if( fabs( 1.0f - exponent_ ) > 0.01f ) {
|
|
|
|
|
+ value = powf( value, exponent_ );
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (b->triIndex_[j] == ray.primID)
|
|
|
|
|
- {
|
|
|
|
|
- found = true;
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return value;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- if (!found)
|
|
|
|
|
- continue;
|
|
|
|
|
|
|
+// ------------------------------------------------------- LightAttenuation ------------------------------------------------------- //
|
|
|
|
|
|
|
|
- bestIndex = i;
|
|
|
|
|
- bestDist = dist;
|
|
|
|
|
|
|
+LightAttenuation::LightAttenuation( const BakeLight* light ) : light_( light )
|
|
|
|
|
+{
|
|
|
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- }
|
|
|
|
|
|
|
+// ---------------------------------------------------- LinearLightAttenuation ---------------------------------------------------- //
|
|
|
|
|
|
|
|
- if (bestIndex == -1)
|
|
|
|
|
- return;
|
|
|
|
|
|
|
|
|
|
- b = samples[bestIndex];
|
|
|
|
|
|
|
+LinearLightAttenuation::LinearLightAttenuation( const BakeLight* light, float radius, float constant, float linear, float quadratic )
|
|
|
|
|
+ : LightAttenuation( light ), radius_( radius ), constant_( constant ), linear_( linear ), quadratic_( quadratic )
|
|
|
|
|
+{
|
|
|
|
|
|
|
|
- // weighted average of src color and radiance for bounce sample
|
|
|
|
|
- Vector3 rad = b->srcColor_ + (b->radiance_/b->hits_);
|
|
|
|
|
- rad /= 2.0f;
|
|
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- float d = 1.0f - Clamp<float>(bestDist / maxDist, 0.01f, 1.0f);
|
|
|
|
|
|
|
|
|
|
- rad *= d;
|
|
|
|
|
|
|
+float LinearLightAttenuation::Calculate( float distance ) const
|
|
|
|
|
+{
|
|
|
|
|
+ if (fabs(radius_) < 0.01f)
|
|
|
|
|
+ return 0.0f;
|
|
|
|
|
|
|
|
- rad *= 0.15f;
|
|
|
|
|
|
|
+ float r = distance / radius_;
|
|
|
|
|
+ return Max<float>( 1.0f / (1.0f + constant_ + linear_ * r + quadratic_ * r * r), 0.0f );
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- if (rad.x_ < 0.0f || rad.y_ < 0.0f || rad.z_ < 0.0f)
|
|
|
|
|
- {
|
|
|
|
|
- ATOMIC_LOGWARNING("BounceBakeLight::Light - negative rad factor");
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+// -------------------------------------------------------- PhotonEmitter --------------------------------------------------------- //
|
|
|
|
|
|
|
|
- source.bakeMesh->ContributeRadiance(lightRay, rad, GLOW_LIGHTMODE_INDIRECT);
|
|
|
|
|
|
|
+PhotonEmitter::PhotonEmitter( const BakeLight* light ) :
|
|
|
|
|
+ light_( light )
|
|
|
|
|
+{
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void BounceBakeLight::SetBounceSamples(const PODVector<BounceSample>& bounceSamples)
|
|
|
|
|
|
|
+int PhotonEmitter::GetPhotonCount( void ) const
|
|
|
{
|
|
{
|
|
|
- bounceSamples_ = bounceSamples;
|
|
|
|
|
|
|
+ return static_cast<int>( light_->GetIntensity() * 25000 );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void BounceBakeLight::SetBakeMesh(BakeMesh* bakeMesh)
|
|
|
|
|
|
|
+void PhotonEmitter::Emit( SceneBaker* sceneBaker, Vector3& position, Vector3& direction ) const
|
|
|
{
|
|
{
|
|
|
- bakeMesh_ = bakeMesh;
|
|
|
|
|
- node_ = bakeMesh->GetNode();
|
|
|
|
|
|
|
+ position = light_->GetPosition();
|
|
|
|
|
+ Vector3::GetRandomDirection(direction);
|
|
|
|
|
|
|
|
- color_ = Color::MAGENTA;
|
|
|
|
|
- range_ = -1.0f;
|
|
|
|
|
- position_ = Vector3::ZERO;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void BounceBakeLight::SetLight(Atomic::Light* light)
|
|
|
|
|
-{
|
|
|
|
|
- node_ = light->GetNode();
|
|
|
|
|
|
|
+// --------------------------------------------------- DirectinalPhotonEmitter ---------------------------------------------------- //
|
|
|
|
|
|
|
|
- color_ = light->GetColor();
|
|
|
|
|
- position_ = node_->GetWorldPosition();
|
|
|
|
|
|
|
+DirectionalPhotonEmitter::DirectionalPhotonEmitter( const BakeLight* light, const Vector3& direction ) :
|
|
|
|
|
+ PhotonEmitter( light ), direction_( direction )
|
|
|
|
|
+{
|
|
|
|
|
+ plane_.Define(direction.Normalized(), light->GetPosition()); // = Plane::calculate( direction, light->position() );
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- range_ = light->GetRange();
|
|
|
|
|
|
|
+/*
|
|
|
|
|
+void DirectionalPhotonEmitter::Emit( const Scene* scene, Vector3& position, Vector3& direction ) const
|
|
|
|
|
+{
|
|
|
|
|
+ const Bounds& bounds = scene->bounds();
|
|
|
|
|
|
|
|
|
|
+ position = m_plane * bounds.randomPointInside() - m_direction * 5;
|
|
|
|
|
+ direction = m_direction;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+*/
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|