I'm probably missing something obvious, but I can't find anything wrong with the code. It's for testing purposes, and all it's supposed to do is set the color of all pixels to white and read the pixel information. The result is an array full of 0s.
unsigned int frameBuffer;
glGenFramebuffers(1,&frameBuffer);
unsigned int texture;
unsigned int depthTexture;
glGenTextures(1,&texture);
glGenTextures(1,&depthTexture);
glBindFramebuffer(GL_FRAMEBUFFER,frameBuffer);
glBindTexture(GL_TEXTURE_2D,texture);
int w = 256;
int h = 256;
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGB,
w,h,
0,GL_RGB,
GL_UNSIGNED_BYTE,
0
);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,texture,0);
glBindTexture(GL_TEXTURE_2D,depthTexture);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_DEPTH_COMPONENT16,
w,h,
0,GL_DEPTH_COMPONENT,
GL_FLOAT,
0
);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D,depthTexture,0);
int status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status == GL_FRAMEBUFFER_COMPLETE)
{
unsigned char *pixels = new unsigned char[w *h *3];
for(unsigned int i=0;i<(w *h *3);i++)
pixels[i] = 255;
glDrawPixels(w,h,GL_RGB,GL_UNSIGNED_BYTE,&pixels[0]);
glReadPixels(0,0,w,h,GL_RGB,GL_UNSIGNED_BYTE,&pixels[0]);
for(unsigned int i=0; i<(w *h *3); i+=3)
{
unsigned int r = pixels[i];
unsigned int g = pixels[i + 1];
unsigned int b = pixels[i + 2];
std::cout<<i<<": "<<r<<","<<g<<","<<b<<std::endl;
}
delete[] pixels;
int err = glGetError(); // No error reported
}
glBindTexture(0,GL_TEXTURE_2D);
glBindFramebuffer(0,GL_FRAMEBUFFER);
No errors reported and the frame buffer is fine. What's going on here?
Someone found out the cause on a different forum.
The solution is to call
glReadBuffer(GL_COLOR_ATTACHMENT0);
before calling glReadPixels, and
glReadBuffer(GL_BACK);
afterwards, to reset it.