visrendercontext.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 : WWPhys *
  23. * *
  24. * $Archive:: /Commando/Code/wwphys/visrendercontext.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 5/17/01 10:42a $*
  29. * *
  30. * $Revision:: 16 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * VisRenderContextClass::VisRenderContextClass -- Constructor *
  35. * VisRenderContextClass::Set_Vis_ID -- set the currently active Vis ID *
  36. * VisRenderContextClass::Set_Resolution -- set the vis rendering resolution *
  37. * VisRenderContextClass::Get_Resolution -- get the current vis rendering resolution *
  38. * VisRenderContextClass::Scan_Frame_Buffer -- scan the frame buffer for visible objects *
  39. * VisRenderContextClass::Compute_2D_Bounds -- compute the 2D bounds of a 3D box *
  40. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  41. #include "visrendercontext.h"
  42. #include "simplevec.h"
  43. #include "win.h"
  44. #include "rawfile.h"
  45. #include "visrasterizer.h"
  46. const int CLEAR_VIS_COLOR = 0x00000000; // Vis id for background/clear pixels
  47. const float BACKFACE_OVERFLOW_FRACTION = 0.005f; // max percentage of backface before overflow (rejection)
  48. static VisRasterizerClass _VisRasterizer; // Instance of a vis rasterizer
  49. /***********************************************************************************************
  50. **
  51. ** VisRenderContextClass Implementation
  52. **
  53. ***********************************************************************************************/
  54. /***********************************************************************************************
  55. * VisRenderContextClass::VisRenderContextClass -- Constructor *
  56. * *
  57. * INPUT: *
  58. * *
  59. * OUTPUT: *
  60. * *
  61. * WARNINGS: *
  62. * *
  63. * HISTORY: *
  64. * 7/18/2000 gth : Created. *
  65. *=============================================================================================*/
  66. VisRenderContextClass::VisRenderContextClass
  67. (
  68. CameraClass & cam,
  69. VisTableClass & vtab
  70. ) :
  71. SpecialRenderInfoClass(cam,RENDER_VIS),
  72. VisTable(vtab)
  73. {
  74. VisRasterizer = &_VisRasterizer;
  75. VisRasterizer->Set_Camera(&cam);
  76. }
  77. VisRenderContextClass::~VisRenderContextClass(void)
  78. {
  79. VisRasterizer->Set_Camera(NULL);
  80. }
  81. /***********************************************************************************************
  82. * VisRenderContextClass::Set_Vis_ID -- set the currently active Vis ID *
  83. * *
  84. * INPUT: *
  85. * *
  86. * OUTPUT: *
  87. * *
  88. * WARNINGS: *
  89. * *
  90. * HISTORY: *
  91. * 7/18/2000 gth : Created. *
  92. *=============================================================================================*/
  93. void VisRenderContextClass::Set_Vis_ID(uint32 id)
  94. {
  95. WWASSERT(id < BACKFACE_VIS_ID);
  96. _VisRasterizer.Set_Frontface_ID(id);
  97. _VisRasterizer.Set_Backface_ID((uint32)BACKFACE_VIS_ID);
  98. }
  99. /***********************************************************************************************
  100. * VisRenderContextClass::Set_Resolution -- set the vis rendering resolution *
  101. * *
  102. * INPUT: *
  103. * *
  104. * OUTPUT: *
  105. * *
  106. * WARNINGS: *
  107. * *
  108. * HISTORY: *
  109. * 3/29/2001 gth : Created. *
  110. *=============================================================================================*/
  111. void VisRenderContextClass::Set_Resolution(int resx,int resy)
  112. {
  113. _VisRasterizer.Set_Resolution(resx,resy);
  114. }
  115. /***********************************************************************************************
  116. * VisRenderContextClass::Get_Resolution -- get the current vis rendering resolution *
  117. * *
  118. * INPUT: *
  119. * *
  120. * OUTPUT: *
  121. * *
  122. * WARNINGS: *
  123. * *
  124. * HISTORY: *
  125. * 3/29/2001 gth : Created. *
  126. *=============================================================================================*/
  127. void VisRenderContextClass::Get_Resolution(int * set_resx,int * set_resy)
  128. {
  129. _VisRasterizer.Get_Resolution(set_resx,set_resy);
  130. }
  131. /***********************************************************************************************
  132. * VisRenderContextClass::Scan_Frame_Buffer -- scan the frame buffer for visible objects *
  133. * *
  134. * INPUT: *
  135. * *
  136. * OUTPUT: *
  137. * *
  138. * WARNINGS: *
  139. * *
  140. * HISTORY: *
  141. * 7/18/2000 gth : Created. *
  142. *=============================================================================================*/
  143. void VisRenderContextClass::Scan_Frame_Buffer
  144. (
  145. const AABoxClass & wrld_bbox,
  146. VisSampleClass * sample
  147. )
  148. {
  149. // transform wrld_bbox into view space and compute the 2d bounding rectangle
  150. Vector2 min_v,max_v;
  151. Compute_2D_Bounds(wrld_bbox,&min_v,&max_v);
  152. Scan_Frame_Buffer(min_v,max_v,sample);
  153. }
  154. /***********************************************************************************************
  155. * VisRenderContextClass::Scan_Frame_Buffer -- scan the frame buffer for visible objects *
  156. * *
  157. * INPUT: *
  158. * *
  159. * OUTPUT: *
  160. * *
  161. * WARNINGS: *
  162. * *
  163. * HISTORY: *
  164. * 7/18/2000 gth : Created. *
  165. *=============================================================================================*/
  166. void VisRenderContextClass::Scan_Frame_Buffer(VisSampleClass * sample)
  167. {
  168. Scan_Frame_Buffer(Vector2(0,0),Vector2(1,1),sample);
  169. }
  170. /***********************************************************************************************
  171. * VisRenderContextClass::Scan_Frame_Buffer -- scan the frame buffer for visible objects *
  172. * *
  173. * INPUT: *
  174. * *
  175. * OUTPUT: *
  176. * *
  177. * WARNINGS: *
  178. * *
  179. * HISTORY: *
  180. * 7/18/2000 gth : Created. *
  181. *=============================================================================================*/
  182. void VisRenderContextClass::Scan_Frame_Buffer
  183. (
  184. const Vector2 & min,
  185. const Vector2 & max,
  186. VisSampleClass * sample
  187. )
  188. {
  189. int width,height;
  190. VisRasterizer->Get_Resolution(&width,&height);
  191. int minx = MAX(min.X * width , 1); // ignore the far left column
  192. int miny = MAX(min.Y * height , 1); // ignore the top row
  193. int maxx = MIN(max.X * width , width-1);
  194. int maxy = MIN(max.Y * height , height-1);
  195. int backface_count = 0;
  196. const uint32 * pixel_row = NULL;
  197. /*
  198. ** Loop over the pixels, counting backfaces and enabling the visibility of
  199. ** each object encountered in the buffer
  200. */
  201. for (int y=miny; y<maxy; y++) {
  202. pixel_row = VisRasterizer->Get_Pixel_Row(y,minx,maxx);
  203. for (int x=0; x<maxx-minx; x++) {
  204. if (pixel_row[x] != 0) {
  205. int id = pixel_row[x];
  206. if (id == BACKFACE_VIS_ID) {
  207. backface_count++;
  208. } else {
  209. VisTable.Set_Bit(id,true);
  210. }
  211. }
  212. }
  213. }
  214. if (sample != NULL) {
  215. int total_pixels = (maxx-minx)*(maxy-miny);
  216. float backface_fraction = (float)backface_count / (float)total_pixels;
  217. if (backface_fraction > BACKFACE_OVERFLOW_FRACTION) {
  218. WWDEBUG_SAY(("%s Backface Overflow ",sample->Get_Cur_Direction_Name()));
  219. sample->Set_Results(VIS_STATUS_BACKFACE_OVERFLOW,backface_fraction);
  220. } else {
  221. if (backface_count > 0) {
  222. WWDEBUG_SAY(("%s Backface Leak ",sample->Get_Cur_Direction_Name()));
  223. sample->Set_Results(VIS_STATUS_BACKFACE_LEAK,backface_fraction);
  224. } else {
  225. WWDEBUG_SAY(("%s ",sample->Get_Cur_Direction_Name()));
  226. sample->Set_Results(VIS_STATUS_OK,0.0f);
  227. }
  228. }
  229. }
  230. }
  231. /***********************************************************************************************
  232. * VisRenderContextClass::Compute_2D_Bounds -- compute the 2D bounds of a 3D box *
  233. * *
  234. * INPUT: *
  235. * *
  236. * OUTPUT: *
  237. * *
  238. * WARNINGS: *
  239. * *
  240. * HISTORY: *
  241. * 7/18/2000 gth : Created. *
  242. *=============================================================================================*/
  243. void VisRenderContextClass::Compute_2D_Bounds
  244. (
  245. const AABoxClass & wrld_bbox,
  246. Vector2 * min_v,
  247. Vector2 * max_v
  248. )
  249. {
  250. #define NUM_BOX_VERTS 8
  251. static float _boxverts[NUM_BOX_VERTS][3] =
  252. {
  253. { 1.0f, 1.0f, 1.0f }, // +z ring of 4 verts
  254. { -1.0f, 1.0f, 1.0f },
  255. { -1.0f,-1.0f, 1.0f },
  256. { 1.0f,-1.0f, 1.0f },
  257. { 1.0f, 1.0f,-1.0f }, // -z ring of 4 verts;
  258. { -1.0f, 1.0f,-1.0f },
  259. { -1.0f,-1.0f,-1.0f },
  260. { 1.0f,-1.0f,-1.0f },
  261. };
  262. // rotate and project the corners of the box
  263. Vector3 corner[8];
  264. for (int ivert=0; ivert<NUM_BOX_VERTS; ivert++) {
  265. corner[ivert].X = wrld_bbox.Center.X + _boxverts[ivert][0] * wrld_bbox.Extent.X;
  266. corner[ivert].Y = wrld_bbox.Center.Y + _boxverts[ivert][1] * wrld_bbox.Extent.Y;
  267. corner[ivert].Z = wrld_bbox.Center.Z + _boxverts[ivert][2] * wrld_bbox.Extent.Z;
  268. if (Camera.Project(corner[ivert],corner[ivert]) == CameraClass::OUTSIDE_NEAR_CLIP) {
  269. min_v->Set(0,0);
  270. max_v->Set(1,1);
  271. return;
  272. }
  273. }
  274. // scan for the min and max
  275. min_v->X = max_v->X = corner[0].X;
  276. min_v->Y = max_v->Y = corner[0].Y;
  277. for (ivert = 1; ivert<NUM_BOX_VERTS; ivert++) {
  278. if (min_v->X > corner[ivert].X) min_v->X = corner[ivert].X;
  279. if (min_v->Y > corner[ivert].Y) min_v->Y = corner[ivert].Y;
  280. if (max_v->X < corner[ivert].X) max_v->X = corner[ivert].X;
  281. if (max_v->Y < corner[ivert].Y) max_v->Y = corner[ivert].Y;
  282. }
  283. return;
  284. }