cubemapData.cpp 6.2 KB

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