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.

160 lines
4.3 KiB

5 years ago
  1. #include "Raster.h"
  2. namespace CELL
  3. {
  4. Raster::Raster( int w,int h,void* buffer )
  5. {
  6. _width = w;
  7. _height = h;
  8. _buffer = (Rgba*)buffer;
  9. }
  10. Raster::~Raster( void )
  11. {
  12. }
  13. void Raster::clear()
  14. {
  15. memset(_buffer,0,_width * _height * sizeof(Rgba));
  16. }
  17. void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
  18. {
  19. switch(ptSize)
  20. {
  21. case 1:
  22. setPixel(x,y,color);
  23. break;
  24. case 2:
  25. setPixel(x + 0, y + 0,color);
  26. setPixel(x + 1, y + 0,color);
  27. setPixel(x + 0, y + 1,color);
  28. setPixel(x + 1, y + 1,color);
  29. break;
  30. case 3:
  31. setPixel(x - 1, y - 1,color);
  32. setPixel(x + 0, y - 1,color);
  33. setPixel(x + 1, y - 1,color);
  34. setPixel(x - 1, y + 0,color);
  35. setPixel(x + 0, y + 0,color);
  36. setPixel(x + 1, y + 0,color);
  37. setPixel(x - 1, y + 1,color);
  38. setPixel(x + 0, y + 1,color);
  39. setPixel(x + 1, y + 1,color);
  40. break;
  41. }
  42. }
  43. void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
  44. {
  45. float xOffset = pt1.x - pt2.x;
  46. float yOffset = pt1.y - pt2.y;
  47. if (xOffset == 0 && yOffset == 0)
  48. {
  49. setPixel(pt1.x,pt1.y,color1);
  50. }
  51. if (fabs(xOffset) > fabs(yOffset))
  52. {
  53. float xMin;
  54. float xMax;
  55. if (pt1.x < pt2.x)
  56. {
  57. xMin = pt1.x;
  58. xMax = pt2.x;
  59. }
  60. else
  61. {
  62. xMin = pt2.x;
  63. xMax = pt1.x;
  64. }
  65. float lenth = xMax - xMin;
  66. float slope = yOffset / xOffset;
  67. for (float x = xMin; x <= xMax ; x += 1.0f)
  68. {
  69. float y = pt1.y + (x - pt1.x) * slope;
  70. float scaler = (x - xMin)/lenth;
  71. Rgba color = colorLerp(color1,color2,scaler);
  72. setPixel(x,y,color);
  73. }
  74. }
  75. else
  76. {
  77. float yMin;
  78. float yMax;
  79. if (pt1.y < pt2.y)
  80. {
  81. yMin = pt1.y;
  82. yMax = pt2.y;
  83. }
  84. else
  85. {
  86. yMin = pt2.y;
  87. yMax = pt1.y;
  88. }
  89. float lenth = yMax - yMin;
  90. float slope = xOffset / yOffset;
  91. for (float y = yMin; y <= yMax ; y += 1.0f)
  92. {
  93. float x = pt1.x + (y - pt1.y) * slope;
  94. float scaler = (y - yMin)/lenth;
  95. Rgba color = colorLerp(color1,color2,scaler);
  96. setPixel(x,y,color);
  97. }
  98. }
  99. }
  100. void Raster::drawArrays( DRAWMODE mode,const float2* points,int count )
  101. {
  102. switch (mode)
  103. {
  104. case DM_POINTS:
  105. {
  106. for (int i = 0 ;i < count ; ++ i)
  107. {
  108. drawPoints(points[i],_color);
  109. }
  110. }
  111. break;
  112. case DM_LINES:
  113. {
  114. count = count/2 * 2;
  115. for (int i = 0 ;i < count ; i += 2)
  116. {
  117. drawLine(points[i],points[i + 1],_color,_color);
  118. }
  119. }
  120. break;
  121. case DM_LINE_LOOP:
  122. {
  123. drawLine(points[0],points[1],_color,_color);
  124. for (int i = 2 ;i < count ; ++ i)
  125. {
  126. drawLine(points[i - 1],points[i],_color,_color);
  127. }
  128. drawLine(points[0],points[count - 1],_color,_color);
  129. }
  130. break;
  131. case DM_LINE_STRIP:
  132. {
  133. drawLine(points[0],points[1],_color,_color);
  134. for (int i = 2 ;i < count ; ++ i)
  135. {
  136. drawLine(points[i - 1],points[i],_color,_color);
  137. }
  138. }
  139. break;
  140. default:
  141. break;
  142. }
  143. }
  144. }