Gl_FragColor and glReadPixels

I'm still trying to read pixels from the fragment shader, and I have some questions. I know that gl_FragColor is returning with vec4, which means RGBA, 4 channels. After that, I use glReadPixels to read the FBO and write it to the data

GLubyte *pixels = new GLubyte[640*480*4];
glReadPixels(0, 0, 640,480, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

This works great, but it's actually a speed issue. Instead, I want to just read RGB to ignore alpha channels. I tried:

GLubyte *pixels = new GLubyte[640*480*3];
glReadPixels(0, 0, 640,480, GL_RGB, GL_UNSIGNED_BYTE, pixels);

and it didn’t work. I guess, because gl_FragColor returns 4 channels, and maybe I should do something before that? In fact, since my returned image (gl_FragColor) has shades of gray, I did something like

float gray = 0.5 //or some other values
gl_FragColor = vec4(gray,gray,gray,1.0);

So, is there an efficient way to use glReadPixels instead of using the first 4 channel method? Any suggestion? By the way, this is opengl es 2.0 code.

+5
1

OpenGL ES 2.0 spec , :

glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

GLint format, type;
glGetIntegerv(IMPLEMENTATION_COLOR_READ_FORMAT, &format);
glGetIntegerv(IMPLEMENTATION_COLOR_READ_TYPE, &type);
glReadPixels(x, y, w, h, format, type, pixels);

format type (pic ):

table

, .

, , , , . , RGB ( 0 -). , , framebuffer?

+4

All Articles