Hello everyone, I’m beginner in OpenGL and I have a little problem that I can’t solve :(! (I’m sorry for my english i’m french x) )
I can’t make a layered rendering work. I would like to capture different layers from the scene compared to the depth of the scene. I would like tohave different textures from the scene at specific depths in order to apply different Blur effect at each layers.
I use a geometry shader with gl_Layer.
This is my function who create a Texture 2D Array:
void Texture::initGLTexture2DArray(int bytesperpixel, int width, int height, int layerCount) {
glAssert(glGenTextures(1, &mTexId));
glAssert(glActiveTexture(GL_TEXTURE0));
glAssert(glBindTexture(GL_TEXTURE_2D_ARRAY, mTexId));
glAssert(glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, bytesperpixel, width, height, layerCount, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL));
glAssert(glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MIN_FILTER,GL_LINEAR));
glAssert(glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MAG_FILTER,GL_LINEAR));
glAssert(glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE));
glAssert(glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE));
}
And I tried to this two functions to add the array at my framebuffer:
void FBO::attachTextureArray(GLenum attachment, Texture *texture) {
glAssert(glFramebufferTexture(GL_FRAMEBUFFER, attachment, texture->getId(), 0));
}
// Using it like this
fboLayered_->attachTextureArray(GL_COLOR_ATTACHMENT0, textureLayered);
void FBO::attachTextureArray2(GLenum attachment, Texture *texture, GLuint layer) {
glAssert(glFramebufferTextureLayer(GL_FRAMEBUFFER, attachment, texture->getId(), 0, layer));
// Using it like this
fboLayered_->attachTextureArray2(GL_COLOR_ATTACHMENT0, textureLayered, 0);
fboLayered_->attachTextureArray2(GL_COLOR_ATTACHMENT1, textureLayered, 1);
}
And this is my render loop:
glm::mat4 modelViewMatrix = camera_->getModelViewMatrix();
glm::mat4 projectionMatrix = camera_->getProjectionMatrix();
//
// Important note before modifying this method :
// see MyRenderer::setViewport for FBO configurations
//
glm::mat4x4 viewToWorldMatrix = glm::inverse(modelViewMatrix);
// draw the scene
fboLayered_->useAsTarget(width_, height_);
glAssert(glDrawBuffers(1, bufs));
glAssert(glClearColor(0.1, 0.1, 0.1, 1.));
glAssert(glClearDepth(1.0));
glAssert(glDepthFunc(GL_LESS));
glAssert(glDisable(GL_BLEND));
fboLayered_->clear(FBO::ALL);// to clear all attached texture
// render ambient and normal
ambientPass(ambientAndNormalLoop_, modelViewMatrix, projectionMatrix, viewToWorldMatrix);
// setup per light rendering : blend each pass onto the previous one
glAssert(glDrawBuffers(1, bufs));
glAssert(glDepthFunc(GL_LEQUAL));
glAssert(glEnable(GL_BLEND));
glAssert(glBlendFunc(GL_ONE, GL_ONE));
glAssert(glDepthMask(GL_FALSE));
// render for each light
lightsPass(mainDrawLoop_, modelViewMatrix, projectionMatrix, viewToWorldMatrix);
// restore parameter
glAssert( glDisable(GL_BLEND) );
glAssert( glDepthFunc(GL_LESS) );
glAssert( glDepthMask(GL_TRUE) );
std::cout << gpuTimers_ << std::endl;
}
Just a last thing the FBOs configuration. I’ve 2 FBO one to render a “normal” mode and the other to obtain what I would like to.
void MyRenderer::setViewport(int width, int height)
{
width_ = width;
height_ = height;
camera_->setScreenWidthAndHeight(width_, height_);
//Renderer::setViewport(width, height);
fbo_->setSize(width_, height_);
if (textures_[DEPTH_TEXTURE]->getId() != 0)
textures_[DEPTH_TEXTURE]->deleteGL();
textures_[DEPTH_TEXTURE]->initGL(GL_DEPTH_COMPONENT24, width_, height_, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
if (textures_[NORMAL_TEXTURE]->getId() != 0)
textures_[NORMAL_TEXTURE]->deleteGL();
if (textures_[COLOR_TEXTURE]->getId() != 0)
textures_[COLOR_TEXTURE]->deleteGL();
if (textureLayered->getId() != 0)
textureLayered->deleteGL();
textures_[NORMAL_TEXTURE]->initGL(GL_RGBA32F, width_, height_, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
textures_[COLOR_TEXTURE]->initGL(GL_RGBA32F, width_, height_, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// Init Texture 2D Array
textureLayered->initGLTexture2DArray(GL_RGB32F, width, height, 2);
fbo_->bind();
fbo_->attachTexture(GL_COLOR_ATTACHMENT0, textures_[COLOR_TEXTURE]);
fbo_->attachTexture(GL_COLOR_ATTACHMENT1, textures_[NORMAL_TEXTURE]);
fbo_->attachTexture(GL_DEPTH_ATTACHMENT, textures_[DEPTH_TEXTURE]);
fbo_->check();
fbo_->unbind();
fboLayered_->setSize(width, height);
fboLayered_->bind();
fboLayered_->attachTextureArray(GL_COLOR_ATTACHMENT0, textureLayered);
fboLayered_->check();
fboLayered_->unbind();
FBO::bindDefault();
glAssert (glDrawBuffer(GL_BACK); );
glAssert (glReadBuffer(GL_BACK); );
}
I am using a 3D engine given by my professor so I don’t really know if you have enought information to see where my problem come from. I’m blocked for severals day I tried everything I found on different forum… :/.
And this is my shaders: [ATTACH]1084[/ATTACH]
[ATTACH]1085[/ATTACH]
[ATTACH]1086[/ATTACH]
[ATTACH]1087[/ATTACH]
[ATTACH]1088[/ATTACH]
Thank you for your help!
Brioche