HeightMap.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #pragma once
  24. #ifndef __HEIGHTMAP_H_
  25. #define __HEIGHTMAP_H_
  26. #include "always.h"
  27. #include "rendobj.h"
  28. #include "w3d_file.h"
  29. #include "dx8vertexbuffer.h"
  30. #include "dx8indexbuffer.h"
  31. #include "dx8wrapper.h"
  32. #include "shader.h"
  33. #include "vertmaterial.h"
  34. #include "Lib/BaseType.h"
  35. #include "common/GameType.h"
  36. #include "WorldHeightMap.h"
  37. #include "BaseHeightMap.h"
  38. #define VERTEX_BUFFER_TILE_LENGTH 32 //tiles of side length 32 (grid of 33x33 vertices).
  39. // Adjust the triangles to make cliff sides most attractive. jba.
  40. #define FLIP_TRIANGLES 1
  41. /// Custom render object that draws the heightmap and handles intersection tests.
  42. /**
  43. Custom W3D render object that's used to process the terrain. It handles
  44. virtually everything to do with the terrain, including: drawing, lighting,
  45. scorchmarks and intersection tests.
  46. */
  47. class HeightMapRenderObjClass : public BaseHeightMapRenderObjClass
  48. {
  49. public:
  50. HeightMapRenderObjClass(void);
  51. virtual ~HeightMapRenderObjClass(void);
  52. // DX8_CleanupHook methods
  53. virtual void ReleaseResources(void); ///< Release all dx8 resources so the device can be reset.
  54. virtual void ReAcquireResources(void); ///< Reacquire all resources after device reset.
  55. /////////////////////////////////////////////////////////////////////////////
  56. // Render Object Interface (W3D methods)
  57. /////////////////////////////////////////////////////////////////////////////
  58. virtual void Render(RenderInfoClass & rinfo);
  59. virtual void On_Frame_Update(void);
  60. ///allocate resources needed to render heightmap
  61. virtual int initHeightData(Int width, Int height, WorldHeightMap *pMap, RefRenderObjListIterator *pLightsIterator, Bool updateExtraPassTiles=TRUE);
  62. virtual Int freeMapResources(void); ///< free resources used to render heightmap
  63. virtual void updateCenter(CameraClass *camera, RefRenderObjListIterator *pLightsIterator);
  64. void renderExtraBlendTiles(void); ///< render 3-way blend tiles that have blend of 3 textures.
  65. virtual void staticLightingChanged(void);
  66. virtual void adjustTerrainLOD(Int adj);
  67. virtual void reset(void);
  68. virtual void doPartialUpdate(const IRegion2D &partialRange, WorldHeightMap *htMap, RefRenderObjListIterator *pLightsIterator);
  69. virtual void oversizeTerrain(Int tilesToOversize);
  70. virtual int updateBlock(Int x0, Int y0, Int x1, Int y1, WorldHeightMap *pMap, RefRenderObjListIterator *pLightsIterator);
  71. protected:
  72. Int *m_extraBlendTilePositions; ///<array holding x,y tile positions of all extra blend tiles. (used for 3 textures per tile).
  73. Int m_numExtraBlendTiles; ///<number of blend tiles in m_extraBlendTilePositions.
  74. Int m_numVisibleExtraBlendTiles; ///<number rendered last frame.
  75. Int m_extraBlendTilePositionsSize; //<total size of array including unused memory.
  76. DX8VertexBufferClass **m_vertexBufferTiles; ///<collection of smaller vertex buffers that make up 1 heightmap
  77. char **m_vertexBufferBackup; ///< In memory copy of the vertex buffer data for quick update of dynamic lighting.
  78. Int m_originX; ///< Origin point in the grid. Slides around.
  79. Int m_originY; ///< Origin point in the grid. Slides around.
  80. DX8IndexBufferClass *m_indexBuffer; ///<indices defining triangles in a VB tile.
  81. Int m_numVBTilesX; ///<dimensions of array containing all the vertex buffers
  82. Int m_numVBTilesY; ///<dimensions of array containing all the vertex buffers
  83. Int m_numVertexBufferTiles; ///<number of vertex buffers needed to store this heightmap
  84. Int m_numBlockColumnsInLastVB;///<a VB tile may be partially filled, this indicates how many 2x2 vertex blocks are filled.
  85. Int m_numBlockRowsInLastVB;///<a VB tile may be partially filled, this indicates how many 2x2 vertex blocks are filled.
  86. UnsignedInt doTheDynamicLight(VERTEX_FORMAT *vb, VERTEX_FORMAT *vbMirror, Vector3*light, Vector3*normal, W3DDynamicLight *pLights[], Int numLights);
  87. Int getXWithOrigin(Int x);
  88. Int getYWithOrigin(Int x);
  89. ///update vertex diffuse color for dynamic lights inside given rectangle
  90. Int updateVBForLight(DX8VertexBufferClass *pVB, char *data, Int x0, Int y0, Int x1, Int y1, Int originX, Int originY, W3DDynamicLight *pLights[], Int numLights);
  91. Int updateVBForLightOptimized(DX8VertexBufferClass *pVB, char *data, Int x0, Int y0, Int x1, Int y1, Int originX, Int originY, W3DDynamicLight *pLights[], Int numLights);
  92. ///update vertex buffer vertices inside given rectangle
  93. Int updateVB(DX8VertexBufferClass *pVB, char *data, Int x0, Int y0, Int x1, Int y1, Int originX, Int originY, WorldHeightMap *pMap, RefRenderObjListIterator *pLightsIterator);
  94. ///upate vertex buffers associated with the given rectangle
  95. void initDestAlphaLUT(void); ///<initialize water depth LUT stored in m_destAlphaTexture
  96. void renderTerrainPass(CameraClass *pCamera); ///< renders additional terrain pass.
  97. Int getNumExtraBlendTiles(Bool visible) { return visible?m_numVisibleExtraBlendTiles:m_numExtraBlendTiles;}
  98. void freeIndexVertexBuffers(void);
  99. };
  100. #endif // end __HEIGHTMAP_H_