gfxCubemap.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. return index;
  44. }
  45. void GFXCubemap::initNormalize( U32 size )
  46. {
  47. Point3F axis[6] =
  48. {Point3F(1.0, 0.0, 0.0), Point3F(-1.0, 0.0, 0.0),
  49. Point3F(0.0, 1.0, 0.0), Point3F( 0.0, -1.0, 0.0),
  50. Point3F(0.0, 0.0, 1.0), Point3F( 0.0, 0.0, -1.0),};
  51. Point3F s[6] =
  52. {Point3F(0.0, 0.0, -1.0), Point3F( 0.0, 0.0, 1.0),
  53. Point3F(1.0, 0.0, 0.0), Point3F( 1.0, 0.0, 0.0),
  54. Point3F(1.0, 0.0, 0.0), Point3F(-1.0, 0.0, 0.0),};
  55. Point3F t[6] =
  56. {Point3F(0.0, -1.0, 0.0), Point3F(0.0, -1.0, 0.0),
  57. Point3F(0.0, 0.0, 1.0), Point3F(0.0, 0.0, -1.0),
  58. Point3F(0.0, -1.0, 0.0), Point3F(0.0, -1.0, 0.0),};
  59. F32 span = 2.0;
  60. F32 start = -1.0;
  61. F32 stride = span / F32(size - 1);
  62. GFXTexHandle faces[6];
  63. for(U32 i=0; i<6; i++)
  64. {
  65. GFXTexHandle &tex = faces[i];
  66. GBitmap *bitmap = new GBitmap(size, size);
  67. // fill in...
  68. for(U32 v=0; v<size; v++)
  69. {
  70. for(U32 u=0; u<size; u++)
  71. {
  72. Point3F vector;
  73. vector = axis[i] +
  74. ((F32(u) * stride) + start) * s[i] +
  75. ((F32(v) * stride) + start) * t[i];
  76. vector.normalizeSafe();
  77. vector = ((vector * 0.5) + Point3F(0.5, 0.5, 0.5)) * 255.0;
  78. vector.x = mClampF(vector.x, 0.0f, 255.0f);
  79. vector.y = mClampF(vector.y, 0.0f, 255.0f);
  80. vector.z = mClampF(vector.z, 0.0f, 255.0f);
  81. // easy way to avoid knowledge of the format (RGB, RGBA, RGBX, ...)...
  82. U8 *bits = bitmap->getAddress(u, v);
  83. bits[0] = U8(vector.x);
  84. bits[1] = U8(vector.y);
  85. bits[2] = U8(vector.z);
  86. }
  87. }
  88. tex.set(bitmap, &GFXStaticTextureSRGBProfile, true, "Cubemap");
  89. }
  90. initStatic(faces);
  91. }
  92. const String GFXCubemap::describeSelf() const
  93. {
  94. // We've got nothing
  95. return String();
  96. }
  97. bool GFXCubemapHandle::set( const String &cubemapDDS )
  98. {
  99. /// Free the previous handle to give us
  100. /// back any texture memory when it can.
  101. free();
  102. // Let the texture manager find this for us.
  103. StrongRefPtr<GFXCubemap>::set( TEXMGR->createCubemap( cubemapDDS ) );
  104. return isValid();
  105. }
  106. const String GFXCubemapArray::describeSelf() const
  107. {
  108. // We've got nothing
  109. return String();
  110. }
  111. void GFXCubemapArray::setCubeTexSize(GFXCubemapHandle* cubemaps)
  112. {
  113. U32 downscalePower = 0;// GFXTextureManager::smTextureReductionLevel;
  114. U32 scaledSize = cubemaps[0]->getSize();
  115. if (downscalePower != 0)
  116. {
  117. // Otherwise apply the appropriate scale...
  118. scaledSize >>= downscalePower;
  119. }
  120. //all cubemaps must be the same size,format and number of mipmaps. Grab the details from the first cubemap
  121. mSize = scaledSize;
  122. mMipMapLevels = cubemaps[0]->getMipMapLevels() - downscalePower;
  123. }
  124. void GFXCubemapArray::setCubeTexSize(U32 cubemapFaceSize)
  125. {
  126. U32 downscalePower = 0;// GFXTextureManager::smTextureReductionLevel;
  127. U32 scaledSize = cubemapFaceSize;
  128. if (downscalePower != 0)
  129. {
  130. scaledSize >>= downscalePower;
  131. }
  132. mSize = scaledSize;
  133. mMipMapLevels = ImageUtil::getMaxMipCount(cubemapFaceSize, cubemapFaceSize) - downscalePower;
  134. }