BakeMesh.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright (c) 2014-2017, THUNDERBEAST GAMES LLC All rights reserved
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. //
  21. #pragma once
  22. #include "Embree.h"
  23. #include <Atomic/Core/Thread.h>
  24. #include <Atomic/Core/Mutex.h>
  25. #include <Atomic/Graphics/StaticModel.h>
  26. #include "BakeMaterial.h"
  27. #include "BakeModel.h"
  28. #include "BakeNode.h"
  29. #include "RadianceMap.h"
  30. namespace AtomicGlow
  31. {
  32. using namespace Atomic;
  33. class LightRay;
  34. class SceneBaker;
  35. class BakeLight;
  36. class BakeMesh;
  37. class BounceBakeLight;
  38. class BakeMesh : public BakeNode
  39. {
  40. friend class BounceBakeLight;
  41. ATOMIC_OBJECT(BakeMesh, BakeNode)
  42. public:
  43. struct MMVertex
  44. {
  45. Vector3 position_;
  46. Vector3 normal_;
  47. Vector2 uv0_;
  48. Vector2 uv1_;
  49. };
  50. struct MMTriangle
  51. {
  52. // index into bakeMaterials_ vector
  53. unsigned materialIndex_;
  54. Vector3 normal_;
  55. // index into vertices_ array
  56. unsigned indices_[3];
  57. };
  58. struct MMSample
  59. {
  60. BakeMesh* bakeMesh;
  61. unsigned triangle;
  62. // coords in radiance_
  63. unsigned radianceX;
  64. unsigned radianceY;
  65. Vector3 position;
  66. Vector3 normal;
  67. Vector2 uv0;
  68. Vector2 uv1;
  69. };
  70. BakeMesh(Context* context, SceneBaker* sceneBaker);
  71. virtual ~BakeMesh();
  72. bool SetStaticModel(StaticModel* staticModel);
  73. /// Get world space bounding box
  74. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  75. const SharedArrayPtr<MMVertex>& GetVertices(unsigned& numVertices) const { numVertices = numVertices_; return vertices_; }
  76. const SharedArrayPtr<MMTriangle>& GetTriangles(unsigned numTriangles) const { numTriangles = numTriangles_; return triangles_; }
  77. void Preprocess();
  78. void Light(GlowLightMode mode);
  79. bool GetLightmap() const { return staticModel_.Null() ? false : staticModel_->GetLightmap(); }
  80. void ContributeRadiance(const LightRay* lightRay, const Vector3 &radiance, GlowLightMode lightMode = GLOW_LIGHTMODE_DIRECT);
  81. int GetRadianceWidth() const { return radianceWidth_; }
  82. int GetRadianceHeight() const { return radianceHeight_; }
  83. BounceBakeLight* GenerateBounceBakeLight();
  84. void GenerateRadianceMap();
  85. inline bool GetRadiance(int x, int y, Vector3& rad, int& triIndex) const
  86. {
  87. rad = Vector3::ZERO;
  88. triIndex = -1;
  89. if (x < 0 || x >= radianceWidth_)
  90. return false;
  91. if (y < 0 || y >= radianceHeight_)
  92. return false;
  93. rad = radiance_[y * radianceWidth_ + x];
  94. triIndex = radianceTriIndices_[y * radianceWidth_ + x];
  95. if (triIndex < 0 || rad.x_ < 0.0f)
  96. return false;
  97. return true;
  98. }
  99. unsigned GetGeomID() const { return embreeGeomID_; }
  100. inline const MMTriangle* GetTriangle(unsigned index) const
  101. {
  102. if (index >= numTriangles_)
  103. return 0;
  104. return &triangles_[index];
  105. }
  106. SharedPtr<RadianceMap> GetRadianceMap() const { return radianceMap_; }
  107. StaticModel* GetStaticModel() const { return staticModel_; }
  108. void Pack(unsigned lightmapIdx, Vector4 tilingOffset);
  109. bool GetUV0Color(int triIndex, const Vector3 &barycentric, Color& colorOut) const;
  110. const Color& GetAmbientColor() const { return ambientColor_; }
  111. private:
  112. struct ShaderData
  113. {
  114. GlowLightMode lightMode_;
  115. BakeMesh* bakeMesh_;
  116. unsigned triangleIdx_;
  117. };
  118. static void LightTrianglesWork(const WorkItem* item, unsigned threadIndex);
  119. static bool FillLexelsCallback(void* param, int x, int y, const Vector3& barycentric,const Vector3& dx, const Vector3& dy, float coverage);
  120. static bool CommonFilter(const BakeMesh* bakeMesh, RTCRay& ray);
  121. static void OcclusionFilter(void* ptr, RTCRay& ray);
  122. static void IntersectFilter(void* ptr, RTCRay& ray);
  123. bool LightPixel(ShaderData* shaderData, int x, int y, const Vector3& barycentric,const Vector3& dx, const Vector3& dy, float coverage);
  124. void ResetBounce();
  125. // mesh geometry, in world space
  126. BoundingBox boundingBox_;
  127. SharedArrayPtr<MMVertex> vertices_;
  128. unsigned numVertices_;
  129. SharedArrayPtr<MMTriangle> triangles_;
  130. unsigned numTriangles_;
  131. SharedPtr<StaticModel> staticModel_;
  132. // resources
  133. PODVector<BakeMaterial*> bakeMaterials_;
  134. SharedPtr<BakeModel> bakeModel_;
  135. // lights affecting this mesh
  136. PODVector<BakeLight*> bakeLights_;
  137. SharedPtr<RadianceMap> radianceMap_;
  138. // can be accessed from multiple threads
  139. SharedArrayPtr<Vector3> radiance_;
  140. SharedArrayPtr<bool> radiancePassAccept_;
  141. // radiance -> triangle contributor
  142. SharedArrayPtr<int> radianceTriIndices_;
  143. SharedArrayPtr<BounceSample> bounceSamples_;
  144. unsigned radianceHeight_;
  145. unsigned radianceWidth_;
  146. unsigned bounceWidth_;
  147. unsigned bounceHeight_;
  148. unsigned bounceGranularity_;
  149. unsigned embreeGeomID_;
  150. // multithreading
  151. Mutex meshMutex_;
  152. unsigned numWorkItems_;
  153. Color ambientColor_;
  154. };
  155. }