I'm using MRT to resolve the 3D picking problem in OpenGL 4.5. (Reference link: http://ogldev.atspace.co.uk/www/tutorial29/tutorial29.html)
According to the information I found online, we generally apply a new FBO when using the MRT. If so, I should daw the scene twice during 3D picking (the new FBO for 3D picking, and the default FBO for the screen), which would be a waste of time.
So I was wondering if the MRT could be used in the default FBO, but I run into some problems.
Here's my code.
// apply the texture buffer attached to GL_COLOR_ATTACHMENT1. (GL_COLOR_ATTACHMENT0 is reserved for the screen ?)
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &m_pickingColorTexture);
glBindTexture(GL_TEXTURE_2D, m_pickingColorTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, w, h, 0, GL_RGB, GL_FLOAT, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_pickingColorTexture, 0);
glDisable(GL_TEXTURE_2D);
// draw code.
GLenum buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
glDrawBuffers(2, buffers);
glDrawElements(........);
// fragment shader, output two kinds of things: frag_color0 for screen display, frag_color1 for 3D picking
.......
layout (location = 0) out vec4 frag_color0;
layout (location = 1) out vec4 frag_color1;
void main() {
frag_color0 = vec4(...);
frag_color1 = vec4(...);
}
// query the result
glReadBuffer(GL_COLOR_ATTACHMENT1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, &pixelInfo);
When I query the GL_COLOR_ATTACHMENT1 at last, the result seems to be the frag_color0 instead of frag_color1.
I would be very appreciated if somebody could help me to resolve the problem, thanks!