ScreenCursor.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 : W3DView *
  23. * *
  24. * $Archive:: /Commando/Code/Tools/W3DView/ScreenCursor.cpp $Modtime:: $*
  25. * *
  26. * $Revision:: 8 $*
  27. * *
  28. *---------------------------------------------------------------------------------------------*
  29. * Functions: *
  30. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  31. #include "StdAfx.H"
  32. #include "ScreenCursor.H"
  33. #include "Utils.H"
  34. #include "ww3d.h"
  35. #include "vertmaterial.h"
  36. #include "shader.h"
  37. #include "scene.h"
  38. #include "rinfo.h"
  39. #include "texture.h"
  40. #include "dx8wrapper.h"
  41. #include "dx8vertexbuffer.h"
  42. #include "dx8indexbuffer.h"
  43. #include "sortingrenderer.h"
  44. ///////////////////////////////////////////////////////////////////
  45. //
  46. // ScreenCursorClass
  47. //
  48. ///////////////////////////////////////////////////////////////////
  49. ScreenCursorClass::ScreenCursorClass (void)
  50. : m_ScreenPos (0, 0),
  51. m_pTexture (NULL),
  52. m_pVertMaterial (NULL),
  53. m_Width (0),
  54. m_Height (0),
  55. m_hWnd (NULL)
  56. {
  57. Initialize ();
  58. return ;
  59. }
  60. ///////////////////////////////////////////////////////////////////
  61. //
  62. // ScreenCursorClass
  63. //
  64. ///////////////////////////////////////////////////////////////////
  65. ScreenCursorClass::ScreenCursorClass (const ScreenCursorClass &src)
  66. : m_ScreenPos (0, 0),
  67. m_pTexture (NULL),
  68. m_hWnd (NULL),
  69. m_pVertMaterial (NULL),
  70. m_Width (0),
  71. m_Height (0),
  72. RenderObjClass (src)
  73. {
  74. Initialize ();
  75. return ;
  76. }
  77. ///////////////////////////////////////////////////////////////////
  78. //
  79. // ~ScreenCursorClass
  80. //
  81. ///////////////////////////////////////////////////////////////////
  82. ScreenCursorClass::~ScreenCursorClass (void)
  83. {
  84. REF_PTR_RELEASE (m_pTexture);
  85. REF_PTR_RELEASE (m_pVertMaterial);
  86. return ;
  87. }
  88. ///////////////////////////////////////////////////////////////////
  89. //
  90. // Initialize
  91. //
  92. ///////////////////////////////////////////////////////////////////
  93. void
  94. ScreenCursorClass::Initialize (void)
  95. {
  96. REF_PTR_RELEASE(m_pVertMaterial);
  97. // Create default vertex material
  98. m_pVertMaterial = NEW_REF( VertexMaterialClass, ());
  99. m_pVertMaterial->Set_Diffuse (1.0F, 1.0F, 1.0F);
  100. m_pVertMaterial->Set_Emissive (0.0F, 0.0F, 0.0F);
  101. m_pVertMaterial->Set_Specular (1.0F, 1.0F, 1.0F);
  102. m_pVertMaterial->Set_Ambient (1.0F, 1.0F, 1.0F);
  103. m_Triangles[0].I = 0;
  104. m_Triangles[0].J = 1;
  105. m_Triangles[0].K = 2;
  106. m_Triangles[1].I = 1;
  107. m_Triangles[1].J = 2;
  108. m_Triangles[1].K = 3;
  109. m_Normals[0].X = 0;
  110. m_Normals[0].Y = 0;
  111. m_Normals[0].Z = -1;
  112. m_Normals[1].X = 0;
  113. m_Normals[1].Y = 0;
  114. m_Normals[1].Z = -1;
  115. m_Normals[2].X = 0;
  116. m_Normals[2].Y = 0;
  117. m_Normals[2].Z = -1;
  118. m_Normals[3].X = 0;
  119. m_Normals[3].Y = 0;
  120. m_Normals[3].Z = -1;
  121. m_UVs[0].X = 0;
  122. m_UVs[0].Y = 0;
  123. m_UVs[1].X = 1.0F;
  124. m_UVs[1].Y = 0;
  125. m_UVs[2].X = 0;
  126. m_UVs[2].Y = 1.0F;
  127. m_UVs[3].X = 1.0F;
  128. m_UVs[3].Y = 1.0F;
  129. return ;
  130. }
  131. ///////////////////////////////////////////////////////////////////
  132. //
  133. // Set_Texture
  134. //
  135. ///////////////////////////////////////////////////////////////////
  136. void
  137. ScreenCursorClass::Set_Texture (TextureClass *texture)
  138. {
  139. MEMBER_ADD (m_pTexture, texture);
  140. // Find the dimensions of the texture:
  141. if (m_pTexture != NULL) {
  142. m_Width = m_pTexture->Get_Width();
  143. m_Height = m_pTexture->Get_Height();
  144. }
  145. return ;
  146. }
  147. ///////////////////////////////////////////////////////////////////
  148. //
  149. // On_Frame_Update
  150. //
  151. ///////////////////////////////////////////////////////////////////
  152. void
  153. ScreenCursorClass::On_Frame_Update (void)
  154. {
  155. //
  156. // Get the current cursor position in screen coords
  157. //
  158. POINT point = { 0 };
  159. ::GetCursorPos (&point);
  160. if (m_hWnd != NULL) {
  161. //
  162. // Normalize the screen position
  163. //
  164. RECT rect = { 0 };
  165. ::GetClientRect (m_hWnd, &rect);
  166. ::ScreenToClient (m_hWnd, &point);
  167. m_ScreenPos.X = ((float)point.x) / ((float)rect.right);
  168. m_ScreenPos.Y = ((float)point.y) / ((float)rect.bottom);
  169. } else {
  170. //
  171. // Normalize the screen position
  172. //
  173. m_ScreenPos.X = ((float)point.x) / ((float)::GetSystemMetrics (SM_CXSCREEN));
  174. m_ScreenPos.Y = ((float)point.y) / ((float)::GetSystemMetrics (SM_CYSCREEN));
  175. }
  176. //
  177. // Determine the current display resolution
  178. //
  179. int screen_cx = 0;
  180. int screen_cy = 0;
  181. int bits = 0;
  182. bool windowed = false;
  183. WW3D::Get_Device_Resolution (screen_cx, screen_cy, bits, windowed);
  184. //
  185. // Calculate the 3D position
  186. //
  187. float normal_width = ((float)m_Width) / (float)screen_cx;
  188. float normal_height = ((float)m_Height) / (float)screen_cy;
  189. float x_pos = floor(m_ScreenPos.X * ((float)screen_cx) + 0.5F) / ((float)screen_cx);
  190. float y_pos = floor(m_ScreenPos.Y * ((float)screen_cy) + 0.5F) / ((float)screen_cy);
  191. float z_pos = 0;
  192. //
  193. // Convert the 3D position to normalized 'view' coords
  194. //
  195. float x_max = ((x_pos + normal_width) * 2) - 1;
  196. float y_max = 1 - ((y_pos + normal_height) * 2);
  197. x_pos = (x_pos * 2) - 1;
  198. y_pos = 1 - (y_pos * 2);
  199. z_pos = 0;
  200. //
  201. // Build the verticies from the position and extents
  202. //
  203. m_Verticies[0].X = x_pos;
  204. m_Verticies[0].Y = y_pos;
  205. m_Verticies[0].Z = z_pos;
  206. m_Verticies[1].X = x_max;
  207. m_Verticies[1].Y = y_pos;
  208. m_Verticies[1].Z = z_pos;
  209. m_Verticies[2].X = x_pos;
  210. m_Verticies[2].Y = y_max;
  211. m_Verticies[2].Z = z_pos;
  212. m_Verticies[3].X = x_max;
  213. m_Verticies[3].Y = y_max;
  214. m_Verticies[3].Z = z_pos;
  215. return ;
  216. }
  217. ///////////////////////////////////////////////////////////////////
  218. //
  219. // Render
  220. //
  221. ///////////////////////////////////////////////////////////////////
  222. void
  223. ScreenCursorClass::Render (RenderInfoClass &rinfo)
  224. {
  225. const int VERTEX_COUNT = 4;
  226. const int FACE_COUNT = 2;
  227. /*
  228. ** Dump the vertices into the dynamic sorting vertex buffer.
  229. */
  230. DynamicVBAccessClass vbaccess(BUFFER_TYPE_DYNAMIC_SORTING,dynamic_fvf_type,VERTEX_COUNT);
  231. {
  232. DynamicVBAccessClass::WriteLockClass lock(&vbaccess);
  233. VertexFormatXYZNDUV2* vb=lock.Get_Formatted_Vertex_Array();
  234. for (int i=0; i<VERTEX_COUNT; i++) {
  235. // Locations
  236. vb->x=m_Verticies[i].X;
  237. vb->y=m_Verticies[i].Y;
  238. vb->z=m_Verticies[i].Z;
  239. // Normals
  240. vb->nx=m_Normals[i].X;
  241. vb->ny=m_Normals[i].Y;
  242. vb->nz=m_Normals[i].Z;
  243. // UV coordinates
  244. vb->u1=m_UVs[i].X;
  245. vb->v1=m_UVs[i].Y;
  246. vb++;
  247. }
  248. }
  249. /*
  250. ** Dump the faces into the dynamic sorting index buffer.
  251. */
  252. DynamicIBAccessClass ibaccess(BUFFER_TYPE_DYNAMIC_SORTING,FACE_COUNT*3);
  253. {
  254. DynamicIBAccessClass::WriteLockClass lock(&ibaccess);
  255. unsigned short * indices = lock.Get_Index_Array();
  256. for (int i=0; i<FACE_COUNT; i++) {
  257. indices[3*i+0] = m_Triangles[i][0];
  258. indices[3*i+1] = m_Triangles[i][1];
  259. indices[3*i+2] = m_Triangles[i][2];
  260. }
  261. }
  262. /*
  263. ** Apply the shader and material
  264. */
  265. DX8Wrapper::Set_Material(m_pVertMaterial);
  266. DX8Wrapper::Set_Shader(ShaderClass::_PresetATestBlend2DShader);
  267. DX8Wrapper::Set_Texture(0,m_pTexture);
  268. DX8Wrapper::Set_Vertex_Buffer(vbaccess);
  269. DX8Wrapper::Set_Index_Buffer(ibaccess,0);
  270. SphereClass sphere;
  271. Get_Obj_Space_Bounding_Sphere(sphere);
  272. SortingRendererClass::Insert_Triangles(
  273. sphere,
  274. 0,
  275. FACE_COUNT*3,
  276. 0,
  277. VERTEX_COUNT*2);
  278. return ;
  279. }
  280. //////////////////////////////////////////////////////////////
  281. //
  282. // Get_Obj_Space_Bounding_Sphere
  283. //
  284. //////////////////////////////////////////////////////////////
  285. void
  286. ScreenCursorClass::Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const
  287. {
  288. sphere.Center = Get_Transform().Get_Translation();
  289. sphere.Radius = max (m_Width, m_Height);
  290. }
  291. //////////////////////////////////////////////////////////////
  292. //
  293. // Get_Obj_Space_Bounding_Box
  294. //
  295. //////////////////////////////////////////////////////////////
  296. void
  297. ScreenCursorClass::Get_Obj_Space_Bounding_Box(AABoxClass & box) const
  298. {
  299. Matrix3D transform = Get_Transform ();
  300. box.Center = transform.Get_Translation ();
  301. box.Extent.Set(0.1F, m_Width, m_Height);
  302. }
  303. //////////////////////////////////////////////////////////////
  304. //
  305. // Notify_Added
  306. //
  307. //////////////////////////////////////////////////////////////
  308. void
  309. ScreenCursorClass::Notify_Added (SceneClass * scene)
  310. {
  311. if (scene != NULL) {
  312. scene->Register (this, SceneClass::ON_FRAME_UPDATE);
  313. }
  314. return ;
  315. }
  316. //////////////////////////////////////////////////////////////
  317. //
  318. // Notify_Removed
  319. //
  320. //////////////////////////////////////////////////////////////
  321. void
  322. ScreenCursorClass::Notify_Removed (SceneClass * scene)
  323. {
  324. if (scene != NULL) {
  325. scene->Unregister (this, SceneClass::ON_FRAME_UPDATE);
  326. }
  327. return ;
  328. }