cubemapData.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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(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(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'", mCubeMapFaceAsset[i]->getImageFile());
  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. //check mCubeMapFile first
  126. if (mCubeMapAsset.notNull())
  127. {
  128. mCubemap = TEXMGR->createCubemap(mCubeMapAsset->getImageFile());
  129. return;
  130. }
  131. else
  132. {
  133. for (U32 i = 0; i < 6; i++)
  134. {
  135. if (mCubeMapFaceAsset[i].notNull())
  136. {
  137. if (!getCubeMapFace(i))
  138. {
  139. Con::errorf("CubemapData::createMap - Failed to load texture '%s'", mCubeMapFaceAsset[i]->getImageFile());
  140. initSuccess = false;
  141. }
  142. else
  143. {
  144. mCubeMapFaceTex[i] = getCubeMapFace(i);
  145. }
  146. }
  147. }
  148. }
  149. if( initSuccess )
  150. {
  151. mCubemap = NULL;
  152. mCubemap = GFX->createCubemap();
  153. if (mCubeMapFaceAsset->isNull())
  154. return;
  155. mCubemap->initStatic(mCubeMapFaceTex);
  156. }
  157. }
  158. void CubemapData::setCubemapFile(FileName newCubemapFile)
  159. {
  160. _setCubeMap(newCubemapFile);
  161. }
  162. void CubemapData::setCubeFaceFile(U32 index, FileName newFaceFile)
  163. {
  164. if (index >= 6)
  165. return;
  166. _setCubeMapFace(newFaceFile, index);
  167. }
  168. void CubemapData::setCubeFaceTexture(U32 index, GFXTexHandle newFaceTexture)
  169. {
  170. if (index >= 6)
  171. return;
  172. mCubeMapFaceTex[index] = newFaceTexture;
  173. }
  174. DefineEngineMethod( CubemapData, updateFaces, void, (),,
  175. "Update the assigned cubemaps faces." )
  176. {
  177. object->updateFaces();
  178. }
  179. DefineEngineMethod( CubemapData, getFilename, const char*, (),,
  180. "Returns the script filename of where the CubemapData object was "
  181. "defined. This is used by the material editor." )
  182. {
  183. return object->getFilename();
  184. }
  185. DefineEngineMethod(CubemapData, save, void, (const char* filename, const GFXFormat format), ("", GFXFormatBC1),
  186. "Returns the script filename of where the CubemapData object was "
  187. "defined. This is used by the material editor.")
  188. {
  189. if (dStrEqual(filename, ""))
  190. filename = object->getName();
  191. //add dds extension if needed
  192. String finalName = String(filename);
  193. if(!finalName.endsWith(".dds") || !finalName.endsWith(".DDS"))
  194. finalName += String(".dds");
  195. CubemapSaver::save(object->mCubemap, finalName, format);
  196. }