CmTextureData.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "CmTextureData.h"
  2. #include "CmException.h"
  3. namespace CamelotEngine
  4. {
  5. TextureData::TextureData(UINT32 width, UINT32 height, UINT32 size,
  6. PixelFormat format, UINT8* data, UINT32 depth, INT32 flags, UINT32 numMipmaps)
  7. :mWidth(width), mHeight(height), mDepth(depth), mSize(size), mFormat(format),
  8. mFlags(flags), mNumMipmaps(numMipmaps), mData(data)
  9. {
  10. mBPP = static_cast<UINT8>(PixelUtil::getNumElemBytes(mFormat)) * 8;
  11. }
  12. TextureData::~TextureData()
  13. {
  14. if(mData != nullptr)
  15. delete[] mData;
  16. }
  17. PixelData TextureData::getPixels(UINT32 mip)
  18. {
  19. if(mip < 0 || mip > mNumMipmaps)
  20. {
  21. CM_EXCEPT(InvalidParametersException, "Mip out of range: " + toString(mip) + ". While maximum available mip is: " + toString((mNumMipmaps)));
  22. }
  23. // Calculate mipmap offset and size
  24. UINT8 *offset = const_cast<UINT8*>(getData());
  25. UINT8 offsetSoFar = 0;
  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. offset += offsetSoFar;
  35. finalWidth = width;
  36. finalHeight = height;
  37. finalDepth = depth;
  38. }
  39. offsetSoFar += PixelUtil::getMemorySize(width, height, depth, getFormat());
  40. /// Half size in each dimension
  41. if(width!=1) width /= 2;
  42. if(height!=1) height /= 2;
  43. if(depth!=1) depth /= 2;
  44. }
  45. // Return subface as pixelbox
  46. PixelData src(finalWidth, finalHeight, finalDepth, getFormat(), offset);
  47. return src;
  48. }
  49. }