pointgr.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. *** 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 : G *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/pointgr.h $*
  25. * *
  26. * $Author:: Naty_h $*
  27. * *
  28. * $Modtime:: 8/02/01 8:34p $*
  29. * *
  30. * $Revision:: 10 $*
  31. * *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef POINTGR_H
  39. #define POINTGR_H
  40. #include "sharebuf.h"
  41. #include "shader.h"
  42. #include "vector4.h"
  43. #include "vector3.h"
  44. #include "vector2.h"
  45. #include "vector.h"
  46. class VertexMaterialClass;
  47. class RenderInfoClass;
  48. class TextureClass;
  49. /*
  50. ** PointGroupClass -- a custom object for rendering
  51. ** groups of points (such as particle systems).
  52. ** It is possible to change mode/number of points/shader/etc. but these
  53. ** changes tend to be expensive if done often. Expected usage is to set the
  54. ** point location/color/active arrays each frame the object is visible (with
  55. ** the same number of points) and perform other changes relatively rarely.
  56. ** NOTE: Currently it is implemented using general triangles (1 or 2 per
  57. ** point), so it is probably suboptimal for software rasterization devices
  58. ** (which would probably perform better with some kind of blit/sprite code).
  59. */
  60. class PointGroupClass
  61. {
  62. public:
  63. enum PointModeEnum {
  64. TRIS, // each point is a triangle
  65. QUADS, // each point is a quad formed out of two triangles
  66. SCREENSPACE // each point is a tri placed to affect certain pixels (should be used with 2D camera)
  67. };
  68. enum FlagsType {
  69. TRANSFORM, // transform points w. modelview matrix (worldspace points)
  70. };
  71. PointGroupClass(void);
  72. virtual ~PointGroupClass(void);
  73. PointGroupClass & operator = (const PointGroupClass & that);
  74. // PointGroupClass interface:
  75. void Set_Arrays(ShareBufferClass<Vector3> *locs,
  76. ShareBufferClass<Vector4> *diffuse = NULL,
  77. ShareBufferClass<unsigned int> *apt = NULL,
  78. ShareBufferClass<float> *sizes = NULL,
  79. ShareBufferClass<unsigned char> *orientations = NULL,
  80. ShareBufferClass<unsigned char> *frames = NULL,
  81. unsigned int active_point_count = -1,
  82. float vpxmin = 0.0f, float vpymin = 0.0f,
  83. float vpxmax = 0.0f, float vpymax = 0.0f);
  84. void Set_Point_Size(float size);
  85. float Get_Point_Size(void);
  86. void Set_Point_Color(Vector3 color);
  87. Vector3 Get_Point_Color(void);
  88. void Set_Point_Alpha(float alpha);
  89. float Get_Point_Alpha(void);
  90. void Set_Point_Orientation(unsigned char orientation);
  91. unsigned char Get_Point_Orientation(void);
  92. void Set_Point_Frame(unsigned char frame);
  93. unsigned char Get_Point_Frame(void);
  94. void Set_Point_Mode(PointModeEnum mode);
  95. PointModeEnum Get_Point_Mode(void);
  96. void Set_Flag(FlagsType flag, bool onoff);
  97. int Get_Flag(FlagsType flag);
  98. void Set_Texture(TextureClass* texture);
  99. TextureClass * Get_Texture(void);
  100. TextureClass * Peek_Texture(void);
  101. void Set_Shader(ShaderClass shader);
  102. ShaderClass Get_Shader(void);
  103. void Set_Billboard(bool shouldBillboard);
  104. bool Get_Billboard(void);
  105. // The frame property is taken from a set of possible frames. The rows/columns in the frame
  106. // texture determine the number of possible frames. Since it must be a power of 2, we represent
  107. // it as its log base 2. This number cannot be greater than 4 (which corresponds to a 16x16
  108. // square of frames, i.e. 256 frames).
  109. unsigned char Get_Frame_Row_Column_Count_Log2(void);
  110. void Set_Frame_Row_Column_Count_Log2(unsigned char frccl2);
  111. int Get_Polygon_Count(void);
  112. void Render(RenderInfoClass &rinfo);
  113. void RenderVolumeParticle(RenderInfoClass &rinfo, unsigned int depth);
  114. protected:
  115. // Update arrays.
  116. void Update_Arrays(Vector3 *point_loc,
  117. Vector4 *point_diffuse,
  118. float *point_size,
  119. unsigned char *point_orientation,
  120. unsigned char *point_frame,
  121. int active_points,
  122. int total_points,
  123. int &vnum,
  124. int &pnum);
  125. // These shared buffers are used for communication to the point group - to
  126. // pass point locations, colors and enables. The location and color arrays
  127. // are 'compressed' using the active point table (if present) and then
  128. // are processed into other arrays which are passed to the GERD.
  129. // SR rather than WWMath types are used so Vector Processors can be used.
  130. // The arrays override the default value if present.
  131. // The orientation and frame properties are "index properties": they select one out of a small
  132. // group of possibilities for each point.
  133. // Orientation: this is the 2D rotation of the point about its center. There are 256 discrete
  134. // orientations. The unit circle is evenly subdivided 256 times to create the set of
  135. // orientations. The reason we discretize orientation is for performance: this way we can
  136. // precalculate vertex offsets.
  137. // Frame: the texture is divided into a 2D square grid, and each point uses one grid square
  138. // instead of the whole texture. The number of possible frames is not invariant - it depends
  139. // on the number of rows / columns in the grid. The reason we do this instead of having
  140. // different textures is to avoid texture state changes. Also for performance reasons, the
  141. // number of possible frames must be a power of two - for this reason the number of frame rows
  142. // and columns, orientations, etc. are represented as the log base 2 of the actual number.
  143. ShareBufferClass<Vector3> * PointLoc; // World/cameraspace point locs
  144. ShareBufferClass<Vector4> * PointDiffuse; // (NULL if not used) RGBA values
  145. ShareBufferClass<unsigned int> * APT; // (NULL if not used) active point table
  146. ShareBufferClass<float> * PointSize; // (NULL if not used) size override table
  147. ShareBufferClass<unsigned char> * PointOrientation; // (NULL if not used) orientation indices
  148. ShareBufferClass<unsigned char> * PointFrame; // (NULL if not used) frame indices
  149. int PointCount; // Active (if APT) or total point count
  150. // See comments for Get/Set_Frame_Row_Column_Count_Log2 above
  151. unsigned char FrameRowColumnCountLog2; // MUST be equal or lesser than 4
  152. // These parameters are passed to the GERD:
  153. TextureClass* Texture;
  154. ShaderClass Shader; // (default created in CTor)
  155. // Internal state:
  156. PointModeEnum PointMode; // are points tris or quads?
  157. unsigned int Flags; // operation control flags
  158. float DefaultPointSize; // point size (size array overrides if present)
  159. Vector3 DefaultPointColor; // point color (color array overrides if present)
  160. float DefaultPointAlpha; // point alpha (alpha array overrides if present)
  161. unsigned char DefaultPointOrientation;// point orientation (orientation array overrides if present)
  162. unsigned char DefaultPointFrame; // point texture frame (frame array overrides if present)
  163. // View plane rectangle (only used in SCREENSPACE mode - set by Set_Arrays
  164. // and used in Update_GERD_Arrays).
  165. float VPXMin;
  166. float VPYMin;
  167. float VPXMax;
  168. float VPYMax;
  169. bool Billboard;
  170. // Static stuff:
  171. // For performance / memory reasons we prepare vertex location and UV
  172. // arrays for various orientations and texture frames, as static data
  173. // members of this class. This avoids the need to create such arrays over
  174. // again for each object. The values in the UV arrays are used as-is, the
  175. // values in the location arrays normally need to be scaled to the point
  176. // sizes and added to the point location before use. We have distinct sets
  177. // of arrays for each point mode.
  178. // There are tables of vertex positions for each of triangle and quad mode
  179. // with 256 discrete orientations. Each entry contains 3 (for triangle
  180. // mode) or 4 (for quad mode) Vector3s.
  181. // There are five tables of texture UVs for each of triangle and quad mode:
  182. // with 1x1(1), 2x2(4), 4x4(16), 8x8(64) and 16x16(256) frames. Each entry
  183. // contains 3 (for triangle mode) or 4 (for quad mode) Vector2s.
  184. // In addition, there is one array of vertex positions for screenspace mode
  185. // with 2 entries (for size 1 and size 2). Each entry contains 3 Vector3s.
  186. // the Init function (which is called by WW3D::Init()) creates these
  187. // arrays, and the Shutdown function (which is called by WW3D::Shutdown()
  188. // releases them.
  189. public:
  190. static void _Init(void);
  191. static void _Shutdown(void);
  192. private:
  193. static Vector3 _TriVertexLocationOrientationTable[256][3];
  194. static Vector3 _QuadVertexLocationOrientationTable[256][4];
  195. static Vector3 _ScreenspaceVertexLocationSizeTable[2][3];
  196. static Vector2 *_TriVertexUVFrameTable[5];
  197. static Vector2 *_QuadVertexUVFrameTable[5];
  198. static VertexMaterialClass *PointMaterial;
  199. // Static arrays for intermediate calcs (never resized down, just up):
  200. static VectorClass<Vector3> compressed_loc; // point locations 'compressed' by APT
  201. static VectorClass<Vector4> compressed_diffuse; // point colors 'compressed' by APT
  202. static VectorClass<float> compressed_size; // point sizes 'compressed' by APT
  203. static VectorClass<unsigned char> compressed_orient; // point orientations 'compressed' by APT
  204. static VectorClass<unsigned char> compressed_frame; // point frames 'compressed' by APT
  205. static VectorClass<Vector3> transformed_loc; // transformed point locations
  206. };
  207. class SegmentGroupClass : public PointGroupClass
  208. {
  209. public:
  210. SegmentGroupClass(void);
  211. virtual ~SegmentGroupClass(void);
  212. };
  213. #endif