/opengles/src/Texture.cpp

http://ftk.googlecode.com/ · C++ · 176 lines · 89 code · 38 blank · 49 comment · 20 complexity · 17ee9af9430b3a9286656fa112b37b6c MD5 · raw file

  1. // ==========================================================================
  2. //
  3. // Texture.cpp Texture Class for 3D Rendering Library
  4. //
  5. // --------------------------------------------------------------------------
  6. //
  7. // 10-15-2003 Hans-Martin Will initial version
  8. //
  9. // --------------------------------------------------------------------------
  10. //
  11. // Copyright (c) 2004, Hans-Martin Will. All rights reserved.
  12. //
  13. // Redistribution and use in source and binary forms, with or without
  14. // modification, are permitted provided that the following conditions are
  15. // met:
  16. //
  17. // * Redistributions of source code must retain the above copyright
  18. // notice, this list of conditions and the following disclaimer.
  19. // * Redistributions in binary form must reproduce the above copyright
  20. // notice, this list of conditions and the following disclaimer in the
  21. // documentation and/or other materials provided with the distribution.
  22. //
  23. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  28. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. // THE POSSIBILITY OF SUCH DAMAGE.
  34. //
  35. // ==========================================================================
  36. #include "stdafx.h"
  37. #include "Texture.h"
  38. #include <stdlib.h>
  39. using namespace EGL;
  40. // --------------------------------------------------------------------------
  41. // Class Texture
  42. // --------------------------------------------------------------------------
  43. const U8 Texture :: s_BytesPerPixel[] = {
  44. 1, // ALPHA
  45. 1, // LUMINANCE
  46. 2, // LUMINANCE_ALPHA
  47. 3, // RGB8
  48. 4, // RGBA8
  49. 2, // RGB565
  50. 2, // RGBA4444
  51. 2 // RGBA5551
  52. };
  53. void Texture :: Init()
  54. {
  55. m_Data = 0;
  56. m_InternalFormat = RasterizerState::TextureFormatInvalid;
  57. }
  58. void Texture :: Dispose() {
  59. if (m_Data != 0) {
  60. free(m_Data);
  61. m_Data = 0;
  62. }
  63. }
  64. namespace {
  65. U32 Log(U32 value) {
  66. U32 result = 0;
  67. U32 mask = 1;
  68. while ((value & mask) != value) {
  69. ++result;
  70. mask = (mask << 1) | 1;
  71. }
  72. return result;
  73. }
  74. }
  75. bool Texture :: Initialize(U32 width, U32 height, RasterizerState::TextureFormat format) {
  76. if (m_Data != 0) {
  77. free(m_Data);
  78. }
  79. m_LogWidth = Log(width);
  80. m_LogHeight = Log(height);
  81. U32 pixels = width * height;
  82. m_InternalFormat = format;
  83. size_t bytes = pixels * GetBytesPerPixel();
  84. m_Data = malloc(bytes);
  85. return m_Data != 0;
  86. }
  87. U32 Texture :: GetLogBytesPerPixel() const {
  88. return Log(GetBytesPerPixel());
  89. }
  90. // --------------------------------------------------------------------------
  91. // Class MultiTexture
  92. // --------------------------------------------------------------------------
  93. MultiTexture :: MultiTexture():
  94. m_MinFilterMode(RasterizerState::FilterModeNearest),
  95. m_MagFilterMode(RasterizerState::FilterModeNearest),
  96. m_MipmapFilterMode(RasterizerState::FilterModeNone),
  97. m_WrappingModeS(RasterizerState::WrappingModeRepeat),
  98. m_WrappingModeT(RasterizerState::WrappingModeRepeat)
  99. {
  100. for (size_t index = 0; index <= MAX_LEVELS; ++index) {
  101. m_TextureLevels[index].Init();
  102. }
  103. }
  104. MultiTexture :: ~MultiTexture() {
  105. for (size_t index = 0; index <= MAX_LEVELS; ++index) {
  106. m_TextureLevels[index].Dispose();
  107. }
  108. }
  109. // --------------------------------------------------------------------------
  110. // Verify that this texture is consistently defined across all mipmapping
  111. // levels (as specified in section 3.8.9)
  112. //
  113. // Returns:
  114. // true - if this texture is defined consistently across all
  115. // levels
  116. // --------------------------------------------------------------------------
  117. bool MultiTexture :: IsComplete() const {
  118. U32 width = m_TextureLevels[0].GetWidth();
  119. U32 height = m_TextureLevels[0].GetHeight();
  120. RasterizerState::TextureFormat format = m_TextureLevels[0].GetInternalFormat();
  121. for (int index = 1; index < MAX_LEVELS; ++index) {
  122. if (width == 1 && height == 1) {
  123. return true;
  124. }
  125. if (width > 1) {
  126. width = width / 2;
  127. }
  128. if (height > 1) {
  129. height = height / 2;
  130. }
  131. if (m_TextureLevels[index].GetWidth() != width ||
  132. m_TextureLevels[index].GetHeight() != height ||
  133. m_TextureLevels[index].GetInternalFormat() != format) {
  134. return false;
  135. }
  136. }
  137. return true;
  138. }