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.

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