Predrag Manojlovic Predrag Manojlovic - 4 months ago 20
C++ Question

OpenGL Framebuffer - rendering to texture creates texture with main framebufer content

This is the part of bigger project basically, I'm Creating Framebuffer with color, depth and stencil buffer in a following way:

// Create texture
glGenTextures(1, &m_textureInput);
glBindTexture(GL_TEXTURE_2D, m_textureInput);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, getWidth(), getHeight(), 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
// Create FBOs
glGenFramebuffers(1, &m_fboInput);

glBindFramebuffer(GL_FRAMEBUFFER, m_fboInput);

// Attach texture to it
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_textureInput, 0);

// Render buffer, depth with stencil
glGenRenderbuffers(1, &m_rbDepthStencilInput);
glBindRenderbuffer(GL_RENDERBUFFER, m_rbDepthStencilInput);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, getWidth(), getHeight());
//Attach depth buffer to FBO
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_rbDepthStencilInput);
//Also attach as a stencil
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_rbDepthStencilInput);

status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
{
// Restore current framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return false;
}

glBindFramebuffer(GL_FRAMEBUFFER, 0);


I bind it to render to it with:

glBindFramebuffer(GL_FRAMEBUFFER, m_fboInput);
// render
glBindFramebuffer(GL_FRAMEBUFFER, 0);


When i dump attached texture with:

glBindTexture(GL_TEXTURE_2D, m_textureInput);
long imageSize = x * y * 4;
unsigned char *data = new unsigned char[imageSize];
glReadPixels(0,0,x,y, GL_BGRA,GL_UNSIGNED_BYTE,data);


i receive content of main framebuffer (0).

Any idea what I'm dong wrong?
Thanks in advance.

Answer

glReadPixels docs say it reads data from the framebuffer. since you bound zero to it before, my guess would be it reads from the default framebuffer.

you can either bind your framebuffer object, then call glReadBuffer with GL_COLOR_ATTACHMENT0 and then do glReadPixels or, maybe more straight forward, use glGetTexImage to directly read from your texture.

Comments