intersec.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. ** Command & Conquer Renegade(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.h $*
  25. * *
  26. * $Author:: Naty_h $*
  27. * *
  28. * $Modtime:: 3/28/01 11:29a $*
  29. * *
  30. * $Revision:: 5 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. /*
  36. Most of the functions of this class are inline functions and if you use them you will need
  37. to include <intersec.inl>.
  38. Exceptions to this are the functions most commonly used by render objects:
  39. The constructors/destructors are inline & implemented here.
  40. */
  41. #if defined(_MSC_VER)
  42. #pragma once
  43. #endif
  44. #ifndef INTERSEC_H
  45. #define INTERSEC_H
  46. #include "always.h"
  47. #include "matrix3d.h"
  48. #include "layer.h"
  49. #include "sphere.h"
  50. class RenderObjClass;
  51. typedef unsigned short POLYGONINDEX;
  52. /*
  53. **
  54. */
  55. class IntersectionResultClass
  56. {
  57. public:
  58. RenderObjClass * IntersectedRenderObject;
  59. POLYGONINDEX IntersectedPolygon;
  60. Matrix3D ModelMatrix; // this is required for transforming mesh normals from model coords
  61. Vector3 ModelLocation; // ditto
  62. Vector3 Intersection; // location of intersection is placed here
  63. float Range; // distance from ray to point of intersection
  64. float Alpha, Beta; // stored for use by Process_Intersection_Results() - used to interpolate normal across a polygon
  65. bool Intersects; // could be in the intersection_type, but it is used in a number of bool operations
  66. enum INTERSECTION_TYPE {
  67. NONE = 0,
  68. GENERIC,
  69. POLYGON
  70. } IntersectionType;
  71. };
  72. class IntersectionClass
  73. {
  74. // member data
  75. public:
  76. enum {
  77. MAX_POLY_INTERSECTION_COUNT = 1000, // arbitrary size of stack array used for storing intersection results within Intersect_Mesh(). Non-recursive function.
  78. MAX_HIERARCHY_NODE_COUNT = 256
  79. };
  80. // this structure is used to store all intersections on the stack for sorting before
  81. // copying the nearest intersection to IntersectionClassObject->Result.
  82. // note: the convex intersection functions return with the first intersection results.
  83. Vector3 *RayLocation; // note: these pointers must be Set() to valid pointers by external code
  84. Vector3 *RayDirection;
  85. Vector3 *IntersectionNormal; // if non-zero then Process_Intersection_Results() will place the (perhaps vertex interpolated) surface normal here.
  86. // 2d screen coordinates for use by Intersect_Screen_Point...() routines.
  87. float ScreenX, ScreenY;
  88. // if true, then interpolate the normal for a polygon intersection from the vertex normals.
  89. // note: intersection routines below which take a FinalResult argument do not interpolate
  90. // the normal since they are intended to be used to find the nearest of several intersections
  91. // and interpolating the normal for intersections that are tossed would be wasteful.
  92. // If you need the normal interpolated in that case anyways then call Interpolate_Normal().
  93. bool InterpolateNormal;
  94. // if true, then perform potentially much faster convex intersection test which will return
  95. // the first valid intersection. Generally used for producing silhouettes.
  96. // If true, all intersections will be convex tests. If false, test mode will be determined (generally)
  97. // by the render object or whatever called the intersection functions and passes the Convex argument.
  98. bool ConvexTest;
  99. // do not consider intersections beyond this range as an intersection.
  100. // Note: Get_Screen_Ray sets this to scene->zstop.
  101. float MaxDistance;
  102. // final intersection results are contained here
  103. IntersectionResultClass Result;
  104. //
  105. // Implementation
  106. //
  107. // configures the member data to use the passed pointers
  108. inline void Set(Vector3 *location, Vector3 *direction, Vector3 *intersection_normal, bool interpolate_normal, float max_distance, bool convex_test = false)
  109. {
  110. RayLocation = location;
  111. RayDirection = direction;
  112. IntersectionNormal = intersection_normal;
  113. InterpolateNormal = interpolate_normal;
  114. MaxDistance = max_distance;
  115. ConvexTest = convex_test;
  116. }
  117. // this constructor uses static variables for the location/direction/normal variables
  118. // so can be only used one thread at a time unless the Set() function is used to
  119. // set them to private vector3's
  120. inline IntersectionClass()
  121. : ConvexTest(false)
  122. {
  123. RayLocation = &_RayLocation;
  124. RayDirection = &_RayDirection;
  125. IntersectionNormal = &_IntersectionNormal;
  126. }
  127. // This will be the most commonly used constructor
  128. inline IntersectionClass(Vector3 *location, Vector3 *direction, Vector3 *intersection_normal, bool interpolate_normal = false, float max_distance = WWMATH_FLOAT_MAX, bool convex_test = false)
  129. {
  130. Set(location, direction, intersection_normal, interpolate_normal, max_distance, convex_test);
  131. }
  132. virtual ~IntersectionClass() {}
  133. // this copy routine is used when the model coords are needed to be copied along with the other information.
  134. inline void IntersectionClass::Copy_Results(IntersectionResultClass *Destination, IntersectionResultClass *Source) {
  135. Destination->ModelMatrix = Source->ModelMatrix;
  136. Destination->ModelLocation = Source->ModelLocation;
  137. Copy_Partial_Results(Destination, Source);
  138. Destination->IntersectedRenderObject = Source->IntersectedRenderObject;
  139. }
  140. inline void IntersectionClass::Copy_Results(IntersectionResultClass *Source) {
  141. Copy_Results(&Result, Source);
  142. }
  143. // this is called only for the nearest intersection. If the request passes a Interpolated_Normal pointer then it will be calculated.
  144. // otherwise the results are copied into the request structure.
  145. // This does not copy the matrix or location members; it is intended to be used during poly testing
  146. // where these values are identical between results, or as a completion function for Copy_Results()
  147. inline void IntersectionClass::Copy_Partial_Results(IntersectionResultClass *Destination, IntersectionResultClass *Source)
  148. {
  149. Destination->IntersectedPolygon = Source->IntersectedPolygon;
  150. Destination->Intersection = Source->Intersection;
  151. Destination->Range = Source->Range;
  152. Destination->Alpha = Source->Alpha;
  153. Destination->Beta = Source->Beta;
  154. Destination->Intersects = true;
  155. Destination->IntersectionType = Source->IntersectionType;
  156. }
  157. // used for creating temporary copies
  158. inline IntersectionClass(IntersectionClass *source)
  159. {
  160. *this = source;
  161. }
  162. inline IntersectionClass *operator =(IntersectionClass *source)
  163. {
  164. Set(source->RayLocation, source->RayDirection, source->IntersectionNormal, source->InterpolateNormal, source->MaxDistance, source->ConvexTest);
  165. Copy_Results(&source->Result);
  166. return this;
  167. }
  168. // find the range to the intersection of the ray and sphere (if any)
  169. // note: Intersection_Request->RayDirection must be a unit vector
  170. // To find the actual intersection location and perhaps the intersection normal, use Intersection_Sphere() instead.
  171. // loosly based on code found in Graphics Gems I, p388
  172. // this will only set the result's range if intersection occurs; it is intended to be used as a first pass intersection test
  173. // before intersecting the mesh polygons itself.
  174. // Note: Does NOT do Max_Distance testing
  175. inline bool IntersectionClass::Intersect_Sphere_Quick(SphereClass &Sphere, IntersectionResultClass *FinalResult)
  176. {
  177. // make a unit vector from the ray origin to the sphere center
  178. Vector3 sphere_vector(Sphere.Center - *RayLocation);
  179. // get the dot product between the sphere_vector and the ray vector
  180. FinalResult->Alpha = Vector3::Dot_Product(sphere_vector, *RayDirection);
  181. FinalResult->Beta = Sphere.Radius * Sphere.Radius - (Vector3::Dot_Product(sphere_vector, sphere_vector) - FinalResult->Alpha * FinalResult->Alpha);
  182. if(FinalResult->Beta < 0.0f) {
  183. return FinalResult->Intersects = false;
  184. }
  185. return FinalResult->Intersects = true;
  186. }
  187. // this will find the intersection with the sphere and the intersection normal if needed.
  188. inline bool IntersectionClass::Intersect_Sphere(SphereClass &Sphere, IntersectionResultClass *FinalResult)
  189. {
  190. if(!Intersect_Sphere_Quick(Sphere, FinalResult))
  191. return false;
  192. // determine range to intersection based on stored alpha/beta values
  193. float d = sqrtf(FinalResult->Beta);
  194. FinalResult->Range = FinalResult->Alpha - d;
  195. if(FinalResult->Range > MaxDistance) return false;
  196. FinalResult->Intersection = *RayLocation + FinalResult->Range * (*RayDirection);
  197. if(IntersectionNormal != 0) {
  198. (*IntersectionNormal) = FinalResult->Intersection - Sphere.Center;
  199. }
  200. return true;
  201. }
  202. // inline declarations
  203. // Usage of these functions requires including intersec.inl
  204. // determine location & direction for projected screen coordinate ray
  205. inline void Get_Screen_Ray(float ScreenX, float ScreenY, const LayerClass &Layer);
  206. // uses the Result's range & the Ray_Direction to calculate the actual point of intersection.
  207. inline void Calculate_Intersection(IntersectionResultClass *Result);
  208. // interpolate the normal for a polygon intersection. Will ONLY work for polygon intersections,
  209. // and the Results.Intersection_Data must refer to a polygon with a valid ->mesh pointer.
  210. inline void Interpolate_Intersection_Normal(IntersectionResultClass *FinalResult);
  211. // various methods for performing intersections.
  212. inline bool Intersect_Plane(IntersectionResultClass *Result, Vector3 &Plane_Normal, Vector3 &Plane_Point);
  213. inline bool Intersect_Plane_Quick(IntersectionResultClass *Result, Vector3 &Plane_Normal, Vector3 &Plane_Point);
  214. inline bool Intersect_Polygon(IntersectionResultClass *Result, Vector3 &PolygonNormal, Vector3 &v1, Vector3 &v2, Vector3 &v3);
  215. inline bool Intersect_Polygon(IntersectionResultClass *Result, Vector3 &v1, Vector3 &v2, Vector3 &v3);
  216. inline bool Intersect_Polygon_Z(IntersectionResultClass *Result, Vector3 &PolygonNormal, Vector3 &v1, Vector3 &v2, Vector3 &v3);
  217. /*
  218. ** This function will fill the passed array with the set of points & uv values that represent
  219. ** the boolean operation of the anding of the ClipPoints with the TrianglePoints. The UV values
  220. ** provided for the TrianglePoints triangle are used to generate accurate UV values for any
  221. ** new points created by this operation.
  222. ** The clipped points have Z values that make them sit on the ClipPoints triangle plane.
  223. */
  224. static inline int _Intersect_Triangles_Z(
  225. Vector3 ClipPoints[3],
  226. Vector3 TrianglePoints[3],
  227. Vector2 UV[3],
  228. Vector3 ClippedPoints[6],
  229. Vector2 ClippedUV[6]
  230. );
  231. /*
  232. ** This function will find the z elevation for the passed Vector3 whose x/y components
  233. ** are defined, using the specified vertex & surface normal to determine the correct value
  234. */
  235. static inline float _Get_Z_Elevation(Vector3 &Point, Vector3 &PlanePoint, Vector3 &PlaneNormal);
  236. // test a 2d screen area with the intersection's screen coords, assigning a GENERIC intersection
  237. // to the specified object.
  238. inline bool Intersect_Screen_Object(IntersectionResultClass *Final_Result, Vector4 &Area, RenderObjClass *obj = 0);
  239. // non-inlined declarations
  240. // accumulates an object array for passing into Intersect_ObjectArray
  241. void Append_Object_Array(int MaxCount, int &CurrentCount, RenderObjClass **ObjectArray, RenderObjClass *Object);
  242. // traverses an RenderObjClass object and adds it's subobjects, potentially performing
  243. // a quick sphere intersection test before adding.
  244. void Append_Hierarchy_Objects(int MaxCount, int &CurrentCount, RenderObjClass **ObjectArray, RenderObjClass *Heirarchy, bool Test_Bounding_Spheres, bool Convex);
  245. // top level intersection routines, most store intersection results in the Intersection.Result
  246. // member structure and perform normal interpolation as a final step if indicated in the member data.
  247. bool Intersect_Object_Array(int ObjectCount, RenderObjClass **ObjectArray,IntersectionResultClass *FinalResult, bool Test_Bounding_Sphere, bool Convex);
  248. bool Intersect_Object_Array(int ObjectCount, RenderObjClass **ObjectArray,IntersectionResultClass *FinalResult, IntersectionResultClass *TemporaryResults, bool Test_Bounding_Sphere, bool Convex);
  249. bool Intersect_RenderObject(RenderObjClass *RObj, IntersectionResultClass *FinalResult = 0);
  250. bool Intersect_Screen_Point_RenderObject(float screen_x, float screen_y, const LayerClass &Layer, RenderObjClass *RObj, IntersectionResultClass *FinalResult);
  251. bool Intersect_Screen_Point_Layer_Range(float ScreenX, float ScreenY, const LayerClass &TopLayer, const LayerClass &BackLayer);
  252. bool Intersect_Screen_Point_Layer(float ScreenX, float ScreenY, const LayerClass &Layer);
  253. bool Intersect_Layer(const LayerClass &Layer, bool Test_All = true);
  254. // the various intersection routines, all of which store their intersection results
  255. // in the passed Intersection_Result strucuture.
  256. bool Intersect_Box(Vector3 &Box_Min, Vector3 &Box_Max, IntersectionResultClass *FinalResult);
  257. bool Intersect_Hierarchy(RenderObjClass *Hierarchy, IntersectionResultClass *FinalResult, bool Test_Bounding_Sphere = true, bool Convex = false);
  258. bool Intersect_Hierarchy_Sphere(RenderObjClass *Hierarchy, IntersectionResultClass *FinalResult);
  259. bool Intersect_Hierarchy_Sphere_Quick(RenderObjClass *Hierarchy, IntersectionResultClass *FinalResult);
  260. /*
  261. ** Identifies exactly what sub object of a render object is under the screen space vector
  262. */
  263. RenderObjClass *Intersect_Sub_Object(float screenx, float screeny, LayerClass &layer, RenderObjClass *robj, IntersectionResultClass *result);
  264. /*
  265. ** Functions related to determining if a 3d point is within a triangle.
  266. */
  267. static inline void _Find_Polygon_Dominant_Plane(Vector3 &Normal, int &Axis_1, int &Axis_2);
  268. static inline int _Largest_Normal_Index(Vector3 &v);
  269. static inline bool _Point_In_Polygon(IntersectionResultClass *FinalResult, Vector3 &Normal, Vector3 &loc1, Vector3 &loc2, Vector3 &loc3);
  270. static inline bool _Point_In_Polygon(IntersectionResultClass *FinalResult, Vector3 &loc1, Vector3 &loc2, Vector3 &loc3, int axis_1, int axis_2);
  271. static inline bool _Point_In_Polygon(Vector3 &Point, Vector3 &loc1, Vector3 &loc2, Vector3 &loc3, int axis_1, int axis_2,float &Alpha,float &Beta);
  272. static inline bool _Point_In_Polygon_Z(Vector3 &Point, Vector3 Corners[3]);
  273. static inline bool _Point_In_Polygon_Z(Vector3 &Point, Vector3 &Corner1, Vector3 &Corner2, Vector3 &Corner3);
  274. protected:
  275. /*
  276. ** Find the intersection between two lines and interpolate the UV values for the intersection.
  277. ** Designed for use with _Intersect_Triangles_Z.
  278. */
  279. //static inline void _Intersect_Lines_Z(Vector3 &A, Vector3 &B, Vector2 &UVStart, Vector2 &UVEnd, Vector3 &C, Vector3 &D, Vector3 ClippedPoints[6], Vector2 ClippedUV[6], int &DestIndex);
  280. static inline bool IntersectionClass::In_Front_Of_Line
  281. (
  282. const Vector3 & p, // point to test
  283. const Vector3 & e0, // point on edge
  284. const Vector3 & de // direction of edge
  285. );
  286. static inline float IntersectionClass::Intersect_Lines
  287. (
  288. const Vector3 & p0, // start of line segment
  289. const Vector3 & p1, // end of line segment
  290. const Vector3 & e0, // point on clipping edge
  291. const Vector3 & de // direction of clipping edge
  292. );
  293. static inline int IntersectionClass::Clip_Triangle_To_LineXY(
  294. int incount,
  295. Vector3 * InPoints,
  296. Vector2 * InUVs,
  297. Vector3 * OutPoints,
  298. Vector2 * OutUVs,
  299. const Vector3 & edge_point0,
  300. const Vector3 & edge_point1
  301. );
  302. inline float Plane_Z_Distance(Vector3 &PlaneNormal, Vector3 &PlanePoint);
  303. inline void Transform_Model_To_World_Coords(IntersectionResultClass *FinalResult);
  304. /*
  305. ** Static vars available for use by temporary intersection class objects.
  306. */
  307. static Vector3 _RayLocation, _RayDirection, _IntersectionNormal;
  308. };
  309. #endif