PageRenderTime 65ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/Code/Libs/Amaterasu3D/Graphics/Shaders/Tools/ManualMipmapping.cpp

https://github.com/beltegeuse/Amaterasu3D
C++ | 163 lines | 115 code | 20 blank | 28 comment | 8 complexity | c682361c0819e76ab7290518b5a8d827 MD5 | raw file
  1. //==========================================================
  2. // Amaterasu3D - perceptual 3D engine
  3. //
  4. // Copyright (C) 2010-2011 Adrien Gruson
  5. //
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc.,
  19. // 59 Temple Place - Suite 330,
  20. // Boston, MA 02111-1307, USA.
  21. //
  22. // E-mail : adrien.gruson@gmail.com
  23. //==========================================================
  24. #include "ManualMipmapping.h"
  25. #include <Logger/Logger.h>
  26. #include <Utilities/Util.h>
  27. namespace ama3D
  28. {
  29. ManualMipmapping::ManualMipmapping(int initialeSize) :
  30. m_InitialSize(initialeSize)
  31. {
  32. // Init object parameters
  33. m_InitialSize = NearestPowerOfTwo(m_InitialSize);
  34. Logger::Log() << "[INFO] Creation of Manual Mip-mapping : " << m_InitialSize
  35. << "\n";
  36. m_NbLevels = log((float)m_InitialSize) / log(2.f);
  37. Logger::Log() << "[INFO] Number levels : " << m_NbLevels << "\n";
  38. }
  39. ManualMipmapping::~ManualMipmapping()
  40. {
  41. }
  42. void ManualMipmapping::Initialize()
  43. {
  44. Logger::Log() << "[INFO] Manual mipmapping : Initialize ... \n";
  45. // Load Shaders
  46. m_ManualMipmappingShader = CShaderManager::Instance().LoadShader(
  47. "ManualMipmappingPass.shader");
  48. m_DownSamplerShader = CShaderManager::Instance().LoadShader(
  49. "ManualDownsamplingPass.shader");
  50. // Configure FBO
  51. FBOTextureBufferParam param;
  52. param.GenerateMipMapping = false;
  53. param.MinFiltering = GL_NEAREST;
  54. param.MaxFiltering = GL_NEAREST;
  55. param.InternalFormat = GL_RGBA32F;
  56. param.ExternalFormat = GL_RGBA;
  57. m_DownSamplerShader->Begin();
  58. param.Attachment = glGetFragDataLocation(
  59. m_DownSamplerShader->GetProgramObject(), "ColorBuffer");
  60. Logger::Log() << " * Attachment : " << param.Attachment << "\n";
  61. m_DownSamplerShader->End();
  62. m_DownSamplerShader->SetFBO(CreateFBO(param, "ColorBuffer", m_InitialSize),
  63. true);
  64. m_ManualMipmappingShader->Begin();
  65. param.Attachment = glGetFragDataLocation(
  66. m_ManualMipmappingShader->GetProgramObject(), "ColorBuffer");
  67. Logger::Log() << " * Attachment : " << param.Attachment << "\n";
  68. m_ManualMipmappingShader->End();
  69. // Create FBOs
  70. Logger::Log() << "[INFO] Create FBO \n";
  71. m_Mipmaps = new FBO*[m_NbLevels];
  72. for (int i = 0; i < m_NbLevels; i++)
  73. {
  74. Logger::Log() << " * Size of FBO : " << m_InitialSize / pow(2.f, i + 1)
  75. << "\n";
  76. m_Mipmaps[i] = CreateFBO(param, "ColorBuffer",
  77. m_InitialSize / pow(2.f, i + 1));
  78. }
  79. }
  80. FBO* ManualMipmapping::CreateFBO(FBOTextureBufferParam p,
  81. const std::string& buffername, int size)
  82. {
  83. std::map<std::string, FBOTextureBufferParam> buffers;
  84. buffers[buffername] = p;
  85. FBODepthBufferParam bufferDepth;
  86. return new FBO(Math::TVector2I(size, size), buffers, FBODEPTH_NONE,
  87. bufferDepth);
  88. }
  89. int ManualMipmapping::NumberLevels()
  90. {
  91. return m_NbLevels + 1;
  92. }
  93. Texture* ManualMipmapping::GetLevel(int l)
  94. {
  95. if (l == 0)
  96. return m_DownSamplerShader->GetFBO()->GetTexture("ColorBuffer");
  97. return m_Mipmaps[l - 1]->GetTexture("ColorBuffer");
  98. }
  99. void ManualMipmapping::Compute(Texture* texture)
  100. {
  101. m_DownSamplerShader->Begin();
  102. texture->activateMultiTex(CUSTOM_TEXTURE + 0);
  103. DrawQuad();
  104. texture->desactivateMultiTex(CUSTOM_TEXTURE + 0);
  105. m_DownSamplerShader->End();
  106. for (int i = 0; i < m_NbLevels; i++)
  107. {
  108. m_ManualMipmappingShader->SetFBO(m_Mipmaps[i], false);
  109. m_ManualMipmappingShader->Begin();
  110. if (i == 0)
  111. m_DownSamplerShader->GetFBO()->GetTexture("ColorBuffer")->activateMultiTex(
  112. CUSTOM_TEXTURE + 0);
  113. else
  114. m_Mipmaps[i - 1]->GetTexture("ColorBuffer")->activateMultiTex(
  115. CUSTOM_TEXTURE + 0);
  116. int size = m_InitialSize / pow(2.f, i);
  117. m_ManualMipmappingShader->SetUniformVector("TexSize",
  118. Math::TVector2F(size, size));
  119. DrawQuad();
  120. if (i == 0)
  121. m_DownSamplerShader->GetFBO()->GetTexture("ColorBuffer")->desactivateMultiTex(
  122. CUSTOM_TEXTURE + 0);
  123. else
  124. m_Mipmaps[i - 1]->GetTexture("ColorBuffer")->desactivateMultiTex(
  125. CUSTOM_TEXTURE + 0);
  126. m_ManualMipmappingShader->End();
  127. }
  128. }
  129. void ManualMipmapping::DrawQuad()
  130. {
  131. // Draw ...
  132. glBegin(GL_QUADS);
  133. glTexCoord2f(0.0, 0.0);
  134. glVertex2f(-1.0, -1.0);
  135. glTexCoord2f(0.0, 1.0);
  136. glVertex2f(-1.0, 1.0);
  137. glTexCoord2f(1.0, 1.0);
  138. glVertex2f(1.0, 1.0);
  139. glTexCoord2f(1.0, 0.0);
  140. glVertex2f(1.0, -1.0);
  141. glEnd();
  142. }
  143. }