bw_render.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/BW_Render.cpp $*
  25. * *
  26. * Original Author:: Jani Penttinen *
  27. * *
  28. * $Author:: Greg_h $*
  29. * *
  30. * $Modtime:: 2/06/01 10:57a $*
  31. * *
  32. * $Revision:: 4 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "bw_render.h"
  38. #include "vp.h"
  39. #include <string.h>
  40. BW_Render::Buffer::Buffer(unsigned char* buffer_, int scale_)
  41. :
  42. buffer(buffer_),
  43. scale(scale_),
  44. minv(3),
  45. maxv(scale_-3)
  46. {
  47. }
  48. BW_Render::Buffer::~Buffer()
  49. {
  50. }
  51. void BW_Render::Buffer::Set_H_Line(int start_x, int end_x, int y)
  52. {
  53. if (y<minv || y>=maxv || end_x<minv || start_x>=maxv) return;
  54. if (start_x<minv) start_x=minv;
  55. if (end_x>=maxv) end_x=maxv-1;
  56. unsigned char* ptr=buffer+scale*y+start_x;
  57. int w=end_x-start_x;
  58. if (w) {
  59. ::memset(ptr,0x00,w);
  60. /* // Blurring (test)
  61. *(ptr-1)&=0x80;
  62. *(ptr-2)&=0xc0;
  63. *(ptr+w)&=0x80;
  64. *(ptr+w+1)&=0xc0;
  65. for (int a=0;a<w;++a) {
  66. *(ptr-scale+a)&=0xc0;
  67. *(ptr+scale+a)&=0xc0;
  68. }
  69. */
  70. }
  71. }
  72. void BW_Render::Buffer::Fill(unsigned char c)
  73. {
  74. memset(buffer,c,scale*scale);
  75. }
  76. // ------------------------------------------------------------------------------
  77. BW_Render::BW_Render(unsigned char* buffer, int buffer_scale)
  78. :
  79. pixel_buffer(buffer,buffer_scale)
  80. {
  81. }
  82. BW_Render::~BW_Render()
  83. {
  84. }
  85. void BW_Render::Fill(unsigned char c)
  86. {
  87. pixel_buffer.Fill(c);
  88. }
  89. // Sets the vertex coordinate buffer location and scales the vertex locations to the kjkj
  90. void BW_Render::Set_Vertex_Locations(Vector2* vertices_,int count)
  91. {
  92. vertices=vertices_;
  93. float half_scale=pixel_buffer.Scale()*0.5f;
  94. VectorProcessorClass::MulAdd(
  95. reinterpret_cast<float*>(vertices),
  96. half_scale,
  97. half_scale,
  98. count*2);
  99. }
  100. // --------------------------------------------------------------------
  101. static inline bool Cull(
  102. const Vector2& c1,
  103. const Vector2& c2,
  104. const Vector2& c3)
  105. {
  106. float x1=c2[0]-c1[0];
  107. float y1=c2[1]-c1[1];
  108. float x2=c3[0]-c1[0];
  109. float y2=c3[1]-c1[1];
  110. float r=x1*y2-x2*y1;
  111. if (r<0) return false;
  112. return true;
  113. }
  114. void BW_Render::Render_Triangle_Strip(const unsigned long* indices,int index_count)
  115. {
  116. index_count-=2;
  117. bool b=false;
  118. for (int n=0;n<index_count;++n) {
  119. b=!b;
  120. int idx_1=indices[0];
  121. int idx_2=indices[1];
  122. int idx_3=indices[2];
  123. indices++;
  124. if (Cull(vertices[idx_1],vertices[idx_2],vertices[idx_3])==b) continue;
  125. Vector2 corner_1(vertices[idx_1][0],vertices[idx_1][1]);
  126. Vector2 corner_2(vertices[idx_2][0],vertices[idx_2][1]);
  127. Vector2 corner_3(vertices[idx_3][0],vertices[idx_3][1]);
  128. // Sort the corners on y axis
  129. if (corner_2[1]<corner_1[1]) Swap(corner_1,corner_2);
  130. if (corner_3[1]<corner_1[1]) Swap(corner_1,corner_3);
  131. if (corner_3[1]<corner_2[1]) Swap(corner_2,corner_3);
  132. Vector3i yci(WWMath::Float_To_Long(corner_1[1]),WWMath::Float_To_Long(corner_2[1]),WWMath::Float_To_Long(corner_3[1]));
  133. Vector3 xcf(corner_1[0],corner_2[0],corner_3[0]);
  134. Render_Preprocessed_Triangle(xcf,yci);
  135. }
  136. }
  137. void BW_Render::Render_Triangles(const unsigned long* indices,int index_count)
  138. {
  139. index_count/=3;
  140. for (int n=0;n<index_count;++n) {
  141. int idx_1=*indices++;
  142. int idx_2=*indices++;
  143. int idx_3=*indices++;
  144. if (Cull(vertices[idx_1],vertices[idx_2],vertices[idx_3])) continue;
  145. Vector2 corner_1(vertices[idx_1][0],vertices[idx_1][1]);
  146. Vector2 corner_2(vertices[idx_2][0],vertices[idx_2][1]);
  147. Vector2 corner_3(vertices[idx_3][0],vertices[idx_3][1]);
  148. // Sort the corners on y axis
  149. if (corner_2[1]<corner_1[1]) Swap(corner_1,corner_2);
  150. if (corner_3[1]<corner_1[1]) Swap(corner_1,corner_3);
  151. if (corner_3[1]<corner_2[1]) Swap(corner_2,corner_3);
  152. Vector3i yci(WWMath::Float_To_Long(corner_1[1]),WWMath::Float_To_Long(corner_2[1]),WWMath::Float_To_Long(corner_3[1]));
  153. Vector3 xcf(corner_1[0],corner_2[0],corner_3[0]);
  154. Render_Preprocessed_Triangle(xcf,yci);
  155. }
  156. }
  157. void BW_Render::Render_Preprocessed_Triangle(Vector3& xcf,Vector3i& yci)
  158. {
  159. float x_left=xcf[0];
  160. float x_right=x_left;
  161. int ycnt=yci[1]-yci[0];
  162. int y=yci[0];
  163. if (ycnt) {
  164. float x_step_1=(xcf[1]-xcf[0])/float(ycnt);
  165. float x_step_2=(xcf[2]-xcf[0])/float(yci[2]-y);
  166. if (x_step_1>x_step_2) {
  167. float t=x_step_1;
  168. x_step_1=x_step_2;
  169. x_step_2=t;
  170. }
  171. while (ycnt>0) {
  172. pixel_buffer.Set_H_Line(WWMath::Float_To_Long(x_left),WWMath::Float_To_Long(x_right),y);
  173. x_left+=x_step_1;
  174. x_right+=x_step_2;
  175. ycnt--;
  176. y++;
  177. }
  178. }
  179. else {
  180. if (xcf[0]<xcf[1]) {
  181. x_left=xcf[0];
  182. x_right=xcf[1];
  183. }
  184. else {
  185. x_right=xcf[0];
  186. x_left=xcf[1];
  187. }
  188. }
  189. ycnt=yci[2]-yci[1];
  190. y=yci[1];
  191. if (ycnt) {
  192. float one_per_ycnt=1.0f/float(ycnt);
  193. float x_step_1=(xcf[2]-x_left)*one_per_ycnt;
  194. float x_step_2=(xcf[2]-x_right)*one_per_ycnt;
  195. while (ycnt>0) {
  196. pixel_buffer.Set_H_Line(WWMath::Float_To_Long(x_left),WWMath::Float_To_Long(x_right),y);
  197. x_left+=x_step_1;
  198. x_right+=x_step_2;
  199. ycnt--;
  200. y++;
  201. }
  202. }
  203. }