BakeMesh.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include "Photons.h"
  31. namespace AtomicGlow
  32. {
  33. using namespace Atomic;
  34. class LightRay;
  35. class SceneBaker;
  36. class BakeLight;
  37. class BakeMesh;
  38. class BounceBakeLight;
  39. class BakeMesh : public BakeNode
  40. {
  41. friend class BounceBakeLight;
  42. ATOMIC_OBJECT(BakeMesh, BakeNode)
  43. public:
  44. struct MMVertex
  45. {
  46. Vector3 position_;
  47. Vector3 normal_;
  48. Vector2 uv0_;
  49. Vector2 uv1_;
  50. };
  51. struct MMTriangle
  52. {
  53. // index into bakeMaterials_ vector
  54. unsigned materialIndex_;
  55. Vector3 normal_;
  56. // index into vertices_ array
  57. unsigned indices_[3];
  58. };
  59. struct MMSample
  60. {
  61. BakeMesh* bakeMesh;
  62. unsigned triangle;
  63. // coords in radiance_
  64. unsigned radianceX;
  65. unsigned radianceY;
  66. Vector3 position;
  67. Vector3 normal;
  68. Vector2 uv0;
  69. Vector2 uv1;
  70. };
  71. BakeMesh(Context* context, SceneBaker* sceneBaker);
  72. virtual ~BakeMesh();
  73. bool SetStaticModel(StaticModel* staticModel);
  74. /// Get world space bounding box
  75. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  76. const SharedArrayPtr<MMVertex>& GetVertices(unsigned& numVertices) const { numVertices = numVertices_; return vertices_; }
  77. const SharedArrayPtr<MMTriangle>& GetTriangles(unsigned& numTriangles) const { numTriangles = numTriangles_; return triangles_; }
  78. void Preprocess();
  79. void Light(GlowLightMode mode);
  80. bool GetLightmap() const { return staticModel_.Null() ? false : staticModel_->GetLightmap(); }
  81. void ContributeRadiance(const LightRay* lightRay, const Vector3 &radiance, GlowLightMode lightMode = GLOW_LIGHTMODE_DIRECT);
  82. int GetRadianceWidth() const { return radianceWidth_; }
  83. int GetRadianceHeight() const { return radianceHeight_; }
  84. BounceBakeLight* GenerateBounceBakeLight();
  85. void GenerateRadianceMap();
  86. inline bool GetRadiance(int x, int y, Vector3& rad, int& triIndex) const
  87. {
  88. rad = Vector3::ZERO;
  89. triIndex = -1;
  90. if (x < 0 || x >= radianceWidth_)
  91. return false;
  92. if (y < 0 || y >= radianceHeight_)
  93. return false;
  94. rad = radiance_[y * radianceWidth_ + x];
  95. triIndex = radianceTriIndices_[y * radianceWidth_ + x];
  96. if (triIndex < 0 || rad.x_ < 0.0f)
  97. return false;
  98. return true;
  99. }
  100. unsigned GetGeomID() const { return embreeGeomID_; }
  101. inline const MMTriangle* GetTriangle(unsigned index) const
  102. {
  103. if (index >= numTriangles_)
  104. return 0;
  105. return &triangles_[index];
  106. }
  107. SharedPtr<RadianceMap> GetRadianceMap() const { return radianceMap_; }
  108. StaticModel* GetStaticModel() const { return staticModel_; }
  109. void Pack(unsigned lightmapIdx, Vector4 tilingOffset);
  110. bool GetUV0Color(int triIndex, const Vector3 &barycentric, Color& colorOut) const;
  111. void GetST(int triIndex, int channel, const Vector3& barycentric, Vector2& st) const;
  112. const Color& GetAmbientColor() const { return ambientColor_; }
  113. PhotonMap* GetPhotonMap() const { return photonMap_; }
  114. void SetPhotonMap(PhotonMap* photonMap) { photonMap_ = photonMap; }
  115. private:
  116. struct ShaderData
  117. {
  118. GlowLightMode lightMode_;
  119. BakeMesh* bakeMesh_;
  120. unsigned triangleIdx_;
  121. };
  122. static void LightTrianglesWork(const WorkItem* item, unsigned threadIndex);
  123. static bool FillLexelsCallback(void* param, int x, int y, const Vector3& barycentric,const Vector3& dx, const Vector3& dy, float coverage);
  124. static bool CommonFilter(const BakeMesh* bakeMesh, RTCRay& ray);
  125. static void OcclusionFilter(void* ptr, RTCRay& ray);
  126. static void IntersectFilter(void* ptr, RTCRay& ray);
  127. bool LightPixel(ShaderData* shaderData, int x, int y, const Vector3& barycentric,const Vector3& dx, const Vector3& dy, float coverage);
  128. // mesh geometry, in world space
  129. BoundingBox boundingBox_;
  130. SharedArrayPtr<MMVertex> vertices_;
  131. unsigned numVertices_;
  132. SharedArrayPtr<MMTriangle> triangles_;
  133. unsigned numTriangles_;
  134. SharedPtr<StaticModel> staticModel_;
  135. // resources
  136. PODVector<BakeMaterial*> bakeMaterials_;
  137. SharedPtr<BakeModel> bakeModel_;
  138. // lights affecting this mesh
  139. PODVector<BakeLight*> bakeLights_;
  140. SharedPtr<RadianceMap> radianceMap_;
  141. // can be accessed from multiple threads
  142. SharedArrayPtr<Vector3> radiance_;
  143. SharedArrayPtr<bool> radiancePassAccept_;
  144. // radiance -> triangle contributor
  145. SharedArrayPtr<int> radianceTriIndices_;
  146. SharedPtr<PhotonMap> photonMap_;
  147. unsigned radianceHeight_;
  148. unsigned radianceWidth_;
  149. unsigned embreeGeomID_;
  150. // multithreading
  151. Mutex meshMutex_;
  152. unsigned numWorkItems_;
  153. Color ambientColor_;
  154. };
  155. }