Impossible to create rendertexture. (failed to link the target texture to the FBO)

Hi, when I try to link my stencil texture to the FBO it fails.
I create the target texture here :


// Initialize the texture
            glCheck(glBindTexture(GL_TEXTURE_2D, m_texture));
            if (!stencilTexture && !depthTexture) {
                glCheck(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_actualSize.x, m_actualSize.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL));
                glCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, m_isRepeated ? GL_REPEAT : GL_CLAMP_TO_EDGE));
                glCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, m_isRepeated ? GL_REPEAT : GL_CLAMP_TO_EDGE));
                glCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, m_isSmooth ? GL_LINEAR : GL_NEAREST));
                glCheck(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, m_isSmooth ? GL_LINEAR : GL_NEAREST));

            } else if (depthTexture) {
                glCheck(glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL));

            } else {
                glCheck(glTexImage2D(GL_TEXTURE_2D, 0, GL_STENCIL_INDEX16, width, height, 0, GL_STENCIL, GL_FLOAT, NULL));
            }

And then I pass it to the FBO.

[code=cpp]
// Create the texture
if(!m_texture.create(width, height))
{
err() << “Impossible to create render texture (failed to create the target texture)” << std::endl;
return false;
}
// We disable smoothing by default for render textures
setSmooth(false);
if (stencilTexture) {
if (!m_stencilTexture.create(width, height, false, true)) {
err() << “Impossible to create render texture (failed to create the target stencil texture)” << std::endl;
return false;
}
}
if (depthTexture) {
if (!m_depthTexture.create(width, height, true, false)) {
err() << “Impossible to create render texture (failed to create the target depth buffer texture)” << std::endl;
return false;
}
}
// Create the implementation
delete m_impl;
if (priv::RenderTextureImplFBO::isAvailable())
{
// Use frame-buffer object (FBO)
m_impl = new priv::RenderTextureImplFBO;
}
else
{
// Use default implementation
m_impl = new priv::RenderTextureImplDefault;
}

        // Initialize the render texture
        if (!m_impl->create(width, height, settings, m_texture.m_texture, m_depthTexture.m_texture, m_stencilTexture.m_texture))
            return false;



// Create the context
m_context = new Context(settings, width, height);
// Create the framebuffer object
GLuint frameBuffer = 0;
glCheck(glGenFramebuffersEXT(1, &frameBuffer));
m_frameBuffer = static_cast(frameBuffer);
if (!m_frameBuffer)
{
err() << “Impossible to create render texture (failed to create the frame buffer object)” << std::endl;
return false;
}
glCheck(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_frameBuffer));
// Create the depth buffer if requested
if (settings.depthBits > 0)
{
GLuint depth = 0;
glCheck(glGenRenderbuffersEXT(1, &depth));
m_depthBuffer = static_cast(depth);
if (!m_depthBuffer)
{
err() << “Impossible to create render texture (failed to create the attached depth buffer)” << std::endl;
return false;
}
glCheck(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, m_depthBuffer));
glCheck(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height));
glCheck(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_depthBuffer));
}
if (settings.stencilBits > 0) {
GLuint stencil = 0;
glCheck(glGenRenderbuffersEXT(1, &stencil));
m_stencilBuffer = static_cast(stencil);
if (!m_stencilBuffer) {
err() << “Impossible to create render texture (failed to create the attached stencil buffer)” << std::endl;
return false;
}
glCheck(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,m_stencilBuffer));
glCheck(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX, width, height));
glCheck(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_stencilBuffer));
}
if (stencilTextureId != 0) {
glCheck(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depthTextureId, 0));
glCheck(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, stencilTextureId, 0));
}
if (depthTextureId != 0 && stencilTextureId == 0) {
glCheck(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depthTextureId, 0));
}
// Link the texture to the frame buffer
glCheck(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, textureId, 0));
// A final check, just to be sure…
if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT)
{
glCheck(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
err() << “Impossible to create render texture (failed to link the target texture to the frame buffer)” << std::endl;
return false;
}
return true;



What's wrong with this ?

What exactly fails? And why are you using an extension from a decade-and-a-half ago instead of the core functionality that’s been around since GL 3.0?

Also, it should be noted that pretty much no hardware supports using separate textures for depth and stencil. So if you use just depth, you’ll probably be fine. If you use just stencil, you’ll probably be fine. But you shouldn’t try to use both at once as separate textures. This is why depth/stencil formats exist.