CmTextureData.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "CmTextureData.h"
  2. #include "CmException.h"
  3. #include "CmTextureDataST.h"
  4. namespace CamelotEngine
  5. {
  6. TextureData::TextureData(UINT32 width, UINT32 height, UINT32 size,
  7. PixelFormat format, UINT8* data, UINT32 depth, INT32 flags, UINT32 numMipmaps)
  8. :mWidth(width), mHeight(height), mDepth(depth), mSize(size), mFormat(format),
  9. mFlags(flags), mNumMipmaps(numMipmaps), mData(data)
  10. {
  11. mBPP = static_cast<UINT8>(PixelUtil::getNumElemBytes(mFormat)) * 8;
  12. }
  13. TextureData::~TextureData()
  14. {
  15. if(mData != nullptr)
  16. delete[] mData;
  17. }
  18. PixelData TextureData::getPixels(UINT32 mip)
  19. {
  20. if(mip < 0 || mip >= mNumMipmaps)
  21. {
  22. CM_EXCEPT(InvalidParametersException, "Mip out of range: " + toString(mip) + ". While maximum available mip is: " + toString((mNumMipmaps)));
  23. }
  24. // Calculate mipmap offset and size
  25. UINT8 *offset = const_cast<UINT8*>(getData());
  26. UINT32 width = getWidth(), height = getHeight(), depth = getDepth();
  27. UINT32 numMips = getNumMipmaps();
  28. // Figure out the offsets
  29. UINT32 finalWidth = 0, finalHeight = 0, finalDepth = 0;
  30. for(UINT32 curMip = 0; curMip <= numMips; ++curMip)
  31. {
  32. if (curMip == mip)
  33. {
  34. finalWidth = width;
  35. finalHeight = height;
  36. finalDepth = depth;
  37. }
  38. offset += PixelUtil::getMemorySize(width, height, depth, getFormat());
  39. /// Half size in each dimension
  40. if(width!=1) width /= 2;
  41. if(height!=1) height /= 2;
  42. if(depth!=1) depth /= 2;
  43. }
  44. // Return subface as pixelbox
  45. PixelData src(finalWidth, finalHeight, finalDepth, getFormat(), offset);
  46. return src;
  47. }
  48. /************************************************************************/
  49. /* SERIALIZATION */
  50. /************************************************************************/
  51. SerializableType* TextureData::getSerializable() const
  52. {
  53. static TextureDataST serializableType;
  54. return &serializableType;
  55. }
  56. TextureData* TextureData::newObject()
  57. {
  58. return new TextureData();
  59. }
  60. }