W3DPropBuffer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: W3DPropBuffer.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: W3DPropBuffer.cpp
  36. //
  37. // Created: John Ahlquist, May 2001
  38. //
  39. // Desc: Draw buffer to handle all the props in a scene.
  40. //
  41. //-----------------------------------------------------------------------------
  42. //-----------------------------------------------------------------------------
  43. // Includes
  44. //-----------------------------------------------------------------------------
  45. #include "W3DDevice/GameClient/W3DPropBuffer.h"
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <assetmgr.h>
  49. #include "Common/Geometry.h"
  50. #include "Common/PerfTimer.h"
  51. #include "Common/Player.h"
  52. #include "Common/PlayerList.h"
  53. #include "WW3D2/Camera.h"
  54. #include "WW3D2/RInfo.h"
  55. #include "WW3D2/Light.h"
  56. #include "WW3D2/DX8Wrapper.h"
  57. #include "WW3D2/dx8renderer.h"
  58. #include "W3DDevice/GameClient/Module/W3DPropDraw.h"
  59. #include "W3DDevice/GameClient/W3DShroud.h"
  60. #include "W3DDevice/GameClient/BaseHeightMap.h"
  61. #include "GameLogic/PartitionManager.h"
  62. #ifdef _INTERNAL
  63. // for occasional debugging...
  64. //#pragma optimize("", off)
  65. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  66. #endif
  67. //-----------------------------------------------------------------------------
  68. // Private Functions
  69. //-----------------------------------------------------------------------------
  70. //=============================================================================
  71. // W3DPropBuffer::cull
  72. //=============================================================================
  73. /** Culls the props, marking the visible flag. If a prop becomes visible, it sets
  74. it's sortKey */
  75. //=============================================================================
  76. void W3DPropBuffer::cull(CameraClass * camera)
  77. {
  78. Int curProp;
  79. for (curProp=0; curProp<m_numProps; curProp++) {
  80. Bool visible = !camera->Cull_Sphere(m_props[curProp].bounds);
  81. m_props[curProp].visible=visible;
  82. }
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Public Functions
  86. //-----------------------------------------------------------------------------
  87. //=============================================================================
  88. // W3DPropBuffer::~W3DPropBuffer
  89. //=============================================================================
  90. /** Destructor. Releases w3d assets. */
  91. //=============================================================================
  92. W3DPropBuffer::~W3DPropBuffer(void)
  93. {
  94. Int i;
  95. for (i=0; i<MAX_TYPES; i++) {
  96. REF_PTR_RELEASE(m_propTypes[i].m_robj);
  97. }
  98. REF_PTR_RELEASE(m_light);
  99. REF_PTR_RELEASE(m_propShroudMaterialPass);
  100. }
  101. //=============================================================================
  102. // W3DPropBuffer::W3DPropBuffer
  103. //=============================================================================
  104. /** Constructor. Sets m_initialized to true if it finds the w3d models it needs
  105. for the props. */
  106. //=============================================================================
  107. W3DPropBuffer::W3DPropBuffer(void)
  108. {
  109. memset(this, sizeof(W3DPropBuffer), 0);
  110. m_initialized = false;
  111. clearAllProps();
  112. m_light = NEW_REF( LightClass, (LightClass::DIRECTIONAL) );
  113. m_propShroudMaterialPass = NEW_REF(W3DShroudMaterialPassClass,());
  114. m_initialized = true;
  115. }
  116. //=============================================================================
  117. // W3DPropBuffer::clearAllProps
  118. //=============================================================================
  119. /** Removes all props. */
  120. //=============================================================================
  121. void W3DPropBuffer::clearAllProps(void)
  122. {
  123. m_numProps=0;
  124. Int i;
  125. for (i=0; i<MAX_TYPES; i++) {
  126. REF_PTR_RELEASE(m_propTypes[i].m_robj);
  127. m_propTypes[i].m_robjName.clear();
  128. }
  129. m_numPropTypes = 0;
  130. }
  131. //=============================================================================
  132. // W3DPropBuffer::addPropTypes
  133. //=============================================================================
  134. /** Adds a type of prop (model & texture). */
  135. //=============================================================================
  136. Int W3DPropBuffer::addPropType(const AsciiString &modelName)
  137. {
  138. if (m_numPropTypes>=MAX_TYPES) {
  139. DEBUG_CRASH(("Too many kinds of props in map. Reduce kinds of props, or raise prop limit. jba."));
  140. return 0;
  141. }
  142. m_propTypes[m_numPropTypes].m_robj = WW3DAssetManager::Get_Instance()->Create_Render_Obj(modelName.str());
  143. if (m_propTypes[m_numPropTypes].m_robj==NULL) {
  144. DEBUG_CRASH(("Unable to find model for prop %s\n", modelName.str()));
  145. return -1;
  146. }
  147. m_propTypes[m_numPropTypes].m_robjName = modelName;
  148. SphereClass bounds = m_propTypes[m_numPropTypes].m_robj->Get_Bounding_Sphere();
  149. m_propTypes[m_numPropTypes].m_bounds = bounds;
  150. m_numPropTypes++;
  151. return m_numPropTypes-1;
  152. }
  153. //=============================================================================
  154. // W3DPropBuffer::addProp
  155. //=============================================================================
  156. /** Adds a prop. Name is the W3D model name, supported models are
  157. ALPINE, DECIDUOUS and SHRUB. */
  158. //=============================================================================
  159. void W3DPropBuffer::addProp(Int id, Coord3D location, Real angle,Real scale, const AsciiString &modelName)
  160. {
  161. if (m_numProps >= MAX_PROPS) {
  162. return;
  163. }
  164. if (!m_initialized) {
  165. return;
  166. }
  167. Int propType = -1;
  168. Int i;
  169. for (i=0; i<m_numPropTypes; i++) {
  170. if (m_propTypes[i].m_robjName.compareNoCase(modelName)==0) {
  171. propType = i;
  172. break;
  173. }
  174. }
  175. if (propType<0) {
  176. propType = addPropType(modelName);
  177. if (propType<0) {
  178. return;
  179. }
  180. }
  181. Matrix3D mtx(true);
  182. mtx.Rotate_Z(angle);
  183. mtx.Scale(scale);
  184. mtx.Set_Translation(Vector3(location.x, location.y, location.z));
  185. m_props[m_numProps].location = location;
  186. m_props[m_numProps].id = id;
  187. m_props[m_numProps].ss = OBJECTSHROUD_INVALID;
  188. m_props[m_numProps].m_robj = m_propTypes[propType].m_robj->Clone();
  189. m_props[m_numProps].m_robj->Set_Transform(mtx);
  190. m_props[m_numProps].m_robj->Set_ObjectScale(scale);
  191. m_props[m_numProps].propType = propType;
  192. // Translate the bounding sphere of the model.
  193. m_props[m_numProps].bounds = m_propTypes[propType].m_bounds;
  194. m_props[m_numProps].bounds.Center += Vector3(location.x, location.y, location.z);
  195. // Initially set it invisible. cull will update it's visiblity flag.
  196. m_props[m_numProps].visible = false;
  197. m_numProps++;
  198. }
  199. //=============================================================================
  200. // W3DPropBuffer::updatePropPosition
  201. //=============================================================================
  202. /** Updates a prop's position */
  203. //=============================================================================
  204. Bool W3DPropBuffer::updatePropPosition(Int id, const Coord3D &location, Real angle, Real scale)
  205. {
  206. Int i;
  207. for (i=0; i<m_numProps; i++) {
  208. if (m_props[i].id == id) {
  209. Matrix3D mtx(true);
  210. mtx.Rotate_Z(angle);
  211. mtx.Scale(scale);
  212. mtx.Set_Translation(Vector3(location.x, location.y, location.z));
  213. m_props[i].location = location;
  214. m_props[i].m_robj->Set_Transform(mtx);
  215. m_props[i].m_robj->Set_ObjectScale(scale);
  216. // Translate the bounding sphere of the model.
  217. m_props[i].bounds = m_propTypes[m_props[i].propType].m_bounds;
  218. m_props[i].bounds.Center += Vector3(location.x, location.y, location.z);
  219. m_anythingChanged = true;
  220. return true;
  221. }
  222. }
  223. return false;
  224. }
  225. //=============================================================================
  226. // W3DPropBuffer::removeProp
  227. //=============================================================================
  228. /** Removes a prop. */
  229. //=============================================================================
  230. void W3DPropBuffer::removeProp(Int id)
  231. {
  232. Int i;
  233. for (i=0; i<m_numProps; i++) {
  234. if (m_props[i].id == id) {
  235. m_props[i].location.set(0,0,0);
  236. m_props[i].propType = -1;
  237. REF_PTR_RELEASE(m_props[i].m_robj);
  238. // Translate the bounding sphere of the model.
  239. m_props[i].bounds.Center = Vector3(0,0,0);
  240. m_props[i].bounds.Radius = 1;
  241. m_anythingChanged = true;
  242. }
  243. }
  244. }
  245. //=============================================================================
  246. // W3DPropBuffer::removePropsForConstruction
  247. //=============================================================================
  248. /** Removes any props that would be under a building. */
  249. //=============================================================================
  250. void W3DPropBuffer::removePropsForConstruction(const Coord3D* pos, const GeometryInfo& geom, Real angle )
  251. {
  252. // Just iterate all trees, as even non-collidable ones get removed. jba. [7/11/2003]
  253. Int i;
  254. for (i=0; i<m_numProps; i++) {
  255. if (m_props[i].m_robj == NULL) {
  256. continue; // already deleted.
  257. }
  258. Real radius = m_props[i].bounds.Radius;
  259. GeometryInfo info(GEOMETRY_CYLINDER, false, 5*radius, 2*radius, 2*radius);
  260. if (ThePartitionManager->geomCollidesWithGeom( pos, geom, angle, &m_props[i].location, info, 0.0f)) {
  261. // remove it [7/11/2003]
  262. m_props[i].location.set(0,0,0);
  263. m_props[i].propType = -1;
  264. REF_PTR_RELEASE(m_props[i].m_robj);
  265. // Translate the bounding sphere of the model.
  266. m_props[i].bounds.Center = Vector3(0,0,0);
  267. m_props[i].bounds.Radius = 1;
  268. m_anythingChanged = true;
  269. }
  270. }
  271. }
  272. //=============================================================================
  273. // W3DPropBuffer::notifyShroudChanged
  274. //=============================================================================
  275. /** Sets the shroud to status, so it is recomputed. */
  276. //=============================================================================
  277. void W3DPropBuffer::notifyShroudChanged()
  278. {
  279. Int i;
  280. for (i=0; i<m_numProps; i++) {
  281. m_props[i].ss = ThePartitionManager?OBJECTSHROUD_INVALID:OBJECTSHROUD_CLEAR;
  282. }
  283. }
  284. DECLARE_PERF_TIMER(Prop_Render)
  285. //=============================================================================
  286. // W3DPropBuffer::drawProps
  287. //=============================================================================
  288. /** Draws the props. Uses camera to cull. */
  289. //=============================================================================
  290. void W3DPropBuffer::drawProps(RenderInfoClass &rinfo)
  291. {
  292. USE_PERF_TIMER(Prop_Render)
  293. Int i;
  294. if (m_doCull) {
  295. cull(&rinfo.Camera);
  296. }
  297. const GlobalData::TerrainLighting *objectLighting = TheGlobalData->m_terrainObjectsLighting[TheGlobalData->m_timeOfDay];
  298. LightEnvironmentClass lightEnv;
  299. Vector3 center(0,0,0); // arbitrary center point. [6/6/2003]
  300. Vector3 ambient(objectLighting[0].ambient.red, objectLighting[0].ambient.green, objectLighting[0].ambient.blue);
  301. lightEnv.Reset(center, ambient);
  302. Matrix3D mtx;
  303. const Vector3 zeroVector(0.0f, 0.0f, 0.0f);
  304. const Vector3 xVector(1.0f, 0.0f, 0.0f);
  305. const Vector3 yVector(0.0f, 1.0f, 0.0f);
  306. for (i = 0; i < MAX_GLOBAL_LIGHTS; ++i)
  307. {
  308. m_light->Set_Ambient(zeroVector);
  309. m_light->Set_Diffuse(Vector3(objectLighting[i].diffuse.red,
  310. objectLighting[i].diffuse.green,
  311. objectLighting[i].diffuse.blue));
  312. m_light->Set_Specular(zeroVector);
  313. mtx.Set(xVector, yVector, Vector3(objectLighting[i].lightPos.x, objectLighting[i].lightPos.y, objectLighting[i].lightPos.z), zeroVector);
  314. m_light->Set_Transform(mtx);
  315. lightEnv.Add_Light(*m_light);
  316. }
  317. rinfo.light_environment = &lightEnv;
  318. for (i=0; i<m_numProps; i++) {
  319. if (!m_props[i].visible) {
  320. continue;
  321. }
  322. if (m_props[i].m_robj==NULL) {
  323. continue;
  324. }
  325. if (!ThePlayerList || !ThePartitionManager) {
  326. // in Worldbuilder. No shroud either. jba. [6/9/2003]
  327. m_props[i].ss = OBJECTSHROUD_CLEAR;
  328. }
  329. if (m_props[i].ss == OBJECTSHROUD_INVALID) {
  330. Int localPlayerIndex = ThePlayerList ? ThePlayerList->getLocalPlayer()->getPlayerIndex() : 0;
  331. m_props[i].ss = ThePartitionManager->getPropShroudStatusForPlayer(localPlayerIndex, &m_props[i].location);
  332. }
  333. if (m_props[i].ss >= OBJECTSHROUD_SHROUDED) {
  334. continue;
  335. }
  336. if (m_props[i].ss <= OBJECTSHROUD_INVALID) {
  337. continue;
  338. }
  339. if (TheTerrainRenderObject->getShroud() && m_props[i].ss != CELLSHROUD_CLEAR) {
  340. rinfo.Push_Material_Pass(m_propShroudMaterialPass);
  341. m_props[i].m_robj->Render(rinfo);
  342. rinfo.Pop_Material_Pass();
  343. } else {
  344. m_props[i].m_robj->Render(rinfo);
  345. }
  346. }
  347. rinfo.light_environment = NULL;
  348. }
  349. // ------------------------------------------------------------------------------------------------
  350. /** CRC */
  351. // ------------------------------------------------------------------------------------------------
  352. void W3DPropBuffer::crc( Xfer *xfer )
  353. {
  354. // empty. jba [8/11/2003]
  355. } // end CRC
  356. // ------------------------------------------------------------------------------------------------
  357. /** Xfer
  358. * Version Info:
  359. * 1: Initial version */
  360. // ------------------------------------------------------------------------------------------------
  361. void W3DPropBuffer::xfer( Xfer *xfer )
  362. {
  363. // version
  364. XferVersion currentVersion = 1;
  365. XferVersion version = currentVersion;
  366. xfer->xferVersion( &version, currentVersion );
  367. } // end xfer
  368. // ------------------------------------------------------------------------------------------------
  369. /** Load post process */
  370. // ------------------------------------------------------------------------------------------------
  371. void W3DPropBuffer::loadPostProcess( void )
  372. {
  373. // empty. jba [8/11/2003]
  374. } // end loadPostProcess