tsPartInstance.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "ts/tsPartInstance.h"
  23. #include "math/mMath.h"
  24. //-------------------------------------------------------------------------------------
  25. // Constructors
  26. //-------------------------------------------------------------------------------------
  27. MRandomR250 TSPartInstance::smRandom;
  28. TSPartInstance::TSPartInstance(TSShapeInstance * sourceShape)
  29. {
  30. VECTOR_SET_ASSOCIATION(mMeshObjects);
  31. init(sourceShape);
  32. }
  33. TSPartInstance::TSPartInstance(TSShapeInstance * sourceShape, S32 objectIndex)
  34. {
  35. init(sourceShape);
  36. addObject(objectIndex);
  37. }
  38. void TSPartInstance::init(TSShapeInstance * sourceShape)
  39. {
  40. mSourceShape = sourceShape;
  41. mSizeCutoffs = NULL;
  42. mPolyCount = NULL;
  43. mNumDetails = 0;
  44. mCurrentObjectDetail = 0;
  45. mCurrentIntraDL = 1.0f;
  46. mData = 0;
  47. mRadius = 0.125;
  48. }
  49. TSPartInstance::~TSPartInstance()
  50. {
  51. delete [] mPolyCount;
  52. }
  53. //-------------------------------------------------------------------------------------
  54. // Methods for updating PartInstances
  55. //-------------------------------------------------------------------------------------
  56. void TSPartInstance::addObject(S32 objectIndex)
  57. {
  58. if (mSourceShape->mMeshObjects[objectIndex].forceHidden ||
  59. mSourceShape->mMeshObjects[objectIndex].visible < 0.01f)
  60. // not visible, don't bother
  61. return;
  62. mMeshObjects.push_back(&mSourceShape->mMeshObjects[objectIndex]);
  63. }
  64. void TSPartInstance::updateBounds()
  65. {
  66. // run through meshes and brute force it?
  67. Box3F bounds;
  68. mBounds.minExtents.set( 10E30f, 10E30f, 10E30f);
  69. mBounds.maxExtents.set(-10E30f,-10E30f,-10E30f);
  70. for (S32 i=0; i<mMeshObjects.size(); i++)
  71. {
  72. if (mMeshObjects[i]->getMesh(0))
  73. mMeshObjects[i]->getMesh(0)->computeBounds(mMeshObjects[i]->getTransform(),bounds,mMeshObjects[i]->frame);
  74. mBounds.minExtents.setMin(bounds.minExtents);
  75. mBounds.maxExtents.setMax(bounds.maxExtents);
  76. }
  77. mCenter = mBounds.minExtents + mBounds.maxExtents;
  78. mCenter *= 0.5f;
  79. Point3F r = mBounds.maxExtents-mCenter;
  80. mRadius = mSqrt(mDot(r,r));
  81. }
  82. //-------------------------------------------------------------------------------------
  83. // Methods for breaking shapes into pieces
  84. //-------------------------------------------------------------------------------------
  85. void TSPartInstance::breakShape(TSShapeInstance * shape, S32 subShape, Vector<TSPartInstance*> & partList, F32 * probShatter, F32 * probBreak, S32 probDepth)
  86. {
  87. AssertFatal(subShape>=0 && subShape<shape->mShape->subShapeFirstNode.size(),"TSPartInstance::breakShape: subShape out of range.");
  88. S32 start = shape->mShape->subShapeFirstNode[subShape];
  89. TSPartInstance::breakShape(shape, NULL, start, partList, probShatter, probBreak, probDepth);
  90. // update bounds (and get rid of empty parts)
  91. for (S32 i=0; i<partList.size(); i++)
  92. {
  93. if (partList[i]->mMeshObjects.size())
  94. {
  95. partList[i]->updateBounds();
  96. // Remove any parts parts with invalid box
  97. Box3F box = partList[i]->getBounds();
  98. if (!box.isValidBox())
  99. {
  100. Con::warnf("TSPartInstance::breakShape - part created with invalid object box. Removing from list.");
  101. partList.erase(i);
  102. i--;
  103. }
  104. }
  105. else
  106. {
  107. partList.erase(i);
  108. i--;
  109. }
  110. }
  111. }
  112. void TSPartInstance::breakShape(TSShapeInstance * shape, TSPartInstance * currentPart, S32 currentNode, Vector<TSPartInstance*> & partList, F32 * probShatter, F32 * probBreak, S32 probDepth)
  113. {
  114. AssertFatal( !probDepth || (probShatter && probBreak),"TSPartInstance::breakShape: probabilities improperly specified.");
  115. const TSShape::Node * node = &shape->mShape->nodes[currentNode];
  116. S32 object = node->firstObject;
  117. S32 child = node->firstChild;
  118. // copy off probabilities and update probability lists for next level
  119. F32 ps = probShatter ? *probShatter : 1.0f;
  120. F32 pb = probBreak ? *probBreak : 1.0f;
  121. if (probDepth>1 && probShatter && probBreak)
  122. {
  123. probShatter++;
  124. probBreak++;
  125. probDepth--;
  126. }
  127. // what to do...depending on how the die roll, we can:
  128. // a) shatter the shape at this level -- meaning we make a part out of each object on this node and
  129. // we make parts out of all the children (perhaps breaking them up further still)
  130. // b) break the shape off at this level -- meaning we make a part out of the intact piece from here
  131. // on down (again, we might break the result further as we iterate through the nodes...what breaking
  132. // the shape really does is separate this piece from the parent piece).
  133. // c) add this piece to the parent -- meaning all objects on this node are added to the parent, and children
  134. // are also added (but children will be recursively sent through this routine, so if a parent gets option
  135. // (c) and the child option (a) or (b), then the child will be ripped from the parents grasp. Cruel
  136. // people us coders are.
  137. // Note: (a) is the only way that two objects on the same node can be separated...that is why both
  138. // option a and option b are needed.
  139. if (!probShatter || smRandom.randF() < ps)
  140. {
  141. // option a -- shatter the shape at this level
  142. // iterate through the objects, make part out of each one
  143. while (object>=0)
  144. {
  145. partList.increment();
  146. partList.last() = new TSPartInstance(shape,object);
  147. object = shape->mShape->objects[object].nextSibling;
  148. }
  149. // iterate through the child nodes, call ourselves on each one with currentPart = NULL
  150. while (child>=0)
  151. {
  152. TSPartInstance::breakShape(shape,NULL,child,partList,probShatter,probBreak,probDepth);
  153. child = shape->mShape->nodes[child].nextSibling;
  154. }
  155. return;
  156. }
  157. if (!probBreak || smRandom.randF() < pb)
  158. // option b -- break the shape off at this level
  159. currentPart = NULL; // fall through to option C
  160. // option c -- add this piece to the parent
  161. if (!currentPart)
  162. {
  163. currentPart = new TSPartInstance(shape);
  164. partList.push_back(currentPart);
  165. }
  166. // iterate through objects, add to currentPart
  167. while (object>=0)
  168. {
  169. currentPart->addObject(object);
  170. object = shape->mShape->objects[object].nextSibling;
  171. }
  172. // iterate through child nodes, call ourselves on each one with currentPart as is
  173. while (child>=0)
  174. {
  175. TSPartInstance::breakShape(shape,currentPart,child,partList,probShatter,probBreak,probDepth);
  176. child = shape->mShape->nodes[child].nextSibling;
  177. }
  178. }
  179. //-------------------------------------------------------------------------------------
  180. // render methods -- we use TSShapeInstance code as much as possible
  181. // issues: setupTexturing expects a detail level, we give it an object detail level
  182. //-------------------------------------------------------------------------------------
  183. void TSPartInstance::render(S32 od, const TSRenderState &rdata)
  184. {
  185. S32 i;
  186. // render mesh objects
  187. for (i=0; i<mMeshObjects.size(); i++)
  188. {
  189. TSRenderState objState = rdata;
  190. const char *meshName = mSourceShape->mShape->names[mMeshObjects[i]->object->nameIndex];
  191. mMeshObjects[i]->render(od,mSourceShape->mShape->mShapeVertexBuffer,mSourceShape->getMaterialList(),objState,1.0, meshName);
  192. }
  193. }
  194. //-------------------------------------------------------------------------------------
  195. // Detail selection
  196. // 2 methods:
  197. // method 1: use source shapes detail levels...
  198. // method 2: pass in our own table...
  199. // In either case, you can compute the pixel size on your own or let open gl do it.
  200. // If you want to use method 2, you have to call setDetailData sometime before selecting detail
  201. //-------------------------------------------------------------------------------------
  202. void TSPartInstance::setDetailData(F32 * sizeCutoffs, S32 numDetails)
  203. {
  204. if (mSizeCutoffs == sizeCutoffs && mNumDetails==numDetails)
  205. return;
  206. mSizeCutoffs = sizeCutoffs;
  207. mNumDetails = numDetails;
  208. delete [] mPolyCount;
  209. mPolyCount = NULL;
  210. }
  211. /*
  212. void TSPartInstance::selectCurrentDetail(bool ignoreScale)
  213. {
  214. if (mSizeCutoffs)
  215. {
  216. selectCurrentDetail(mSizeCutoffs,mNumDetails,ignoreScale);
  217. return;
  218. }
  219. mSourceShape->selectCurrentDetail(ignoreScale);
  220. mCurrentObjectDetail = mSourceShape->getCurrentDetail();
  221. mCurrentIntraDL = mSourceShape->getCurrentIntraDetail();
  222. }
  223. void TSPartInstance::selectCurrentDetail(F32 pixelSize)
  224. {
  225. if (mSizeCutoffs)
  226. {
  227. selectCurrentDetail(pixelSize,mSizeCutoffs,mNumDetails);
  228. return;
  229. }
  230. mSourceShape->selectCurrentDetail(pixelSize);
  231. mCurrentObjectDetail = mSourceShape->getCurrentDetail();
  232. mCurrentIntraDL = mSourceShape->getCurrentIntraDetail();
  233. }
  234. void TSPartInstance::selectCurrentDetail(F32 dist, F32 invScale)
  235. {
  236. if (mSizeCutoffs)
  237. {
  238. const RectI &viewport = GFX->getViewport();
  239. F32 pixelScale = viewport.extent.x * 1.6f / 640.0f;
  240. F32 pixelSize = GFX->projectRadius(dist*invScale,mSourceShape->getShape()->radius) * pixelScale * TSShapeInstance::smDetailAdjust;
  241. selectCurrentDetail(pixelSize,mSizeCutoffs,mNumDetails);
  242. return;
  243. }
  244. mSourceShape->selectCurrentDetail(dist, invScale);
  245. mCurrentObjectDetail = mSourceShape->getCurrentDetail();
  246. mCurrentIntraDL = mSourceShape->getCurrentIntraDetail();
  247. }
  248. void TSPartInstance::selectCurrentDetail(F32 * sizeCutoffs, S32 numDetails, bool ignoreScale)
  249. {
  250. // compute pixel size
  251. Point3F p;
  252. MatrixF toCam = GFX->getWorldMatrix();
  253. toCam.mulP(mCenter,&p);
  254. F32 dist = mDot(p,p);
  255. F32 scale = 1.0f;
  256. if (!ignoreScale)
  257. {
  258. // any scale?
  259. Point3F x,y,z;
  260. toCam.getRow(0,&x);
  261. toCam.getRow(1,&y);
  262. toCam.getRow(2,&z);
  263. F32 scalex = mDot(x,x);
  264. F32 scaley = mDot(y,y);
  265. F32 scalez = mDot(z,z);
  266. scale = scalex;
  267. if (scaley > scale)
  268. scale = scaley;
  269. if (scalez > scale)
  270. scale = scalez;
  271. }
  272. dist /= scale;
  273. dist = mSqrt(dist);
  274. const RectI &viewport = GFX->getViewport();
  275. // JMQMERGE: is this using a hardcoded res/aspect ? (and the code above)
  276. F32 pixelScale = viewport.extent.x * 1.6f / 640.0f;
  277. F32 pixelRadius = GFX->projectRadius(dist,mRadius) * pixelScale * TSShapeInstance::smDetailAdjust;
  278. selectCurrentDetail(pixelRadius,sizeCutoffs,numDetails);
  279. }
  280. void TSPartInstance::selectCurrentDetail(F32 pixelSize, F32 * sizeCutoffs, S32 numDetails)
  281. {
  282. mCurrentObjectDetail = 0;
  283. while (numDetails)
  284. {
  285. if (pixelSize > *sizeCutoffs)
  286. return;
  287. mCurrentObjectDetail++;
  288. numDetails--;
  289. sizeCutoffs++;
  290. }
  291. mCurrentObjectDetail = -1;
  292. }
  293. */
  294. //-------------------------------------------------------------------------------------
  295. // Detail query methods...complicated because there are two ways that detail information
  296. // can be determined...1) using source shape, or 2) using mSizeCutoffs
  297. //-------------------------------------------------------------------------------------
  298. F32 TSPartInstance::getDetailSize(S32 dl) const
  299. {
  300. if (dl<0)
  301. return 0;
  302. else if (mSizeCutoffs && dl<mNumDetails)
  303. return mSizeCutoffs[dl];
  304. else if (!mSizeCutoffs && dl<=mSourceShape->getShape()->mSmallestVisibleDL)
  305. return mSourceShape->getShape()->details[dl].size;
  306. else return 0;
  307. }
  308. S32 TSPartInstance::getPolyCount(S32 dl)
  309. {
  310. if (!mPolyCount)
  311. computePolyCount();
  312. if (dl<0 || dl>=mNumDetails)
  313. return 0;
  314. else
  315. return mPolyCount[dl];
  316. }
  317. void TSPartInstance::computePolyCount()
  318. {
  319. if (!mSizeCutoffs)
  320. mNumDetails = mSourceShape->getShape()->mSmallestVisibleDL+1;
  321. delete [] mPolyCount;
  322. mPolyCount = new S32[mNumDetails];
  323. for (S32 i=0; i<mNumDetails; i++)
  324. {
  325. mPolyCount[i] = 0;
  326. for (S32 j=0; j<mMeshObjects.size(); j++)
  327. {
  328. if (mMeshObjects[j]->getMesh(i))
  329. mPolyCount[i] += mMeshObjects[j]->getMesh(i)->getNumPolys();
  330. }
  331. }
  332. }