visrasterizer.h 7.8 KB

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