W3DDebugIcons.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: W3DDebugIcons.cpp ////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Westwood Studios Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2001 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // Project: RTS3
  34. //
  35. // File name: Heightmap.cpp
  36. //
  37. // Created: John Ahlquist, March 2002
  38. //
  39. // Desc: Draws huge numbers of debug icons for pathfinding quickly.
  40. //
  41. //-----------------------------------------------------------------------------
  42. //-----------------------------------------------------------------------------
  43. // Includes
  44. //-----------------------------------------------------------------------------
  45. #include "W3DDevice/GameClient/W3DDebugIcons.h"
  46. #include "Common/GlobalData.h"
  47. #include "GameLogic/GameLogic.h"
  48. #include "Common/MapObject.h"
  49. #include "WW3D2/DX8Wrapper.h"
  50. #if defined _DEBUG || defined _INTERNAL
  51. // Texturing, no zbuffer, disabled zbuffer write, primary gradient, alpha blending
  52. #define SC_OPAQUE ( SHADE_CNST(ShaderClass::PASS_ALWAYS, ShaderClass::DEPTH_WRITE_DISABLE, ShaderClass::COLOR_WRITE_ENABLE, ShaderClass::SRCBLEND_ONE, \
  53. ShaderClass::DSTBLEND_ZERO, ShaderClass::FOG_DISABLE, ShaderClass::GRADIENT_DISABLE, ShaderClass::SECONDARY_GRADIENT_DISABLE, ShaderClass::TEXTURING_ENABLE, \
  54. ShaderClass::ALPHATEST_DISABLE, ShaderClass::CULL_MODE_DISABLE, \
  55. ShaderClass::DETAILCOLOR_DISABLE, ShaderClass::DETAILALPHA_DISABLE) )
  56. // Texturing, no zbuffer, disabled zbuffer write, primary gradient, alpha blending
  57. #define SC_ALPHA ( SHADE_CNST(ShaderClass::PASS_ALWAYS, ShaderClass::DEPTH_WRITE_DISABLE, ShaderClass::COLOR_WRITE_ENABLE, ShaderClass::SRCBLEND_SRC_ALPHA, \
  58. ShaderClass::DSTBLEND_ONE_MINUS_SRC_ALPHA, ShaderClass::FOG_DISABLE, ShaderClass::GRADIENT_MODULATE, ShaderClass::SECONDARY_GRADIENT_DISABLE, ShaderClass::TEXTURING_ENABLE, \
  59. ShaderClass::ALPHATEST_DISABLE, ShaderClass::CULL_MODE_ENABLE, \
  60. ShaderClass::DETAILCOLOR_DISABLE, ShaderClass::DETAILALPHA_DISABLE) )
  61. // Texturing, no zbuffer, disabled zbuffer write, primary gradient, alpha blending
  62. #define SC_ALPHA_Z ( SHADE_CNST(ShaderClass::PASS_LEQUAL, ShaderClass::DEPTH_WRITE_DISABLE, ShaderClass::COLOR_WRITE_ENABLE, ShaderClass::SRCBLEND_SRC_ALPHA, \
  63. ShaderClass::DSTBLEND_ONE_MINUS_SRC_ALPHA, ShaderClass::FOG_DISABLE, ShaderClass::GRADIENT_MODULATE, ShaderClass::SECONDARY_GRADIENT_DISABLE, ShaderClass::TEXTURING_ENABLE, \
  64. ShaderClass::ALPHATEST_DISABLE, ShaderClass::CULL_MODE_DISABLE, \
  65. ShaderClass::DETAILCOLOR_DISABLE, ShaderClass::DETAILALPHA_DISABLE) )
  66. // Texturing, no zbuffer, disabled zbuffer write, primary gradient, alpha blending
  67. #define SC_OPAQUE_Z ( SHADE_CNST(ShaderClass::PASS_LEQUAL, ShaderClass::DEPTH_WRITE_DISABLE, ShaderClass::COLOR_WRITE_ENABLE, ShaderClass::SRCBLEND_ONE, \
  68. ShaderClass::DSTBLEND_ZERO, ShaderClass::FOG_DISABLE, ShaderClass::GRADIENT_DISABLE, ShaderClass::SECONDARY_GRADIENT_DISABLE, ShaderClass::TEXTURING_ENABLE, \
  69. ShaderClass::ALPHATEST_DISABLE, ShaderClass::CULL_MODE_DISABLE, \
  70. ShaderClass::DETAILCOLOR_DISABLE, ShaderClass::DETAILALPHA_DISABLE) )
  71. void addIcon(const Coord3D *pos, Real width, Int numFramesDuration, RGBColor color)
  72. {
  73. W3DDebugIcons::addIcon(pos, width, numFramesDuration, color);
  74. }
  75. struct DebugIcon {
  76. Coord3D position;
  77. Real width; // all are squares centered about pos.
  78. RGBColor color;
  79. Int endFrame; // Frame when this disappears.
  80. };
  81. DebugIcon *W3DDebugIcons::m_debugIcons = NULL;
  82. Int W3DDebugIcons::m_numDebugIcons = 0;
  83. W3DDebugIcons::~W3DDebugIcons(void)
  84. {
  85. REF_PTR_RELEASE(m_vertexMaterialClass);
  86. if (m_debugIcons) delete m_debugIcons;
  87. m_debugIcons = NULL;
  88. m_numDebugIcons = 0;
  89. }
  90. W3DDebugIcons::W3DDebugIcons(void)
  91. {
  92. //go with a preset material for now.
  93. m_vertexMaterialClass=VertexMaterialClass::Get_Preset(VertexMaterialClass::PRELIT_DIFFUSE);
  94. allocateIconsArray();
  95. }
  96. bool W3DDebugIcons::Cast_Ray(RayCollisionTestClass & raytest)
  97. {
  98. return false;
  99. }
  100. //@todo: MW Handle both of these properly!!
  101. W3DDebugIcons::W3DDebugIcons(const W3DDebugIcons & src)
  102. {
  103. *this = src;
  104. }
  105. W3DDebugIcons & W3DDebugIcons::operator = (const W3DDebugIcons & that)
  106. {
  107. DEBUG_CRASH(("oops"));
  108. return *this;
  109. }
  110. void W3DDebugIcons::Get_Obj_Space_Bounding_Sphere(SphereClass & sphere) const
  111. {
  112. Vector3 ObjSpaceCenter(TheGlobalData->m_waterExtentX,TheGlobalData->m_waterExtentY,50*MAP_XY_FACTOR);
  113. float length = ObjSpaceCenter.Length();
  114. sphere.Init(ObjSpaceCenter, length);
  115. }
  116. void W3DDebugIcons::Get_Obj_Space_Bounding_Box(AABoxClass & box) const
  117. {
  118. Vector3 minPt(-2*TheGlobalData->m_waterExtentX,-2*TheGlobalData->m_waterExtentY,0);
  119. Vector3 maxPt(2*TheGlobalData->m_waterExtentX,2*TheGlobalData->m_waterExtentY,100*MAP_XY_FACTOR);
  120. box.Init(minPt,maxPt);
  121. }
  122. Int W3DDebugIcons::Class_ID(void) const
  123. {
  124. return RenderObjClass::CLASSID_UNKNOWN;
  125. }
  126. RenderObjClass * W3DDebugIcons::Clone(void) const
  127. {
  128. return NEW W3DDebugIcons(*this); // poolify
  129. }
  130. void W3DDebugIcons::allocateIconsArray(void)
  131. {
  132. m_debugIcons = NEW DebugIcon[MAX_ICONS];
  133. m_numDebugIcons = 0;
  134. }
  135. void W3DDebugIcons::compressIconsArray(void)
  136. {
  137. if (m_debugIcons && m_numDebugIcons > 0) {
  138. Int newNum = 0;
  139. Int i;
  140. for (i=0; i<m_numDebugIcons; i++) {
  141. if (m_debugIcons[i].endFrame >= TheGameLogic->getFrame() && i>newNum) {
  142. m_debugIcons[newNum] = m_debugIcons[i];
  143. newNum++;
  144. }
  145. }
  146. m_numDebugIcons = newNum;
  147. }
  148. }
  149. static Int maxIcons = 0;
  150. void W3DDebugIcons::addIcon(const Coord3D *pos, Real width, Int numFramesDuration, RGBColor color)
  151. {
  152. if (pos==NULL) {
  153. if (m_numDebugIcons > maxIcons) {
  154. DEBUG_LOG(("Max icons %d\n", m_numDebugIcons));
  155. maxIcons = m_numDebugIcons;
  156. }
  157. m_numDebugIcons = 0;
  158. return;
  159. }
  160. if (m_numDebugIcons>= MAX_ICONS) return;
  161. if (m_debugIcons==NULL) return;
  162. m_debugIcons[m_numDebugIcons].position = *pos;
  163. m_debugIcons[m_numDebugIcons].width = width;
  164. m_debugIcons[m_numDebugIcons].color = color;
  165. m_debugIcons[m_numDebugIcons].endFrame = TheGameLogic->getFrame()+numFramesDuration;
  166. m_numDebugIcons++;
  167. }
  168. /** Render draws into the current 3d context. */
  169. void W3DDebugIcons::Render(RenderInfoClass & rinfo)
  170. {
  171. //
  172. if (WW3D::Are_Static_Sort_Lists_Enabled()) {
  173. WW3D::Add_To_Static_Sort_List(this, 1);
  174. return;
  175. }
  176. //
  177. Bool anyVanished = false;
  178. if (m_numDebugIcons==0) return;
  179. DX8Wrapper::Apply_Render_State_Changes();
  180. DX8Wrapper::Set_Material(m_vertexMaterialClass);
  181. DX8Wrapper::Set_Texture(0, NULL);
  182. DX8Wrapper::Apply_Render_State_Changes();
  183. Matrix3D tm(Transform);
  184. DX8Wrapper::Set_Transform(D3DTS_WORLD,tm);
  185. Int numRect = m_numDebugIcons;
  186. static Real offset = 30;
  187. const Int MAX_RECT = 5000; // cap drawing n rects.
  188. if (numRect > MAX_RECT) numRect = MAX_RECT;
  189. offset+= 0.5f;
  190. Int k;
  191. for (k=0; k<m_numDebugIcons;) {
  192. Int curIndex = 0;
  193. Int numVertex = 0;
  194. DynamicVBAccessClass vb_access(BUFFER_TYPE_DYNAMIC_DX8,DX8_FVF_XYZNDUV2,numRect*4);
  195. DynamicIBAccessClass ib_access(BUFFER_TYPE_DYNAMIC_DX8,numRect*6);
  196. {
  197. DynamicVBAccessClass::WriteLockClass lock(&vb_access);
  198. VertexFormatXYZNDUV2* vb= lock.Get_Formatted_Vertex_Array();
  199. DynamicIBAccessClass::WriteLockClass lockib(&ib_access);
  200. if (!vb) return;
  201. UnsignedShort *ib=lockib.Get_Index_Array();
  202. UnsignedShort *curIb = ib;
  203. // VertexFormatXYZNDUV2 *curVb = vb;
  204. Real shadeR, shadeG, shadeB;
  205. shadeR = 0;
  206. shadeG = 0;
  207. shadeB = 255;
  208. for(; numVertex<numRect*4 && k<m_numDebugIcons; k++) {
  209. Int theAlpha = 64;
  210. const Int FADE_FRAMES = 100;
  211. Int framesLeft = m_debugIcons[k].endFrame - TheGameLogic->getFrame();
  212. if (framesLeft < 1) {
  213. anyVanished = true;
  214. continue;
  215. }
  216. if (framesLeft<FADE_FRAMES) {
  217. theAlpha *= (Real)framesLeft/FADE_FRAMES;
  218. }
  219. RGBColor clr = m_debugIcons[k].color;
  220. Real halfWidth = m_debugIcons[k].width/2;
  221. Int diffuse = clr.getAsInt() | ((int)theAlpha << 24);
  222. Coord3D pt1 = m_debugIcons[k].position;
  223. vb->x= pt1.x-halfWidth;
  224. vb->y= pt1.y-halfWidth;
  225. vb->z= pt1.z;
  226. vb->diffuse=diffuse; // b g<<8 r<<16 a<<24.
  227. vb->u1=0 ;
  228. vb->v1=0 ;
  229. vb++;
  230. vb->x= pt1.x+halfWidth;
  231. vb->y= pt1.y-halfWidth;
  232. vb->z= pt1.z;
  233. vb->diffuse=diffuse; // b g<<8 r<<16 a<<24.
  234. vb->u1=0 ;
  235. vb->v1=0 ;
  236. vb++;
  237. vb->x= pt1.x+halfWidth;
  238. vb->y= pt1.y+halfWidth;
  239. vb->z= pt1.z;
  240. vb->diffuse=diffuse; // b g<<8 r<<16 a<<24.
  241. vb->u1=0 ;
  242. vb->v1=0 ;
  243. vb++;
  244. vb->x= pt1.x-halfWidth;
  245. vb->y= pt1.y+halfWidth;
  246. vb->z= pt1.z;
  247. vb->diffuse=diffuse; // b g<<8 r<<16 a<<24.
  248. vb->u1=0 ;
  249. vb->v1=0 ;
  250. vb++;
  251. *curIb++ = numVertex;
  252. *curIb++ = numVertex+1;
  253. *curIb++ = numVertex+2;
  254. *curIb++ = numVertex;
  255. *curIb++ = numVertex+2;
  256. *curIb++ = numVertex+3;
  257. curIndex += 6;
  258. numVertex += 4;
  259. }
  260. }
  261. if (numVertex == 0) break;
  262. DX8Wrapper::Set_Shader(ShaderClass(SC_ALPHA));
  263. DX8Wrapper::Set_Index_Buffer(ib_access,0);
  264. DX8Wrapper::Set_Vertex_Buffer(vb_access);
  265. DX8Wrapper::Draw_Triangles( 0,curIndex/3, 0, numVertex); //draw a quad, 2 triangles, 4 verts
  266. }
  267. if (anyVanished) {
  268. compressIconsArray();
  269. }
  270. }
  271. #endif // _DEBUG or _INTERNAL