intersec.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : G *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/intersec.cpp $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 2/06/01 5:41p $*
  29. * *
  30. * $Revision:: 3 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "intersec.h"
  36. #include "camera.h"
  37. #include "scene.h"
  38. #include "intersec.inl"
  39. //////////////////////////////////////////////////////////////////////
  40. // Construction/Destruction
  41. //////////////////////////////////////////////////////////////////////
  42. // these statics are used for single-threaded use of the IntersectionClass ONLY
  43. Vector3 IntersectionClass::_RayLocation(0,0,0);
  44. Vector3 IntersectionClass::_RayDirection(0,0,0);
  45. Vector3 IntersectionClass::_IntersectionNormal(0,0,0);
  46. bool IntersectionClass::Intersect_Screen_Point_RenderObject(float screen_x, float screen_y, const LayerClass &Layer, RenderObjClass *RObj, IntersectionResultClass *FinalResult)
  47. {
  48. Get_Screen_Ray(screen_x, screen_y, Layer);
  49. return Intersect_RenderObject(RObj, FinalResult);
  50. }
  51. bool IntersectionClass::Intersect_RenderObject(RenderObjClass *RObj, IntersectionResultClass *FinalResult)
  52. {
  53. if(FinalResult == 0)
  54. FinalResult = &Result;
  55. return RObj->Intersect(this, FinalResult);
  56. }
  57. // iterate through the layers of a world, front to back, returning true if/when an intersection
  58. // with an object occurs.
  59. bool IntersectionClass::Intersect_Screen_Point_Layer_Range
  60. (
  61. float screen_x,
  62. float screen_y,
  63. const LayerClass &TopLayer,
  64. const LayerClass &BackLayer
  65. )
  66. {
  67. // intersect from front layer to back layers. An intersection with an object
  68. // in any layer is assumed to be in front of any potential intersections in layers
  69. // below it.
  70. // find the last layer in the list
  71. const LayerClass *Layer = &TopLayer;
  72. // iterate through all layers in list
  73. while(Layer->Is_Valid()) {
  74. if(Intersect_Screen_Point_Layer(screen_x, screen_y, *Layer))
  75. return true;
  76. // if this is the back layer then that is all we need to test
  77. if(Layer == &BackLayer)
  78. return false;
  79. Layer = Layer->Next();
  80. }
  81. return false;
  82. }
  83. bool IntersectionClass::Intersect_Screen_Point_Layer(float screen_x, float screen_y, const LayerClass &Layer)
  84. {
  85. // mark this object as not intersecting yet
  86. Result.Intersects = false;
  87. // first, do a test to make sure the screen coords are within the rendering area for this layer.
  88. const ViewportClass &v = Layer.Camera->Get_Viewport();
  89. if((screen_x < v.Min.X) ||
  90. (screen_x > v.Max.X) ||
  91. (screen_y < v.Min.Y) ||
  92. (screen_y > v.Max.Y))
  93. return false;
  94. Result.Range = Layer.Camera->Get_Depth(); //scene->depth * scene->zstop;
  95. // get the ray for these screen coordinates
  96. Get_Screen_Ray(screen_x, screen_y, Layer);
  97. return Intersect_Layer(Layer, false);
  98. }
  99. bool IntersectionClass::Intersect_Layer(const LayerClass &Layer, bool Test_All)
  100. {
  101. IntersectionResultClass FinalResult;
  102. Result.Intersects = false;
  103. SceneIterator *it = Layer.Scene->Create_Iterator(!Test_All);
  104. // select the first object
  105. it->First();
  106. // loop through all render objects in this layer:
  107. while(!it->Is_Done()) {
  108. // get the render object
  109. RenderObjClass * robj = it->Current_Item();
  110. it->Next();
  111. // only intersect if it was visible or if we must test all in layer
  112. // Added 'Generals' code to only detect intersection on matching Collision_Type. MW
  113. if( robj->Get_Collision_Type() & Result.CollisionType && (Test_All || robj->Is_Really_Visible()) && robj->Intersect(this, &FinalResult)) {
  114. if(FinalResult.Range < Result.Range) {
  115. Copy_Results(&FinalResult);
  116. }
  117. }
  118. }
  119. Layer.Scene->Destroy_Iterator(it);
  120. return Result.Intersects;
  121. }
  122. void IntersectionClass::Append_Object_Array(
  123. int MaxCount,
  124. int &CurrentCount,
  125. RenderObjClass **ObjectArray,
  126. RenderObjClass *Object)
  127. {
  128. if(CurrentCount < MaxCount) {
  129. ObjectArray[CurrentCount] = Object;
  130. CurrentCount++;
  131. return;
  132. }
  133. WWDEBUG_SAY(("IntersectionClass::Append_Object_Array - Too many objects\n"));
  134. }
  135. // determines if specified plane-intersection point (co-planar with polygon) is within the the passed polygon.
  136. // If Interpolated_Normal is specified, it will interpolate the normal for the intersection point
  137. // note: Polygon normal MUST BE CORRECT
  138. // this will return true if the ray intersects the specified box
  139. // sets the point of intersection within the Request->Result.Intersection vector
  140. bool IntersectionClass::Intersect_Box(Vector3 &Box_Min, Vector3 &Box_Max, IntersectionResultClass *FinalResult) {
  141. // Fast Ray-Box Intersection, modified from code written by Andrew Woo from "Graphics Gems", Academic Press, 1990
  142. enum {
  143. RIGHT = 0,
  144. LEFT,
  145. MIDDLE,
  146. PLANE_COUNT
  147. };
  148. bool inside = true;
  149. char quadrant[PLANE_COUNT];
  150. int counter;
  151. float distance[PLANE_COUNT];
  152. float candidate_plane[PLANE_COUNT];
  153. register Vector3 *intersection = &FinalResult->Intersection;
  154. // Find candidate planes and determine if the ray is outside the box
  155. for (counter = 0; counter < PLANE_COUNT; counter++) {
  156. if((*RayLocation)[counter] < Box_Min[counter]) {
  157. quadrant[counter] = LEFT;
  158. candidate_plane[counter] = Box_Min[counter];
  159. inside = false;
  160. } else {
  161. if ((*RayLocation)[counter] > Box_Max[counter]) {
  162. quadrant[counter] = RIGHT;
  163. candidate_plane[counter] = Box_Max[counter];
  164. inside = false;
  165. } else {
  166. quadrant[counter] = MIDDLE;
  167. }
  168. }
  169. }
  170. // check to see if the ray origin is inside bounding box
  171. if(inside) {
  172. *intersection = *RayLocation;
  173. return FinalResult->Intersects = true;
  174. }
  175. // Calculate distances to candidate planes
  176. for (counter = 0; counter < PLANE_COUNT; counter++) {
  177. if ((quadrant[counter] != MIDDLE) && ((*RayDirection)[counter] != 0.0f))
  178. distance[counter] = (candidate_plane[counter] - (*RayLocation)[counter]) / (*RayDirection)[counter];
  179. else
  180. distance[counter] = -1.0f;
  181. }
  182. // get the largest of the distances for final choice of intersection
  183. int nearest_plane = 0;
  184. for (counter = 1; counter < PLANE_COUNT; counter++) {
  185. if (distance[nearest_plane] < distance[counter])
  186. nearest_plane = counter;
  187. }
  188. // Check to make sure the nearest plane is not behind the ray (inside box tested above)
  189. if (distance[nearest_plane] < 0.0f)
  190. return FinalResult->Intersects = false;
  191. for (counter = 0; counter < PLANE_COUNT; counter++) {
  192. if (nearest_plane != counter) {
  193. (*intersection)[counter] = (*RayLocation)[counter] + distance[nearest_plane] *(*RayDirection)[counter];
  194. if ((*intersection)[counter] < Box_Min[counter] || (*intersection)[counter] > Box_Max[counter])
  195. return FinalResult->Intersects = false;
  196. } else {
  197. (*intersection)[counter] = candidate_plane[counter];
  198. }
  199. }
  200. return FinalResult->Intersects = true; // ray hits box
  201. }
  202. // simply returns true if a ray hits the bounding sphere of any node in a hierarchy
  203. // note: Result will only contain range, not the intersection point/normal.
  204. bool IntersectionClass::Intersect_Hierarchy_Sphere_Quick(RenderObjClass *Hierarchy, IntersectionResultClass *FinalResult)
  205. {
  206. int counter = Hierarchy->Get_Num_Sub_Objects();
  207. while(counter--) {
  208. RenderObjClass *obj = Hierarchy->Get_Sub_Object(counter);
  209. obj->Release_Ref(); // you already own a reference to this object indirectly..
  210. if(obj->Intersect_Sphere_Quick(this, FinalResult))
  211. return true;
  212. }
  213. return false;
  214. }
  215. // returns true if a ray hits the bounding sphere of any node in a hierarchy
  216. // note: Result will contain range and the intersection point/normal.
  217. bool IntersectionClass::Intersect_Hierarchy_Sphere(RenderObjClass *Hierarchy, IntersectionResultClass *FinalResult) {
  218. int counter = Hierarchy->Get_Num_Sub_Objects();
  219. while(counter--) {
  220. RenderObjClass *obj = Hierarchy->Get_Sub_Object(counter);
  221. obj->Release_Ref(); // you already own a reference to this object indirectly..
  222. if(obj->Intersect_Sphere(this, FinalResult))
  223. return true;
  224. }
  225. return false;
  226. }
  227. void IntersectionClass::Append_Hierarchy_Objects(
  228. int MaxCount,
  229. int &CurrentCount,
  230. RenderObjClass **ObjectArray,
  231. RenderObjClass *Hierarchy,
  232. bool Test_Bounding_Sphere,
  233. bool Convex)
  234. {
  235. IntersectionResultClass result;
  236. // first check the bounding spheres for hits (if specified)
  237. int counter = Hierarchy->Get_Num_Sub_Objects();
  238. if(Test_Bounding_Sphere) {
  239. while(counter--) {
  240. RenderObjClass *obj = Hierarchy->Get_Sub_Object(counter);
  241. obj->Release_Ref(); // you already own a reference to the object indirectly
  242. if(obj->Intersect_Sphere_Quick(this, &result)) {
  243. Append_Object_Array(MaxCount, CurrentCount, ObjectArray, obj);
  244. // OutputDebugString("o"); // this shows one o per sphere intersection
  245. } else {
  246. // OutputDebugString("."); // this shows one . per sphere miss
  247. }
  248. }
  249. } else {
  250. // simply copy the pointers into the array
  251. while(counter--) {
  252. RenderObjClass *obj = Hierarchy->Get_Sub_Object(counter);
  253. Append_Object_Array(MaxCount, CurrentCount, ObjectArray, obj);
  254. obj->Release_Ref(); // you already own a reference to this object indirectly..
  255. }
  256. }
  257. }
  258. bool IntersectionClass::Intersect_Hierarchy(RenderObjClass *Hierarchy, IntersectionResultClass *FinalResult, bool Test_Bounding_Sphere, bool Convex ) {
  259. // OutputDebugString("\n");
  260. // return FinalResult->Intersects = false;
  261. RenderObjClass *candidate_objects[MAX_HIERARCHY_NODE_COUNT];
  262. int candidate_count = 0;
  263. Append_Hierarchy_Objects(MAX_HIERARCHY_NODE_COUNT, candidate_count, candidate_objects, Hierarchy, Test_Bounding_Sphere, Convex);
  264. // make sure there's at least one sphere hit before continuing to more expensive tests below..
  265. if(candidate_count == 0) {
  266. // OutputDebugString("/"); // no sphere intersections
  267. return FinalResult->Intersects = false;
  268. }
  269. // note: Test_Bounding_Sphere argument is false because the Append_Hierarchy_Objects will have
  270. // already performed that test if indicated.
  271. if(Intersect_Object_Array(candidate_count, candidate_objects, FinalResult, false, Convex)) {
  272. return true;
  273. }
  274. return false;
  275. }
  276. RenderObjClass *IntersectionClass::Intersect_Sub_Object(float screenx, float screeny, LayerClass &layer, RenderObjClass *robj, IntersectionResultClass *result)
  277. {
  278. if (robj->Get_Num_Sub_Objects()) {
  279. for (int lp = 0; lp < robj->Get_Num_Sub_Objects(); lp++) {
  280. RenderObjClass *sub = robj->Get_Sub_Object(lp);
  281. RenderObjClass *retval = Intersect_Sub_Object(screenx, screeny, layer, sub, result);
  282. sub->Release_Ref();
  283. if (retval) return retval;
  284. }
  285. }
  286. if (Intersect_Screen_Point_RenderObject(screenx, screeny, layer, robj, result)) {
  287. return robj;
  288. }
  289. return NULL;
  290. }
  291. // finds the intersection of the nearest object in the array.
  292. // This will usually be the last stage after determining potential intersections
  293. // using Intersect_Sphere_Quick() and adding hits to the array for this
  294. // more accurate test, as done in Intersect_Heirarchy().
  295. bool IntersectionClass::Intersect_Object_Array(
  296. int Object_Count,
  297. RenderObjClass **ObjectArray,
  298. IntersectionResultClass *FinalResult,
  299. bool Test_Bounding_Sphere,
  300. bool Convex
  301. )
  302. {
  303. IntersectionResultClass TemporaryResults[MAX_HIERARCHY_NODE_COUNT];
  304. assert(Object_Count <= MAX_HIERARCHY_NODE_COUNT);
  305. return Intersect_Object_Array(Object_Count, ObjectArray, FinalResult, TemporaryResults, Test_Bounding_Sphere, Convex);
  306. }
  307. bool IntersectionClass::Intersect_Object_Array(
  308. int Object_Count,
  309. RenderObjClass **ObjectArray,
  310. IntersectionResultClass *FinalResult,
  311. IntersectionResultClass *TemporaryResults,
  312. bool Test_Bounding_Sphere,
  313. bool Convex
  314. )
  315. {
  316. // Determine ranges for all intersections
  317. IntersectionClass temp(this);
  318. int counter = Object_Count;
  319. bool hit = false;
  320. // if it's a convex hierarchy (ie a control panel) then find the first hit otherwise use the more expensive exact intersection routine
  321. // for use with potentially concave hierarchies.
  322. int nearest_index = -1;
  323. if(ConvexTest || Convex) {
  324. if(Test_Bounding_Sphere) {
  325. while(counter--) {
  326. if(ObjectArray[counter]->Intersect_Sphere_Quick(this, &TemporaryResults[counter])) {
  327. hit = ObjectArray[counter]->Intersect(this, FinalResult);
  328. }
  329. if(hit) {
  330. nearest_index = counter;
  331. counter = 0;
  332. }
  333. }
  334. } else {
  335. while(counter--) {
  336. hit = ObjectArray[counter]->Intersect(this, FinalResult);
  337. if(hit) {
  338. nearest_index = counter;
  339. counter = 0;
  340. }
  341. }
  342. }
  343. } else {
  344. if(Test_Bounding_Sphere) {
  345. while(counter--) {
  346. if(ObjectArray[counter]->Intersect_Sphere_Quick(this, &TemporaryResults[counter])) {
  347. hit |= ObjectArray[counter]->Intersect(this, &TemporaryResults[counter]);
  348. }
  349. }
  350. } else {
  351. while(counter--) {
  352. hit |= ObjectArray[counter]->Intersect(this, &TemporaryResults[counter]);
  353. }
  354. }
  355. }
  356. // test to see if anything actually hit a mesh
  357. if( ! hit ) {
  358. // OutputDebugString("!"); // no mesh intersections
  359. return FinalResult->Intersects = false;
  360. }
  361. if(! (Convex || ConvexTest)) {
  362. // now find the nearest of the actual hits
  363. float nearest_range = (float) (2<<28);
  364. counter = Object_Count;
  365. while(counter--) {
  366. if(TemporaryResults[counter].Intersects && (nearest_range > TemporaryResults[counter].Range)) {
  367. nearest_index = counter;
  368. nearest_range = TemporaryResults[counter].Range;
  369. }
  370. }
  371. Copy_Results(FinalResult, &TemporaryResults[nearest_index]);
  372. }
  373. // OutputDebugString("+");
  374. // Debug.Print("Mesh ", Object_Array[nearest_index]);
  375. // Intersection_Node = candidate_indices[nearest_index];
  376. return true;
  377. }