BambooIsland: C:/Users/easterbunny/Desktop/Echtzeitgraphik-LU/trunk/src/SceneObject_FBO.cpp Source File

BambooIsland Abgabe 2

- Main Page
- Related Pages
- Classes
- Files

- File List

# C:/Users/easterbunny/Desktop/Echtzeitgraphik-LU/trunk/src/SceneObject_FBO.cpp

    00001 #include <stack>
    00002 #include <iostream>
    00003
    00004 #include "SceneObject_FBO.h"
    00005 #include "TextureManager.h"
    00006 #include "Logger.h"
    00007
    00008 std::stack<GLuint> bound_fbos;
    00009
    00021 SceneObject_FBO::SceneObject_FBO(int iWidth, int iHeight, const char *szColorTextureName, bool bFloating16, bool bMipMapped) : m_iWidth(iWidth), m_iHeight(iHeight), m_szColorTextureName(szColorTextureName), m_szDepthTextureName(0), m_bMipMapped(bMipMapped)
    00022 {
    00023     //write down which rendertargets we use
    00024     m_bColorTexture = true;         //we use a color texture
    00025     m_bDepthTexture = false;        //we use no depth texture
    00026     m_bDepthRenderbuffer = true;    //but we use a renderbuffer for the depth
    00027
    00028     //get the id of a free texture unit from the texture manager
    00029     GLuint textureUnit = TextureManager::instance()->getFreeUnit();
    00030
    00031     //activate this unit
    00032     glActiveTexture(GL_TEXTURE0 + textureUnit);
    00033
    00034     //generate color texture (=create new opengl id)
    00035     glGenTextures(1, &m_nColorTexture);
    00036
    00037     //bind color texture
    00038     glBindTexture(GL_TEXTURE_2D, m_nColorTexture);
    00039
    00040     //set texture format and data
    00041     if (bFloating16 == false)
    00042         glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, m_iWidth, m_iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    00043     else
    00044         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, m_iWidth, m_iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    00045
    00046     //set texture parameters
    00047     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    00048     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    00049     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    00050
    00051     if (bMipMapped)
    00052     {
    00053         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    00054         glGenerateMipmap(GL_TEXTURE_2D);
    00055     }
    00056     else
    00057         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    00058
    00059     //unbind texture
    00060     glBindTexture(GL_TEXTURE_2D, 0);
    00061
    00062     //release unit
    00063     TextureManager::instance()->releaseUnit(textureUnit);
    00064
    00065     //register color texture in texture manager, for easy use by simple calling TextureManager::useTexture(szColorTextureName);
    00066     TextureManager::instance()->registerManualTexture(szColorTextureName, m_nColorTexture);
    00067
    00068     //generate renderbuffer
    00069     glGenRenderbuffers(1, &m_nDepthRenderbuffer);
    00070
    00071     //bind renderbuffer
    00072     glBindRenderbuffer(GL_RENDERBUFFER, m_nDepthRenderbuffer);
    00073
    00074     //set renderbuffer format and data
    00075     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, m_iWidth, m_iHeight);
    00076
    00077     //unbind renderbuffer
    00078     glBindRenderbuffer(GL_RENDERBUFFER, 0);
    00079
    00080     //generate frame buffer
    00081     glGenFramebuffers(1, &m_nFramebuffer);
    00082
    00083     //bind framebuffer
    00084     glBindFramebuffer(GL_FRAMEBUFFER, m_nFramebuffer);
    00085
    00086     //attach color texture to framebuffer
    00087     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_nColorTexture, 0);
    00088
    00089     //attach depth renderbuffer to framebuffer
    00090     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_nDepthRenderbuffer);
    00091
    00092     //check framebuffer status
    00093     GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    00094
    00095     //check if its complete
    00096     if(status != GL_FRAMEBUFFER_COMPLETE)
    00097         Logger::instance()->log(Logger::FATAL, "Failed to initialize FBO");
    00098     else
    00099         Logger::instance()->log(Logger::DEBUG, "Initialized FBO");
    00100
    00101     //unbind framebuffer
    00102     glBindFramebuffer(GL_FRAMEBUFFER, 0);
    00103 }
    00104
    00117 SceneObject_FBO::SceneObject_FBO(int iWidth, int iHeight, const char *szColorTextureName, const char *szDepthTextureName, bool bMipMapped) : m_iWidth(iWidth), m_iHeight(iHeight), m_szColorTextureName(szColorTextureName), m_szDepthTextureName(szDepthTextureName), m_bMipMapped(bMipMapped)
    00118 {
    00119     //write down which rendertargets we use
    00120     m_bColorTexture = true;         //we use a color texture
    00121     m_bDepthTexture = true;         //we use a depth texture
    00122     m_bDepthRenderbuffer = false;   //so we don't use a depth renderbuffer
    00123
    00124     //get the id of a free texture unit from the texture manager
    00125     GLuint textureUnit = TextureManager::instance()->getFreeUnit(); //ask for a free texture unit
    00126
    00127     //activate unit
    00128     glActiveTexture(GL_TEXTURE0 + textureUnit);
    00129
    00130     //generate color texture (=create new opengl id)
    00131     glGenTextures(1, &m_nColorTexture);
    00132
    00133     //bind color texture
    00134     glBindTexture(GL_TEXTURE_2D, m_nColorTexture);
    00135
    00136     //set texture format and data
    00137     glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, m_iWidth, m_iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    00138     //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, m_iWidth, m_iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    00139
    00140
    00141     //set texture parameters
    00142     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    00143     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    00144     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    00145
    00146     //if mipmapping is activated, set filter to mipmap and generate mipmaps
    00147     if (bMipMapped)
    00148     {
    00149         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    00150         glGenerateMipmap(GL_TEXTURE_2D);
    00151     }
    00152     else
    00153         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    00154
    00155     //generate depth texture
    00156     glGenTextures(1, &m_nDepthTexture);
    00157
    00158     //bind depth texture
    00159     glBindTexture(GL_TEXTURE_2D, m_nDepthTexture);
    00160
    00161     //set texture format and data
    00162     glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, m_iWidth, m_iHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
    00163    // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
    00164     //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
    00165
    00166
    00167     //set texture parameters
    00168     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    00169     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    00170     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    00171     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    00172
    00173     //register color texture in texture manager, for easy use by simple calling TextureManager::useTexture(szColorTextureName);
    00174     TextureManager::instance()->registerManualTexture(szColorTextureName, m_nColorTexture);
    00175
    00176     //register depth texture in texture manager, for easy use by simple calling TextureManager::useTexture(szColorTextureName);
    00177     TextureManager::instance()->registerManualTexture(szDepthTextureName, m_nDepthTexture);
    00178
    00179     glGenFramebuffers(1, &m_nFramebuffer);
    00180     glBindFramebuffer(GL_FRAMEBUFFER, m_nFramebuffer);
    00181
    00182     // attach the texture to FBO color attachment point
    00183     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_nColorTexture, 0);
    00184
    00185     // attach the renderbuffer to depth attachment point
    00186     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_nDepthTexture, 0);
    00187
    00188     //check fbo status
    00189     GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    00190
    00191     if(status != GL_FRAMEBUFFER_COMPLETE)
    00192         Logger::instance()->log(Logger::FATAL, "Failed to initialize FBO");
    00193     else
    00194         Logger::instance()->log(Logger::DEBUG, "Initialized FBO");
    00195
    00196     //release used texture unit
    00197     TextureManager::instance()->releaseUnit(textureUnit);
    00198
    00199     //unbind fbo
    00200     glBindFramebuffer(GL_FRAMEBUFFER, 0);
    00201 }
    00202
    00203 SceneObject_FBO::~SceneObject_FBO()
    00204 {
    00205     //delete color texture, if used
    00206     if (m_bColorTexture)
    00207         glDeleteTextures(1, &m_nColorTexture);
    00208
    00209     //delete depth texture, if used
    00210     if (m_bDepthTexture)
    00211         glDeleteTextures(1, &m_nDepthTexture);
    00212
    00213     //delete depth renderbuffer, if used
    00214     if (m_bDepthRenderbuffer != -1)
    00215         glDeleteRenderbuffers(1, &m_nDepthRenderbuffer);
    00216
    00217     //finally, delete the used fbo
    00218     glDeleteFramebuffers(1, &m_nFramebuffer);
    00219 }
    00220
    00225 void SceneObject_FBO::itlBeforeRender()
    00226 {
    00227     //save current viewport params
    00228     glGetIntegerv(GL_VIEWPORT, m_iGeneralViewportParams);
    00229
    00230     //bind fbo
    00231     glBindFramebuffer(GL_FRAMEBUFFER, m_nFramebuffer);
    00232
    00233     //set viewport size
    00234     glViewport(0,0, m_iWidth, m_iHeight);
    00235
    00236     // clear buffers
    00237     glClearColor(0.0, 0.0, 0.0, 1.0);
    00238     glClearDepth(1.0);
    00239     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    00240
    00242     bound_fbos.push(m_nFramebuffer);
    00243 }
    00244
    00249 void SceneObject_FBO::itlRender()
    00250 {
    00251     //if mipmapping is activated, update mipmaps
    00252     if (m_bMipMapped)
    00253     {
    00254         TextureManager::instance()->useTexture(m_szColorTextureName);
    00255         glGenerateMipmap(GL_TEXTURE_2D);
    00256         TextureManager::instance()->unuseTexture(m_szColorTextureName);
    00257     }
    00258
    00259     //remove the fbo (THIS fbo) from the bound_fbos stack
    00260     bound_fbos.pop();
    00261
    00262     //if there was a SceneObject_FBO bound in the SceneObject_Tree, rebind the previous bound fbo
    00263     if (bound_fbos.size() > 0)
    00264     {
    00265         GLuint previous_bound = bound_fbos.top();
    00266         glBindFramebuffer(GL_FRAMEBUFFER, previous_bound);
    00267     }
    00268     else
    00269         glBindFramebuffer(GL_FRAMEBUFFER, 0);
    00270
    00271     //restore viewport params
    00272     glViewport(m_iGeneralViewportParams[0], m_iGeneralViewportParams[1], m_iGeneralViewportParams[2], m_iGeneralViewportParams[3]);
    00273 }

All Classes Functions Variables Enumerations Enumerator