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.

222 lines
5.8 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. public:
  88. bool isInRect(int2 pt)
  89. {
  90. if (pt.x >= 0 && pt.x <= _width && pt.y >= 0 && pt.y <= _height)
  91. {
  92. return true;
  93. }
  94. return false;
  95. }
  96. void drawTriangle(int2 p0,int2 p1,int2 p2,Rgba c0,Rgba c1,Rgba c2)
  97. {
  98. // if ((!isInRect(p0)) && (!isInRect(p1)) && (!isInRect(p2)))
  99. // {
  100. // return;
  101. // }
  102. Ege eges[3] =
  103. {
  104. Ege(p0.x,p0.y,c0, p1.x,p1.y,c1),
  105. Ege(p1.x,p1.y,c1, p2.x,p2.y,c2),
  106. Ege(p2.x,p2.y,c2, p0.x,p0.y,c0),
  107. };
  108. int iMax = 0;
  109. int length = eges[0]._y2 - eges[0]._y1;
  110. for (int i = 1 ;i < 3 ; ++ i)
  111. {
  112. int len = eges[i]._y2 - eges[i]._y1;
  113. if (len > length)
  114. {
  115. length = len;
  116. iMax = i;
  117. }
  118. }
  119. int iShort1 = (iMax + 1)%3;
  120. int iShort2 = (iMax + 2)%3;
  121. drawEge(eges[iMax],eges[iShort1]);
  122. drawEge(eges[iMax],eges[iShort2]);
  123. }
  124. void drawEge(const Ege& e1,const Ege& e2)
  125. {
  126. float yOffset1 = e1._y2 - e1._y1;
  127. if (yOffset1 == 0)
  128. {
  129. return;
  130. }
  131. float yOffset = e2._y2 - e2._y1;
  132. if (yOffset == 0)
  133. {
  134. return;
  135. }
  136. float xOffset = e2._x2 - e2._x1;
  137. float scale = 0;
  138. float step = 1.0f/yOffset;
  139. float xOffset1 = e1._x2 - e1._x1;
  140. float scale1 = (float)(e2._y1 - e1._y1)/yOffset1;
  141. float step1 = 1.0f/yOffset1;
  142. for (int y = e2._y1 ; y < e2._y2 ; ++ y)
  143. {
  144. int x1 = e1._x1 + (int)(scale1 * xOffset1);
  145. int x2 = e2._x1 + (int)(scale * xOffset);
  146. Rgba color2 = colorLerp(e2._color1,e2._color2,scale);
  147. Rgba color1 = colorLerp(e1._color1,e1._color2,scale1);
  148. Span span(x1,x2,y,color1,color2);
  149. drawSpan(span);
  150. scale += step;
  151. scale1 += step1;
  152. }
  153. }
  154. void drawSpan(const Span& span)
  155. {
  156. float length = span._xEnd - span._xStart;
  157. float scale = 0;
  158. float step = 1.0f/length;
  159. for (int x = span._xStart ; x < span._xEnd; ++ x)
  160. {
  161. Rgba color = colorLerp(
  162. span._colorStart
  163. ,span._colorEnd
  164. ,scale
  165. );
  166. scale += step;
  167. setPixel(x,span._y,color);
  168. }
  169. }
  170. void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
  171. void drawPoints(float2 pt1,Rgba4Byte color)
  172. {
  173. }
  174. inline void setPixelEx(unsigned x,unsigned y,Rgba color)
  175. {
  176. _buffer[y * _width + x] = color._color;
  177. }
  178. inline void setPixel(unsigned x,unsigned y,Rgba color)
  179. {
  180. if (x >= _width || y >= _height)
  181. {
  182. return;
  183. }
  184. _buffer[y * _width + x] = color._color;
  185. }
  186. };
  187. }