gfxCubemap.cpp 4.1 KB

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