cubemapData.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/gfxTransformSaver.h"
  29. #include "gfx/gfxDebugEvent.h"
  30. #include "scene/sceneManager.h"
  31. #include "console/engineAPI.h"
  32. #include "math/mathUtils.h"
  33. IMPLEMENT_CONOBJECT( CubemapData );
  34. CubemapData::CubemapData()
  35. {
  36. mCubemap = NULL;
  37. }
  38. CubemapData::~CubemapData()
  39. {
  40. mCubemap = NULL;
  41. }
  42. ConsoleDocClass( CubemapData,
  43. "@brief Used to create static or dynamic cubemaps.\n\n"
  44. "This object is used with Material, WaterObject, and other objects for cubemap reflections.\n\n"
  45. "A simple declaration of a static cubemap:\n"
  46. "@tsexample\n"
  47. "singleton CubemapData( SkyboxCubemap )\n"
  48. "{\n"
  49. " cubeFace[0] = \"./skybox_1\";\n"
  50. " cubeFace[1] = \"./skybox_2\";\n"
  51. " cubeFace[2] = \"./skybox_3\";\n"
  52. " cubeFace[3] = \"./skybox_4\";\n"
  53. " cubeFace[4] = \"./skybox_5\";\n"
  54. " cubeFace[5] = \"./skybox_6\";\n"
  55. "};\n"
  56. "@endtsexample\n"
  57. "@note The dynamic cubemap functionality in CubemapData has been depreciated in favor of ReflectorDesc.\n"
  58. "@see ReflectorDesc\n"
  59. "@ingroup GFX\n" );
  60. void CubemapData::initPersistFields()
  61. {
  62. addField( "cubeFace", TypeStringFilename, Offset(mCubeFaceFile, CubemapData), 6,
  63. "@brief The 6 cubemap face textures for a static cubemap.\n\n"
  64. "They are in the following order:\n"
  65. " - cubeFace[0] is -X\n"
  66. " - cubeFace[1] is +X\n"
  67. " - cubeFace[2] is -Z\n"
  68. " - cubeFace[3] is +Z\n"
  69. " - cubeFace[4] is -Y\n"
  70. " - cubeFace[5] is +Y\n" );
  71. Parent::initPersistFields();
  72. }
  73. bool CubemapData::onAdd()
  74. {
  75. if( !Parent::onAdd() )
  76. return false;
  77. // Do NOT call this here as it forces every single cubemap defined to load its images, immediately, without mercy
  78. // createMap();
  79. return true;
  80. }
  81. void CubemapData::createMap()
  82. {
  83. if( !mCubemap )
  84. {
  85. bool initSuccess = true;
  86. for( U32 i=0; i<6; i++ )
  87. {
  88. if( !mCubeFaceFile[i].isEmpty() )
  89. {
  90. if(!mCubeFace[i].set(mCubeFaceFile[i], &GFXDefaultStaticDiffuseProfile, avar("%s() - mCubeFace[%d] (line %d)", __FUNCTION__, i, __LINE__) ))
  91. {
  92. Con::errorf("CubemapData::createMap - Failed to load texture '%s'", mCubeFaceFile[i].c_str());
  93. initSuccess = false;
  94. }
  95. }
  96. }
  97. if( initSuccess )
  98. {
  99. mCubemap = GFX->createCubemap();
  100. mCubemap->initStatic( mCubeFace );
  101. }
  102. }
  103. }
  104. void CubemapData::updateFaces()
  105. {
  106. bool initSuccess = true;
  107. for( U32 i=0; i<6; i++ )
  108. {
  109. if( !mCubeFaceFile[i].isEmpty() )
  110. {
  111. if(!mCubeFace[i].set(mCubeFaceFile[i], &GFXDefaultStaticDiffuseProfile, avar("%s() - mCubeFace[%d] (line %d)", __FUNCTION__, i, __LINE__) ))
  112. {
  113. initSuccess = false;
  114. Con::errorf("CubemapData::createMap - Failed to load texture '%s'", mCubeFaceFile[i].c_str());
  115. }
  116. }
  117. }
  118. if( initSuccess )
  119. {
  120. mCubemap = NULL;
  121. mCubemap = GFX->createCubemap();
  122. mCubemap->initStatic( mCubeFace );
  123. }
  124. }
  125. DefineEngineMethod( CubemapData, updateFaces, void, (),,
  126. "Update the assigned cubemaps faces." )
  127. {
  128. object->updateFaces();
  129. }
  130. DefineEngineMethod( CubemapData, getFilename, const char*, (),,
  131. "Returns the script filename of where the CubemapData object was "
  132. "defined. This is used by the material editor." )
  133. {
  134. return object->getFilename();
  135. }