cubemapData.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. IMPLEMENT_CONOBJECT( CubemapData );
  36. CubemapData::CubemapData()
  37. {
  38. mCubemap = NULL;
  39. }
  40. CubemapData::~CubemapData()
  41. {
  42. mCubemap = NULL;
  43. }
  44. ConsoleDocClass( CubemapData,
  45. "@brief Used to create static or dynamic cubemaps.\n\n"
  46. "This object is used with Material, WaterObject, and other objects for cubemap reflections.\n\n"
  47. "A simple declaration of a static cubemap:\n"
  48. "@tsexample\n"
  49. "singleton CubemapData( SkyboxCubemap )\n"
  50. "{\n"
  51. " cubeFace[0] = \"./skybox_1\";\n"
  52. " cubeFace[1] = \"./skybox_2\";\n"
  53. " cubeFace[2] = \"./skybox_3\";\n"
  54. " cubeFace[3] = \"./skybox_4\";\n"
  55. " cubeFace[4] = \"./skybox_5\";\n"
  56. " cubeFace[5] = \"./skybox_6\";\n"
  57. "};\n"
  58. "@endtsexample\n"
  59. "@note The dynamic cubemap functionality in CubemapData has been depreciated in favor of ReflectorDesc.\n"
  60. "@see ReflectorDesc\n"
  61. "@ingroup GFX\n" );
  62. void CubemapData::initPersistFields()
  63. {
  64. addField( "cubeFace", TypeStringFilename, Offset(mCubeFaceFile, CubemapData), 6,
  65. "@brief The 6 cubemap face textures for a static cubemap.\n\n"
  66. "They are in the following order:\n"
  67. " - cubeFace[0] is -X\n"
  68. " - cubeFace[1] is +X\n"
  69. " - cubeFace[2] is -Z\n"
  70. " - cubeFace[3] is +Z\n"
  71. " - cubeFace[4] is -Y\n"
  72. " - cubeFace[5] is +Y\n" );
  73. addField("cubeMap", TypeStringFilename, Offset(mCubeMapFile, CubemapData),
  74. "@brief Cubemap dds file.\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 (!mCubeMapFile.isEmpty())
  91. {
  92. mCubemap = TEXMGR->createCubemap(mCubeMapFile);
  93. return;
  94. }
  95. else
  96. {
  97. for (U32 i = 0; i < 6; i++)
  98. {
  99. if (!mCubeFaceFile[i].isEmpty())
  100. {
  101. if (!mCubeFace[i].set(mCubeFaceFile[i], &GFXStaticTextureSRGBProfile, avar("%s() - mCubeFace[%d] (line %d)", __FUNCTION__, i, __LINE__)))
  102. {
  103. Con::errorf("CubemapData::createMap - Failed to load texture '%s'", mCubeFaceFile[i].c_str());
  104. initSuccess = false;
  105. }
  106. }
  107. }
  108. }
  109. if( initSuccess )
  110. {
  111. mCubemap = GFX->createCubemap();
  112. if (mCubeFace == NULL || mCubeFace->isNull()) return;
  113. mCubemap->initStatic( mCubeFace );
  114. }
  115. }
  116. }
  117. void CubemapData::updateFaces()
  118. {
  119. bool initSuccess = true;
  120. for( U32 i=0; i<6; i++ )
  121. {
  122. //check mCubeMapFile first
  123. if (!mCubeMapFile.isEmpty())
  124. {
  125. mCubemap = TEXMGR->createCubemap(mCubeMapFile);
  126. return;
  127. }
  128. else
  129. {
  130. if (!mCubeFaceFile[i].isEmpty())
  131. {
  132. if (!mCubeFace[i].set(mCubeFaceFile[i], &GFXStaticTextureProfile, avar("%s() - mCubeFace[%d] (line %d)", __FUNCTION__, i, __LINE__)))
  133. {
  134. initSuccess = false;
  135. Con::errorf("CubemapData::createMap - Failed to load texture '%s'", mCubeFaceFile[i].c_str());
  136. }
  137. }
  138. }
  139. }
  140. if( initSuccess )
  141. {
  142. mCubemap = NULL;
  143. mCubemap = GFX->createCubemap();
  144. mCubemap->initStatic( mCubeFace );
  145. }
  146. }
  147. void CubemapData::setCubeFaceFile(U32 index, FileName newFaceFile)
  148. {
  149. if (index >= 6)
  150. return;
  151. mCubeFaceFile[index] = newFaceFile;
  152. }
  153. DefineEngineMethod( CubemapData, updateFaces, void, (),,
  154. "Update the assigned cubemaps faces." )
  155. {
  156. object->updateFaces();
  157. }
  158. DefineEngineMethod( CubemapData, getFilename, const char*, (),,
  159. "Returns the script filename of where the CubemapData object was "
  160. "defined. This is used by the material editor." )
  161. {
  162. return object->getFilename();
  163. }