sr_util.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. ** Command & Conquer Generals(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. /* $Header: /Commando/Code/ww3d/sr_util.h 16 5/09/00 1:10p Jani_p $ */
  19. /***********************************************************************************************
  20. *** Confidential - Westwood Studios ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Commando / G 3D Library *
  24. * *
  25. * $Archive:: /Commando/Code/ww3d/sr_util.h $*
  26. * *
  27. * $Author:: Jani_p $*
  28. * *
  29. * $Modtime:: 5/09/00 10:23a $*
  30. * *
  31. * $Revision:: 16 $*
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #if defined(_MSC_VER)
  37. #pragma once
  38. #endif
  39. #ifndef SR_UTIL_H
  40. #define SR_UTIL_H
  41. #include "always.h"
  42. #include "matrix3d.h"
  43. #include "vector3i.h"
  44. #include <srVector3i.hpp>
  45. #include <srVector3.hpp>
  46. #include <srVector2.hpp>
  47. #include <srMatrix4x3.hpp>
  48. class srNode;
  49. class srMeshModel;
  50. class srCamera;
  51. class srGERD;
  52. class CameraClass;
  53. /*
  54. ** Macros for setting and clearing a pointer to a surrender object. There are
  55. ** similar macros for our ref-counting system in refcount.h...
  56. */
  57. #define SR_REF_PTR_SET(dst,src) { if (dst) dst->release(); dst = src; if(dst) dst->addReference(); }
  58. #define SR_REF_PTR_RELEASE(x) { if (x) x->release(); x = NULL; }
  59. /*
  60. ** PUSH_TRANSFORM, POP_TRANSFORM macros. These are just some macros that push
  61. ** and pop a render object's transform in/out of the GERD. I made them macros
  62. ** so that they could be inline without greatly increasing dependencies.
  63. */
  64. #define PUSH_TRANSFORM(renderinfo,tm) \
  65. srMatrix4x3 srtm;\
  66. Convert_Westwood_Matrix(tm,&srtm); \
  67. renderinfo.Gerd.matrixMode(srGERD::MODELVIEW); \
  68. renderinfo.Gerd.pushMultMatrix(srtm);
  69. #define POP_TRANSFORM(renderinfo) \
  70. renderinfo.Gerd.matrixMode(srGERD::MODELVIEW); \
  71. renderinfo.Gerd.popMatrix();
  72. /*
  73. ** These functions convert between "westwood" (we use right-handed and column vectors)
  74. ** and "surrender" (left-handed, column vector) matrices. Object and cameras are
  75. ** treated separately.
  76. */
  77. void Set_SR_Transform(srNode * obj,const Matrix3D & tm);
  78. Matrix3D Get_SR_Transform(srNode * obj);
  79. void Set_SR_Camera_Transform(srCamera * obj,const Matrix3D & transform);
  80. Matrix3D Get_SR_Camera_Transform(srCamera * obj);
  81. /*
  82. ** This function is used to "pushmultiply" a W3D matrix into the given GERD
  83. */
  84. void Push_Multiply_Westwood_Matrix(srGERD * gerd,const Matrix3D & tm);
  85. /*
  86. ** Functions for converting between Matrix3D's and srMatrix4's
  87. ** Only does the "type" conversion, no coordinate system changes...
  88. */
  89. void Convert_Westwood_Matrix(const Matrix3D & wtm,srMatrix4 * set_sr_tm);
  90. void Convert_Westwood_Matrix(const Matrix3D & wtm,srMatrix4d * set_sr_tm);
  91. void Convert_Westwood_Matrix(const Matrix3D & wtm,srMatrix4x3 * set_sr_tm);
  92. void Convert_Westwood_Matrix(const Matrix3D & wtm,srMatrix3 * set_sr_tm,srVector3 * set_sr_translation);
  93. void Convert_Westwood_Matrix(const Matrix4 & wtm,srMatrix4 * set_sr_tm);
  94. void Convert_Surrender_Matrix(const srMatrix4 & srtm,Matrix3D * set_w3d_tm);
  95. void Convert_Surrender_Matrix(const srMatrix4x3 & srtm,Matrix3D * set_w3d_tm);
  96. void Multiply_Westwood_And_Surrender_Matrix(const Matrix3D& w3d_tm,const srMatrix4& srtm_s,srMatrix4 & srtm_d);
  97. /*
  98. ** This function will "convert" a pointer to an srVector3 into a pointer to a Vector3.
  99. ** Yes, this sucks. In places where we are dealing with surrender vectors a lot,
  100. ** we should probably just use the surrender math functions.
  101. */
  102. inline Vector3 * As_Vector3(srVector3 * v) { return (Vector3 *)v; }
  103. inline Vector3 & As_Vector3(srVector3 & v) { return (Vector3 &) v; }
  104. inline srVector3 * As_srVector3(Vector3 * v) { return (srVector3 *)v; }
  105. inline srVector3 & As_srVector3(Vector3 & v) { return (srVector3 &) v; }
  106. inline const srVector3 * As_srVector3(const Vector3 * v) { return (const srVector3 *)v; }
  107. inline const srVector3 & As_srVector3(const Vector3 & v) { return (const srVector3 &) v; }
  108. /*
  109. ** Here's a set of equally sucky functions for dealing with srVector4 and Vector4's.
  110. */
  111. inline Vector4 * As_Vector4(srVector4 * v) { return (Vector4 *)v; }
  112. inline Vector4 & As_Vector4(srVector4 & v) { return (Vector4 &) v; }
  113. inline srVector4 * As_srVector4(Vector4 * v) { return (srVector4 *)v; }
  114. inline srVector4 & As_srVector4(Vector4 & v) { return (srVector4 &) v; }
  115. inline const srVector4 * As_srVector4(const Vector4 * v) { return (const srVector4 *)v; }
  116. inline const srVector4 & As_srVector4(const Vector4 & v) { return (const srVector4 &) v; }
  117. /*
  118. ** More suckage, but now with 2's!
  119. */
  120. inline Vector2 * As_Vector2(srVector2 * v) { return (Vector2 *)v; }
  121. inline Vector2 & As_Vector2(srVector2 & v) { return (Vector2 &) v; }
  122. inline srVector2 * As_srVector2(Vector2 * v) { return (srVector2 *)v; }
  123. inline srVector2 & As_srVector2(Vector2 & v) { return (srVector2 &) v; }
  124. inline const srVector2 * As_srVector2(const Vector2 * v) { return (const srVector2 *)v; }
  125. inline const srVector2 & As_srVector2(const Vector2 & v) { return (const srVector2 &) v; }
  126. /*
  127. ** Yahoo, here is some more. If they break this we're gonna really be hurting!!!!
  128. */
  129. inline Vector3i * As_Vector3i(srVector3i * v) { return (Vector3i *)v; }
  130. inline Vector3i & As_Vector3i(srVector3i & v) { return (Vector3i &) v; }
  131. inline srVector3i * As_srVector3i(Vector3i * v) { return (srVector3i *)v; }
  132. inline srVector3i & As_srVector3i(Vector3i & v) { return (srVector3i &) v; }
  133. inline const srVector3i * As_srVector3i(const Vector3i * v) { return (const srVector3i *)v; }
  134. inline const srVector3i & As_srVector3i(const Vector3i & v) { return (const srVector3i &)v; }
  135. /*
  136. ** This function returns the worldspace coordinates of the eight frustum
  137. ** corners of the given camera.
  138. */
  139. void Get_Camera_Frustum_Corners(const CameraClass * camera, Vector3 points[8]);
  140. /*
  141. ** This function returns the worldspace coordinates of the eight frustum
  142. ** corners of the given camera, with the depth (z distance) clamped to two
  143. ** given values.
  144. */
  145. bool Get_ZClamped_Camera_Frustum_Corners(const CameraClass * camera,
  146. Vector3 points[8], float minz, float maxz);
  147. // the SRMeshClass is used to store all the information neccessary to perform intersection
  148. // testing on a particular mesh. It is used by the Intersection class within WW3D, and
  149. // elsewhere within G's planet mode.
  150. // feel free to extend this to store other srTriMesh data members as needed
  151. class RenderObjClass;
  152. typedef unsigned short POLYGONINDEX;
  153. class SRMeshClass {
  154. public:
  155. RenderObjClass *RenderObject; // what initialized this structure
  156. POLYGONINDEX PolygonCount;
  157. srVector3i *PolygonVertices;
  158. srVector4 *PolygonEquations;
  159. srVector3 *VertexLocations;
  160. srVector3 *VertexNormals;
  161. void Copy(SRMeshClass &info) {
  162. RenderObject = info.RenderObject;
  163. PolygonCount = info.PolygonCount;
  164. PolygonVertices = info.PolygonVertices;
  165. PolygonEquations = info.PolygonEquations;
  166. VertexLocations = info.VertexLocations;
  167. VertexNormals = info.VertexNormals;
  168. }
  169. void Initialize(RenderObjClass *renderObject, srMeshModel *meshmodel);
  170. };
  171. #endif