cubemapData.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "platform/platform.h"
  23. #include "gfx/sim/cubemapData.h"
  24. #include "core/stream/bitStream.h"
  25. #include "console/consoleTypes.h"
  26. #include "gfx/gfxCubemap.h"
  27. #include "gfx/gfxDevice.h"
  28. #include "gfx/gfxTextureManager.h"
  29. #include "gfx/gfxTransformSaver.h"
  30. #include "gfx/gfxDebugEvent.h"
  31. #include "gfx/gfxAPI.h"
  32. #include "scene/sceneManager.h"
  33. #include "console/engineAPI.h"
  34. #include "math/mathUtils.h"
  35. #include "gfx/bitmap/cubemapSaver.h"
  36. IMPLEMENT_CONOBJECT( CubemapData );
  37. CubemapData::CubemapData()
  38. {
  39. mCubemap = NULL;
  40. for (U32 i = 0; i < 6; i++)
  41. {
  42. INIT_IMAGEASSET_ARRAY(CubeMapFace, GFXStaticTextureSRGBProfile, i);
  43. }
  44. INIT_ASSET(CubeMap);
  45. }
  46. CubemapData::~CubemapData()
  47. {
  48. mCubemap = NULL;
  49. }
  50. ConsoleDocClass( CubemapData,
  51. "@brief Used to create static or dynamic cubemaps.\n\n"
  52. "This object is used with Material, WaterObject, and other objects for cubemap reflections.\n\n"
  53. "A simple declaration of a static cubemap:\n"
  54. "@tsexample\n"
  55. "singleton CubemapData( SkyboxCubemap )\n"
  56. "{\n"
  57. " cubeFace[0] = \"./skybox_1\";\n"
  58. " cubeFace[1] = \"./skybox_2\";\n"
  59. " cubeFace[2] = \"./skybox_3\";\n"
  60. " cubeFace[3] = \"./skybox_4\";\n"
  61. " cubeFace[4] = \"./skybox_5\";\n"
  62. " cubeFace[5] = \"./skybox_6\";\n"
  63. "};\n"
  64. "@endtsexample\n"
  65. "@note The dynamic cubemap functionality in CubemapData has been depreciated in favor of ReflectorDesc.\n"
  66. "@see ReflectorDesc\n"
  67. "@ingroup GFX\n" );
  68. void CubemapData::initPersistFields()
  69. {
  70. addProtectedField( "cubeFace", TypeStringFilename, Offset(mCubeMapFaceName, CubemapData), _setCubeMapFaceData, defaultProtectedGetFn, 6,
  71. "@brief The 6 cubemap face textures for a static cubemap.\n\n"
  72. "They are in the following order:\n"
  73. " - cubeFace[0] is -X\n"
  74. " - cubeFace[1] is +X\n"
  75. " - cubeFace[2] is -Z\n"
  76. " - cubeFace[3] is +Z\n"
  77. " - cubeFace[4] is -Y\n"
  78. " - cubeFace[5] is +Y\n", AbstractClassRep::FIELD_HideInInspectors );
  79. INITPERSISTFIELD_IMAGEASSET_ARRAY(CubeMapFace, 6, CubemapData, "@brief The 6 cubemap face textures for a static cubemap.\n\n"
  80. "They are in the following order:\n"
  81. " - cubeFace[0] is -X\n"
  82. " - cubeFace[1] is +X\n"
  83. " - cubeFace[2] is -Z\n"
  84. " - cubeFace[3] is +Z\n"
  85. " - cubeFace[4] is -Y\n"
  86. " - cubeFace[5] is +Y\n");
  87. INITPERSISTFIELD_IMAGEASSET(CubeMap, CubemapData, "@brief Cubemap dds Image Asset.\n\n");
  88. }
  89. bool CubemapData::onAdd()
  90. {
  91. if( !Parent::onAdd() )
  92. return false;
  93. // Do NOT call this here as it forces every single cubemap defined to load its images, immediately, without mercy
  94. // createMap();
  95. return true;
  96. }
  97. void CubemapData::createMap()
  98. {
  99. if( !mCubemap )
  100. {
  101. bool initSuccess = true;
  102. //check mCubeMapFile first
  103. if (getCubeMap() != StringTable->EmptyString())
  104. {
  105. mCubemap = TEXMGR->createCubemap(getCubeMap());
  106. return;
  107. }
  108. else
  109. {
  110. for (U32 i = 0; i < 6; i++)
  111. {
  112. if (!_setCubeMapFace(getCubeMapFace(i), i))
  113. {
  114. Con::errorf("CubemapData::createMap - Failed to load texture '%s'", getCubeMapFace(i));
  115. initSuccess = false;
  116. }
  117. }
  118. }
  119. if( initSuccess )
  120. {
  121. mCubemap = GFX->createCubemap();
  122. if (!mCubeMapFace || mCubeMapFace->isNull())
  123. return;
  124. mCubemap->initStatic(mCubeMapFace);
  125. }
  126. }
  127. }
  128. void CubemapData::updateFaces()
  129. {
  130. bool initSuccess = true;
  131. for( U32 i=0; i<6; i++ )
  132. {
  133. //check mCubeMapFile first
  134. if (getCubeMap() != StringTable->EmptyString())
  135. {
  136. mCubemap = TEXMGR->createCubemap(getCubeMap());
  137. return;
  138. }
  139. else
  140. {
  141. if (!_setCubeMapFace(getCubeMapFace(i), i))
  142. {
  143. Con::errorf("CubemapData::createMap - Failed to load texture '%s'", getCubeMapFace(i));
  144. initSuccess = false;
  145. }
  146. }
  147. }
  148. if( initSuccess )
  149. {
  150. mCubemap = NULL;
  151. mCubemap = GFX->createCubemap();
  152. mCubemap->initStatic( mCubeMapFace );
  153. }
  154. }
  155. void CubemapData::setCubemapFile(FileName newCubemapFile)
  156. {
  157. mCubeMapName = newCubemapFile;
  158. }
  159. void CubemapData::setCubeFaceFile(U32 index, FileName newFaceFile)
  160. {
  161. if (index >= 6)
  162. return;
  163. mCubeMapFaceName[index] = newFaceFile;
  164. }
  165. void CubemapData::setCubeFaceTexture(U32 index, GFXTexHandle newFaceTexture)
  166. {
  167. if (index >= 6)
  168. return;
  169. mCubeMapFace[index] = newFaceTexture;
  170. }
  171. DefineEngineMethod( CubemapData, updateFaces, void, (),,
  172. "Update the assigned cubemaps faces." )
  173. {
  174. object->updateFaces();
  175. }
  176. DefineEngineMethod( CubemapData, getFilename, const char*, (),,
  177. "Returns the script filename of where the CubemapData object was "
  178. "defined. This is used by the material editor." )
  179. {
  180. return object->getFilename();
  181. }
  182. DefineEngineMethod(CubemapData, save, void, (const char* filename, const GFXFormat format), ("", GFXFormatBC1),
  183. "Returns the script filename of where the CubemapData object was "
  184. "defined. This is used by the material editor.")
  185. {
  186. if (dStrEqual(filename, ""))
  187. filename = object->getName();
  188. //add dds extension if needed
  189. String finalName = String(filename);
  190. if(!finalName.endsWith(".dds") || !finalName.endsWith(".DDS"))
  191. finalName += String(".dds");
  192. CubemapSaver::save(object->mCubemap, finalName, format);
  193. }