forceField.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 "platform/platform.h"
  23. #include "interior/forceField.h"
  24. #include "core/stream/stream.h"
  25. #include "math/mathIO.h"
  26. #include "console/console.h"
  27. #include "collision/abstractPolyList.h"
  28. #include "scene/sceneObject.h"
  29. ForceField::ForceField()
  30. {
  31. VECTOR_SET_ASSOCIATION( mTriggers );
  32. VECTOR_SET_ASSOCIATION( mPlanes );
  33. VECTOR_SET_ASSOCIATION( mPoints );
  34. VECTOR_SET_ASSOCIATION( mBSPNodes );
  35. VECTOR_SET_ASSOCIATION( mBSPSolidLeaves );
  36. VECTOR_SET_ASSOCIATION( mSolidLeafSurfaces );
  37. VECTOR_SET_ASSOCIATION( mWindings );
  38. VECTOR_SET_ASSOCIATION( mSurfaces );
  39. mPreppedForRender = false;
  40. }
  41. ForceField::~ForceField()
  42. {
  43. mPreppedForRender = false;
  44. }
  45. bool ForceField::prepForRendering()
  46. {
  47. if (mPreppedForRender == true)
  48. return true;
  49. mPreppedForRender = true;
  50. return true;
  51. }
  52. void ForceField::render(const ColorF& rColor, const F32 fade)
  53. {
  54. }
  55. //--------------------------------------------------------------------------
  56. //-------------------------------------- Persistence interfaces
  57. //
  58. const U32 ForceField::smFileVersion = 0;
  59. bool ForceField::read(Stream& stream)
  60. {
  61. AssertFatal(stream.hasCapability(Stream::StreamRead), "ForceField::read: non-read capable stream passed");
  62. AssertFatal(stream.getStatus() == Stream::Ok, "ForceField::read: Error, stream in inconsistent state");
  63. U32 i;
  64. // Version this stream
  65. U32 fileVersion;
  66. stream.read(&fileVersion);
  67. if (fileVersion != smFileVersion) {
  68. Con::errorf(ConsoleLogEntry::General, "ForceField::read: incompatible file version found.");
  69. return false;
  70. }
  71. mName = stream.readSTString();
  72. U32 numTriggers;
  73. stream.read(&numTriggers);
  74. mTriggers.setSize(numTriggers);
  75. for (i = 0; i < mTriggers.size(); i++)
  76. mTriggers[i] = stream.readSTString();
  77. // Geometry factors...
  78. mathRead(stream, &mBoundingBox);
  79. mathRead(stream, &mBoundingSphere);
  80. // Now read in our data vectors.
  81. U32 vectorSize;
  82. // mPlanes
  83. readPlaneVector(stream);
  84. // mPoints
  85. stream.read(&vectorSize);
  86. mPoints.setSize(vectorSize);
  87. for (i = 0; i < mPoints.size(); i++)
  88. mathRead(stream, &mPoints[i]);
  89. // mBSPNodes;
  90. stream.read(&vectorSize);
  91. mBSPNodes.setSize(vectorSize);
  92. for (i = 0; i < mBSPNodes.size(); i++) {
  93. stream.read(&mBSPNodes[i].planeIndex);
  94. stream.read(&mBSPNodes[i].frontIndex);
  95. stream.read(&mBSPNodes[i].backIndex);
  96. }
  97. // mBSPSolidLeaves
  98. stream.read(&vectorSize);
  99. mBSPSolidLeaves.setSize(vectorSize);
  100. for (i = 0; i < mBSPSolidLeaves.size(); i++) {
  101. stream.read(&mBSPSolidLeaves[i].surfaceIndex);
  102. stream.read(&mBSPSolidLeaves[i].surfaceCount);
  103. }
  104. // mWindings
  105. stream.read(&vectorSize);
  106. mWindings.setSize(vectorSize);
  107. for (i = 0; i < mWindings.size(); i++) {
  108. stream.read(&mWindings[i]);
  109. }
  110. // mSurfaces
  111. stream.read(&vectorSize);
  112. mSurfaces.setSize(vectorSize);
  113. for (i = 0; i < mSurfaces.size(); i++) {
  114. stream.read(&mSurfaces[i].windingStart);
  115. stream.read(&mSurfaces[i].windingCount);
  116. stream.read(&mSurfaces[i].planeIndex);
  117. stream.read(&mSurfaces[i].surfaceFlags);
  118. stream.read(&mSurfaces[i].fanMask);
  119. }
  120. // mSolidLeafSurfaces
  121. stream.read(&vectorSize);
  122. mSolidLeafSurfaces.setSize(vectorSize);
  123. for (i = 0; i < mSolidLeafSurfaces.size(); i++) {
  124. stream.read(&mSolidLeafSurfaces[i]);
  125. }
  126. stream.read(&mColor);
  127. return stream.getStatus() == Stream::Ok;
  128. }
  129. bool ForceField::write(Stream& stream) const
  130. {
  131. AssertFatal(stream.hasCapability(Stream::StreamWrite), "Interior::write: non-write capable stream passed");
  132. AssertFatal(stream.getStatus() == Stream::Ok, "Interior::write: Error, stream in inconsistent state");
  133. U32 i;
  134. // Version this stream
  135. stream.write(smFileVersion);
  136. stream.writeString(mName);
  137. stream.write(mTriggers.size());
  138. for (i = 0; i < mTriggers.size(); i++)
  139. stream.writeString(mTriggers[i]);
  140. mathWrite(stream, mBoundingBox);
  141. mathWrite(stream, mBoundingSphere);
  142. // Now write out our data vectors. Remember, for cross-platform capability, no
  143. // structure writing is allowed...
  144. // mPlanes
  145. writePlaneVector(stream);
  146. // mPoints
  147. stream.write(mPoints.size());
  148. for (i = 0; i < mPoints.size(); i++)
  149. mathWrite(stream, mPoints[i]);
  150. // mBSPNodes;
  151. stream.write(mBSPNodes.size());
  152. for (i = 0; i < mBSPNodes.size(); i++) {
  153. stream.write(mBSPNodes[i].planeIndex);
  154. stream.write(mBSPNodes[i].frontIndex);
  155. stream.write(mBSPNodes[i].backIndex);
  156. }
  157. // mBSPSolidLeaves
  158. stream.write(mBSPSolidLeaves.size());
  159. for (i = 0; i < mBSPSolidLeaves.size(); i++) {
  160. stream.write(mBSPSolidLeaves[i].surfaceIndex);
  161. stream.write(mBSPSolidLeaves[i].surfaceCount);
  162. }
  163. // mWindings
  164. stream.write(mWindings.size());
  165. for (i = 0; i < mWindings.size(); i++) {
  166. stream.write(mWindings[i]);
  167. }
  168. // mSurfaces
  169. stream.write(mSurfaces.size());
  170. for (i = 0; i < mSurfaces.size(); i++) {
  171. stream.write(mSurfaces[i].windingStart);
  172. stream.write(mSurfaces[i].windingCount);
  173. stream.write(mSurfaces[i].planeIndex);
  174. stream.write(mSurfaces[i].surfaceFlags);
  175. stream.write(mSurfaces[i].fanMask);
  176. }
  177. // mSolidLeafSurfaces
  178. stream.write(mSolidLeafSurfaces.size());
  179. for (i = 0; i < mSolidLeafSurfaces.size(); i++) {
  180. stream.write(mSolidLeafSurfaces[i]);
  181. }
  182. stream.write(mColor);
  183. return stream.getStatus() == Stream::Ok;
  184. }
  185. bool ForceField::writePlaneVector(Stream& stream) const
  186. {
  187. // This is pretty slow, but who cares?
  188. //
  189. Vector<Point3F> uniqueNormals(mPlanes.size());
  190. Vector<U16> uniqueIndices(mPlanes.size());
  191. U32 i;
  192. for (i = 0; i < mPlanes.size(); i++) {
  193. bool inserted = false;
  194. for (U32 j = 0; j < uniqueNormals.size(); j++) {
  195. if (mPlanes[i] == uniqueNormals[j]) {
  196. // Hah! Already have this one...
  197. uniqueIndices.push_back(j);
  198. inserted = true;
  199. break;
  200. }
  201. }
  202. if (inserted == false) {
  203. // Gotta do it ourselves...
  204. uniqueIndices.push_back(uniqueNormals.size());
  205. uniqueNormals.push_back(Point3F(mPlanes[i].x, mPlanes[i].y, mPlanes[i].z));
  206. }
  207. }
  208. // Ok, what we have now, is a list of unique normals, a set of indices into
  209. // that vector, and the distances that we still have to write out by hand.
  210. // Hop to it!
  211. stream.write(uniqueNormals.size());
  212. for (i = 0; i < uniqueNormals.size(); i++)
  213. mathWrite(stream, uniqueNormals[i]);
  214. stream.write(mPlanes.size());
  215. for (i = 0; i < mPlanes.size(); i++) {
  216. stream.write(uniqueIndices[i]);
  217. stream.write(mPlanes[i].d);
  218. }
  219. return (stream.getStatus() == Stream::Ok);
  220. }
  221. bool ForceField::readPlaneVector(Stream& stream)
  222. {
  223. Vector<Point3F> normals;
  224. U32 vectorSize;
  225. stream.read(&vectorSize);
  226. normals.setSize(vectorSize);
  227. U32 i;
  228. for (i = 0; i < normals.size(); i++)
  229. mathRead(stream, &normals[i]);
  230. U16 index;
  231. stream.read(&vectorSize);
  232. mPlanes.setSize(vectorSize);
  233. for (i = 0; i < mPlanes.size(); i++) {
  234. stream.read(&index);
  235. stream.read(&mPlanes[i].d);
  236. mPlanes[i].x = normals[index].x;
  237. mPlanes[i].y = normals[index].y;
  238. mPlanes[i].z = normals[index].z;
  239. }
  240. return (stream.getStatus() == Stream::Ok);
  241. }
  242. //--------------------------------------------------------------------------
  243. //-------------------------------------- Collision support. Essentially
  244. // copied from the interiorCollision
  245. //
  246. void ForceField::collisionFanFromSurface(const Surface& rSurface, U32* fanIndices, U32* numIndices) const
  247. {
  248. U32 tempIndices[32];
  249. tempIndices[0] = 0;
  250. U32 idx = 1;
  251. U32 i;
  252. for (i = 1; i < rSurface.windingCount; i += 2)
  253. tempIndices[idx++] = i;
  254. for (i = ((rSurface.windingCount - 1) & (~0x1)); i > 0; i -= 2)
  255. tempIndices[idx++] = i;
  256. idx = 0;
  257. for (i = 0; i < rSurface.windingCount; i++) {
  258. if (rSurface.fanMask & (1 << i)) {
  259. fanIndices[idx++] = mWindings[rSurface.windingStart + tempIndices[i]];
  260. }
  261. }
  262. *numIndices = idx;
  263. }
  264. bool ForceField::castRay(const Point3F& s, const Point3F& e, RayInfo* info)
  265. {
  266. bool hit = castRay_r(0, s, e, info);
  267. if (hit) {
  268. Point3F vec = e - s;
  269. F32 len = vec.len();
  270. vec /= len;
  271. info->t = mDot(info->point - s, vec) / len;
  272. }
  273. return hit;
  274. }
  275. bool ForceField::castRay_r(const U16 node,
  276. const Point3F& s,
  277. const Point3F& e,
  278. RayInfo* info)
  279. {
  280. if (isBSPLeafIndex(node) == false) {
  281. const IBSPNode& rNode = mBSPNodes[node];
  282. const PlaneF& rPlane = getPlane(rNode.planeIndex);
  283. PlaneF::Side sSide = rPlane.whichSide(s);
  284. PlaneF::Side eSide = rPlane.whichSide(e);
  285. switch (PlaneSwitchCode(sSide, eSide)) {
  286. case PlaneSwitchCode(PlaneF::Front, PlaneF::Front):
  287. case PlaneSwitchCode(PlaneF::Front, PlaneF::On):
  288. case PlaneSwitchCode(PlaneF::On, PlaneF::Front):
  289. return castRay_r(rNode.frontIndex, s, e, info);
  290. break;
  291. case PlaneSwitchCode(PlaneF::On, PlaneF::Back):
  292. case PlaneSwitchCode(PlaneF::Back, PlaneF::On):
  293. case PlaneSwitchCode(PlaneF::Back, PlaneF::Back):
  294. return castRay_r(rNode.backIndex, s, e, info);
  295. break;
  296. case PlaneSwitchCode(PlaneF::On, PlaneF::On):
  297. // Line lies on the plane
  298. if (isBSPLeafIndex(rNode.backIndex) == false) {
  299. if (castRay_r(rNode.backIndex, s, e, info))
  300. return true;
  301. }
  302. if (isBSPLeafIndex(rNode.frontIndex) == false) {
  303. if (castRay_r(rNode.frontIndex, s, e, info))
  304. return true;
  305. }
  306. return false;
  307. break;
  308. case PlaneSwitchCode(PlaneF::Front, PlaneF::Back): {
  309. Point3F ip;
  310. F32 intersectT = rPlane.intersect(s, e);
  311. AssertFatal(intersectT != PARALLEL_PLANE, "Error, this should never happen in this case!");
  312. ip.interpolate(s, e, intersectT);
  313. if (castRay_r(rNode.frontIndex, s, ip, info))
  314. return true;
  315. return castRay_r(rNode.backIndex, ip, e, info);
  316. }
  317. break;
  318. case PlaneSwitchCode(PlaneF::Back, PlaneF::Front): {
  319. Point3F ip;
  320. F32 intersectT = rPlane.intersect(s, e);
  321. AssertFatal(intersectT != PARALLEL_PLANE, "Error, this should never happen in this case!");
  322. ip.interpolate(s, e, intersectT);
  323. if (castRay_r(rNode.backIndex, s, ip, info))
  324. return true;
  325. return castRay_r(rNode.frontIndex, ip, e, info);
  326. }
  327. break;
  328. default:
  329. AssertFatal(false, "Misunderstood switchCode in ForceField::castRay_r");
  330. return false;
  331. }
  332. }
  333. if (isBSPSolidLeaf(node)) {
  334. // DMM: Set material info here
  335. info->point = s;
  336. return true;
  337. }
  338. return false;
  339. }
  340. void ForceField::buildPolyList_r(const U16 node, Vector<U16>& collPlanes, AbstractPolyList* list, SphereF& s)
  341. {
  342. if (isBSPLeafIndex(node) == false) {
  343. const IBSPNode& rNode = mBSPNodes[node];
  344. const PlaneF& rPlane = getPlane(rNode.planeIndex);
  345. F32 dist = rPlane.distToPlane(s.center);
  346. if (mFabs(dist) <= s.radius) {
  347. // Have to do both, and push the plane back on the list...
  348. collPlanes.push_back(rNode.planeIndex);
  349. buildPolyList_r(rNode.frontIndex, collPlanes, list, s);
  350. buildPolyList_r(rNode.backIndex, collPlanes, list, s);
  351. collPlanes.pop_back();
  352. } else if (dist > 0.0f) {
  353. buildPolyList_r(rNode.frontIndex, collPlanes, list, s);
  354. } else {
  355. buildPolyList_r(rNode.backIndex, collPlanes, list, s);
  356. }
  357. return;
  358. }
  359. if (isBSPSolidLeaf(node)) {
  360. const IBSPLeafSolid& rLeaf = mBSPSolidLeaves[getBSPSolidLeafIndex(node)];
  361. for (U32 i = 0; i < rLeaf.surfaceCount; i++) {
  362. U32 surfaceIndex = mSolidLeafSurfaces[rLeaf.surfaceIndex + i];
  363. const Surface& rSurface = mSurfaces[surfaceIndex];
  364. for (U32 j = 0; j < collPlanes.size(); j++) {
  365. if (areEqualPlanes(rSurface.planeIndex, collPlanes[j]) == true) {
  366. U32 fanVerts[32];
  367. U32 numVerts;
  368. collisionFanFromSurface(rSurface, fanVerts, &numVerts);
  369. // DMM: Material here
  370. list->begin(0, rSurface.planeIndex);
  371. U32 vertStart = list->addPoint(mPoints[fanVerts[0]]);
  372. list->vertex(vertStart);
  373. for (U32 k = 1; k < numVerts; k++) {
  374. list->addPoint(mPoints[fanVerts[k]]);
  375. list->vertex(vertStart + k);
  376. }
  377. list->plane(vertStart, vertStart + 1, vertStart + 2);
  378. list->end();
  379. break;
  380. }
  381. }
  382. }
  383. }
  384. }
  385. bool ForceField::buildPolyList(AbstractPolyList* list, SphereF& sphere)
  386. {
  387. Vector<U16> planes;
  388. buildPolyList_r(0, planes, list, sphere);
  389. AssertFatal(planes.size() == 0, "Error, unbalanced plane stack!");
  390. return !list->isEmpty();
  391. }