boxrobj.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. /***********************************************************************************************
  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 : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/boxrobj.h $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 2/19/01 1:11p $*
  29. * *
  30. * $Revision:: 5 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef BOXROBJ_H
  39. #define BOXROBJ_H
  40. #include "always.h"
  41. #include "rendobj.h"
  42. #include "w3d_file.h"
  43. #include "shader.h"
  44. #include "proto.h"
  45. #include "obbox.h"
  46. class VertexMaterialClass;
  47. /**
  48. ** BoxRenderObjClass: base class for AABox and OBBox collision boxes
  49. **
  50. ** NOTE: these render objects were designed from the start to be used for
  51. ** collision boxes. They are designed to normally never render unless you
  52. ** set the display mask and then, all boxes of that type will render.
  53. ** The display mask is 'AND'ed with the collision bits in the base render
  54. ** object class to determine if the box should be rendered. WW3D provides
  55. ** an interface for setting this mask in your app.
  56. **
  57. ** NOTE2: AABoxRenderObjClass is an axis-aligned box which will be positioned
  58. ** at a world-space offset (its local center) from the origin of the transform.
  59. ** This is done because AABoxes are used for rotationally invariant collision
  60. ** detection so we don't want the boxes to move around as the object rotates.
  61. ** For this reason, any AABoxes you use in a hierarchical model should be attached
  62. ** to the root and be constructed symmetrically...
  63. **
  64. ** NOTE3: OBBoxRenderObjClass is an oriented box which is aligned with its transform
  65. ** but can have a center point that is offest from the transform's origin.
  66. **
  67. */
  68. class BoxRenderObjClass : public RenderObjClass
  69. {
  70. public:
  71. BoxRenderObjClass(void);
  72. BoxRenderObjClass(const W3dBoxStruct & def);
  73. BoxRenderObjClass(const BoxRenderObjClass & src);
  74. BoxRenderObjClass & operator = (const BoxRenderObjClass &);
  75. virtual int Get_Num_Polys(void) const;
  76. virtual const char * Get_Name(void) const;
  77. virtual void Set_Name(const char * name);
  78. void Set_Color(const Vector3 & color);
  79. static void Init(void);
  80. static void Shutdown(void);
  81. static void Set_Box_Display_Mask(int mask);
  82. static int Get_Box_Display_Mask(void);
  83. void Set_Local_Center_Extent(const Vector3 & center,const Vector3 & extent);
  84. void Set_Local_Min_Max(const Vector3 & min,const Vector3 & max);
  85. const Vector3 & Get_Local_Center(void) { return ObjSpaceCenter; }
  86. const Vector3 & Get_Local_Extent(void) { return ObjSpaceExtent; }
  87. protected:
  88. virtual void update_cached_box(void) = 0;
  89. void render_box(RenderInfoClass & rinfo,const Vector3 & center,const Vector3 & extent);
  90. void vis_render_box(SpecialRenderInfoClass & rinfo,const Vector3 & center,const Vector3 & extent);
  91. char Name[2*W3D_NAME_LEN];
  92. Vector3 Color;
  93. Vector3 ObjSpaceCenter;
  94. Vector3 ObjSpaceExtent;
  95. static bool IsInitted;
  96. static int DisplayMask;
  97. };
  98. inline void BoxRenderObjClass::Set_Local_Center_Extent(const Vector3 & center,const Vector3 & extent)
  99. {
  100. ObjSpaceCenter = center;
  101. ObjSpaceExtent = extent;
  102. update_cached_box();
  103. }
  104. inline void BoxRenderObjClass::Set_Local_Min_Max(const Vector3 & min,const Vector3 & max)
  105. {
  106. ObjSpaceCenter = (max + min) / 2.0f;
  107. ObjSpaceExtent = (max - min) / 2.0f;
  108. update_cached_box();
  109. }
  110. /*
  111. ** AABoxRenderObjClass -- RenderObject for axis-aligned collision boxes.
  112. */
  113. class AABoxRenderObjClass : public W3DMPO, public BoxRenderObjClass
  114. {
  115. W3DMPO_GLUE(AABoxRenderObjClass)
  116. public:
  117. AABoxRenderObjClass(void);
  118. AABoxRenderObjClass(const W3dBoxStruct & def);
  119. AABoxRenderObjClass(const AABoxRenderObjClass & src);
  120. AABoxRenderObjClass(const AABoxClass & box);
  121. AABoxRenderObjClass & operator = (const AABoxRenderObjClass &);
  122. /////////////////////////////////////////////////////////////////////////////
  123. // Render Object Interface
  124. /////////////////////////////////////////////////////////////////////////////
  125. virtual RenderObjClass * Clone(void) const;
  126. virtual int Class_ID(void) const;
  127. virtual void Render(RenderInfoClass & rinfo);
  128. virtual void Special_Render(SpecialRenderInfoClass & rinfo);
  129. virtual void Set_Transform(const Matrix3D &m);
  130. virtual void Set_Position(const Vector3 &v);
  131. virtual bool Cast_Ray(RayCollisionTestClass & raytest);
  132. virtual bool Cast_AABox(AABoxCollisionTestClass & boxtest);
  133. virtual bool Cast_OBBox(OBBoxCollisionTestClass & boxtest);
  134. virtual bool Intersect_AABox(AABoxIntersectionTestClass & boxtest);
  135. virtual bool Intersect_OBBox(OBBoxIntersectionTestClass & boxtest);
  136. virtual void Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const;
  137. virtual void Get_Obj_Space_Bounding_Box(AABoxClass & box) const;
  138. /////////////////////////////////////////////////////////////////////////////
  139. // AABoxRenderObjClass Interface
  140. /////////////////////////////////////////////////////////////////////////////
  141. const AABoxClass & Get_Box(void);
  142. protected:
  143. virtual void update_cached_box(void);
  144. AABoxClass CachedBox;
  145. };
  146. inline const AABoxClass & AABoxRenderObjClass::Get_Box(void)
  147. {
  148. Validate_Transform();
  149. update_cached_box();
  150. return CachedBox;
  151. }
  152. /*
  153. ** OBBoxRenderObjClass - render object for oriented collision boxes
  154. */
  155. class OBBoxRenderObjClass : public W3DMPO, public BoxRenderObjClass
  156. {
  157. W3DMPO_GLUE(OBBoxRenderObjClass)
  158. public:
  159. OBBoxRenderObjClass(void);
  160. OBBoxRenderObjClass(const W3dBoxStruct & def);
  161. OBBoxRenderObjClass(const OBBoxRenderObjClass & src);
  162. OBBoxRenderObjClass(const OBBoxClass & box);
  163. OBBoxRenderObjClass & operator = (const OBBoxRenderObjClass &);
  164. /////////////////////////////////////////////////////////////////////////////
  165. // Render Object Interface
  166. /////////////////////////////////////////////////////////////////////////////
  167. virtual RenderObjClass * Clone(void) const;
  168. virtual int Class_ID(void) const;
  169. virtual void Render(RenderInfoClass & rinfo);
  170. virtual void Special_Render(SpecialRenderInfoClass & rinfo);
  171. virtual void Set_Transform(const Matrix3D &m);
  172. virtual void Set_Position(const Vector3 &v);
  173. virtual bool Cast_Ray(RayCollisionTestClass & raytest);
  174. virtual bool Cast_AABox(AABoxCollisionTestClass & boxtest);
  175. virtual bool Cast_OBBox(OBBoxCollisionTestClass & boxtest);
  176. virtual bool Intersect_AABox(AABoxIntersectionTestClass & boxtest);
  177. virtual bool Intersect_OBBox(OBBoxIntersectionTestClass & boxtest);
  178. virtual void Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const;
  179. virtual void Get_Obj_Space_Bounding_Box(AABoxClass & box) const;
  180. /////////////////////////////////////////////////////////////////////////////
  181. // OBBoxRenderObjClass Interface
  182. /////////////////////////////////////////////////////////////////////////////
  183. OBBoxClass & Get_Box(void);
  184. protected:
  185. virtual void update_cached_box(void);
  186. OBBoxClass CachedBox;
  187. };
  188. /*
  189. ** Loader for boxes
  190. */
  191. class BoxLoaderClass : public PrototypeLoaderClass
  192. {
  193. public:
  194. virtual int Chunk_Type (void) { return W3D_CHUNK_BOX; }
  195. virtual PrototypeClass * Load_W3D(ChunkLoadClass & cload);
  196. };
  197. // ----------------------------------------------------------------------------
  198. /*
  199. ** Prototype for Box objects
  200. */
  201. class BoxPrototypeClass : public W3DMPO, public PrototypeClass
  202. {
  203. W3DMPO_GLUE(BoxPrototypeClass)
  204. public:
  205. BoxPrototypeClass(W3dBoxStruct box);
  206. virtual const char * Get_Name(void) const;
  207. virtual int Get_Class_ID(void) const;
  208. virtual RenderObjClass * Create(void);
  209. virtual void DeleteSelf() { delete this; }
  210. private:
  211. W3dBoxStruct Definition;
  212. };
  213. /*
  214. ** Instance of the loader which the asset manager installs
  215. */
  216. extern BoxLoaderClass _BoxLoader;
  217. #endif