here is what my following code does:
- create an FBO
- create a 2x2 RGBA texture and attach it as the color buffer of the FBO
- set clear color to 0.0, 1.0, 0.0, 1.0 and clear the color buffer(the texture in this case)
- read the color buffer of the FBO using glReadPixels
I expect the content of each pixel would be: 0, 255, 0,255,
but it’s not, instead, they look like some random numbers,
what am I doing wrong?
P.S. I use opengl es 2.0
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
GLuint fboID;
glGenFramebuffers(1, &fboID);
glBindFramebuffer(GL_FRAMEBUFFER, fboID);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
glClearColor(0.0, 1.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
GLubyte* pixels = (GLubyte*) malloc(2 * 2 * 4 * sizeof(GLubyte));
glReadPixels(0, 0, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, pixels);