visrasterizer.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 : ww3d *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/visrasterizer.h $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 5/17/01 10:41a $*
  31. * *
  32. * $Revision:: 4 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #if defined(_MSC_VER)
  38. #pragma once
  39. #endif
  40. #ifndef VISRASTERIZER_H
  41. #define VISRASTERIZER_H
  42. #include "always.h"
  43. #include "matrix3d.h"
  44. #include "matrix4.h"
  45. #include "vector3i.h"
  46. #include "vector3.h"
  47. #include "simplevec.h"
  48. #include "bittype.h"
  49. #include "plane.h"
  50. class CameraClass;
  51. class AABoxClass;
  52. struct GradientsStruct;
  53. struct EdgeStruct;
  54. /**
  55. ** IDBufferClass
  56. ** This class manages the ID buffer and the Z buffer. It provides the low level
  57. ** rasterization code and stats about how many pixels and triangles are drawn.
  58. */
  59. class IDBufferClass
  60. {
  61. public:
  62. IDBufferClass(void);
  63. ~IDBufferClass(void);
  64. /*
  65. ** State interface
  66. */
  67. void Set_Resolution(int w,int h);
  68. void Get_Resolution(int * get_w,int * get_h);
  69. void Set_Backface_ID(uint32 id) { BackfaceID = id; }
  70. void Set_Frontface_ID(uint32 id) { FrontfaceID = id; }
  71. uint32 Get_Backface_ID(void) { return BackfaceID; }
  72. uint32 Get_Frontface_ID(void) { return FrontfaceID; }
  73. enum ModeType { OCCLUDER_MODE = 0, NON_OCCLUDER_MODE };
  74. void Set_Render_Mode(ModeType mode) { RenderMode = mode; }
  75. ModeType Get_Render_Mode(void) { return RenderMode; }
  76. void Reset_Pixel_Counter(void) { PixelCounter = 0; }
  77. int Get_Pixel_Counter(void) { return PixelCounter; }
  78. /*
  79. ** Rendering interface
  80. */
  81. void Clear(void);
  82. bool Render_Triangle(const Vector3 & p0,const Vector3 & p1,const Vector3 & p2);
  83. const uint32 * Get_Pixel_Row(int y,int min_x,int max_x);
  84. protected:
  85. void Reset(void);
  86. void Allocate_Buffers(void);
  87. bool Is_Backfacing(const Vector3 & p0,const Vector3 & p1,const Vector3 & p2);
  88. int Render_Occluder_Scanline(GradientsStruct & grads,EdgeStruct * left,EdgeStruct * right);
  89. int Render_Non_Occluder_Scanline(GradientsStruct & grads,EdgeStruct * left,EdgeStruct * right);
  90. int Pixel_Coords_To_Address(int x,int y) { return y*ResWidth + x; }
  91. uint32 BackfaceID;
  92. uint32 FrontfaceID;
  93. uint32 CurID;
  94. int PixelCounter;
  95. ModeType RenderMode;
  96. int ResWidth;
  97. int ResHeight;
  98. uint32 * IDBuffer;
  99. float * ZBuffer; // actually a 1/z buffer...
  100. };
  101. inline const uint32 * IDBufferClass::Get_Pixel_Row(int y,int min_x,int max_x)
  102. {
  103. WWASSERT(y>=0);
  104. WWASSERT(y<ResHeight);
  105. WWASSERT(min_x>=0);
  106. WWASSERT(max_x<=ResWidth);
  107. int addr = Pixel_Coords_To_Address(min_x,y);
  108. return &(IDBuffer[addr]);
  109. }
  110. inline bool IDBufferClass::Is_Backfacing(const Vector3 & p0,const Vector3 & p1,const Vector3 & p2)
  111. {
  112. float x1=p1[0]-p0[0];
  113. float y1=p1[1]-p0[1];
  114. float x2=p2[0]-p0[0];
  115. float y2=p2[1]-p0[1];
  116. float r=x1*y2-x2*y1;
  117. if (r<0.0f) return true;
  118. return false;
  119. }
  120. /**
  121. ** VisRasterizerClass
  122. ** This class encapsulates the "ID buffer rasterization" code needed by the vis system. Basically
  123. ** it is a floating point z-buffer and an id buffer which is used by the visiblity precalculation system.
  124. ** The VisRasterizer will transform and clip triangles into homogeneous view space; then the clipped
  125. ** triangles will be passed on to the IDBufferClass which will scan convert them.
  126. */
  127. class VisRasterizerClass
  128. {
  129. public:
  130. VisRasterizerClass(void);
  131. ~VisRasterizerClass(void);
  132. /*
  133. ** ID Buffer Interface
  134. */
  135. void Set_Render_Mode(IDBufferClass::ModeType mode) { IDBuffer.Set_Render_Mode(mode); }
  136. IDBufferClass::ModeType Get_Render_Mode(void) { return IDBuffer.Get_Render_Mode(); }
  137. void Set_Backface_ID(uint32 id) { IDBuffer.Set_Backface_ID(id); }
  138. void Set_Frontface_ID(uint32 id) { IDBuffer.Set_Frontface_ID(id); }
  139. uint32 Get_Backface_ID(void) { return IDBuffer.Get_Backface_ID(); }
  140. uint32 Get_Frontface_ID(void) { return IDBuffer.Get_Frontface_ID(); }
  141. void Set_Resolution(int width,int height);
  142. void Get_Resolution(int * set_width,int * set_height);
  143. void Reset_Pixel_Counter(void) { IDBuffer.Reset_Pixel_Counter(); }
  144. int Get_Pixel_Counter(void) { return IDBuffer.Get_Pixel_Counter(); }
  145. /*
  146. ** Rendering Interface
  147. */
  148. void Set_Model_Transform(const Matrix3D & model);
  149. void Set_Camera(CameraClass * camera);
  150. const Matrix3D & Get_Model_Transform(void);
  151. CameraClass * Get_Camera(void);
  152. CameraClass * Peek_Camera(void);
  153. void Clear(void) { IDBuffer.Clear(); }
  154. bool Render_Triangles(const Vector3 * verts,int vcount,const Vector3i * tris, int tcount,const AABoxClass & bounds);
  155. const uint32 * Get_Pixel_Row(int y,int min_x,int max_x) { return IDBuffer.Get_Pixel_Row(y,min_x,max_x); }
  156. protected:
  157. void Update_MV_Transform(void);
  158. const Matrix3D & Get_MV_Transform(void);
  159. Vector3 * Get_Temp_Vertex_Buffer(int count);
  160. bool Render_Triangles_Clip(const Vector3 * verts,int vcount,const Vector3i * tris, int tcount);
  161. bool Render_Triangles_No_Clip(const Vector3 * verts,int vcount,const Vector3i * tris, int tcount);
  162. Matrix3D ModelTransform; // AKA "World Transform"
  163. CameraClass * Camera;
  164. Matrix3D MVTransform;
  165. IDBufferClass IDBuffer;
  166. SimpleVecClass<Vector3> TempVertexBuffer;
  167. };
  168. #endif //VISRASTERIZER_H