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.

284 lines
8.4 KiB

5 years ago
  1. #include "Raster.h"
  2. #include "FreeImage.h"
  3. namespace CELL
  4. {
  5. Image* Image::loadFromFile(const char* fileName)
  6. {
  7. //1 ��ȡͼƬ��ʽ
  8. FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(fileName, 0);
  9. if (fifmt == FIF_UNKNOWN)
  10. {
  11. return 0;
  12. }
  13. //2 ����ͼƬ
  14. FIBITMAP *dib = FreeImage_Load(fifmt, fileName,0);
  15. FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(dib);
  16. //! ��ȡ����ָ��
  17. FIBITMAP* temp = dib;
  18. dib = FreeImage_ConvertTo32Bits(dib);
  19. FreeImage_Unload(temp);
  20. BYTE* pixels = (BYTE*)FreeImage_GetBits(dib);
  21. int width = FreeImage_GetWidth(dib);
  22. int height = FreeImage_GetHeight(dib);
  23. int pitch = width*4;
  24. BYTE* row = new BYTE[width*4];
  25. for(int j = 0; j < height / 2; j++)
  26. {
  27. memcpy(row,pixels + j * pitch,pitch );
  28. memcpy(pixels + j * pitch,pixels + (height - j - 1) * pitch,pitch );
  29. memcpy(pixels + (height - j - 1) * pitch,row,pitch );
  30. }
  31. delete []row;
  32. Image* image = new Image(width,height,pixels);
  33. FreeImage_Unload(dib);
  34. return image;
  35. }
  36. Raster::Raster( int w,int h,void* buffer )
  37. {
  38. _texture = 0;
  39. _width = w;
  40. _height = h;
  41. _buffer = (uint*)buffer;
  42. memset(&_poitionPointer,0, sizeof(_poitionPointer));
  43. memset(&_colorPointer, 0, sizeof(_colorPointer));
  44. memset(&_uvPointer, 0, sizeof(_uvPointer));
  45. _defaultColorPointer._size = 4;
  46. _defaultColorPointer._type = DT_BYTE;
  47. _defaultColorPointer._stride= sizeof(Rgba);
  48. _defaultColorPointer._data = _defaultColorArray;
  49. _defaultUVPointer._size = 2;
  50. _defaultUVPointer._type = DT_FLOAT;
  51. _defaultUVPointer._stride = sizeof(float2);
  52. _defaultUVPointer._data = _detaultUVArray;
  53. _matModel = CELL::matrix4(1);
  54. _matView = CELL::matrix4(1);
  55. _matProj = CELL::matrix4(1);
  56. }
  57. Raster::~Raster( void )
  58. {
  59. }
  60. void Raster::clear()
  61. {
  62. memset(_buffer,0,_width * _height * sizeof(Rgba));
  63. }
  64. void Raster::drawImage( int startX,int startY,const Image* image )
  65. {
  66. int left = tmax<int>(startX,0);
  67. int top = tmax<int>(startY,0);
  68. int right = tmin<int>(startX + image->width(),_width);
  69. int bottom = tmin<int>(startY + image->height(),_height);
  70. for (int x = left ; x < right ; ++ x)
  71. {
  72. for (int y = top ; y < bottom ; ++ y)
  73. {
  74. Rgba color = image->pixelAt(x - left,y - top);
  75. setPixelEx(x,y,color);
  76. }
  77. }
  78. }
  79. void Raster::drawArrays( DRAWMODE pri,int start,int count )
  80. {
  81. if (_poitionPointer._data == 0)
  82. {
  83. return;
  84. }
  85. DateElementDes colorPointerdesc = _colorPointer;
  86. DateElementDes uvPointerdesc = _uvPointer;
  87. if (colorPointerdesc._data == 0)
  88. {
  89. colorPointerdesc = _defaultColorPointer;
  90. }
  91. if (uvPointerdesc._data == 0)
  92. {
  93. uvPointerdesc = _defaultUVPointer;
  94. }
  95. char* posData = (char*)_poitionPointer._data;
  96. char* cData = (char*)colorPointerdesc._data;
  97. char* uvData = (char*)uvPointerdesc._data;
  98. _matProjView = _matProj * _matView;
  99. matrix4 matPVT = _matProjView.transpose();
  100. _frust.loadFrustum(matPVT);
  101. for(int i = start ;i < start + count; i += 3)
  102. {
  103. float* fData = (float*)posData;
  104. float3 p01 (fData[0],fData[1],fData[2]);
  105. posData += _poitionPointer._stride;
  106. fData = (float*)(posData);
  107. float3 p11 (fData[0],fData[1],fData[2]);
  108. posData += _poitionPointer._stride;
  109. fData = (float*)(posData);
  110. float3 p21 (fData[0],fData[1],fData[2]);
  111. posData += _poitionPointer._stride;
  112. p01 = p01 * _matModel;
  113. p11 = p11 * _matModel;
  114. p21 = p21 * _matModel;
  115. if ( _frust.pointInFrustum(p01)
  116. || _frust.pointInFrustum(p11)
  117. || _frust.pointInFrustum(p21)
  118. )
  119. {
  120. p01 = piplineTransform(p01);
  121. p11 = piplineTransform(p11);
  122. p21 = piplineTransform(p21);
  123. //! ת��Ϊ��Ļ����
  124. int2 p0(p01.x,p01.y);
  125. int2 p1(p11.x,p11.y);
  126. int2 p2(p21.x,p21.y);
  127. Rgba c0 (*(Rgba*)cData);
  128. cData += _colorPointer._stride;
  129. Rgba c1 (*(Rgba*)cData);
  130. cData += _colorPointer._stride;
  131. Rgba c2 (*(Rgba*)cData);
  132. cData += _colorPointer._stride;
  133. float* pUV = (float*)uvData;
  134. float2 uv0 (pUV[0],pUV[1]);
  135. uvData += _uvPointer._stride;
  136. pUV = (float*)uvData;
  137. float2 uv1(pUV[0],pUV[1]);
  138. uvData += _uvPointer._stride;
  139. pUV = (float*)uvData;
  140. float2 uv2(pUV[0],pUV[1]);
  141. uvData += _uvPointer._stride;
  142. Ege eges[3] =
  143. {
  144. Ege(p0.x,p0.y,c0, uv0, p1.x,p1.y,c1, uv1),
  145. Ege(p1.x,p1.y,c1, uv1, p2.x,p2.y,c2, uv2),
  146. Ege(p2.x,p2.y,c2, uv2, p0.x,p0.y,c0, uv0),
  147. };
  148. drawTrianle(eges);
  149. if (_colorPointer._data == 0)
  150. {
  151. cData = (char*)colorPointerdesc._data;
  152. }
  153. if (_uvPointer._data == 0 )
  154. {
  155. uvData = (char*)uvPointerdesc._data;
  156. }
  157. }
  158. }
  159. }
  160. void Raster::bindTexture( Image* image )
  161. {
  162. _texture = image;
  163. }
  164. void Raster::textureCoordPointer( int size,DATETYPE type,int stride,const void* data )
  165. {
  166. _uvPointer._size = size;
  167. _uvPointer._type = type;
  168. _uvPointer._stride = stride;
  169. _uvPointer._data = data;
  170. }
  171. void Raster::colorPointer( int size,DATETYPE type,int stride,const void* data )
  172. {
  173. _colorPointer._size = size;
  174. _colorPointer._type = type;
  175. _colorPointer._stride = stride;
  176. _colorPointer._data = data;
  177. }
  178. void Raster::vertexPointer( int size,DATETYPE type,int stride,const void* data )
  179. {
  180. _poitionPointer._size = size;
  181. _poitionPointer._type = type;
  182. _poitionPointer._stride = stride;
  183. _poitionPointer._data = data;
  184. }
  185. void Raster::loadMatrix( const CELL::matrix4& mat )
  186. {
  187. _matModel = mat;
  188. }
  189. void Raster::loadIdentity()
  190. {
  191. _matModel = CELL::matrix4(1);
  192. }
  193. void Raster::loadProjMatrix( const CELL::matrix4& mat )
  194. {
  195. _matProj = mat;
  196. }
  197. void Raster::loadProjIdentity( const CELL::matrix4& mat )
  198. {
  199. _matProj = CELL::matrix4(1);
  200. }
  201. void Raster::loadViewMatrix( const CELL::matrix4& mat )
  202. {
  203. _matView = mat;
  204. }
  205. void Raster::loadViewIdentity( const CELL::matrix4& mat )
  206. {
  207. _matView = CELL::matrix4(1);
  208. }
  209. void Raster::setPerspective( float fovy, float aspect, float zNear, float zFar )
  210. {
  211. _matProj = CELL::perspective<float>(fovy,aspect,zNear,zFar);
  212. }
  213. void Raster::lookat( float3 const & eye,float3 const & center,float3 const & up )
  214. {
  215. _matView = CELL::lookAt(eye,center,up);
  216. }
  217. void Raster::setView( const matrix4& viewMat )
  218. {
  219. _matView = viewMat;
  220. }
  221. void Raster::setViewPort( int x,int y,int w,int h )
  222. {
  223. _viewPort.x = w;
  224. _viewPort.y = h;
  225. }
  226. }