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.

376 lines
11 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. if (_poitionPointer._data == 0)
  165. {
  166. return;
  167. }
  168. char* posData = (char*)_poitionPointer._data;
  169. char* cData = (char*)_colorPointer._data;
  170. char* uvData = (char*)_uvPointer._data;
  171. float* fData = (float*)posData;
  172. int2 p0 (fData[0],fData[1]);
  173. posData += _poitionPointer._stride;
  174. fData = (float*)(posData);
  175. int2 p1 (fData[0],fData[1]);
  176. posData += _poitionPointer._stride;
  177. fData = (float*)(posData);
  178. int2 p2 (fData[0],fData[1]);
  179. Rgba* pColor = (Rgba*)cData;
  180. Rgba c0 (*pColor);
  181. cData += _colorPointer._stride;
  182. Rgba c1 (*pColor);
  183. cData += _colorPointer._stride;
  184. Rgba c2 (*pColor);
  185. float* pUV = (float*)uvData;
  186. float2 uv0 (pUV[0],pUV[1]);
  187. uvData += _uvPointer._stride;
  188. pUV = (float*)uvData;
  189. float2 uv1(pUV[0],pUV[1]);
  190. uvData += _uvPointer._stride;
  191. pUV = (float*)uvData;
  192. float2 uv2(pUV[0],pUV[1]);
  193. Ege eges[3] =
  194. {
  195. Ege(p0.x,p0.y,c0, uv0, p1.x,p1.y,c1, uv1),
  196. Ege(p1.x,p1.y,c1, uv1, p2.x,p2.y,c2, uv2),
  197. Ege(p2.x,p2.y,c2, uv2, p0.x,p0.y,c0, uv0),
  198. };
  199. drawTrianle(eges,0);
  200. }
  201. void drawTrianle(Ege eges[3],Image* image)
  202. {
  203. int iMax = 0;
  204. int length = eges[0]._y2 - eges[0]._y1;
  205. for (int i = 1 ;i < 3 ; ++ i)
  206. {
  207. int len = eges[i]._y2 - eges[i]._y1;
  208. if (len > length)
  209. {
  210. length = len;
  211. iMax = i;
  212. }
  213. }
  214. int iShort1 = (iMax + 1)%3;
  215. int iShort2 = (iMax + 2)%3;
  216. drawEge(eges[iMax],eges[iShort1],image);
  217. drawEge(eges[iMax],eges[iShort2],image);
  218. }
  219. void drawTriangle(const Vertex& vertex,Image* image)
  220. {
  221. Ege eges[3] =
  222. {
  223. Ege(vertex.p0.x,vertex.p0.y,vertex.c0, vertex.uv0, vertex.p1.x,vertex.p1.y,vertex.c1, vertex.uv1),
  224. Ege(vertex.p1.x,vertex.p1.y,vertex.c1, vertex.uv1, vertex.p2.x,vertex.p2.y,vertex.c2, vertex.uv2),
  225. Ege(vertex.p2.x,vertex.p2.y,vertex.c2, vertex.uv2, vertex.p0.x,vertex.p0.y,vertex.c0, vertex.uv0),
  226. };
  227. int iMax = 0;
  228. int length = eges[0]._y2 - eges[0]._y1;
  229. for (int i = 1 ;i < 3 ; ++ i)
  230. {
  231. int len = eges[i]._y2 - eges[i]._y1;
  232. if (len > length)
  233. {
  234. length = len;
  235. iMax = i;
  236. }
  237. }
  238. int iShort1 = (iMax + 1)%3;
  239. int iShort2 = (iMax + 2)%3;
  240. drawEge(eges[iMax],eges[iShort1],image);
  241. drawEge(eges[iMax],eges[iShort2],image);
  242. }
  243. void drawEge(const Ege& e1,const Ege& e2,Image* image)
  244. {
  245. float yOffset1 = e1._y2 - e1._y1;
  246. if (yOffset1 == 0)
  247. {
  248. return;
  249. }
  250. float yOffset = e2._y2 - e2._y1;
  251. if (yOffset == 0)
  252. {
  253. return;
  254. }
  255. float xOffset = e2._x2 - e2._x1;
  256. float scale = 0;
  257. float step = 1.0f/yOffset;
  258. float xOffset1 = e1._x2 - e1._x1;
  259. float scale1 = (float)(e2._y1 - e1._y1)/yOffset1;
  260. float step1 = 1.0f/yOffset1;
  261. for (int y = e2._y1 ; y < e2._y2 ; ++ y)
  262. {
  263. int x1 = e1._x1 + (int)(scale1 * xOffset1);
  264. int x2 = e2._x1 + (int)(scale * xOffset);
  265. Rgba color2 = colorLerp(e2._color1,e2._color2,scale);
  266. Rgba color1 = colorLerp(e1._color1,e1._color2,scale1);
  267. float2 uvStart = uvLerp(e1._uv1,e1._uv2,scale1);
  268. float2 uvEnd = uvLerp(e2._uv1,e2._uv2,scale);
  269. Span span(x1,x2,y,color1,color2,uvStart,uvEnd);
  270. drawSpan(span,image);
  271. scale += step;
  272. scale1 += step1;
  273. }
  274. }
  275. void drawSpan(const Span& span,Image* image)
  276. {
  277. float length = span._xEnd - span._xStart;
  278. float scale = 0;
  279. float step = 1.0f/length;
  280. for (int x = span._xStart ; x < span._xEnd; ++ x)
  281. {
  282. Rgba color = colorLerp(span._colorStart,span._colorEnd,scale);
  283. float2 uv = uvLerp(span._uvStart,span._uvEnd,scale);
  284. Rgba pixel = image->pixelUV(uv.x,uv.y);
  285. Rgba dst = color + pixel;
  286. scale += step;
  287. setPixel(x,span._y,dst);
  288. }
  289. }
  290. void drawLine(float2 pt1,float2 pt2,Rgba color1,Rgba color2);
  291. void drawPoints(float2 pt1,Rgba4Byte color)
  292. {
  293. }
  294. inline void setPixelEx(unsigned x,unsigned y,Rgba color)
  295. {
  296. _buffer[y * _width + x] = color._color;
  297. }
  298. inline Rgba getPixel(unsigned x,unsigned y)
  299. {
  300. return Rgba(_buffer[y * _width + x]);
  301. }
  302. inline void setPixel(unsigned x,unsigned y,Rgba color)
  303. {
  304. if (x >= _width || y >= _height)
  305. {
  306. return;
  307. }
  308. _buffer[y * _width + x] = color._color;
  309. }
  310. };
  311. }