gfxCubemap.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "gfx/gfxCubemap.h"
  23. #include "gfx/gfxDevice.h"
  24. #include "gfx/bitmap/gBitmap.h"
  25. #include "gfx/gfxTextureManager.h"
  26. #include "gfx/bitmap/imageUtils.h"
  27. GFXCubemap::GFXCubemap()
  28. {
  29. mPath = "";
  30. mMipMapLevels = 0;
  31. mInitialized = false;
  32. }
  33. GFXCubemap::~GFXCubemap()
  34. {
  35. // If we're not dynamic and we were loaded from a
  36. // file then give the texture manager a chance to
  37. // remove us from the cache.
  38. if ( mPath.isNotEmpty() )
  39. TEXMGR->releaseCubemap( this );
  40. }
  41. U32 GFXCubemap::zUpFaceIndex(const U32 index)
  42. {
  43. switch (index)
  44. {
  45. case 2:
  46. return 4;
  47. case 3:
  48. return 5;
  49. case 4:
  50. return 2;
  51. case 5:
  52. return 3;
  53. default:
  54. return index;
  55. };
  56. }
  57. void GFXCubemap::initNormalize( U32 size )
  58. {
  59. Point3F axis[6] =
  60. {Point3F(1.0, 0.0, 0.0), Point3F(-1.0, 0.0, 0.0),
  61. Point3F(0.0, 1.0, 0.0), Point3F( 0.0, -1.0, 0.0),
  62. Point3F(0.0, 0.0, 1.0), Point3F( 0.0, 0.0, -1.0),};
  63. Point3F s[6] =
  64. {Point3F(0.0, 0.0, -1.0), Point3F( 0.0, 0.0, 1.0),
  65. Point3F(1.0, 0.0, 0.0), Point3F( 1.0, 0.0, 0.0),
  66. Point3F(1.0, 0.0, 0.0), Point3F(-1.0, 0.0, 0.0),};
  67. Point3F t[6] =
  68. {Point3F(0.0, -1.0, 0.0), Point3F(0.0, -1.0, 0.0),
  69. Point3F(0.0, 0.0, 1.0), Point3F(0.0, 0.0, -1.0),
  70. Point3F(0.0, -1.0, 0.0), Point3F(0.0, -1.0, 0.0),};
  71. F32 span = 2.0;
  72. F32 start = -1.0;
  73. F32 stride = span / F32(size - 1);
  74. GFXTexHandle faces[6];
  75. for(U32 i=0; i<6; i++)
  76. {
  77. GFXTexHandle &tex = faces[i];
  78. GBitmap *bitmap = new GBitmap(size, size);
  79. // fill in...
  80. for(U32 v=0; v<size; v++)
  81. {
  82. for(U32 u=0; u<size; u++)
  83. {
  84. Point3F vector;
  85. vector = axis[i] +
  86. ((F32(u) * stride) + start) * s[i] +
  87. ((F32(v) * stride) + start) * t[i];
  88. vector.normalizeSafe();
  89. vector = ((vector * 0.5) + Point3F(0.5, 0.5, 0.5)) * 255.0;
  90. vector.x = mClampF(vector.x, 0.0f, 255.0f);
  91. vector.y = mClampF(vector.y, 0.0f, 255.0f);
  92. vector.z = mClampF(vector.z, 0.0f, 255.0f);
  93. // easy way to avoid knowledge of the format (RGB, RGBA, RGBX, ...)...
  94. U8 *bits = bitmap->getAddress(u, v);
  95. bits[0] = U8(vector.x);
  96. bits[1] = U8(vector.y);
  97. bits[2] = U8(vector.z);
  98. }
  99. }
  100. tex.set(bitmap, &GFXStaticTextureSRGBProfile, true, "Cubemap");
  101. }
  102. initStatic(faces);
  103. }
  104. const String GFXCubemap::describeSelf() const
  105. {
  106. // We've got nothing
  107. return String();
  108. }
  109. bool GFXCubemapHandle::set( const String &cubemapDDS )
  110. {
  111. /// Free the previous handle to give us
  112. /// back any texture memory when it can.
  113. free();
  114. // Let the texture manager find this for us.
  115. StrongRefPtr<GFXCubemap>::set( TEXMGR->createCubemap( cubemapDDS ) );
  116. return isValid();
  117. }
  118. const String GFXCubemapArray::describeSelf() const
  119. {
  120. // We've got nothing
  121. return String();
  122. }
  123. void GFXCubemapArray::setCubeTexSize(GFXCubemapHandle* cubemaps)
  124. {
  125. U32 downscalePower = 0;// GFXTextureManager::smTextureReductionLevel;
  126. U32 scaledSize = cubemaps[0]->getSize();
  127. if (downscalePower != 0)
  128. {
  129. // Otherwise apply the appropriate scale...
  130. scaledSize >>= downscalePower;
  131. }
  132. //all cubemaps must be the same size,format and number of mipmaps. Grab the details from the first cubemap
  133. mSize = scaledSize;
  134. mMipMapLevels = cubemaps[0]->getMipMapLevels() - downscalePower;
  135. }
  136. void GFXCubemapArray::setCubeTexSize(U32 cubemapFaceSize)
  137. {
  138. U32 downscalePower = 0;// GFXTextureManager::smTextureReductionLevel;
  139. U32 scaledSize = cubemapFaceSize;
  140. if (downscalePower != 0)
  141. {
  142. scaledSize >>= downscalePower;
  143. }
  144. mSize = scaledSize;
  145. mMipMapLevels = ImageUtil::getMaxMipCount(cubemapFaceSize, cubemapFaceSize) - downscalePower;
  146. }