cubemapSaver.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "gfx/bitmap/cubemapSaver.h"
  23. #include "platform/platform.h"
  24. #include "gfx/bitmap/ddsFile.h"
  25. #include "gfx/bitmap/imageUtils.h"
  26. #include "gfx/gfxDevice.h"
  27. #include "gfx/gfxTransformSaver.h"
  28. #include "gfx/gfxTextureManager.h"
  29. #include "materials/shaderData.h"
  30. #include "core/stream/fileStream.h"
  31. #include "math/mathUtils.h"
  32. #include "math/mTransform.h"
  33. namespace CubemapSaver
  34. {
  35. const U32 CubeFaces = 6;
  36. bool save(GFXCubemapHandle cubemap, const Torque::Path &path, GFXFormat compressionFormat)
  37. {
  38. if (!cubemap.isValid())
  39. {
  40. Con::errorf("CubemapSaver: cubemap handle is not valid");
  41. return false;
  42. }
  43. GFXCubemap *pCubemap = cubemap.getPointer();
  44. const U32 faceSize = pCubemap->getSize();
  45. const U32 mipLevels = pCubemap->getMipMapLevels();
  46. GFXFormat targetFmt = pCubemap->getFormat();
  47. //setup render targets
  48. GFXTexHandle pTextures[CubeFaces];
  49. for (U32 face = 0; face < CubeFaces; face++)
  50. {
  51. pTextures[face].set(faceSize, faceSize, targetFmt,
  52. &GFXTexturePersistentProfile, avar("%s() - (line %d)", __FUNCTION__, __LINE__),
  53. mipLevels, GFXTextureManager::AA_MATCH_BACKBUFFER);
  54. // yep t3d has funky z up, need to change the face order
  55. GFX->copyResource(pTextures[face], pCubemap, GFXCubemap::zUpFaceIndex(face) );
  56. }
  57. GBitmap *pBitmaps[CubeFaces];
  58. bool error = false;
  59. const bool compressedFormat = ImageUtil::isCompressedFormat(compressionFormat);
  60. const bool hasMips = mipLevels > 1 ? true : false;
  61. for (U32 i = 0; i < CubeFaces; i++)
  62. {
  63. pBitmaps[i] = new GBitmap(faceSize, faceSize, hasMips, targetFmt);
  64. bool result = pTextures[i].copyToBmp(pBitmaps[i]);
  65. if (!result)
  66. {
  67. Con::errorf("CubemapSaver: cubemap number %u failed to copy", i);
  68. error = true;
  69. }
  70. }
  71. if (!error)
  72. {
  73. DDSFile *pDds = DDSFile::createDDSCubemapFileFromGBitmaps(pBitmaps);
  74. if (pDds)
  75. {
  76. // compressed and floating point don't need swizzling
  77. if (!compressedFormat && targetFmt != GFXFormatR16G16B16A16F)
  78. ImageUtil::swizzleDDS(pDds, Swizzles::bgra);
  79. if(compressedFormat)
  80. ImageUtil::ddsCompress(pDds, compressionFormat);
  81. FileStream stream;
  82. stream.open(path, Torque::FS::File::Write);
  83. if (stream.getStatus() == Stream::Ok)
  84. pDds->write(stream);
  85. else
  86. Con::errorf("CubemapSaver: failed to open file stream for file %s", path.getFullPath().c_str());
  87. SAFE_DELETE(pDds);
  88. }
  89. }
  90. //cleanup
  91. for (U32 i = 0; i < CubeFaces; i++)
  92. SAFE_DELETE(pBitmaps[i]);
  93. return true;
  94. }
  95. bool getBitmaps(GFXCubemapHandle cubemap, GFXFormat compressionFormat, GBitmap* faceBitmaps[6])
  96. {
  97. if (!cubemap.isValid())
  98. {
  99. Con::errorf("CubemapSaver: cubemap handle is not valid");
  100. return false;
  101. }
  102. return false;
  103. }
  104. }