tsPartInstance.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. partList[i]->updateBounds();
  95. // Remove any parts parts with invalid box
  96. Box3F box = partList[i]->getBounds();
  97. if(!box.isValidBox() ){
  98. Con::warnf("TSPartInstance::breakShape - part created with invalid object box. Removing from list.");
  99. partList.erase(i);
  100. i--;
  101. }
  102. }else{
  103. partList.erase(i);
  104. i--;
  105. }
  106. }
  107. }
  108. void TSPartInstance::breakShape(TSShapeInstance * shape, TSPartInstance * currentPart, S32 currentNode, Vector<TSPartInstance*> & partList, F32 * probShatter, F32 * probBreak, S32 probDepth)
  109. {
  110. AssertFatal( !probDepth || (probShatter && probBreak),"TSPartInstance::breakShape: probabilities improperly specified.");
  111. const TSShape::Node * node = &shape->mShape->nodes[currentNode];
  112. S32 object = node->firstObject;
  113. S32 child = node->firstChild;
  114. // copy off probabilities and update probability lists for next level
  115. F32 ps = probShatter ? *probShatter : 1.0f;
  116. F32 pb = probBreak ? *probBreak : 1.0f;
  117. if (probDepth>1 && probShatter && probBreak)
  118. {
  119. probShatter++;
  120. probBreak++;
  121. probDepth--;
  122. }
  123. // what to do...depending on how the die roll, we can:
  124. // a) shatter the shape at this level -- meaning we make a part out of each object on this node and
  125. // we make parts out of all the children (perhaps breaking them up further still)
  126. // b) break the shape off at this level -- meaning we make a part out of the intact piece from here
  127. // on down (again, we might break the result further as we iterate through the nodes...what breaking
  128. // the shape really does is separate this piece from the parent piece).
  129. // c) add this piece to the parent -- meaning all objects on this node are added to the parent, and children
  130. // are also added (but children will be recursively sent through this routine, so if a parent gets option
  131. // (c) and the child option (a) or (b), then the child will be ripped from the parents grasp. Cruel
  132. // people us coders are.
  133. // Note: (a) is the only way that two objects on the same node can be separated...that is why both
  134. // option a and option b are needed.
  135. if (!probShatter || smRandom.randF() < ps)
  136. {
  137. // option a -- shatter the shape at this level
  138. // iterate through the objects, make part out of each one
  139. while (object>=0)
  140. {
  141. partList.increment();
  142. partList.last() = new TSPartInstance(shape,object);
  143. object = shape->mShape->objects[object].nextSibling;
  144. }
  145. // iterate through the child nodes, call ourselves on each one with currentPart = NULL
  146. while (child>=0)
  147. {
  148. TSPartInstance::breakShape(shape,NULL,child,partList,probShatter,probBreak,probDepth);
  149. child = shape->mShape->nodes[child].nextSibling;
  150. }
  151. return;
  152. }
  153. if (!probBreak || smRandom.randF() < pb)
  154. // option b -- break the shape off at this level
  155. currentPart = NULL; // fall through to option C
  156. // option c -- add this piece to the parent
  157. if (!currentPart)
  158. {
  159. currentPart = new TSPartInstance(shape);
  160. partList.push_back(currentPart);
  161. }
  162. // iterate through objects, add to currentPart
  163. while (object>=0)
  164. {
  165. currentPart->addObject(object);
  166. object = shape->mShape->objects[object].nextSibling;
  167. }
  168. // iterate through child nodes, call ourselves on each one with currentPart as is
  169. while (child>=0)
  170. {
  171. TSPartInstance::breakShape(shape,currentPart,child,partList,probShatter,probBreak,probDepth);
  172. child = shape->mShape->nodes[child].nextSibling;
  173. }
  174. }
  175. //-------------------------------------------------------------------------------------
  176. // render methods -- we use TSShapeInstance code as much as possible
  177. // issues: setupTexturing expects a detail level, we give it an object detail level
  178. //-------------------------------------------------------------------------------------
  179. void TSPartInstance::render(S32 od, const TSRenderState &rdata)
  180. {
  181. S32 i;
  182. // render mesh objects
  183. for (i=0; i<mMeshObjects.size(); i++)
  184. {
  185. TSRenderState objState = rdata;
  186. const char *meshName = mSourceShape->mShape->names[mMeshObjects[i]->object->nameIndex];
  187. mMeshObjects[i]->render(od,mSourceShape->mShape->mShapeVertexBuffer,mSourceShape->getMaterialList(),objState,1.0, meshName);
  188. }
  189. }
  190. //-------------------------------------------------------------------------------------
  191. // Detail selection
  192. // 2 methods:
  193. // method 1: use source shapes detail levels...
  194. // method 2: pass in our own table...
  195. // In either case, you can compute the pixel size on your own or let open gl do it.
  196. // If you want to use method 2, you have to call setDetailData sometime before selecting detail
  197. //-------------------------------------------------------------------------------------
  198. void TSPartInstance::setDetailData(F32 * sizeCutoffs, S32 numDetails)
  199. {
  200. if (mSizeCutoffs == sizeCutoffs && mNumDetails==numDetails)
  201. return;
  202. mSizeCutoffs = sizeCutoffs;
  203. mNumDetails = numDetails;
  204. delete [] mPolyCount;
  205. mPolyCount = NULL;
  206. }
  207. /*
  208. void TSPartInstance::selectCurrentDetail(bool ignoreScale)
  209. {
  210. if (mSizeCutoffs)
  211. {
  212. selectCurrentDetail(mSizeCutoffs,mNumDetails,ignoreScale);
  213. return;
  214. }
  215. mSourceShape->selectCurrentDetail(ignoreScale);
  216. mCurrentObjectDetail = mSourceShape->getCurrentDetail();
  217. mCurrentIntraDL = mSourceShape->getCurrentIntraDetail();
  218. }
  219. void TSPartInstance::selectCurrentDetail(F32 pixelSize)
  220. {
  221. if (mSizeCutoffs)
  222. {
  223. selectCurrentDetail(pixelSize,mSizeCutoffs,mNumDetails);
  224. return;
  225. }
  226. mSourceShape->selectCurrentDetail(pixelSize);
  227. mCurrentObjectDetail = mSourceShape->getCurrentDetail();
  228. mCurrentIntraDL = mSourceShape->getCurrentIntraDetail();
  229. }
  230. void TSPartInstance::selectCurrentDetail(F32 dist, F32 invScale)
  231. {
  232. if (mSizeCutoffs)
  233. {
  234. const RectI &viewport = GFX->getViewport();
  235. F32 pixelScale = viewport.extent.x * 1.6f / 640.0f;
  236. F32 pixelSize = GFX->projectRadius(dist*invScale,mSourceShape->getShape()->radius) * pixelScale * TSShapeInstance::smDetailAdjust;
  237. selectCurrentDetail(pixelSize,mSizeCutoffs,mNumDetails);
  238. return;
  239. }
  240. mSourceShape->selectCurrentDetail(dist, invScale);
  241. mCurrentObjectDetail = mSourceShape->getCurrentDetail();
  242. mCurrentIntraDL = mSourceShape->getCurrentIntraDetail();
  243. }
  244. void TSPartInstance::selectCurrentDetail(F32 * sizeCutoffs, S32 numDetails, bool ignoreScale)
  245. {
  246. // compute pixel size
  247. Point3F p;
  248. MatrixF toCam = GFX->getWorldMatrix();
  249. toCam.mulP(mCenter,&p);
  250. F32 dist = mDot(p,p);
  251. F32 scale = 1.0f;
  252. if (!ignoreScale)
  253. {
  254. // any scale?
  255. Point3F x,y,z;
  256. toCam.getRow(0,&x);
  257. toCam.getRow(1,&y);
  258. toCam.getRow(2,&z);
  259. F32 scalex = mDot(x,x);
  260. F32 scaley = mDot(y,y);
  261. F32 scalez = mDot(z,z);
  262. scale = scalex;
  263. if (scaley > scale)
  264. scale = scaley;
  265. if (scalez > scale)
  266. scale = scalez;
  267. }
  268. dist /= scale;
  269. dist = mSqrt(dist);
  270. const RectI &viewport = GFX->getViewport();
  271. // JMQMERGE: is this using a hardcoded res/aspect ? (and the code above)
  272. F32 pixelScale = viewport.extent.x * 1.6f / 640.0f;
  273. F32 pixelRadius = GFX->projectRadius(dist,mRadius) * pixelScale * TSShapeInstance::smDetailAdjust;
  274. selectCurrentDetail(pixelRadius,sizeCutoffs,numDetails);
  275. }
  276. void TSPartInstance::selectCurrentDetail(F32 pixelSize, F32 * sizeCutoffs, S32 numDetails)
  277. {
  278. mCurrentObjectDetail = 0;
  279. while (numDetails)
  280. {
  281. if (pixelSize > *sizeCutoffs)
  282. return;
  283. mCurrentObjectDetail++;
  284. numDetails--;
  285. sizeCutoffs++;
  286. }
  287. mCurrentObjectDetail = -1;
  288. }
  289. */
  290. //-------------------------------------------------------------------------------------
  291. // Detail query methods...complicated because there are two ways that detail information
  292. // can be determined...1) using source shape, or 2) using mSizeCutoffs
  293. //-------------------------------------------------------------------------------------
  294. F32 TSPartInstance::getDetailSize(S32 dl) const
  295. {
  296. if (dl<0)
  297. return 0;
  298. else if (mSizeCutoffs && dl<mNumDetails)
  299. return mSizeCutoffs[dl];
  300. else if (!mSizeCutoffs && dl<=mSourceShape->getShape()->mSmallestVisibleDL)
  301. return mSourceShape->getShape()->details[dl].size;
  302. else return 0;
  303. }
  304. S32 TSPartInstance::getPolyCount(S32 dl)
  305. {
  306. if (!mPolyCount)
  307. computePolyCount();
  308. if (dl<0 || dl>=mNumDetails)
  309. return 0;
  310. else
  311. return mPolyCount[dl];
  312. }
  313. void TSPartInstance::computePolyCount()
  314. {
  315. if (!mSizeCutoffs)
  316. mNumDetails = mSourceShape->getShape()->mSmallestVisibleDL+1;
  317. delete [] mPolyCount;
  318. mPolyCount = new S32[mNumDetails];
  319. for (S32 i=0; i<mNumDetails; i++)
  320. {
  321. mPolyCount[i] = 0;
  322. for (S32 j=0; j<mMeshObjects.size(); j++)
  323. {
  324. if (mMeshObjects[j]->getMesh(i))
  325. mPolyCount[i] += mMeshObjects[j]->getMesh(i)->getNumPolys();
  326. }
  327. }
  328. }