BakeLight.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. // Copyright (c) 2014-2017, THUNDERBEAST GAMES LLC All rights reserved
  2. // Portions Copyright (c) 2015 Dmitry Sovetov
  3. // Copyright 2009-2017 Intel Corporation
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include <Atomic/IO/Log.h>
  24. #include <Atomic/Graphics/Zone.h>
  25. #include <AtomicGlow/Common/GlowSettings.h>
  26. #include "EmbreeScene.h"
  27. #include "LightRay.h"
  28. #include "BakeLight.h"
  29. #include "BakeMesh.h"
  30. #include "SceneBaker.h"
  31. namespace AtomicGlow
  32. {
  33. BakeLight::BakeLight(Context* context, SceneBaker* sceneBaker) : BakeNode(context, sceneBaker),
  34. color_(Color::WHITE),
  35. intensity_( 0.0f ),
  36. castsShadow_( false )
  37. {
  38. }
  39. BakeLight::~BakeLight()
  40. {
  41. }
  42. LightCutoff* BakeLight::GetCutoffModel( void ) const
  43. {
  44. return cutoffModel_;
  45. }
  46. void BakeLight::SetCutoffModel( LightCutoff* value )
  47. {
  48. cutoffModel_ = value;
  49. }
  50. LightInfluence* BakeLight::GetInfluenceModel( void ) const
  51. {
  52. return influenceModel_;
  53. }
  54. void BakeLight::SetInfluenceModel( LightInfluence* value )
  55. {
  56. influenceModel_ = value;
  57. }
  58. /*
  59. LightVertexGenerator* BakeLight::vertexGenerator( void ) const
  60. {
  61. return vertexGenerator_;
  62. }
  63. // ** BakeLight::setVertexGenerator
  64. void BakeLight::setVertexGenerator( LightVertexGenerator* value )
  65. {
  66. delete m_vertexGenerator;
  67. m_vertexGenerator = value;
  68. }
  69. */
  70. LightAttenuation* BakeLight::GetAttenuationModel( void ) const
  71. {
  72. return attenuationModel_;
  73. }
  74. void BakeLight::SetAttenuationModel( LightAttenuation* value )
  75. {
  76. attenuationModel_ = value;
  77. }
  78. PhotonEmitter* BakeLight::GetPhotonEmitter( void ) const
  79. {
  80. return photonEmitter_;
  81. }
  82. void BakeLight::SetPhotonEmitter( PhotonEmitter* value )
  83. {
  84. photonEmitter_ = value;
  85. }
  86. const Vector3& BakeLight::GetPosition( void ) const
  87. {
  88. return position_;
  89. }
  90. void BakeLight::SetPosition( const Vector3& value )
  91. {
  92. position_ = value;
  93. }
  94. const Color& BakeLight::GetColor( void ) const
  95. {
  96. return color_;
  97. }
  98. void BakeLight::SetColor( const Color& value )
  99. {
  100. color_ = value;
  101. }
  102. float BakeLight::GetIntensity( void ) const
  103. {
  104. return intensity_;
  105. }
  106. void BakeLight::SetIntensity( float value )
  107. {
  108. intensity_ = value;
  109. }
  110. bool BakeLight::GetCastsShadow( void ) const
  111. {
  112. return castsShadow_;
  113. }
  114. void BakeLight::SetCastsShadow( bool value )
  115. {
  116. castsShadow_ = value;
  117. }
  118. BakeLight* BakeLight::CreateZoneLight( SceneBaker* baker, const Color& color)
  119. {
  120. BakeLight* light = new BakeLight(baker->GetContext(), baker);
  121. light->SetCastsShadow( false );
  122. light->SetColor( color );
  123. light->SetIntensity(1.0f);
  124. if (GlobalGlowSettings.aoEnabled_)
  125. {
  126. light->SetInfluenceModel( new AmbientOcclusionInfluence( light ) );
  127. }
  128. return light;
  129. }
  130. BakeLight* BakeLight::CreatePointLight( SceneBaker* baker, const Vector3& position, float radius, const Color& color, float intensity, bool castsShadow )
  131. {
  132. BakeLight* light = new BakeLight(baker->GetContext(), baker);
  133. light->SetInfluenceModel( new LightInfluence( light ) );
  134. light->SetAttenuationModel( new LinearLightAttenuation( light, radius ) );
  135. light->SetCutoffModel( new LightCutoff( light ) );
  136. light->SetPhotonEmitter( new PhotonEmitter( light ) );
  137. light->SetCastsShadow( castsShadow );
  138. light->SetPosition( position );
  139. light->SetColor( color );
  140. light->SetIntensity( intensity );
  141. return light;
  142. }
  143. BakeLight* BakeLight::CreateSpotLight( SceneBaker* baker, const Vector3& position, const Vector3& direction, float cutoff, float radius, const Color& color, float intensity, bool castsShadow )
  144. {
  145. BakeLight* light = new BakeLight(baker->GetContext(), baker);
  146. light->SetInfluenceModel( new LightInfluence( light ) );
  147. light->SetAttenuationModel( new LinearLightAttenuation( light, radius ) );
  148. light->SetCutoffModel( new LightSpotCutoff( light, direction, cutoff, 1.0f ) );
  149. light->SetPhotonEmitter( new PhotonEmitter( light ) );
  150. light->SetCastsShadow( castsShadow );
  151. light->SetPosition( position );
  152. light->SetColor( color );
  153. light->SetIntensity( intensity );
  154. return light;
  155. }
  156. BakeLight* BakeLight::CreateDirectionalLight( SceneBaker* baker, const Vector3& direction, const Color& color, float intensity, bool castsShadow )
  157. {
  158. BakeLight* light = new BakeLight(baker->GetContext(), baker);
  159. light->SetInfluenceModel( new DirectionalLightInfluence( light, direction ) );
  160. light->SetPhotonEmitter( new DirectionalPhotonEmitter( light, direction ) );
  161. light->SetCastsShadow( castsShadow );
  162. light->SetColor( color );
  163. light->SetIntensity( intensity );
  164. return light;
  165. }
  166. /*
  167. BakeLight* BakeLight::CreateAreaLight( SceneBaker* baker, const Mesh* mesh, const Vector3& position, const Rgb& color, float intensity, bool castsShadow )
  168. {
  169. Light* light = new Light;
  170. light->SetInfluence( new LightInfluence( light ) );
  171. light->SetAttenuation( new LinearLightAttenuation( light, mesh->bounds().volume() ) );
  172. light->SetPhotonEmitter( new PhotonEmitter( light ) );
  173. light->SetCutoff( new LightCutoff( light ) );
  174. //light->SetVertexGenerator( new FaceLightVertexGenerator( mesh, true, 3 ) );
  175. //light->SetVertexGenerator( new FaceLightVertexGenerator( mesh, false, 0 ) );
  176. light->SetVertexGenerator( new FaceLightVertexGenerator( mesh, true, 0 ) );
  177. // light->SetVertexGenerator( new LightVertexGenerator( mesh ) );
  178. light->SetCastsShadow( castsShadow );
  179. light->SetPosition( position );
  180. light->SetColor( color );
  181. light->SetIntensity( intensity );
  182. light->GetVertexGenerator()->Generate();
  183. return light;
  184. }
  185. */
  186. // ------------------------------------------------------- LightInfluence --------------------------------------------------------- //
  187. LightInfluence::LightInfluence( const BakeLight* light ) :
  188. light_( light )
  189. {
  190. }
  191. float LightInfluence::Calculate(LightRay* lightRay, const Vector3& light, float& distance ) const
  192. {
  193. LightRay::SamplePoint& source = lightRay->samplePoint_;
  194. Vector3 direction = light - source.position;
  195. distance = direction.Length();
  196. direction.Normalize();
  197. // ** Calculate Lambert's cosine law intensity
  198. float intensity = GetLambert( direction, source.normal );
  199. if( intensity <= 0.001f ) {
  200. return 0.0f;
  201. }
  202. // ** Cast shadow to point
  203. if( light_->GetCastsShadow() )
  204. {
  205. LightRay::SamplePoint& source = lightRay->samplePoint_;
  206. // clean this mess up
  207. RTCScene scene = source.bakeMesh->GetSceneBaker()->GetEmbreeScene()->GetRTCScene();
  208. lightRay->SetupRay(source.position, direction, .001f, distance);
  209. rtcOccluded(scene, lightRay->rtcRay_);
  210. // obstructed? TODO: glass, etc
  211. if (lightRay->rtcRay_.geomID != RTC_INVALID_GEOMETRY_ID)
  212. {
  213. return 0.0f;
  214. }
  215. }
  216. return intensity;
  217. }
  218. float LightInfluence::GetLambert( const Vector3& direction, const Vector3& normal )
  219. {
  220. float dp = direction.DotProduct(normal);
  221. return dp < 0.0f ? 0.0f : dp;
  222. }
  223. // --------------------------------------------------- DirectionalLightInfluence -------------------------------------------------- //
  224. DirectionalLightInfluence::DirectionalLightInfluence( const BakeLight* light, const Vector3& direction )
  225. : LightInfluence( light ), direction_( direction )
  226. {
  227. }
  228. float DirectionalLightInfluence::Calculate(LightRay* lightRay, const Vector3& light, float& distance ) const
  229. {
  230. LightRay::SamplePoint& source = lightRay->samplePoint_;
  231. float intensity = GetLambert( -direction_, source.normal );
  232. if( intensity <= 0.001f )
  233. {
  234. return 0.0f;
  235. }
  236. // ** Cast shadow to point
  237. if( light_->GetCastsShadow() )
  238. {
  239. // FIXME: tracer
  240. // intensity *= tracer->traceSegment( point, point - m_direction * 1000, rt::HitUseAlpha ) ? 0.0f : 1.0f;
  241. }
  242. return intensity;
  243. }
  244. // --------------------------------------------------- AmbientOcclusionInfluence -------------------------------------------------- //
  245. AmbientOcclusionInfluence::AmbientOcclusionInfluence( const BakeLight* light )
  246. : LightInfluence( light )
  247. {
  248. }
  249. float AmbientOcclusionInfluence::Calculate(LightRay* lightRay, const Vector3& light, float& distance ) const
  250. {
  251. LightRay::SamplePoint& source = lightRay->samplePoint_;
  252. if (source.normal == Vector3::ZERO)
  253. return 1.0f;
  254. // clean this mess up
  255. RTCScene scene = source.bakeMesh->GetSceneBaker()->GetEmbreeScene()->GetRTCScene();
  256. const Color& color = light_->GetColor();
  257. Vector3 rad(color.r_, color.g_, color.b_);
  258. if (!GlobalGlowSettings.aoEnabled_)
  259. {
  260. return 1.0f;
  261. }
  262. // TODO: AO using ray packets/streams
  263. RTCRay& ray = lightRay->rtcRay_;
  264. unsigned nsamples = GlobalGlowSettings.nsamples_;
  265. // this needs to be based on model/scale likely?
  266. float aoDepth = GlobalGlowSettings.aoDepth_;
  267. // smallest percent of ao value to use
  268. float aoMin = GlobalGlowSettings.aoMin_;
  269. // brightness control
  270. float multiply = GlobalGlowSettings.aoMultiply_;
  271. // Shoot rays through the differential hemisphere.
  272. int nhits = 0;
  273. float avgDepth = 0.0f;
  274. for (unsigned nsamp = 0; nsamp < nsamples; nsamp++)
  275. {
  276. Vector3 rayDir;
  277. Vector3::GetRandomHemisphereDirection(rayDir, source.normal);
  278. float dotp = source.normal.x_ * rayDir.x_ +
  279. source.normal.y_ * rayDir.y_ +
  280. source.normal.z_ * rayDir.z_;
  281. if (dotp < 0.1f)
  282. {
  283. continue;
  284. }
  285. float variance = 0.0f;//nsamples <= 32 ? 0.0f : aoDepth * ((float) rand() / (float) RAND_MAX) * 0.25f;
  286. float depth = aoDepth + variance;
  287. lightRay->SetupRay(source.position, rayDir, .001f, depth);
  288. rtcOccluded(scene, ray);
  289. if (ray.geomID != RTC_INVALID_GEOMETRY_ID)
  290. {
  291. avgDepth += Min<float>(ray.tfar, aoDepth);
  292. nhits++;
  293. }
  294. }
  295. if (nhits)// && (nsamples <= 32 ? true : nhits > 4))
  296. {
  297. avgDepth /= float(nhits);
  298. avgDepth /= aoDepth;
  299. avgDepth = Clamp<float>(avgDepth, 0.1f, 1.0f) * 100.0f;
  300. avgDepth *= avgDepth;
  301. float ao = avgDepth / 10000.0f;
  302. ao = aoMin + ao/2.0f;
  303. ao *= multiply;
  304. ao = Clamp<float>(ao, aoMin, 1.0f);
  305. return ao;
  306. }
  307. return 1.0f;
  308. }
  309. // --------------------------------------------------------- LightCutoff ---------------------------------------------------------- //
  310. LightCutoff::LightCutoff( const BakeLight* light ) : light_( light )
  311. {
  312. }
  313. float LightCutoff::Calculate( const Vector3& point ) const
  314. {
  315. return 1.0f;
  316. }
  317. float LightCutoff::GetCutoffForDirection( const Vector3& direction ) const
  318. {
  319. return 1.0f;
  320. }
  321. // ------------------------------------------------------- LightSpotCutoff -------------------------------------------------------- //
  322. LightSpotCutoff::LightSpotCutoff( const BakeLight* light, const Vector3& direction, float cutoff, float exponent ) :
  323. LightCutoff( light ), direction_( direction ), cutoff_( cutoff ), exponent_( exponent )
  324. {
  325. }
  326. float LightSpotCutoff::Calculate( const Vector3& point ) const
  327. {
  328. Vector3 dir = point - light_->GetPosition();
  329. dir.Normalize();
  330. return GetCutoffForDirection( dir );
  331. }
  332. float LightSpotCutoff::GetCutoffForDirection( const Vector3& direction ) const
  333. {
  334. float value = direction.DotProduct(direction_);
  335. if( value <= cutoff_ ) {
  336. return 0.0f;
  337. }
  338. value = (1.0f - (1.0f - value) * 1.0f / (1.0f - cutoff_));
  339. if( fabs( 1.0f - exponent_ ) > 0.01f ) {
  340. value = powf( value, exponent_ );
  341. }
  342. return value;
  343. }
  344. // ------------------------------------------------------- LightAttenuation ------------------------------------------------------- //
  345. LightAttenuation::LightAttenuation( const BakeLight* light ) : light_( light )
  346. {
  347. }
  348. // ---------------------------------------------------- LinearLightAttenuation ---------------------------------------------------- //
  349. LinearLightAttenuation::LinearLightAttenuation( const BakeLight* light, float radius, float constant, float linear, float quadratic )
  350. : LightAttenuation( light ), radius_( radius ), constant_( constant ), linear_( linear ), quadratic_( quadratic )
  351. {
  352. }
  353. float LinearLightAttenuation::Calculate( float distance ) const
  354. {
  355. if (fabs(radius_) < 0.01f)
  356. return 0.0f;
  357. float r = distance / radius_;
  358. return Max<float>( 1.0f / (1.0f + constant_ + linear_ * r + quadratic_ * r * r), 0.0f );
  359. }
  360. // -------------------------------------------------------- PhotonEmitter --------------------------------------------------------- //
  361. PhotonEmitter::PhotonEmitter( const BakeLight* light ) :
  362. light_( light )
  363. {
  364. }
  365. int PhotonEmitter::GetPhotonCount( void ) const
  366. {
  367. return static_cast<int>( light_->GetIntensity() * 25000 );
  368. }
  369. void PhotonEmitter::Emit( SceneBaker* sceneBaker, Vector3& position, Vector3& direction ) const
  370. {
  371. position = light_->GetPosition();
  372. Vector3::GetRandomDirection(direction);
  373. }
  374. // --------------------------------------------------- DirectinalPhotonEmitter ---------------------------------------------------- //
  375. DirectionalPhotonEmitter::DirectionalPhotonEmitter( const BakeLight* light, const Vector3& direction ) :
  376. PhotonEmitter( light ), direction_( direction )
  377. {
  378. plane_.Define(direction.Normalized(), light->GetPosition()); // = Plane::calculate( direction, light->position() );
  379. }
  380. /*
  381. void DirectionalPhotonEmitter::Emit( const Scene* scene, Vector3& position, Vector3& direction ) const
  382. {
  383. const Bounds& bounds = scene->bounds();
  384. position = m_plane * bounds.randomPointInside() - m_direction * 5;
  385. direction = m_direction;
  386. }
  387. */
  388. }