cubemapData.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. addField( "cubeFace", TypeStringFilename, Offset(mCubeFaceFile, CubemapData), 6,
  66. "@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. addField("cubeMap", TypeStringFilename, Offset(mCubeMapFile, CubemapData),
  75. "@brief Cubemap dds file.\n\n");
  76. }
  77. bool CubemapData::onAdd()
  78. {
  79. if( !Parent::onAdd() )
  80. return false;
  81. // Do NOT call this here as it forces every single cubemap defined to load its images, immediately, without mercy
  82. // createMap();
  83. return true;
  84. }
  85. void CubemapData::createMap()
  86. {
  87. if( !mCubemap )
  88. {
  89. bool initSuccess = true;
  90. //check mCubeMapFile first
  91. if (!mCubeMapFile.isEmpty())
  92. {
  93. mCubemap = TEXMGR->createCubemap(mCubeMapFile);
  94. return;
  95. }
  96. else
  97. {
  98. for (U32 i = 0; i < 6; i++)
  99. {
  100. if (!mCubeFaceFile[i].isEmpty())
  101. {
  102. if (!mCubeFace[i].set(mCubeFaceFile[i], &GFXStaticTextureProfile, avar("%s() - mCubeFace[%d] (line %d)", __FUNCTION__, i, __LINE__)))
  103. {
  104. Con::errorf("CubemapData::createMap - Failed to load texture '%s'", mCubeFaceFile[i].c_str());
  105. initSuccess = false;
  106. }
  107. }
  108. }
  109. }
  110. if( initSuccess )
  111. {
  112. mCubemap = GFX->createCubemap();
  113. if (mCubeFace == NULL || mCubeFace->isNull()) return;
  114. mCubemap->initStatic( mCubeFace );
  115. }
  116. }
  117. }
  118. void CubemapData::updateFaces()
  119. {
  120. bool initSuccess = true;
  121. for( U32 i=0; i<6; i++ )
  122. {
  123. //check mCubeMapFile first
  124. if (!mCubeMapFile.isEmpty())
  125. {
  126. mCubemap = TEXMGR->createCubemap(mCubeMapFile);
  127. return;
  128. }
  129. else
  130. {
  131. if (!mCubeFaceFile[i].isEmpty())
  132. {
  133. if (!mCubeFace[i].set(mCubeFaceFile[i], &GFXStaticTextureProfile, avar("%s() - mCubeFace[%d] (line %d)", __FUNCTION__, i, __LINE__)))
  134. {
  135. initSuccess = false;
  136. Con::errorf("CubemapData::createMap - Failed to load texture '%s'", mCubeFaceFile[i].c_str());
  137. }
  138. }
  139. }
  140. }
  141. if( initSuccess )
  142. {
  143. mCubemap = NULL;
  144. mCubemap = GFX->createCubemap();
  145. mCubemap->initStatic( mCubeFace );
  146. }
  147. }
  148. void CubemapData::setCubeFaceFile(U32 index, FileName newFaceFile)
  149. {
  150. if (index >= 6)
  151. return;
  152. mCubeFaceFile[index] = newFaceFile;
  153. }
  154. DefineEngineMethod( CubemapData, updateFaces, void, (),,
  155. "Update the assigned cubemaps faces." )
  156. {
  157. object->updateFaces();
  158. }
  159. DefineEngineMethod( CubemapData, getFilename, const char*, (),,
  160. "Returns the script filename of where the CubemapData object was "
  161. "defined. This is used by the material editor." )
  162. {
  163. return object->getFilename();
  164. }
  165. DefineEngineMethod(CubemapData, save, void, (const char* filename, const GFXFormat format), ("", GFXFormatBC1),
  166. "Returns the script filename of where the CubemapData object was "
  167. "defined. This is used by the material editor.")
  168. {
  169. if (filename == "")
  170. filename = object->getName();
  171. //add dds extension if needed
  172. String finalName = String(filename);
  173. if(!finalName.endsWith(".dds") || !finalName.endsWith(".DDS"))
  174. finalName += String(".dds");
  175. CubemapSaver::save(object->mCubemap, finalName, format);
  176. }