CmTextureData.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. UINT8 offsetSoFar = 0;
  27. UINT32 width = getWidth(), height = getHeight(), depth = getDepth();
  28. UINT32 numMips = getNumMipmaps();
  29. // Figure out the offsets
  30. UINT32 finalWidth = 0, finalHeight = 0, finalDepth = 0;
  31. for(UINT32 curMip = 0; curMip <= numMips; ++curMip)
  32. {
  33. if (curMip == mip)
  34. {
  35. offset += offsetSoFar;
  36. finalWidth = width;
  37. finalHeight = height;
  38. finalDepth = depth;
  39. }
  40. offsetSoFar += PixelUtil::getMemorySize(width, height, depth, getFormat());
  41. /// Half size in each dimension
  42. if(width!=1) width /= 2;
  43. if(height!=1) height /= 2;
  44. if(depth!=1) depth /= 2;
  45. }
  46. // Return subface as pixelbox
  47. PixelData src(finalWidth, finalHeight, finalDepth, getFormat(), offset);
  48. return src;
  49. }
  50. /************************************************************************/
  51. /* SERIALIZATION */
  52. /************************************************************************/
  53. SerializableType* TextureData::getSerializable() const
  54. {
  55. static TextureDataST serializableType;
  56. return &serializableType;
  57. }
  58. TextureData* TextureData::newObject()
  59. {
  60. return new TextureData();
  61. }
  62. }