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