gfxCubemap.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. void GFXCubemap::initNormalize( U32 size )
  35. {
  36. Point3F axis[6] =
  37. {Point3F(1.0, 0.0, 0.0), Point3F(-1.0, 0.0, 0.0),
  38. Point3F(0.0, 1.0, 0.0), Point3F( 0.0, -1.0, 0.0),
  39. Point3F(0.0, 0.0, 1.0), Point3F( 0.0, 0.0, -1.0),};
  40. Point3F s[6] =
  41. {Point3F(0.0, 0.0, -1.0), Point3F( 0.0, 0.0, 1.0),
  42. Point3F(1.0, 0.0, 0.0), Point3F( 1.0, 0.0, 0.0),
  43. Point3F(1.0, 0.0, 0.0), Point3F(-1.0, 0.0, 0.0),};
  44. Point3F t[6] =
  45. {Point3F(0.0, -1.0, 0.0), Point3F(0.0, -1.0, 0.0),
  46. Point3F(0.0, 0.0, 1.0), Point3F(0.0, 0.0, -1.0),
  47. Point3F(0.0, -1.0, 0.0), Point3F(0.0, -1.0, 0.0),};
  48. F32 span = 2.0;
  49. F32 start = -1.0;
  50. F32 stride = span / F32(size - 1);
  51. GFXTexHandle faces[6];
  52. for(U32 i=0; i<6; i++)
  53. {
  54. GFXTexHandle &tex = faces[i];
  55. GBitmap *bitmap = new GBitmap(size, size);
  56. // fill in...
  57. for(U32 v=0; v<size; v++)
  58. {
  59. for(U32 u=0; u<size; u++)
  60. {
  61. Point3F vector;
  62. vector = axis[i] +
  63. ((F32(u) * stride) + start) * s[i] +
  64. ((F32(v) * stride) + start) * t[i];
  65. vector.normalizeSafe();
  66. vector = ((vector * 0.5) + Point3F(0.5, 0.5, 0.5)) * 255.0;
  67. vector.x = mClampF(vector.x, 0.0f, 255.0f);
  68. vector.y = mClampF(vector.y, 0.0f, 255.0f);
  69. vector.z = mClampF(vector.z, 0.0f, 255.0f);
  70. // easy way to avoid knowledge of the format (RGB, RGBA, RGBX, ...)...
  71. U8 *bits = bitmap->getAddress(u, v);
  72. bits[0] = U8(vector.x);
  73. bits[1] = U8(vector.y);
  74. bits[2] = U8(vector.z);
  75. }
  76. }
  77. tex.set(bitmap, &GFXDefaultStaticDiffuseProfile, true, "Cubemap");
  78. }
  79. initStatic(faces);
  80. }
  81. const String GFXCubemap::describeSelf() const
  82. {
  83. // We've got nothing
  84. return String();
  85. }
  86. bool GFXCubemapHandle::set( const String &cubemapDDS )
  87. {
  88. /// Free the previous handle to give us
  89. /// back any texture memory when it can.
  90. free();
  91. // Let the texture manager find this for us.
  92. StrongRefPtr<GFXCubemap>::set( TEXMGR->createCubemap( cubemapDDS ) );
  93. return isValid();
  94. }