tsPartInstance.cpp 13 KB

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