| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- /*
- ** Command & Conquer Renegade(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /***********************************************************************************************
- *** 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 ***
- ***********************************************************************************************
- * *
- * Project Name : W3DView *
- * *
- * $Archive:: /Commando/Code/Tools/W3DView/ScreenCursor.cpp $Modtime:: $*
- * *
- * $Revision:: 8 $*
- * *
- *---------------------------------------------------------------------------------------------*
- * Functions: *
- * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
- #include "StdAfx.H"
- #include "ScreenCursor.H"
- #include "Utils.H"
- #include "ww3d.h"
- #include "vertmaterial.h"
- #include "shader.h"
- #include "scene.h"
- #include "rinfo.h"
- #include "texture.h"
- #include "dx8wrapper.h"
- #include "dx8vertexbuffer.h"
- #include "dx8indexbuffer.h"
- #include "sortingrenderer.h"
- ///////////////////////////////////////////////////////////////////
- //
- // ScreenCursorClass
- //
- ///////////////////////////////////////////////////////////////////
- ScreenCursorClass::ScreenCursorClass (void)
- : m_ScreenPos (0, 0),
- m_pTexture (NULL),
- m_pVertMaterial (NULL),
- m_Width (0),
- m_Height (0),
- m_hWnd (NULL)
- {
- Initialize ();
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- //
- // ScreenCursorClass
- //
- ///////////////////////////////////////////////////////////////////
- ScreenCursorClass::ScreenCursorClass (const ScreenCursorClass &src)
- : m_ScreenPos (0, 0),
- m_pTexture (NULL),
- m_hWnd (NULL),
- m_pVertMaterial (NULL),
- m_Width (0),
- m_Height (0),
- RenderObjClass (src)
- {
- Initialize ();
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- //
- // ~ScreenCursorClass
- //
- ///////////////////////////////////////////////////////////////////
- ScreenCursorClass::~ScreenCursorClass (void)
- {
- REF_PTR_RELEASE (m_pTexture);
- REF_PTR_RELEASE (m_pVertMaterial);
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- //
- // Initialize
- //
- ///////////////////////////////////////////////////////////////////
- void
- ScreenCursorClass::Initialize (void)
- {
- REF_PTR_RELEASE(m_pVertMaterial);
- // Create default vertex material
- m_pVertMaterial = NEW_REF( VertexMaterialClass, ());
- m_pVertMaterial->Set_Diffuse (1.0F, 1.0F, 1.0F);
- m_pVertMaterial->Set_Emissive (0.0F, 0.0F, 0.0F);
- m_pVertMaterial->Set_Specular (1.0F, 1.0F, 1.0F);
- m_pVertMaterial->Set_Ambient (1.0F, 1.0F, 1.0F);
- m_Triangles[0].I = 0;
- m_Triangles[0].J = 1;
- m_Triangles[0].K = 2;
- m_Triangles[1].I = 1;
- m_Triangles[1].J = 2;
- m_Triangles[1].K = 3;
- m_Normals[0].X = 0;
- m_Normals[0].Y = 0;
- m_Normals[0].Z = -1;
- m_Normals[1].X = 0;
- m_Normals[1].Y = 0;
- m_Normals[1].Z = -1;
- m_Normals[2].X = 0;
- m_Normals[2].Y = 0;
- m_Normals[2].Z = -1;
- m_Normals[3].X = 0;
- m_Normals[3].Y = 0;
- m_Normals[3].Z = -1;
- m_UVs[0].X = 0;
- m_UVs[0].Y = 0;
- m_UVs[1].X = 1.0F;
- m_UVs[1].Y = 0;
- m_UVs[2].X = 0;
- m_UVs[2].Y = 1.0F;
- m_UVs[3].X = 1.0F;
- m_UVs[3].Y = 1.0F;
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- //
- // Set_Texture
- //
- ///////////////////////////////////////////////////////////////////
- void
- ScreenCursorClass::Set_Texture (TextureClass *texture)
- {
- MEMBER_ADD (m_pTexture, texture);
- // Find the dimensions of the texture:
- if (m_pTexture != NULL) {
- m_Width = m_pTexture->Get_Width();
- m_Height = m_pTexture->Get_Height();
- }
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- //
- // On_Frame_Update
- //
- ///////////////////////////////////////////////////////////////////
- void
- ScreenCursorClass::On_Frame_Update (void)
- {
- //
- // Get the current cursor position in screen coords
- //
- POINT point = { 0 };
- ::GetCursorPos (&point);
- if (m_hWnd != NULL) {
-
- //
- // Normalize the screen position
- //
- RECT rect = { 0 };
- ::GetClientRect (m_hWnd, &rect);
- ::ScreenToClient (m_hWnd, &point);
- m_ScreenPos.X = ((float)point.x) / ((float)rect.right);
- m_ScreenPos.Y = ((float)point.y) / ((float)rect.bottom);
- } else {
- //
- // Normalize the screen position
- //
- m_ScreenPos.X = ((float)point.x) / ((float)::GetSystemMetrics (SM_CXSCREEN));
- m_ScreenPos.Y = ((float)point.y) / ((float)::GetSystemMetrics (SM_CYSCREEN));
- }
- //
- // Determine the current display resolution
- //
- int screen_cx = 0;
- int screen_cy = 0;
- int bits = 0;
- bool windowed = false;
- WW3D::Get_Device_Resolution (screen_cx, screen_cy, bits, windowed);
- //
- // Calculate the 3D position
- //
- float normal_width = ((float)m_Width) / (float)screen_cx;
- float normal_height = ((float)m_Height) / (float)screen_cy;
- float x_pos = floor(m_ScreenPos.X * ((float)screen_cx) + 0.5F) / ((float)screen_cx);
- float y_pos = floor(m_ScreenPos.Y * ((float)screen_cy) + 0.5F) / ((float)screen_cy);
- float z_pos = 0;
- //
- // Convert the 3D position to normalized 'view' coords
- //
- float x_max = ((x_pos + normal_width) * 2) - 1;
- float y_max = 1 - ((y_pos + normal_height) * 2);
- x_pos = (x_pos * 2) - 1;
- y_pos = 1 - (y_pos * 2);
- z_pos = 0;
-
- //
- // Build the verticies from the position and extents
- //
- m_Verticies[0].X = x_pos;
- m_Verticies[0].Y = y_pos;
- m_Verticies[0].Z = z_pos;
- m_Verticies[1].X = x_max;
- m_Verticies[1].Y = y_pos;
- m_Verticies[1].Z = z_pos;
- m_Verticies[2].X = x_pos;
- m_Verticies[2].Y = y_max;
- m_Verticies[2].Z = z_pos;
- m_Verticies[3].X = x_max;
- m_Verticies[3].Y = y_max;
- m_Verticies[3].Z = z_pos;
- return ;
- }
- ///////////////////////////////////////////////////////////////////
- //
- // Render
- //
- ///////////////////////////////////////////////////////////////////
- void
- ScreenCursorClass::Render (RenderInfoClass &rinfo)
- {
- const int VERTEX_COUNT = 4;
- const int FACE_COUNT = 2;
- /*
- ** Dump the vertices into the dynamic sorting vertex buffer.
- */
- DynamicVBAccessClass vbaccess(BUFFER_TYPE_DYNAMIC_SORTING,dynamic_fvf_type,VERTEX_COUNT);
- {
- DynamicVBAccessClass::WriteLockClass lock(&vbaccess);
- VertexFormatXYZNDUV2* vb=lock.Get_Formatted_Vertex_Array();
- for (int i=0; i<VERTEX_COUNT; i++) {
- // Locations
- vb->x=m_Verticies[i].X;
- vb->y=m_Verticies[i].Y;
- vb->z=m_Verticies[i].Z;
-
- // Normals
- vb->nx=m_Normals[i].X;
- vb->ny=m_Normals[i].Y;
- vb->nz=m_Normals[i].Z;
-
- // UV coordinates
- vb->u1=m_UVs[i].X;
- vb->v1=m_UVs[i].Y;
- vb++;
- }
- }
- /*
- ** Dump the faces into the dynamic sorting index buffer.
- */
- DynamicIBAccessClass ibaccess(BUFFER_TYPE_DYNAMIC_SORTING,FACE_COUNT*3);
- {
- DynamicIBAccessClass::WriteLockClass lock(&ibaccess);
- unsigned short * indices = lock.Get_Index_Array();
- for (int i=0; i<FACE_COUNT; i++) {
- indices[3*i+0] = m_Triangles[i][0];
- indices[3*i+1] = m_Triangles[i][1];
- indices[3*i+2] = m_Triangles[i][2];
- }
- }
- /*
- ** Apply the shader and material
- */
- DX8Wrapper::Set_Material(m_pVertMaterial);
- DX8Wrapper::Set_Shader(ShaderClass::_PresetATestBlend2DShader);
- DX8Wrapper::Set_Texture(0,m_pTexture);
-
- DX8Wrapper::Set_Vertex_Buffer(vbaccess);
- DX8Wrapper::Set_Index_Buffer(ibaccess,0);
-
- SphereClass sphere;
- Get_Obj_Space_Bounding_Sphere(sphere);
- SortingRendererClass::Insert_Triangles(
- sphere,
- 0,
- FACE_COUNT*3,
- 0,
- VERTEX_COUNT*2);
- return ;
- }
- //////////////////////////////////////////////////////////////
- //
- // Get_Obj_Space_Bounding_Sphere
- //
- //////////////////////////////////////////////////////////////
- void
- ScreenCursorClass::Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const
- {
- sphere.Center = Get_Transform().Get_Translation();
- sphere.Radius = max (m_Width, m_Height);
- }
- //////////////////////////////////////////////////////////////
- //
- // Get_Obj_Space_Bounding_Box
- //
- //////////////////////////////////////////////////////////////
- void
- ScreenCursorClass::Get_Obj_Space_Bounding_Box(AABoxClass & box) const
- {
- Matrix3D transform = Get_Transform ();
- box.Center = transform.Get_Translation ();
- box.Extent.Set(0.1F, m_Width, m_Height);
- }
- //////////////////////////////////////////////////////////////
- //
- // Notify_Added
- //
- //////////////////////////////////////////////////////////////
- void
- ScreenCursorClass::Notify_Added (SceneClass * scene)
- {
- if (scene != NULL) {
- scene->Register (this, SceneClass::ON_FRAME_UPDATE);
- }
- return ;
- }
- //////////////////////////////////////////////////////////////
- //
- // Notify_Removed
- //
- //////////////////////////////////////////////////////////////
- void
- ScreenCursorClass::Notify_Removed (SceneClass * scene)
- {
- if (scene != NULL) {
- scene->Unregister (this, SceneClass::ON_FRAME_UPDATE);
- }
- return ;
- }
|