projector.cpp 16 KB

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