projector.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/projector.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Kenny Mitchell *
  29. * *
  30. * $Modtime:: 06/26/02 4:04p $*
  31. * *
  32. * $Revision:: 6 $*
  33. * *
  34. * 06/26/02 KM Matrix name change to avoid MAX conflicts *
  35. *---------------------------------------------------------------------------------------------*
  36. * Functions: *
  37. * ProjectorClass::ProjectorClass -- Constructor *
  38. * ProjectorClass::~ProjectorClass -- Destructor *
  39. * ProjectorClass::Set_Transform -- Set the transform for the projector *
  40. * ProjectorClass::Get_Transform -- Returns the current transform *
  41. * ProjectorClass::Set_Perspective_Projection -- Set up a perspective projection *
  42. * ProjectorClass::Set_Ortho_Projection -- Set up an orthographic projection *
  43. * ProjectorClass::Compute_Texture_Coordinate -- computes texcoord for given world-space poi *
  44. * ProjectorClass::Update_WS_Bounding_Volume -- Recalculate the world-space bounding box *
  45. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  46. #include "projector.h"
  47. #include "refcount.h"
  48. #include "matrixmapper.h"
  49. /***********************************************************************************************
  50. * ProjectorClass::ProjectorClass -- Constructor *
  51. * *
  52. * INPUT: *
  53. * *
  54. * OUTPUT: *
  55. * *
  56. * WARNINGS: *
  57. * *
  58. * HISTORY: *
  59. *=============================================================================================*/
  60. ProjectorClass::ProjectorClass(void) :
  61. Transform(1),
  62. Projection(1),
  63. LocalBoundingVolume(Vector3(0,0,0),Vector3(1,1,1)),
  64. WorldBoundingVolume(Vector3(0,0,0),Vector3(1,1,1),Matrix3x3(1))
  65. {
  66. Mapper=NEW_REF(MatrixMapperClass,(0));
  67. }
  68. /***********************************************************************************************
  69. * ProjectorClass::~ProjectorClass -- Destructor *
  70. * *
  71. * INPUT: *
  72. * *
  73. * OUTPUT: *
  74. * *
  75. * WARNINGS: *
  76. * *
  77. * HISTORY: *
  78. *=============================================================================================*/
  79. ProjectorClass::~ProjectorClass(void)
  80. {
  81. REF_PTR_RELEASE(Mapper);
  82. }
  83. /***********************************************************************************************
  84. * ProjectorClass::Set_Transform -- Set the transform for the projector *
  85. * *
  86. * Projectors can be positioned in world space just like cameras! Just point the -Z axis *
  87. * at the target. *
  88. * *
  89. * INPUT: *
  90. * *
  91. * OUTPUT: *
  92. * *
  93. * WARNINGS: *
  94. * *
  95. * HISTORY: *
  96. * 1/11/00 gth : Created. *
  97. * 1/27/00 gth : Created. *
  98. *=============================================================================================*/
  99. void ProjectorClass::Set_Transform(const Matrix3D & tm)
  100. {
  101. Transform = tm;
  102. Update_WS_Bounding_Volume();
  103. }
  104. /***********************************************************************************************
  105. * ProjectorClass::Get_Transform -- Returns the current transform *
  106. * *
  107. * INPUT: *
  108. * *
  109. * OUTPUT: *
  110. * *
  111. * WARNINGS: *
  112. * *
  113. * HISTORY: *
  114. * 1/11/00 gth : Created. *
  115. *=============================================================================================*/
  116. const Matrix3D & ProjectorClass::Get_Transform(void) const
  117. {
  118. return Transform;
  119. }
  120. /***********************************************************************************************
  121. * ProjectorClass::Set_Perspective_Projection -- Set up a perspective projection *
  122. * *
  123. * INPUT: *
  124. * hfov - radians, horizontal field of view of the projection *
  125. * vfov - radians, vertical field of view of the projection *
  126. * znear - distance to near clipping plane *
  127. * zfar - distance to far clipping plane *
  128. * *
  129. * OUTPUT: *
  130. * *
  131. * WARNINGS: *
  132. * Remember that znear and zfar are *distances*. They are positive numbers as in the OpenGL *
  133. * convention even though the coordinates will be negative. *
  134. * *
  135. * HISTORY: *
  136. * 1/11/00 gth : Created. *
  137. *=============================================================================================*/
  138. void ProjectorClass::Set_Perspective_Projection(float hfov,float vfov,float znear,float zfar)
  139. {
  140. Mapper->Set_Type(MatrixMapperClass::PERSPECTIVE_PROJECTION);
  141. Projection.Init_Perspective(hfov,vfov,0.1f,zfar); // don't use znear for the projection matrix
  142. float tan_hfov2 = tan(hfov) * 0.5f;
  143. float tan_vfov2 = tan(vfov) * 0.5f;
  144. LocalBoundingVolume.Center.Set(0.0f,0.0f,-(zfar+znear)*0.5f); // note, zcenter is negative
  145. LocalBoundingVolume.Extent.X = zfar * tan_hfov2;
  146. LocalBoundingVolume.Extent.Y = zfar * tan_vfov2;
  147. LocalBoundingVolume.Extent.Z = (zfar-znear)*0.5f;
  148. Update_WS_Bounding_Volume();
  149. }
  150. /***********************************************************************************************
  151. * ProjectorClass::Set_Ortho_Projection -- Set up an orthographic projection *
  152. * *
  153. * INPUT: *
  154. * xmin - "left" x coordinate in texture (camera) space *
  155. * xmax - "right" x coordinate *
  156. * ymin - "bottom" y coordinate *
  157. * ymax - "top" y coordinate *
  158. * znear - distance to near clipping plane *
  159. * zfar - distance to far clipping plane *
  160. * *
  161. * OUTPUT: *
  162. * *
  163. * WARNINGS: *
  164. * Remember that znear and zfar are *distances*. They are positive numbers as in the OpenGL *
  165. * convention even though the coordinates will be negative. *
  166. * *
  167. * HISTORY: *
  168. * 1/11/00 gth : Created. *
  169. *=============================================================================================*/
  170. void ProjectorClass::Set_Ortho_Projection(float xmin,float xmax,float ymin,float ymax,float znear,float zfar)
  171. {
  172. Mapper->Set_Type(MatrixMapperClass::ORTHO_PROJECTION);
  173. Projection.Init_Ortho(xmin,xmax,ymin,ymax,0.1f,zfar); // don't use znear for the projection matrix
  174. LocalBoundingVolume.Center.Set((xmax+xmin)*0.5f, (ymax+ymin)*0.5f, -(zfar+znear)*0.5f);
  175. LocalBoundingVolume.Extent.Set((xmax-xmin)*0.5f, (ymax-ymin)*0.5f, (zfar-znear)*0.5f);
  176. Update_WS_Bounding_Volume();
  177. }
  178. /***********************************************************************************************
  179. * ProjectorClass::Compute_Texture_Coordinate -- computes texcoord for given world-space point *
  180. * *
  181. * INPUT: *
  182. * *
  183. * OUTPUT: *
  184. * *
  185. * WARNINGS: *
  186. * *
  187. * HISTORY: *
  188. * 1/27/00 gth : Created. *
  189. *=============================================================================================*/
  190. void ProjectorClass::Compute_Texture_Coordinate(const Vector3 & point,Vector3 * set_stq)
  191. {
  192. Mapper->Compute_Texture_Coordinate(point,set_stq);
  193. }
  194. /***********************************************************************************************
  195. * ProjectorClass::Update_WS_Bounding_Volume -- Recalculate the world-space bounding box *
  196. * *
  197. * INPUT: *
  198. * *
  199. * OUTPUT: *
  200. * *
  201. * WARNINGS: *
  202. * *
  203. * HISTORY: *
  204. * 1/11/00 gth : Created. *
  205. *=============================================================================================*/
  206. void ProjectorClass::Update_WS_Bounding_Volume(void)
  207. {
  208. /*
  209. ** Recompute our world-space bounding volume
  210. */
  211. OBBoxClass localbox(LocalBoundingVolume.Center,LocalBoundingVolume.Extent,Matrix3x3(1));
  212. OBBoxClass::Transform(Transform,localbox,&WorldBoundingVolume);
  213. }