Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.36 KB | None | 0 0
  1. void OfflineLPV::regenVolume()
  2. {
  3. Box3F worldBox = getWorldBox();
  4. Point3F bottom_corner = worldBox.minExtents;
  5. Point3F top_corner = worldBox.maxExtents;
  6. Point3F wsVoxelSize = getWorldSpaceVoxelSize();
  7. Point3I voxelCount = getVoxelCount();
  8.  
  9. mTextureCache.clear();
  10. //get our editor status bar so we can keep tabs on progress
  11. GuiTextCtrl * statusBarGuiCtrl = dynamic_cast<GuiTextCtrl*>(Sim::findObject("EWorldEditorStatusBarInfo"));
  12. String statusBarGuiText = "";
  13.  
  14. if (statusBarGuiCtrl)
  15. {
  16. statusBarGuiText = statusBarGuiCtrl->getText();
  17. statusBarGuiCtrl->setText("Beginning Regen of the LPV. Clearing old LPV data.");
  18. }
  19.  
  20. RayInfo rayInfo;
  21. rayInfo.generateTexCoord = true;
  22. SceneContainer* container = getContainer();
  23. if ( !container ) return;
  24.  
  25. // Allocate our grids.
  26. SAFE_DELETE_ARRAY(mGeometryGrid);
  27. SAFE_DELETE_ARRAY(mLightGrid);
  28. SAFE_DELETE_ARRAY(mPropagatedLightGridA);
  29. SAFE_DELETE_ARRAY(mPropagatedLightGridB);
  30. mGeometryGrid = new GeometryVoxel[voxelCount.x * voxelCount.y * voxelCount.z];
  31. mLightGrid = new SHVoxel[voxelCount.x * voxelCount.y * voxelCount.z];
  32. mPropagatedLightGridA = new SHVoxel[voxelCount.x * voxelCount.y * voxelCount.z];
  33. mPropagatedLightGridB = new SHVoxel[voxelCount.x * voxelCount.y * voxelCount.z];
  34.  
  35. // Initialize the volume textures (if they aren't already.)
  36. _initVolumeTextures(voxelCount);
  37.  
  38. mPropagationStage = 0;
  39. mPropagatedLightGrid = mPropagatedLightGridA;
  40.  
  41. mDebugVoxels.clear();
  42.  
  43. //Step one, we poll the scene container and find all objects that overlap with us
  44. SphereF sphere;
  45. sphere.center = (worldBox.minExtents + worldBox.maxExtents) * 0.5;
  46. VectorF bv = worldBox.maxExtents - sphere.center;
  47. sphere.radius = bv.len();
  48.  
  49. if (statusBarGuiCtrl)
  50. {
  51. statusBarGuiCtrl->setText("Voxelizing Static Geometry. 0% complete.");
  52. }
  53.  
  54. S32 processedTris = 0;
  55. S32 totalTris = 0;
  56.  
  57. Vector<OptimizedPolyList> polyLists;
  58.  
  59. SimpleQueryList sql;
  60. container->findObjects(worldBox, STATIC_COLLISION_TYPEMASK, SimpleQueryList::insertionCallback, &sql);
  61. for (U32 i = 0; i < sql.mList.size(); i++)
  62. {
  63. OptimizedPolyList polyList;
  64.  
  65. sql.mList[i]->buildPolyList(PLC_Export, &polyList, worldBox, sphere);
  66.  
  67. polyLists.push_back(polyList);
  68.  
  69. totalTris += polyList.mPolyList.size();
  70. }
  71.  
  72. for (U32 i = 0; i < polyLists.size(); i++)
  73. {
  74. if (!polyLists[i].isEmpty())
  75. {
  76. Vector<U32> tempIndices;
  77. tempIndices.reserve(4);
  78.  
  79. for (U32 j = 0; j < polyLists[i].mPolyList.size(); j++)
  80. {
  81. const OptimizedPolyList::Poly& poly = polyLists[i].mPolyList[j];
  82.  
  83. if (poly.vertexCount < 3)
  84. continue;
  85.  
  86. tempIndices.setSize(poly.vertexCount);
  87. dMemset(tempIndices.address(), 0, poly.vertexCount);
  88.  
  89. if (poly.type == OptimizedPolyList::TriangleStrip||
  90. poly.type == OptimizedPolyList::TriangleFan)
  91. {
  92. tempIndices[0] = 0;
  93. U32 idx = 1;
  94.  
  95. for (U32 k = 1; k < poly.vertexCount; k += 2)
  96. tempIndices[idx++] = k;
  97.  
  98. for (U32 k = ((poly.vertexCount - 1) & (~0x1)); k > 0; k -= 2)
  99. tempIndices[idx++] = k;
  100. }
  101. else if (poly.type == OptimizedPolyList::TriangleList)
  102. {
  103. for (U32 k = 0; k < poly.vertexCount; k++)
  104. tempIndices[k] = k;
  105. }
  106. else
  107. continue;
  108.  
  109. const U32& firstIdx = polyLists[i].mIndexList[poly.vertexStart];
  110. const OptimizedPolyList::VertIndex& firstVertIdx = polyLists[i].mVertexList[firstIdx];
  111.  
  112. for (U32 k = 1; k < poly.vertexCount - 1; k++)
  113. {
  114. const U32& secondIdx = polyLists[i].mIndexList[poly.vertexStart + tempIndices[k]];
  115. const U32& thirdIdx = polyLists[i].mIndexList[poly.vertexStart + tempIndices[k + 1]];
  116.  
  117. const OptimizedPolyList::VertIndex& secondVertIdx = polyLists[i].mVertexList[secondIdx];
  118. const OptimizedPolyList::VertIndex& thirdVertIdx = polyLists[i].mVertexList[thirdIdx];
  119.  
  120. Point3F vertA = polyLists[i].mPoints[firstVertIdx.vertIdx];
  121. Point3F vertB = polyLists[i].mPoints[secondVertIdx.vertIdx];
  122. Point3F vertC = polyLists[i].mPoints[thirdVertIdx.vertIdx];
  123.  
  124. Point2F uvA = polyLists[i].mUV0s[firstVertIdx.uv0Idx];
  125. Point2F uvB = polyLists[i].mUV0s[secondVertIdx.uv0Idx];
  126. Point2F uvC = polyLists[i].mUV0s[thirdVertIdx.uv0Idx];
  127.  
  128. //First, test if any of them are contained inside. If they are, we're done
  129. if (worldBox.isContained(vertA) || worldBox.isContained(vertB) || worldBox.isContained(vertC))
  130. {
  131. //can only do 65000 verts in a call, so split it up
  132. if (mDebugRender.wireMeshRender.bufferData.empty() || mDebugRender.wireMeshRender.bufferData.last().triCount >= 21666)
  133. {
  134. DebugRenderStash::WireMeshData::data newBufferData;
  135. mDebugRender.wireMeshRender.bufferData.push_back(newBufferData);
  136. }
  137.  
  138. mDebugRender.wireMeshRender.bufferData.last().triCount++;
  139. mDebugRender.wireMeshRender.bufferData.last().vertA.push_back(vertA);
  140. mDebugRender.wireMeshRender.bufferData.last().vertB.push_back(vertB);
  141. mDebugRender.wireMeshRender.bufferData.last().vertC.push_back(vertC);
  142.  
  143. PlaneF triPlane = PlaneF(vertA, vertB, vertC);
  144.  
  145. Point3F triCenter = (vertA + vertB + vertC) / 3;
  146.  
  147. Box3F triBox;
  148.  
  149. Vector<Point3F> verts;
  150. verts.push_back(vertA);
  151. verts.push_back(vertB);
  152. verts.push_back(vertC);
  153.  
  154. triBox = Box3F::aroundPoints(verts.address(), verts.size());
  155.  
  156. //get the voxels our tri's bounds overlap
  157. Point3I minExtIdx = getVoxel(triBox.minExtents);
  158. Point3I maxExtIdx = getVoxel(triBox.maxExtents);
  159.  
  160. U32 xVoxCount = mAbs(maxExtIdx.x - minExtIdx.x);
  161. U32 yVoxCount = mAbs(maxExtIdx.y - minExtIdx.y);
  162. U32 zVoxCount = mAbs(maxExtIdx.z - minExtIdx.z);
  163.  
  164. xVoxCount = xVoxCount > 0 ? xVoxCount : 1;
  165. yVoxCount = yVoxCount > 0 ? yVoxCount : 1;
  166. zVoxCount = zVoxCount > 0 ? zVoxCount : 1;
  167.  
  168. U32 xStart = maxExtIdx.x > minExtIdx.x ? minExtIdx.x : maxExtIdx.x;
  169. U32 yStart = maxExtIdx.y > minExtIdx.y ? minExtIdx.y : maxExtIdx.y;
  170. U32 zStart = maxExtIdx.z > minExtIdx.z ? minExtIdx.z : maxExtIdx.z;
  171.  
  172. //now, iterate through the smaller subset of voxels that this encompasses, and test them. Early out if it isn't valid
  173. for (U32 x = xStart; x < xStart + xVoxCount; x++)
  174. {
  175. for (U32 y = yStart; y < yStart + yVoxCount; y++)
  176. {
  177. for (U32 z = zStart; z < zStart + zVoxCount; z++)
  178. {
  179.  
  180. S32 voxIndex = getVoxelIndex(x, y, z);
  181. if (voxIndex == -1)
  182. continue;
  183.  
  184. ColorF voxel_color = ColorF(0, 0, 0, 0);
  185. bool emissive = false;
  186.  
  187. Box3F voxBox = Box3F(bottom_corner + Point3F(mVoxelSize * x, mVoxelSize * y, mVoxelSize * z),
  188. bottom_corner + Point3F(mVoxelSize * (x + 1), mVoxelSize * (y + 1), mVoxelSize * (z + 1)));
  189.  
  190. if (triPlane.whichSide(voxBox) == PlaneF::Side::On)
  191. {
  192. //early out test. If the box containts any or all of the verts, it's assumed to intersect
  193. bool containsVertA = voxBox.isContained(vertA);
  194. bool containsVertB = voxBox.isContained(vertB);
  195. bool containsVertC = voxBox.isContained(vertC);
  196. if (containsVertA || containsVertB || containsVertC)
  197. {
  198. voxel_color = ColorF(255, 255, 255, 255);
  199. if ( poly.material > -1 )
  200. {
  201. Point2F uv;
  202. if ( containsVertA ) uv = uvA;
  203. if ( containsVertB ) uv = uvB;
  204. if ( containsVertC ) uv = uvC;
  205.  
  206. Material* mat = dynamic_cast<Material*>(polyLists[i].mMaterialList[poly.material]->getMaterial());
  207. if (mat)
  208. {
  209. Resource<GBitmap> diffuseTex = getOrCreateTexture(mat->mDiffuseMapFilename[0]);
  210. if (diffuseTex != NULL)
  211. {
  212. voxel_color = diffuseTex->sampleTexel(uv.x, uv.y)*mat->mDiffuse[0];
  213. } else {
  214. voxel_color = mat->mDiffuse[0];
  215. }
  216. voxel_color.red = x / xVoxCount;
  217. voxel_color.green = y / yVoxCount;
  218. voxel_color.blue = z / zVoxCount;
  219. emissive = mat->mEmissive[0];
  220. }
  221. }
  222.  
  223. U32 voxelIdx = getVoxelIndex(x, y, z);
  224. mGeometryGrid[voxelIdx].color = voxel_color;
  225. }
  226. else
  227. {
  228. //first, test if any of the tri edges intersect the box
  229. F32 collideAPos;
  230. Point3F collideANorm;
  231. bool collideA = voxBox.collideLine(vertA, vertB, &collideAPos, &collideANorm);
  232.  
  233. F32 collideBPos;
  234. Point3F collideBNorm;
  235. bool collideB = voxBox.collideLine(vertB, vertC, &collideBPos, &collideBNorm);
  236.  
  237. F32 collideCPos;
  238. Point3F collideCNorm;
  239. bool collideC = voxBox.collideLine(vertC, vertA, &collideCPos, &collideCNorm);
  240.  
  241. if (collideA || collideB || collideC)
  242. {
  243. voxel_color = ColorF(255, 255, 255, 255);
  244. if ( poly.material > -1 )
  245. {
  246. Point2F uv;
  247. if ( collideA ) uv = uvA + (collideAPos * (uvB - uvA));
  248. if ( collideB ) uv = uvB + (collideBPos * (uvC - uvB));
  249. if ( collideC ) uv = uvC + (collideCPos * (uvA - uvC));
  250.  
  251. Material* mat = dynamic_cast<Material*>(polyLists[i].mMaterialList[poly.material]->getMaterial());
  252. if (mat)
  253. {
  254. Resource<GBitmap> diffuseTex = getOrCreateTexture(mat->mDiffuseMapFilename[0]);
  255. if (diffuseTex != NULL)
  256. {
  257. voxel_color = diffuseTex->sampleTexel(uv.x, uv.y)*mat->mDiffuse[0];
  258. } else {
  259. voxel_color = mat->mDiffuse[0];
  260. }
  261. voxel_color.red = x / xVoxCount;
  262. voxel_color.green = y / yVoxCount;
  263. voxel_color.blue = z / zVoxCount;
  264.  
  265. emissive = mat->mEmissive[0];
  266. }
  267. }
  268.  
  269. //indeed it does
  270. U32 voxelIdx = getVoxelIndex(x, y, z);
  271. mGeometryGrid[voxelIdx].color = voxel_color;
  272. mGeometryGrid[voxelIdx].emissive = emissive;
  273. }
  274. else
  275. {
  276. //it doesn't, so we have to test if the voxel intersects the tri(like in cases where the triangle is larger than the voxel)
  277. //we do this second because it requires more tests and is thus slower.
  278. for (U32 e = 0; e < BoxBase::Edges::NUM_EDGES; e++)
  279. {
  280. BoxBase::Points a, b;
  281. voxBox.getEdgePointIndices((BoxBase::Edges)e, a, b);
  282.  
  283. Point3F edgeA = voxBox.computeVertex(a);
  284. Point3F edgeB = voxBox.computeVertex(b);
  285.  
  286. Point3F intersect;
  287.  
  288. if (triPlane.clipSegment(edgeA, edgeB, intersect))
  289. //if (MathUtils::mLineTriangleCollide(edgeA, edgeB, vertA, vertB, vertC, NULL, &t))
  290. {
  291. if (intersect == edgeA || intersect == edgeB)
  292. continue;
  293.  
  294. voxel_color = ColorF(255, 255, 255, 255);
  295. if ( poly.material > -1 )
  296. {
  297. VectorF f1 = vertA - intersect;
  298. VectorF f2 = vertB - intersect;
  299. VectorF f3 = vertC - intersect;
  300.  
  301. F32 a = mCross(vertA - vertB, vertA - vertC).magnitudeSafe();
  302. F32 a1 = mCross(f2, f3).magnitudeSafe() / a;
  303. F32 a2 = mCross(f3, f1).magnitudeSafe() / a;
  304. F32 a3 = mCross(f1, f2).magnitudeSafe() / a;
  305.  
  306. Point2F uv = (uvA * a1) + (uvB * a2) + (uvC * a3);
  307.  
  308. Material* mat = dynamic_cast<Material*>(polyLists[i].mMaterialList[poly.material]->getMaterial());
  309. if (mat)
  310. {
  311. Resource<GBitmap> diffuseTex = getOrCreateTexture(mat->mDiffuseMapFilename[0]);
  312. if (diffuseTex != NULL)
  313. {
  314. voxel_color = diffuseTex->sampleTexel(uv.x, uv.y)*mat->mDiffuse[0];
  315. } else {
  316. voxel_color = mat->mDiffuse[0];
  317. }
  318. voxel_color.red = x / xVoxCount;
  319. voxel_color.green = y / yVoxCount;
  320. voxel_color.blue = z / zVoxCount;
  321.  
  322. emissive = mat->mEmissive[0];
  323. }
  324. }
  325.  
  326. U32 voxelIdx = getVoxelIndex(x, y, z);
  327. mGeometryGrid[voxelIdx].color = voxel_color;
  328. mGeometryGrid[voxelIdx].emissive = emissive;
  329. break;
  330. }
  331. }
  332. }
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }
  339. }
  340.  
  341. processedTris++;
  342.  
  343. if (statusBarGuiCtrl)
  344. {
  345. char buff[256];
  346. F32 percetile = processedTris / totalTris;
  347. dSprintf(buff, 256, "Voxelizing Static Geometry. %g % complete.", percetile);
  348. statusBarGuiCtrl->setText(buff);
  349. }
  350. }
  351. }
  352. }
  353.  
  354. if (statusBarGuiCtrl)
  355. {
  356. statusBarGuiCtrl->setText(statusBarGuiText);
  357. }
  358.  
  359. mTextureCache.clear();
  360. //_rebuildDebugVoxels();
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement