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.

311 lines
8.6 KiB

5 years ago
  1. #pragma once
  2. #include "CELLMath.hpp"
  3. #include "Image.hpp"
  4. namespace CELL
  5. {
  6. enum DRAWMODE
  7. {
  8. DM_POINTS = 0,
  9. DM_LINES = 1,
  10. DM_LINE_LOOP = 2,
  11. DM_LINE_STRIP = 3,
  12. DM_TRIANGES = 4,
  13. };
  14. enum DATETYPE
  15. {
  16. DT_BYTE,
  17. DT_FLOAT,
  18. DT_DOUBLE,
  19. };
  20. struct DateElementDes
  21. {
  22. int _size;
  23. DATETYPE _type;
  24. int _stride;
  25. const void* _data;
  26. };
  27. class Span
  28. {
  29. public:
  30. int _xStart;
  31. int _xEnd;
  32. Rgba _colorStart;
  33. Rgba _colorEnd;
  34. float2 _uvStart;
  35. float2 _uvEnd;
  36. int _y;
  37. public:
  38. Span(int xStart,int xEnd,int y,Rgba colorStart,Rgba colorEnd,float2 uvStart,float2 uvEnd)
  39. {
  40. if (xStart < xEnd)
  41. {
  42. _xStart = xStart;
  43. _xEnd = xEnd;
  44. _colorStart = colorStart;
  45. _colorEnd = colorEnd;
  46. _uvStart = uvStart;
  47. _uvEnd = uvEnd;
  48. _y = y;
  49. }
  50. else
  51. {
  52. _xStart = xEnd;
  53. _xEnd = xStart;
  54. _colorStart = colorEnd;
  55. _colorEnd = colorStart;
  56. _uvStart = uvEnd;
  57. _uvEnd = uvStart;
  58. _y = y;
  59. }
  60. }
  61. };
  62. class Ege
  63. {
  64. public:
  65. int _x1;
  66. int _y1;
  67. float2 _uv1;
  68. Rgba _color1;
  69. int _x2;
  70. int _y2;
  71. float2 _uv2;
  72. Rgba _color2;
  73. Ege(int x1,int y1,Rgba color1,float2 uv1,int x2,int y2,Rgba color2,float2 uv2)
  74. {
  75. if (y1 < y2)
  76. {
  77. _x1 = x1;
  78. _y1 = y1;
  79. _uv1 = uv1;
  80. _color1 = color1;
  81. _x2 = x2;
  82. _y2 = y2;
  83. _uv2 = uv2;
  84. _color2 = color2;
  85. }
  86. else
  87. {
  88. _x1 = x2;
  89. _y1 = y2;
  90. _uv1 = uv2;
  91. _color1 = color2;
  92. _x2 = x1;
  93. _y2 = y1;
  94. _uv2 = uv1;
  95. _color2 = color1;
  96. }
  97. }
  98. };
  99. class Raster
  100. {
  101. public:
  102. uint* _buffer;
  103. int _width;
  104. int _height;
  105. Rgba _color;
  106. DateElementDes _poitionPointer;
  107. DateElementDes _colorPointer;
  108. DateElementDes _uvPointer;
  109. public:
  110. Raster(int w,int h,void* buffer);
  111. ~Raster(void);
  112. void clear();
  113. struct Vertex
  114. {
  115. int2 p0;
  116. float2 uv0;
  117. Rgba c0;
  118. int2 p1;
  119. float2 uv1;
  120. Rgba c1;
  121. int2 p2;
  122. float2 uv2;
  123. Rgba c2;
  124. };
  125. void drawImage(int startX,int startY,const Image* image)
  126. {
  127. int left = tmax<int>(startX,0);
  128. int top = tmax<int>(startY,0);
  129. int right = tmin<int>(startX + image->width(),_width);
  130. int bottom = tmin<int>(startY + image->height(),_height);
  131. for (int x = left ; x < right ; ++ x)
  132. {
  133. for (int y = top ; y < bottom ; ++ y)
  134. {
  135. Rgba color = image->pixelAt(x - left,y - top);
  136. setPixelEx(x,y,color);
  137. }
  138. }
  139. }
  140. public:
  141. void vertexPointer(int size,DATETYPE type,int stride,const void* data)
  142. {
  143. _poitionPointer._size = size;
  144. _poitionPointer._type = type;
  145. _poitionPointer._stride = stride;
  146. _poitionPointer._data = data;
  147. }
  148. void colorPointer(int size,DATETYPE type,int stride,const void* data)
  149. {
  150. _colorPointer._size = size;
  151. _colorPointer._type = type;
  152. _colorPointer._stride = stride;
  153. _colorPointer._data = data;
  154. }
  155. void textureCoordPointer(int size,DATETYPE type,int stride,const void* data)
  156. {
  157. _uvPointer._size = size;
  158. _uvPointer._type = type;
  159. _uvPointer._stride = stride;
  160. _uvPointer._data = data;
  161. }
  162. void drawArrays(DRAWMODE pri,int start,int count)
  163. {
  164. }
  165. void drawTriangle(const Vertex& vertex,Image* image)
  166. {
  167. Ege eges[3] =
  168. {
  169. Ege(vertex.p0.x,vertex.p0.y,vertex.c0, vertex.uv0, vertex.p1.x,vertex.p1.y,vertex.c1, vertex.uv1),
  170. Ege(vertex.p1.x,vertex.p1.y,vertex.c1, vertex.uv1, vertex.p2.x,vertex.p2.y,vertex.c2, vertex.uv2),
  171. Ege(vertex.p2.x,vertex.p2.y,vertex.c2, vertex.uv2, vertex.p0.x,vertex.p0.y,vertex.c0, vertex.uv0),
  172. };
  173. int iMax = 0;
  174. int length = eges[0]._y2 - eges[0]._y1;
  175. for (int i = 1 ;i < 3 ; ++ i)
  176. {
  177. int len = eges[i]._y2 - eges[i]._y1;
  178. if (len > length)
  179. {
  180. length = len;
  181. iMax = i;
  182. }
  183. }
  184. int iShort1 = (iMax + 1)%3;
  185. int iShort2 = (iMax + 2)%3;
  186. drawEge(eges[iMax],eges[iShort1],image);
  187. drawEge(eges[iMax],eges[iShort2],image);
  188. }
  189. void drawEge(const Ege& e1,const Ege& e2,Image* image)
  190. {
  191. float yOffset1 = e1._y2 - e1._y1;
  192. if (yOffset1 == 0)
  193. {
  194. return;
  195. }
  196. float yOffset = e2._y2 - e2._y1;
  197. if (yOffset == 0)
  198. {
  199. return;
  200. }
  201. float xOffset = e2._x2 - e2._x1;
  202. float scale = 0;
  203. float step = 1.0f/yOffset;
  204. float xOffset1 = e1._x2 - e1._x1;
  205. float scale1 = (float)(e2._y1 - e1._y1)/yOffset1;
  206. float step1 = 1.0f/yOffset1;
  207. for (int y = e2._y1 ; y < e2._y2 ; ++ y)
  208. {
  209. int x1 = e1._x1 + (int)(scale1 * xOffset1);
  210. int x2 = e2._x1 + (int)(scale * xOffset);
  211. Rgba color2 = colorLerp(e2._color1,e2._color2,scale);
  212. Rgba color1 = colorLerp(e1._color1,e1._color2,scale1);
  213. float2 uvStart = uvLerp(e1._uv1,e1._uv2,scale1);
  214. float2 uvEnd = uvLerp(e2._uv1,e2._uv2,scale);
  215. Span span(x1,x2,y,color1,color2,uvStart,uvEnd);
  216. drawSpan(span,image);
  217. scale += step;
  218. scale1 += step1;
  219. }
  220. }
  221. void drawSpan(const Span& span,Image* image)
  222. {
  223. float length = span._xEnd - span._xStart;
  224. float scale = 0;
  225. float step = 1.0f/length;
  226. for (int x = span._xStart ; x < span._xEnd; ++ x)
  227. {
  228. Rgba color = colorLerp(span._colorStart,span._colorEnd,scale);
  229. float2 uv = uvLerp(span._uvStart,span._uvEnd,scale);
  230. Rgba pixel = image->pixelUV(uv.x,uv.y);
  231. Rgba dst = color + pixel;
  232. scale += step;
  233. setPixel(x,span._y,dst);
  234. }
  235. }
  236. void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
  237. void drawPoints(float2 pt1,Rgba4Byte color)
  238. {
  239. }
  240. inline void setPixelEx(unsigned x,unsigned y,Rgba color)
  241. {
  242. _buffer[y * _width + x] = color._color;
  243. }
  244. inline Rgba getPixel(unsigned x,unsigned y)
  245. {
  246. return Rgba(_buffer[y * _width + x]);
  247. }
  248. inline void setPixel(unsigned x,unsigned y,Rgba color)
  249. {
  250. if (x >= _width || y >= _height)
  251. {
  252. return;
  253. }
  254. _buffer[y * _width + x] = color._color;
  255. }
  256. };
  257. }