You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
6.0 KiB

5 years ago
  1. #pragma once
  2. #include "CELLMath.hpp"
  3. namespace CELL
  4. {
  5. enum DRAWMODE
  6. {
  7. DM_POINTS = 0,
  8. DM_LINES = 1,
  9. DM_LINE_LOOP = 2,
  10. DM_LINE_STRIP = 3,
  11. };
  12. class Span
  13. {
  14. public:
  15. int _xStart;
  16. int _xEnd;
  17. Rgba _colorStart;
  18. Rgba _colorEnd;
  19. int _y;
  20. public:
  21. Span(int xStart,int xEnd,int y,Rgba colorStart,Rgba colorEnd)
  22. {
  23. if (xStart < xEnd)
  24. {
  25. _xStart = xStart;
  26. _xEnd = xEnd;
  27. _colorStart = colorStart;
  28. _colorEnd = colorEnd;
  29. _y = y;
  30. }
  31. else
  32. {
  33. _xStart = _xEnd;
  34. _xEnd = _xStart;
  35. _colorStart = colorEnd;
  36. _colorEnd = colorStart;
  37. _y = y;
  38. }
  39. }
  40. };
  41. class Ege
  42. {
  43. public:
  44. int _x1;
  45. int _y1;
  46. Rgba _color1;
  47. int _x2;
  48. int _y2;
  49. Rgba _color2;
  50. Ege(int x1,int y1,Rgba color1,int x2,int y2,Rgba color2)
  51. {
  52. if (y1 < y2)
  53. {
  54. _x1 = x1;
  55. _y1 = y1;
  56. _color1 = color1;
  57. _x2 = x2;
  58. _y2 = y2;
  59. _color2 = color2;
  60. }
  61. else
  62. {
  63. _x1 = x2;
  64. _y1 = y2;
  65. _color1 = color2;
  66. _x2 = x1;
  67. _y2 = y1;
  68. _color2 = color1;
  69. }
  70. }
  71. };
  72. class Raster
  73. {
  74. public:
  75. uint* _buffer;
  76. int _width;
  77. int _height;
  78. Rgba _color;
  79. public:
  80. Raster(int w,int h,void* buffer);
  81. ~Raster(void);
  82. void clear();
  83. void drawPoint(int x,int y, Rgba color,int ptSize);
  84. void drawArrays(DRAWMODE mode,const float2* points,int count);
  85. void drawFilleRect(int startX,int startY,int w,int h);
  86. void drawRect(const int2* points,const Rgba* colors);
  87. void drawImage(int startX,int startY,int w,int h)
  88. {
  89. int left = tmax<int>(startX,0);
  90. int top = tmax<int>(startY,0);
  91. int right = tmin<int>(startX + w,_width);
  92. int bottom = tmin<int>(startY + h,_height);
  93. for (int x = left ; x < right ; ++ x)
  94. {
  95. for (int y = top ; y < bottom ; ++ y)
  96. {
  97. Rgba color(rand()%256,rand()%256,rand()%256);
  98. setPixelEx(x,y,color);
  99. }
  100. }
  101. }
  102. public:
  103. void drawTriangle(int2 p0,int2 p1,int2 p2,Rgba c0,Rgba c1,Rgba c2)
  104. {
  105. Ege eges[3] =
  106. {
  107. Ege(p0.x,p0.y,c0, p1.x,p1.y,c1),
  108. Ege(p1.x,p1.y,c1, p2.x,p2.y,c2),
  109. Ege(p2.x,p2.y,c2, p0.x,p0.y,c0),
  110. };
  111. int iMax = 0;
  112. int length = eges[0]._y2 - eges[0]._y1;
  113. for (int i = 1 ;i < 3 ; ++ i)
  114. {
  115. int len = eges[i]._y2 - eges[i]._y1;
  116. if (len > length)
  117. {
  118. length = i;
  119. }
  120. }
  121. int iShort1 = (iMax + 1)%3;
  122. int iShort2 = (iMax + 2)%3;
  123. drawEge(eges[iMax],eges[iShort1]);
  124. drawEge(eges[iMax],eges[iShort2]);
  125. }
  126. void drawEge(const Ege& e1,const Ege& e2)
  127. {
  128. float yOffset1 = e1._y2 - e1._y1;
  129. if (yOffset1 == 0)
  130. {
  131. return;
  132. }
  133. float yOffset = e2._y2 - e2._y1;
  134. if (yOffset == 0)
  135. {
  136. return;
  137. }
  138. float xOffset = e2._x2 - e2._x1;
  139. float scale = 0;
  140. float step = 1.0f/yOffset;
  141. float xOffset1 = e1._x2 - e1._x1;
  142. float scale1 = (float)(e2._y1 - e1._y1)/yOffset1;
  143. float step1 = 1.0f/yOffset1;
  144. for (int y = e2._y1 ; y < e2._y2 ; ++ y)
  145. {
  146. int x1 = e1._x1 + (int)(scale1 * xOffset1);
  147. int x2 = e2._x1 + (int)(scale * xOffset);
  148. Rgba color2 = colorLerp(e2._color1,e2._color2,scale);
  149. Rgba color1 = colorLerp(e1._color1,e1._color2,scale1);
  150. Span span(x1,x2,y,color1,color2);
  151. drawSpan(span);
  152. scale += step;
  153. scale1 += step1;
  154. }
  155. }
  156. void drawSpan(const Span& span)
  157. {
  158. float length = span._xEnd - span._xStart;
  159. float scale = 0;
  160. float step = 1.0f/length;
  161. for (int x = span._xStart ; x < span._xEnd; ++ x)
  162. {
  163. Rgba color = colorLerp(
  164. span._colorStart
  165. ,span._colorEnd
  166. ,scale
  167. );
  168. scale += step;
  169. setPixel(x,span._y,color);
  170. }
  171. }
  172. void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
  173. void drawPoints(float2 pt1,Rgba4Byte color)
  174. {
  175. }
  176. inline void setPixelEx(unsigned x,unsigned y,Rgba color)
  177. {
  178. _buffer[y * _width + x] = color._color;
  179. }
  180. inline void setPixel(unsigned x,unsigned y,Rgba color)
  181. {
  182. if (x >= _width || y >= _height)
  183. {
  184. return;
  185. }
  186. _buffer[y * _width + x] = color._color;
  187. }
  188. };
  189. }