BsLightProbes.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsLightProbes.h"
  4. #include "BsLightProbeVolume.h"
  5. #include "BsGpuBuffer.h"
  6. #include "BsRendererView.h"
  7. #include "BsRenderBeastIBLUtility.h"
  8. #include "BsRenderBeast.h"
  9. namespace bs { namespace ct
  10. {
  11. LightProbes::LightProbes()
  12. :mTetrahedronVolumeDirty(false), mNumAllocatedEntries(0), mNumUsedEntries(0)
  13. {
  14. resizeCoefficientBuffer(512);
  15. }
  16. void LightProbes::notifyAdded(const SPtr<LightProbeVolume>& volume)
  17. {
  18. UINT32 handle = (UINT32)mVolumes.size();
  19. VolumeInfo info;
  20. info.volume = volume;
  21. info.isDirty = true;
  22. mVolumes.push_back(info);
  23. volume->setRendererId(handle);
  24. notifyDirty(volume);
  25. }
  26. void LightProbes::notifyDirty(const SPtr<LightProbeVolume>& volume)
  27. {
  28. UINT32 handle = volume->getRendererId();
  29. mVolumes[handle].isDirty = true;
  30. mTetrahedronVolumeDirty = true;
  31. }
  32. void LightProbes::notifyRemoved(const SPtr<LightProbeVolume>& volume)
  33. {
  34. UINT32 handle = volume->getRendererId();
  35. LightProbeVolume* lastVolume = mVolumes.back().volume.get();
  36. UINT32 lastHandle = lastVolume->getRendererId();
  37. if (handle != lastHandle)
  38. {
  39. // Swap current last element with the one we want to erase
  40. std::swap(mVolumes[handle], mVolumes[lastHandle]);
  41. lastVolume->setRendererId(handle);
  42. }
  43. // Erase last (empty) element
  44. mVolumes.erase(mVolumes.end() - 1);
  45. mTetrahedronVolumeDirty = true;
  46. }
  47. void LightProbes::updateProbes()
  48. {
  49. if(mTetrahedronVolumeDirty)
  50. {
  51. // Gather all positions
  52. for(auto& entry : mVolumes)
  53. {
  54. const Vector<Vector3>& positions = entry.volume->getLightProbePositions();
  55. Vector3 offset = entry.volume->getPosition();
  56. Quaternion rotation = entry.volume->getRotation();
  57. for(auto& localPos : positions)
  58. {
  59. Vector3 transformedPos = rotation.rotate(localPos) + offset;
  60. mTempTetrahedronPositions.push_back(transformedPos);
  61. }
  62. }
  63. mTetrahedronInfos.clear();
  64. mTetrahedronBounds.clear();
  65. generateTetrahedronData(mTempTetrahedronPositions, mTetrahedronInfos, false);
  66. // Generate bounds
  67. for(auto& entry : mTetrahedronInfos)
  68. {
  69. // Skipping outer faces
  70. if (entry.volume.neighbors[3] < 0)
  71. continue;
  72. AABox aabox = AABox(Vector3::INF, -Vector3::INF);
  73. for (int i = 0; i < 4; ++i)
  74. aabox.merge(mTempTetrahedronPositions[entry.volume.neighbors[i]]);
  75. mTetrahedronBounds.push_back(aabox);
  76. }
  77. mTempTetrahedronPositions.clear();
  78. mTetrahedronVolumeDirty = false;
  79. }
  80. }
  81. void LightProbes::resizeTetrahedronBuffers(VisibleLightProbeData& data, UINT32 count)
  82. {
  83. {
  84. GPU_BUFFER_DESC desc;
  85. desc.type = GBT_STRUCTURED;
  86. desc.elementSize = sizeof(TetrahedronBoundsGPU);
  87. desc.elementCount = count;
  88. desc.usage = GBU_STATIC;
  89. desc.format = BF_UNKNOWN;
  90. SPtr<GpuBuffer> newBuffer = GpuBuffer::create(desc);
  91. if (data.tetrahedronBounds)
  92. newBuffer->copyData(*data.tetrahedronBounds, 0, 0, data.tetrahedronBounds->getSize(), true);
  93. data.tetrahedronBounds = newBuffer;
  94. }
  95. {
  96. GPU_BUFFER_DESC desc;
  97. desc.type = GBT_STRUCTURED;
  98. desc.elementSize = sizeof(TetrahedronDataGPU);
  99. desc.elementCount = count;
  100. desc.usage = GBU_STATIC;
  101. desc.format = BF_UNKNOWN;
  102. SPtr<GpuBuffer> newBuffer = GpuBuffer::create(desc);
  103. if (data.tetrahedronInfos)
  104. newBuffer->copyData(*data.tetrahedronInfos, 0, 0, data.tetrahedronInfos->getSize(), true);
  105. data.tetrahedronInfos = newBuffer;
  106. }
  107. data.maxNumEntries = count;
  108. }
  109. void LightProbes::resizeCoefficientBuffer(UINT32 count)
  110. {
  111. GPU_BUFFER_DESC desc;
  112. desc.type = GBT_STRUCTURED;
  113. desc.elementSize = sizeof(SHVector3RGB);
  114. desc.elementCount = count;
  115. desc.usage = GBU_STATIC;
  116. desc.format = BF_UNKNOWN;
  117. SPtr<GpuBuffer> newBuffer = GpuBuffer::create(desc);
  118. if (mProbeCoefficientsGPU)
  119. newBuffer->copyData(*mProbeCoefficientsGPU, 0, 0, mProbeCoefficientsGPU->getSize(), true);
  120. mProbeCoefficientsGPU = newBuffer;
  121. mNumAllocatedEntries = count;
  122. }
  123. void LightProbes::updateVisibleProbes(const RendererView& view, VisibleLightProbeData& output)
  124. {
  125. // Ignore all probes past this point
  126. static const float MAX_PROBE_DISTANCE = 100.0f;
  127. const RendererViewProperties& viewProps = view.getProperties();
  128. const ConvexVolume& worldFrustum = viewProps.cullFrustum;
  129. const float maxProbeDistance2 = MAX_PROBE_DISTANCE * MAX_PROBE_DISTANCE;
  130. for (UINT32 i = 0; i < (UINT32)mTetrahedronBounds.size(); i++)
  131. {
  132. float distance2 = viewProps.viewOrigin.squaredDistance(mTetrahedronBounds[i].getCenter());
  133. if (distance2 > maxProbeDistance2)
  134. continue;
  135. if (worldFrustum.intersects(mTetrahedronBounds[i]))
  136. mTempTetrahedronVisibility.push_back(i);
  137. }
  138. UINT32 numVisibleTets = (UINT32)mTempTetrahedronVisibility.size();
  139. if (numVisibleTets > output.maxNumEntries)
  140. {
  141. UINT32 newBufferSize = 256;
  142. if(output.maxNumEntries > 0)
  143. newBufferSize = Math::divideAndRoundUp(numVisibleTets, output.maxNumEntries) * output.maxNumEntries;
  144. resizeTetrahedronBuffers(output, newBufferSize);
  145. }
  146. // Write bounds
  147. {
  148. TetrahedronBoundsGPU* dst = (TetrahedronBoundsGPU*)output.tetrahedronBounds->lock(0,
  149. output.tetrahedronBounds->getSize(), GBL_WRITE_ONLY_DISCARD);
  150. for (auto& entry : mTempTetrahedronVisibility)
  151. {
  152. const AABox& aabox = mTetrahedronBounds[entry];
  153. dst->center = aabox.getCenter();
  154. dst->extents = aabox.getHalfSize();
  155. dst++;
  156. }
  157. output.tetrahedronBounds->unlock();
  158. }
  159. // Write other information
  160. {
  161. TetrahedronDataGPU* dst = (TetrahedronDataGPU*)output.tetrahedronInfos->lock(0,
  162. output.tetrahedronInfos->getSize(), GBL_WRITE_ONLY_DISCARD);
  163. for (auto& entry : mTempTetrahedronVisibility)
  164. {
  165. const TetrahedronData& data = mTetrahedronInfos[entry];
  166. memcpy(dst->indices, data.volume.vertices, sizeof(UINT32) * 4);
  167. memcpy(&dst->transform, &data.transform, sizeof(float) * 12);
  168. dst++;
  169. }
  170. output.tetrahedronBounds->unlock();
  171. }
  172. mTempTetrahedronVisibility.clear();
  173. }
  174. /** Hash value generator for std::pair<INT32, INT32>. */
  175. struct pair_hash
  176. {
  177. size_t operator()(const std::pair<INT32, INT32>& key) const
  178. {
  179. size_t hash = 0;
  180. bs::hash_combine(hash, key.first);
  181. bs::hash_combine(hash, key.second);
  182. return hash;
  183. }
  184. };
  185. void LightProbes::generateTetrahedronData(const Vector<Vector3>& positions, Vector<TetrahedronData>& output,
  186. bool includeOuterFaces)
  187. {
  188. bs_frame_mark();
  189. {
  190. TetrahedronVolume volume = Triangulation::tetrahedralize(positions);
  191. // Generate matrices
  192. UINT32 numOutputTets = (UINT32)volume.tetrahedra.size();
  193. if (includeOuterFaces)
  194. numOutputTets += (UINT32)volume.outerFaces.size();
  195. output.reserve(includeOuterFaces);
  196. // Insert inner tetrahedrons, generate matrices
  197. for(UINT32 i = 0; i < (UINT32)volume.tetrahedra.size(); ++i)
  198. {
  199. TetrahedronData entry;
  200. entry.volume = volume.tetrahedra[i];
  201. // Generate a matrix that can be used for calculating barycentric coordinates
  202. // To determine a point within a tetrahedron, using barycentric coordinates, we use:
  203. // P = (P1 - P4) * a + (P2 - P4) * b + (P3 - P4) * c + P4
  204. //
  205. // Where P1, P2, P3, P4 are the corners of the tetrahedron.
  206. //
  207. // Expanded for each coordinate this is:
  208. // x = (x1 - x4) * a + (x2 - x4) * b + (x3 - x4) * c + x4
  209. // y = (y1 - y4) * a + (y2 - y4) * b + (y3 - y4) * c + y4
  210. // z = (z1 - z4) * a + (z2 - z4) * b + (z3 - z4) * c + z4
  211. //
  212. // In matrix form this is:
  213. // a
  214. // P = [P1 - P4, P2 - P4, P3 - P4, P4] [b]
  215. // c
  216. // 1
  217. //
  218. // Solved for barycentric coordinates:
  219. // a
  220. // [b] = Minv * P
  221. // c
  222. // 1
  223. //
  224. // Where Minv is the inverse of the matrix above.
  225. const Vector3& P1 = positions[volume.tetrahedra[i].vertices[0]];
  226. const Vector3& P2 = positions[volume.tetrahedra[i].vertices[1]];
  227. const Vector3& P3 = positions[volume.tetrahedra[i].vertices[2]];
  228. const Vector3& P4 = positions[volume.tetrahedra[i].vertices[3]];
  229. Matrix4 mat;
  230. mat.setColumn(0, Vector4(P1 - P4, 0.0f));
  231. mat.setColumn(1, Vector4(P2 - P4, 0.0f));
  232. mat.setColumn(2, Vector4(P3 - P4, 0.0f));
  233. mat.setColumn(3, Vector4(P4, 1.0f));
  234. entry.transform = mat.inverse();
  235. output.push_back(entry);
  236. }
  237. if (includeOuterFaces)
  238. {
  239. // Put outer faces into the Tetrahedron structure, for convenience
  240. UINT32 outerFaceOffset = (UINT32)volume.tetrahedra.size();
  241. FrameVector<Tetrahedron> outerTetrahedrons;
  242. outerTetrahedrons.resize(volume.outerFaces.size());
  243. for (UINT32 i = 0; i < (UINT32)volume.outerFaces.size(); ++i)
  244. {
  245. Tetrahedron outerTetrahedron;
  246. memcpy(outerTetrahedron.vertices, volume.outerFaces[i].vertices, sizeof(INT32) * 3);
  247. memset(outerTetrahedron.neighbors, -1, sizeof(INT32) * 3);
  248. outerTetrahedron.vertices[4] = -1; // Marks the tetrahedron as an outer face
  249. outerTetrahedron.neighbors[4] = volume.outerFaces[i].tetrahedron;
  250. outerTetrahedrons[i] = outerTetrahedron;
  251. }
  252. // Connect boundary tetrahedrons with these new outer tetrahedrons
  253. for (UINT32 i = 0; i < (UINT32)volume.outerFaces.size(); ++i)
  254. {
  255. Tetrahedron& tet = volume.tetrahedra[volume.outerFaces[i].tetrahedron];
  256. for (UINT32 j = 0; j < 4; j++)
  257. {
  258. if (tet.neighbors[j] == -1)
  259. tet.neighbors[j] = outerFaceOffset + i;
  260. }
  261. }
  262. // Make a map between outer edges and faces, used in the following algorithms
  263. struct Edge
  264. {
  265. INT32 faces[2];
  266. INT32 oppositeVerts[2];
  267. };
  268. FrameUnorderedMap<std::pair<INT32, INT32>, Edge, pair_hash> edgeMap;
  269. for (UINT32 i = 0; i < (UINT32)volume.outerFaces.size(); ++i)
  270. {
  271. for (UINT32 j = 0; j < 3; ++j)
  272. {
  273. INT32 v0 = volume.outerFaces[i].vertices[j];
  274. INT32 v1 = volume.outerFaces[i].vertices[(j + 1) % 3];
  275. // Keep the same ordering so other faces can find the same edge
  276. if (v0 > v1)
  277. std::swap(v0, v1);
  278. auto iterFind = edgeMap.find(std::make_pair(v0, v1));
  279. if (iterFind != edgeMap.end())
  280. {
  281. iterFind->second.faces[1] = i;
  282. iterFind->second.oppositeVerts[1] = (j + 2) % 3;
  283. }
  284. else
  285. {
  286. Edge edge;
  287. edge.faces[0] = i;
  288. edge.oppositeVerts[0] = (j + 2) % 3;
  289. edgeMap.insert(std::make_pair(std::make_pair(v0, v1), edge));
  290. }
  291. }
  292. }
  293. // Form connections between outer tetrahedrons
  294. for (auto& entry : edgeMap)
  295. {
  296. const Edge& edge = entry.second;
  297. Tetrahedron& tet0 = outerTetrahedrons[outerFaceOffset + edge.faces[0]];
  298. tet0.neighbors[edge.oppositeVerts[0]] = outerFaceOffset + edge.faces[1];
  299. Tetrahedron& tet1 = outerTetrahedrons[outerFaceOffset + edge.faces[1]];
  300. tet1.neighbors[edge.oppositeVerts[1]] = outerFaceOffset + edge.faces[0];
  301. }
  302. // Generate face normals
  303. FrameVector<Vector3> faceNormals(volume.outerFaces.size());
  304. for (UINT32 i = 0; i < (UINT32)volume.outerFaces.size(); ++i)
  305. {
  306. const Vector3& v0 = positions[volume.outerFaces[i].vertices[0]];
  307. const Vector3& v1 = positions[volume.outerFaces[i].vertices[1]];
  308. const Vector3& v2 = positions[volume.outerFaces[i].vertices[2]];
  309. Vector3 e0 = v1 - v0;
  310. Vector3 e1 = v2 - v0;
  311. faceNormals[i] = Vector3::normalize(e1.cross(e0));
  312. }
  313. // Generate vertex normals
  314. struct VertexAccum
  315. {
  316. Vector3 sum;
  317. float weight;
  318. };
  319. FrameUnorderedMap<INT32, Vector3> vertexNormals;
  320. for (auto& entry : edgeMap)
  321. {
  322. const Edge& edge = entry.second;
  323. auto accumulateNormalForEdgeVertex = [&](UINT32 v0Idx, UINT32 v1Idx)
  324. {
  325. auto iter = vertexNormals.insert(std::make_pair(v0Idx, Vector3(BsZero)));
  326. Vector3& accum = iter.first->second;
  327. const Vector3& v0 = positions[v0Idx];
  328. auto accumulateNormalForFace = [&](INT32 faceIdx, INT32 v2LocIdx)
  329. {
  330. const TetrahedronFace& face = volume.outerFaces[faceIdx];
  331. // Vertices on the face, that aren't the vertex we're calculating the normal for
  332. const Vector3& v1 = positions[v1Idx];
  333. const Vector3& v2 = positions[face.vertices[v2LocIdx]];
  334. // Weight the contribution to the normal based on the angle spanned by the triangle
  335. Vector3 e0 = Vector3::normalize(v1 - v0);
  336. Vector3 e1 = Vector3::normalize(v2 - v0);
  337. float weight = acos(e0.dot(e1));
  338. accum += weight * faceNormals[faceIdx];
  339. };
  340. accumulateNormalForFace(edge.faces[0], entry.second.oppositeVerts[0]);
  341. accumulateNormalForFace(edge.faces[1], entry.second.oppositeVerts[1]);
  342. };
  343. accumulateNormalForEdgeVertex(entry.first.first, entry.first.second);
  344. accumulateNormalForEdgeVertex(entry.first.second, entry.first.first);
  345. }
  346. for (auto& entry : vertexNormals)
  347. entry.second.normalize();
  348. // Insert outer tetrahedrons, generate matrices
  349. for(UINT32 i = 0; i < (UINT32)outerTetrahedrons.size(); ++i)
  350. {
  351. TetrahedronData entry;
  352. entry.volume = outerTetrahedrons[i];
  353. // We need a way to project a point outside the tetrahedron volume onto an outer face, then calculate
  354. // triangle's barycentric coordinates. Use use the per-vertex normals to extrude the triangle face into
  355. // infinity.
  356. // Our point can be represented as:
  357. // p == a (p0 + t*v0) + b (p1 + t*v1) + c (p2 + t*v2)
  358. //
  359. // where a, b and c are barycentric coordinates,
  360. // p0, p1, p2 are the corners of the face
  361. // v0, v1, v2 are the vertex normals, per corner
  362. // t is the distance from the triangle to the point
  363. //
  364. // Essentially we're calculating the corners of a bigger triangle that's "t" units away from the
  365. // face, and its corners lie along the per-vertex normals. Point "p" will lie on that triangle, for which
  366. // we can then calculate barycentric coordinates normally.
  367. //
  368. // First we substitute: c = 1 - a - b
  369. // p == a (p0 + t v0) + b (p1 + t v1) + (1 - a - b) (p2 + t v2)
  370. // p == a (p0 + t v0) + b (p1 + t v1) + (p2 + t v2) - a (p2 + t v2) - b (p2 + t v2)
  371. // p == a (p0 - p2 + t v0 - t v2) + b (p1 - p2 + t v1 - t v2) + (p2 + t v2)
  372. //
  373. // And move everything to one side:
  374. // p - p2 - t v2 == a (p0 - p2 + t ( v0 - v2)) + b (p1 - p2 + t ( v1 - v2))
  375. // a (p0 - p2 + t ( v0 - v2)) + b (p1 - p2 + t ( v1 - v2)) - (p - p2 - t v2) == 0
  376. //
  377. // We rewrite it using:
  378. // Ap = p0 - p2
  379. // Av = v0 - v2
  380. // Bp = p1 - p2
  381. // Bv = v1 - v2
  382. // Cp = p - p2
  383. // Cv = -v2
  384. //
  385. // Which yields:
  386. // a (Ap + t Av) + b (Bp + t Bv) - (Cp + t Cv) == 0
  387. //
  388. // Which can be written in matrix form:
  389. //
  390. // M = {Ap + t Av, Bp + t Bv, Cp + t Cv}
  391. // a 0
  392. // M * [ b ] = [0]
  393. // -1 0
  394. //
  395. // From that we can tell that matrix M cannot be inverted, because if we multiply the zero vector with the
  396. // inverted matrix the result would be zero, and not [a, b, -1]. Since the matrix cannot be inverted
  397. // det(M) == 0.
  398. //
  399. // We can use that fact to calculate "t". After we have "t" we can calculate barycentric coordinates
  400. // normally.
  401. //
  402. // Solving equation det(M) == 0 yields a cubic in form:
  403. // p t^3 + q t^2 + r t + s = 0
  404. //
  405. // We'll convert this to monic form, by dividing by p:
  406. // t^3 + q/p t^2 + r/p t + s/p = 0
  407. //
  408. // Or if p ends up being zero, we end up with a quadratic instead:
  409. // q t^2 + r t + s = 0
  410. //
  411. // We want to create a matrix that when multiplied with the position, yields us the three coefficients,
  412. // which we can then use to solve for "t". For this we create a 4x3 matrix, where each row represents
  413. // a solution for one of the coefficients. We factor contributons to each coefficient whether they depend on
  414. // position x, y, z, or don't depend on position (row columns, in that order respectively).
  415. const Vector3& p0 = positions[entry.volume.vertices[0]];
  416. const Vector3& p1 = positions[entry.volume.vertices[1]];
  417. const Vector3& p2 = positions[entry.volume.vertices[2]];
  418. const Vector3& v0 = vertexNormals[entry.volume.vertices[0]];
  419. const Vector3& v1 = vertexNormals[entry.volume.vertices[1]];
  420. const Vector3& v2 = vertexNormals[entry.volume.vertices[2]];
  421. float p =
  422. v2.x * v1.y * v0.z -
  423. v1.x * v2.y * v0.z -
  424. v2.x * v0.y * v1.z +
  425. v0.x * v2.y * v1.z +
  426. v1.x * v0.y * v2.z -
  427. v0.x * v1.y * v2.z;
  428. float qx = -v1.y * v0.z + v2.y * v0.z + v0.y * v1.z - v2.y * v1.z - v0.y * v2.z + v1.y * v2.z;
  429. float qy = v1.x * v0.z - v2.x * v0.z - v0.x * v1.z + v2.x * v1.z + v0.x * v2.z - v1.x * v2.z;
  430. float qz = -v1.x * v0.y + v2.x * v0.y + v0.x * v1.y - v2.x * v1.y - v0.x * v2.y + v1.x * v2.y;
  431. float qw = v2.y * v1.z * p0.x - v1.y * v2.z * p0.x - v2.y * v0.z * p1.x + v0.y * v2.z * p1.x +
  432. v1.y * v0.z * p2.x - v0.y * v1.z * p2.x - v2.x * v1.z * p0.y + v1.x * v2.z * p0.y +
  433. v2.x * v0.z * p1.y - v0.x * v2.z * p1.y - v1.x * v0.z * p2.y + v0.x * v1.z * p2.y +
  434. v2.x * v1.y * p0.z - v1.x * v2.y * p0.z - v2.x * v0.y * p1.z + v0.x * v2.y * p1.z +
  435. v1.x * v0.y * p2.z - v0.x * v1.y * p2.z;
  436. float rx = v1.z * p0.y - v2.z * p0.y - v0.z * p1.y + v2.z * p1.y + v0.z * p2.y - v1.z * p2.y -
  437. v1.y * p0.z + v2.y * p0.z + v0.y * p1.z - v2.y * p1.z - v0.y * p2.z + v1.y * p2.z;
  438. float ry = -v1.z * p0.x + v2.z * p0.x + v0.z * p1.x - v2.z * p1.x - v0.z * p2.x + v1.z * p2.x +
  439. v1.x * p0.z - v2.x * p0.z - v0.x * p1.z + v2.x * p1.z + v0.x * p2.z - v1.x * p2.z;
  440. float rz = v1.y * p0.x - v2.y * p0.x - v0.y * p1.x + v2.y * p1.x + v0.y * p2.x - v1.y * p2.x -
  441. v1.x * p0.y + v2.x * p0.y + v0.x * p1.y - v2.x * p1.y - v0.x * p2.y + v1.x * p2.y;
  442. float rw = v2.z * p1.x * p0.y - v1.z * p2.x * p0.y - v2.z * p0.x * p1.y + v0.z * p2.x * p1.y +
  443. v1.z * p0.x * p2.y - v0.z * p1.x * p2.y - v2.y * p1.x * p0.z + v1.y * p2.x * p0.z +
  444. v2.x * p1.y * p0.z - v1.x * p2.y * p0.z + v2.y * p0.x * p1.z - v0.y * p2.x * p1.z -
  445. v2.x * p0.y * p1.z + v0.x * p2.y * p1.z - v1.y * p0.x * p2.z + v0.y * p1.x * p2.z +
  446. v1.x * p0.y * p2.z - v0.x * p1.y * p2.z;
  447. float sx = -p1.y * p0.z + p2.y * p0.z + p0.y * p1.z - p2.y * p1.z - p0.y * p2.z + p1.y * p2.z;
  448. float sy = p1.x * p0.z - p2.x * p0.z - p0.x * p1.z + p2.x * p1.z + p0.x * p2.z - p1.x * p2.z;
  449. float sz = -p1.x * p0.y + p2.x * p0.y + p0.x * p1.y - p2.x * p1.y - p0.x * p2.y + p1.x * p2.y;
  450. float sw = p2.x * p1.y * p0.z - p1.x * p2.y * p0.z - p2.x * p0.y * p1.z +
  451. p0.x * p2.y * p1.z + p1.x * p0.y * p2.z - p0.x * p1.y * p2.z;
  452. entry.transform[0][0] = qx;
  453. entry.transform[0][1] = qy;
  454. entry.transform[0][2] = qz;
  455. entry.transform[0][3] = qw;
  456. entry.transform[1][0] = rx;
  457. entry.transform[1][1] = ry;
  458. entry.transform[1][2] = rz;
  459. entry.transform[1][3] = rw;
  460. entry.transform[2][0] = sx;
  461. entry.transform[2][1] = sy;
  462. entry.transform[2][2] = sz;
  463. entry.transform[2][3] = sw;
  464. // Unused
  465. entry.transform[3][0] = 0.0f;
  466. entry.transform[3][1] = 0.0f;
  467. entry.transform[3][2] = 0.0f;
  468. entry.transform[3][3] = 0.0f;
  469. if (fabs(p) > 0.00001f)
  470. entry.transform = entry.transform * (1.0f / p);
  471. else // Quadratic
  472. entry.volume.neighbors[3] = -2;
  473. output.push_back(entry);
  474. }
  475. }
  476. }
  477. bs_frame_clear();
  478. }
  479. }}