CmTextureData.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. UINT32 width = getWidth(), height = getHeight(), depth = getDepth();
  26. UINT32 numMips = getNumMipmaps();
  27. // Figure out the offsets
  28. UINT32 finalWidth = 0, finalHeight = 0, finalDepth = 0;
  29. for(UINT32 curMip = 0; curMip <= numMips; ++curMip)
  30. {
  31. if (curMip == mip)
  32. {
  33. finalWidth = width;
  34. finalHeight = height;
  35. finalDepth = depth;
  36. }
  37. offset += PixelUtil::getMemorySize(width, height, depth, getFormat());
  38. /// Half size in each dimension
  39. if(width!=1) width /= 2;
  40. if(height!=1) height /= 2;
  41. if(depth!=1) depth /= 2;
  42. }
  43. // Return subface as pixelbox
  44. PixelData src(finalWidth, finalHeight, finalDepth, getFormat(), offset);
  45. return src;
  46. }
  47. }