ModelAOBake.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. #include <embree2/rtcore.h>
  2. #include <embree2/rtcore_ray.h>
  3. #include <xmmintrin.h>
  4. #include <pmmintrin.h>
  5. #include <cmath>
  6. #include <cfloat>
  7. #include <Atomic/IO/Log.h>
  8. #include <Atomic/Resource/Image.h>
  9. #include <Atomic/Resource/ResourceCache.h>
  10. #include "ModelAOBake.h"
  11. #ifdef __llvm__
  12. double omp_get_wtime() { return 1; }
  13. int omp_get_max_threads() { return 1; }
  14. int omp_get_thread_num() { return 1; }
  15. #else
  16. #include <omp.h>
  17. #endif
  18. namespace AtomicGlow
  19. {
  20. static inline void GetBarycentricCoordinates(const Vector2& p1, const Vector2& p2, const Vector2& p3, const Vector2& p, Vector3& out)
  21. {
  22. out.x_ = out.y_ = 0.0f;
  23. float denom = (-p1.x_ * p3.y_ - p2.x_ * p1.y_ + p2.x_ * p3.y_ + p1.y_ * p3.x_ + p2.y_ *p1.x_ - p2.y_ * p3.x_ );
  24. if (fabs(denom) > M_EPSILON)
  25. {
  26. out.x_ = (p2.x_ * p3.y_ - p2.y_ * p3.x_ - p.x_ * p3.y_ + p3.x_ * p.y_ - p2.x_ * p.y_ + p2.y_ * p.x_) / denom;
  27. out.y_ = -(-p1.x_ * p.y_ + p1.x_ * p3.y_ + p1.y_ * p.x_ - p.x_ * p3.y_ + p3.x_ * p.y_ - p1.y_ * p3.x_) / denom;
  28. }
  29. out.z_ = 1 - out.x_ - out.y_;
  30. }
  31. static inline int GetPixelCoordinate(float textureCoord, unsigned extent)
  32. {
  33. if (!extent)
  34. return 0;
  35. int pixel = (int)(textureCoord * extent);
  36. if (pixel < 0)
  37. pixel = 0;
  38. if (pixel >= (int) extent)
  39. pixel = extent - 1;
  40. return pixel;
  41. }
  42. // http://www.altdevblogaday.com/2012/05/03/generating-uniformly-distributed-points-on-sphere/
  43. static inline void GetRandomDirection(Vector3& result)
  44. {
  45. float z = 2.0f * rand() / RAND_MAX - 1.0f;
  46. float t = 2.0f * rand() / RAND_MAX * 3.14f;
  47. float r = sqrt(1.0f - z * z);
  48. result.x_ = r * (float) cos(t);
  49. result.y_ = r * (float) sin(t);
  50. result.z_ = z;
  51. }
  52. ModelAOBake::ModelAOBake(Context* context) : Object(context),
  53. numIndices_(0)
  54. {
  55. }
  56. ModelAOBake::~ModelAOBake()
  57. {
  58. }
  59. /*
  60. unsigned ModelAOBake::GetImageSize(float pixelsPerUnit, bool powerOfTwo)
  61. {
  62. if (!lmVertices_.Size())
  63. return 0;
  64. Vector2 tMin(999999, 999999);
  65. Vector2 tMax(-999999, -999999);
  66. Vector3 pMin(999999, 999999, 999999);
  67. Vector3 pMax(-999999, -999999, -999999);
  68. for (unsigned i = 0; i < lmVertices_.Size(); i++)
  69. {
  70. const LMVertex& v = lmVertices_[i];
  71. if (tMin.x_ > v.uv1_.x_)
  72. tMin.x_ = v.uv1_.x_;
  73. if (tMin.y_ > v.uv1_.y_)
  74. tMin.y_ = v.uv1_.y_;
  75. if (tMax.x_ < v.uv1_.x_)
  76. tMax.x_ = v.uv1_.x_;
  77. if (tMax.y_ < v.uv1_.y_)
  78. tMax.y_ = v.uv1_.y_;
  79. }
  80. ATOMIC_LOGINFOF("%f %f : %f %f", tMin.x_, tMin.y_, tMax.x_, tMax.y_);
  81. return 0;
  82. }
  83. */
  84. void ModelAOBake::TraceAORays(unsigned nsamples, float aoDepth, float multiply)
  85. {
  86. // Intel says to do this, so we're doing it.
  87. _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
  88. _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
  89. // Create the embree device and scene.
  90. RTCDevice device = rtcNewDevice(NULL);
  91. assert(device && "Unable to create embree device.");
  92. RTCScene scene = rtcDeviceNewScene(device, RTC_SCENE_STATIC, RTC_INTERSECT1);
  93. assert(scene);
  94. // Create the embree mesh
  95. uint32_t gid = rtcNewTriangleMesh(scene, RTC_GEOMETRY_STATIC, numIndices_ / 3, lmVertices_.Size());
  96. // Populate vertices
  97. float* vertices = (float*) rtcMapBuffer(scene, gid, RTC_VERTEX_BUFFER);
  98. for (unsigned i = 0; i < lmVertices_.Size(); i++)
  99. {
  100. const LMVertex& v = lmVertices_[i];
  101. *vertices++ = v.position_.x_;
  102. *vertices++ = v.position_.y_;
  103. *vertices++ = v.position_.z_;
  104. vertices++;
  105. }
  106. rtcUnmapBuffer(scene, gid, RTC_VERTEX_BUFFER);
  107. uint32_t* triangles = (uint32_t*) rtcMapBuffer(scene, gid, RTC_INDEX_BUFFER);
  108. for (size_t i = 0; i < numIndices_; i++)
  109. {
  110. *triangles++ = indices_[i];
  111. }
  112. rtcUnmapBuffer(scene, gid, RTC_INDEX_BUFFER);
  113. rtcCommit(scene);
  114. // Iterate over each pixel in the light map, row by row.
  115. ATOMIC_LOGINFOF("Rendering ambient occlusion (%d threads)...", omp_get_max_threads());
  116. double begintime = omp_get_wtime();
  117. SharedArrayPtr<unsigned char> results(new unsigned char[lightmap_->GetWidth() * lightmap_->GetHeight()]);
  118. memset(&results[0], 0, sizeof(unsigned char) * lightmap_->GetWidth() * lightmap_->GetHeight());
  119. const unsigned npixels = lightmap_->GetWidth() * lightmap_->GetHeight();
  120. const float E = 0.001f;
  121. #pragma omp parallel
  122. {
  123. srand(omp_get_thread_num());
  124. RTCRay ray;
  125. ray.primID = RTC_INVALID_GEOMETRY_ID;
  126. ray.instID = RTC_INVALID_GEOMETRY_ID;
  127. ray.mask = 0xFFFFFFFF;
  128. ray.time = 0.f;
  129. #pragma omp for
  130. for (unsigned i = 0; i < npixels; i++)
  131. {
  132. LMLexel& lexel = lmLexels_[i];
  133. if (lexel.normal_ == Vector3::ZERO)
  134. continue;
  135. ray.org[0] = lexel.position_.x_;
  136. ray.org[1] = lexel.position_.y_;
  137. ray.org[2] = lexel.position_.z_;
  138. int nhits = 0;
  139. // Shoot rays through the differential hemisphere.
  140. for (unsigned nsamp = 0; nsamp < nsamples; nsamp++)
  141. {
  142. Vector3 rayDir;
  143. GetRandomDirection(rayDir);
  144. float dotp = lexel.normal_.x_ * rayDir.x_ +
  145. lexel.normal_.y_ * rayDir.y_ +
  146. lexel.normal_.z_ * rayDir.z_;
  147. if (dotp < 0)
  148. {
  149. rayDir = -rayDir;
  150. }
  151. ray.dir[0] = rayDir.x_;
  152. ray.dir[1] = rayDir.y_;
  153. ray.dir[2] = rayDir.z_;
  154. ray.tnear = E;
  155. float variance = 0.0f;//(aoDepth * (float) rand() / (float) RAND_MAX);
  156. ray.tfar = aoDepth + variance;
  157. ray.geomID = RTC_INVALID_GEOMETRY_ID;
  158. rtcOccluded(scene, ray);
  159. if (ray.geomID == 0)
  160. {
  161. nhits++;
  162. }
  163. }
  164. float ao = multiply * (1.0f - (float) nhits / nsamples);
  165. float result = Min<float>(1.0f, ao);
  166. lexel.color_ = Color(result, result, result);
  167. }
  168. }
  169. // Free all embree data.
  170. rtcDeleteGeometry(scene, gid);
  171. rtcDeleteScene(scene);
  172. rtcDeleteDevice(device);
  173. // Dilate the image by 2 pixels to allow bilinear texturing near seams.
  174. // Note that this still allows seams when mipmapping, unless mipmap levels
  175. // are generated very carefully.
  176. for (int step = 0; step < 2; step++)
  177. {
  178. SharedArrayPtr<Color> tmp(new Color[lightmap_->GetWidth() * lightmap_->GetHeight()]);
  179. memset (&tmp[0], 0, lightmap_->GetWidth() * lightmap_->GetHeight() * sizeof(Color));
  180. for (int y = 0; y < lightmap_->GetHeight() ; y++)
  181. {
  182. for (int x = 0; x < lightmap_->GetWidth(); x++)
  183. {
  184. int center = x + y * lightmap_->GetWidth();
  185. const LMLexel& lexel = lmLexels_[center];
  186. const Vector3& norm = lexel.normal_;
  187. tmp[center] = lexel.color_;
  188. if (norm.x_ == 0 && norm.y_ == 0 && norm.z_ == 0 && lexel.color_ == Color::BLACK)
  189. {
  190. for (int k = 0; k < 9; k++)
  191. {
  192. int i = (k / 3) - 1, j = (k % 3) - 1;
  193. if (i == 0 && j == 0)
  194. {
  195. continue;
  196. }
  197. i += x;
  198. j += y;
  199. if (i < 0 || j < 0 || i >= lightmap_->GetWidth() || j >= lightmap_->GetHeight() )
  200. {
  201. continue;
  202. }
  203. const LMLexel& lexel2 = lmLexels_[i + j * lightmap_->GetWidth()];
  204. if (lexel2.color_ != Color::BLACK)
  205. {
  206. tmp[center] = lexel2.color_;
  207. break;
  208. }
  209. }
  210. }
  211. }
  212. }
  213. for (unsigned i = 0; i < lmLexels_.Size(); i++)
  214. {
  215. lmLexels_[i].color_ = tmp[i];
  216. }
  217. }
  218. }
  219. bool ModelAOBake::GenerateLexels()
  220. {
  221. return true;
  222. }
  223. bool ModelAOBake::FillLexelsCallback(void* param, int x, int y, const Vector3& barycentric,const Vector3& dx, const Vector3& dy, float coverage)
  224. {
  225. ShaderData* shaderData = (ShaderData*) param;
  226. ModelAOBake* bake = shaderData->bake_;
  227. LMLexel& lexel = bake->lmLexels_[ y * bake->lightmap_->GetWidth() + x];
  228. lexel.position_ = shaderData->triPositions_[0] * barycentric.x_ +
  229. shaderData->triPositions_[1] * barycentric.y_ +
  230. shaderData->triPositions_[2] * barycentric.z_;
  231. lexel.normal_ = shaderData->faceNormal_;
  232. return true;
  233. }
  234. bool ModelAOBake::GenerateLODLevelAOMap(MPLODLevel *lodLevel)
  235. {
  236. curLOD_ = lodLevel;
  237. if (curLOD_.Null())
  238. {
  239. curLOD_ = 0;
  240. return false;
  241. }
  242. // LOD must have LM coords
  243. if (!curLOD_->HasElement(TYPE_VECTOR2, SEM_TEXCOORD, 1))
  244. {
  245. curLOD_ = 0;
  246. return false;
  247. }
  248. unsigned totalVertices = 0;
  249. curLOD_->GetTotalCounts(totalVertices, numIndices_);
  250. if (!totalVertices || ! numIndices_)
  251. {
  252. curLOD_ = 0;
  253. return false;
  254. }
  255. // allocate
  256. lmVertices_.Resize(totalVertices);
  257. indices_ = new unsigned[numIndices_];
  258. LMVertex* vOut = &lmVertices_[0];
  259. unsigned vertexStart = 0;
  260. unsigned indexStart = 0;
  261. for (unsigned i = 0; i < curLOD_->mpGeometry_.Size(); i++)
  262. {
  263. MPGeometry* mpGeo = curLOD_->mpGeometry_[i];
  264. // Copy Vertices
  265. MPVertex* vIn = &mpGeo->vertices_[0];
  266. for (unsigned j = 0; j < mpGeo->vertices_.Size(); j++)
  267. {
  268. vOut->position_ = vIn->position_;
  269. vOut->normal_ = vIn->normal_;
  270. vOut->uv0_ = vIn->uv0_;
  271. vOut->uv1_ = vIn->uv1_;
  272. vOut++;
  273. vIn++;
  274. }
  275. // Copy Indices
  276. for (unsigned j = 0; j < mpGeo->numIndices_; j++)
  277. {
  278. indices_[j + indexStart] = mpGeo->indices_[j] + vertexStart;
  279. }
  280. indexStart += mpGeo->numIndices_;
  281. vertexStart += mpGeo->vertices_.Size();
  282. }
  283. lightmap_ = new Image(context_);
  284. unsigned w, h;
  285. w = 4096;
  286. h = 4096;
  287. lightmap_->SetSize(w, h, 2, 3);
  288. lmLexels_.Resize(w * h);
  289. for (unsigned y = 0; y < h; y++)
  290. {
  291. for (unsigned x = 0; x < w ; x++)
  292. {
  293. LMLexel& lexel = lmLexels_[y * w + x];
  294. lexel.color_ = Color::BLACK;
  295. lexel.pixelCoord_.x_ = x;
  296. lexel.pixelCoord_.y_ = y;
  297. lexel.normal_ = Vector3(0, 0, 0);
  298. lexel.position_ = Vector3(0, 0, 0);
  299. }
  300. }
  301. // for all triangles
  302. Vector2 extents(lightmap_->GetWidth(), lightmap_->GetHeight());
  303. Vector2 triUV[3];
  304. ShaderData shaderData;
  305. shaderData.bake_ = this;
  306. for (unsigned i = 0; i < numIndices_; i += 3)
  307. {
  308. shaderData.triPositions_[0] = lmVertices_[indices_[i]].position_;
  309. shaderData.triPositions_[1] = lmVertices_[indices_[i + 1]].position_;
  310. shaderData.triPositions_[2] = lmVertices_[indices_[i + 2]].position_;
  311. shaderData.triNormals_[0] = lmVertices_[indices_[i]].normal_;
  312. shaderData.triNormals_[1] = lmVertices_[indices_[i + 1]].normal_;
  313. shaderData.triNormals_[2] = lmVertices_[indices_[i + 2]].normal_;
  314. triUV[0] = lmVertices_[indices_[i]].uv1_;
  315. triUV[1] = lmVertices_[indices_[i + 1]].uv1_;
  316. triUV[2] = lmVertices_[indices_[i + 2]].uv1_;
  317. triUV[0].x_ *= w;
  318. triUV[1].x_ *= w;
  319. triUV[2].x_ *= w;
  320. triUV[0].y_ *= h;
  321. triUV[1].y_ *= h;
  322. triUV[2].y_ *= h;
  323. Vector3 A = shaderData.triPositions_[1] - shaderData.triPositions_[0];
  324. Vector3 B = shaderData.triPositions_[2] - shaderData.triPositions_[0];
  325. shaderData.faceNormal_ = A.CrossProduct(B);
  326. shaderData.faceNormal_.Normalize();
  327. Raster::DrawTriangle(true, extents, true, triUV, FillLexelsCallback, &shaderData );
  328. }
  329. // Raytrace
  330. TraceAORays(128, 0.5f);
  331. for (unsigned i = 0; i < lmLexels_.Size(); i++)
  332. {
  333. const LMLexel& lexel = lmLexels_[i];
  334. lightmap_->SetPixelInt(lexel.pixelCoord_.x_, lexel.pixelCoord_.y_, 0, lexel.color_.ToUInt());
  335. }
  336. lightmap_->SavePNG("/Users/jenge/Dev/atomic/AtomicExamples/GlowTest/Resources/Textures/lightmap.png");
  337. // GetImageSize(32, false);
  338. curLOD_ = 0;
  339. return true;
  340. }
  341. bool ModelAOBake::LoadModel(const String& pathName)
  342. {
  343. ResourceCache* cache = GetSubsystem<ResourceCache>();
  344. Model* model = cache->GetResource<Model>(pathName);
  345. if (!model)
  346. return false;
  347. modelPacker_ = new ModelPacker(context_);
  348. if (!modelPacker_->Unpack(model))
  349. return false;
  350. for (unsigned i = 0; i < modelPacker_->lodLevels_.Size(); i++)
  351. {
  352. GenerateLODLevelAOMap(modelPacker_->lodLevels_[i]);
  353. }
  354. return true;
  355. }
  356. }