gfxCubemap.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. GFXCubemap::~GFXCubemap()
  27. {
  28. // If we're not dynamic and we were loaded from a
  29. // file then give the texture manager a chance to
  30. // remove us from the cache.
  31. if ( mPath.isNotEmpty() )
  32. TEXMGR->releaseCubemap( this );
  33. }
  34. U32 GFXCubemap::_zUpFaceIndex(const U32 index)
  35. {
  36. switch (index)
  37. {
  38. case 2:
  39. return 4;
  40. case 3:
  41. return 5;
  42. case 4:
  43. return 2;
  44. case 5:
  45. return 3;
  46. default:
  47. return index;
  48. };
  49. }
  50. void GFXCubemap::initNormalize( U32 size )
  51. {
  52. Point3F axis[6] =
  53. {Point3F(1.0, 0.0, 0.0), Point3F(-1.0, 0.0, 0.0),
  54. Point3F(0.0, 1.0, 0.0), Point3F( 0.0, -1.0, 0.0),
  55. Point3F(0.0, 0.0, 1.0), Point3F( 0.0, 0.0, -1.0),};
  56. Point3F s[6] =
  57. {Point3F(0.0, 0.0, -1.0), Point3F( 0.0, 0.0, 1.0),
  58. Point3F(1.0, 0.0, 0.0), Point3F( 1.0, 0.0, 0.0),
  59. Point3F(1.0, 0.0, 0.0), Point3F(-1.0, 0.0, 0.0),};
  60. Point3F t[6] =
  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(0.0, -1.0, 0.0), Point3F(0.0, -1.0, 0.0),};
  64. F32 span = 2.0;
  65. F32 start = -1.0;
  66. F32 stride = span / F32(size - 1);
  67. GFXTexHandle faces[6];
  68. for(U32 i=0; i<6; i++)
  69. {
  70. GFXTexHandle &tex = faces[i];
  71. GBitmap *bitmap = new GBitmap(size, size);
  72. // fill in...
  73. for(U32 v=0; v<size; v++)
  74. {
  75. for(U32 u=0; u<size; u++)
  76. {
  77. Point3F vector;
  78. vector = axis[i] +
  79. ((F32(u) * stride) + start) * s[i] +
  80. ((F32(v) * stride) + start) * t[i];
  81. vector.normalizeSafe();
  82. vector = ((vector * 0.5) + Point3F(0.5, 0.5, 0.5)) * 255.0;
  83. vector.x = mClampF(vector.x, 0.0f, 255.0f);
  84. vector.y = mClampF(vector.y, 0.0f, 255.0f);
  85. vector.z = mClampF(vector.z, 0.0f, 255.0f);
  86. // easy way to avoid knowledge of the format (RGB, RGBA, RGBX, ...)...
  87. U8 *bits = bitmap->getAddress(u, v);
  88. bits[0] = U8(vector.x);
  89. bits[1] = U8(vector.y);
  90. bits[2] = U8(vector.z);
  91. }
  92. }
  93. tex.set(bitmap, &GFXStaticTextureSRGBProfile, true, "Cubemap");
  94. }
  95. initStatic(faces);
  96. }
  97. const String GFXCubemap::describeSelf() const
  98. {
  99. // We've got nothing
  100. return String();
  101. }
  102. bool GFXCubemapHandle::set( const String &cubemapDDS )
  103. {
  104. /// Free the previous handle to give us
  105. /// back any texture memory when it can.
  106. free();
  107. // Let the texture manager find this for us.
  108. StrongRefPtr<GFXCubemap>::set( TEXMGR->createCubemap( cubemapDDS ) );
  109. return isValid();
  110. }
  111. const String GFXCubemapArray::describeSelf() const
  112. {
  113. // We've got nothing
  114. return String();
  115. }