Hello board,
I’ve been trying to figure out this bug for a couple days now, so any ideas or even inklings are greatly appreciated! It’s a long post, but it’s a pretty stripped down and simple program I’m describing.
Basically, the data I’m getting from glReadPixels() is not what I’m expecting (so the problem could really be anywhere in my program!). I’ve tried to simplify the program as much as possible to just isolate this problem.
My shaders look like this:
// Fragment shader
// Every pixel is given arbitrary color vector 0x9F109F10
void main (void)
{
gl_FragColor = vec4(0.0625, 0.625, 0.0625, 0.625);
};
// Vertex shader
attribute highp vec4 vPosition;
void main(void)
{
gl_Position = vPosition;
}
I have a VBO that contains the vertex positions so that a square is drawn that covers the entire viewport on every render pass.
GLfloat vertices[] = { -1.0f, -1.0f, 0.0f,
1.0f , -1.0f, 0.0f,
1.0f , 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
};
// Generate the vertex buffer object (VBO)
glGenBuffers(1, vboPtr);
// Bind the VBO so we can fill it with data
glBindBuffer(GL_ARRAY_BUFFER, *vboPtr);
// Set the buffer's data
unsigned int size = 4 * (sizeof(GLfloat) * 3);
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
// Unbind the buffer
glBindBuffer(GL_ARRAY_BUFFER, 0);
And drawing the VBO consists of:
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindAttribLocation(program, 0, "vPosition");
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDisableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
I'm also using an FBO and doing render-to-texture.
My viewport is set as glViewport(0, 0, IN_WIDTH, IN_WIDTH) where IN_WIDTH is a constant (currently set to 8).
After doing one render pass, I use glReadPixels to inspect the output.
unsigned int data[IN_WIDTH * IN_WIDTH];
glReadPixels(0, 0, IN_WIDTH, IN_WIDTH, GL_RGBA, GL_UNSIGNED_BYTE, data);
// Init data to 0xEE so I can tell if data's been written to
memset(data, 0xEE, sizeof(unsigned int) * IN_WIDTH * IN_WIDTH);
for (idx = 0; idx < IN_WIDTH * IN_WIDTH; idx++)
{
printf("output[%d] = %08Xn", idx, data[idx]);
}
The expected output would be this:
output[0] = 9F109F10
output[1] = 9F109F10
...
output[63] = 9F109F10
But the actual output is this:
output[0] = 9F109F10
output[1] = 9F109F10
output[2] = 9F109F10
output[3] = 9F109F10
output[4] = 9F109F10
output[5] = 9F109F10
output[6] = 9F109F10
output[7] = 9F109F10
output[8] = 40BBD000
output[9] = 00001000
output[10] = 40BBE000
output[11] = 00001000
output[12] = 40BBF000
output[13] = 00001000
output[14] = 40BC0000
output[15] = 00001000
output[16] = 40BC1000
output[17] = 00001000
output[18] = 40BC2000
output[19] = 00001000
output[20] = 40BC3000
output[21] = 00001000
output[22] = 40BC4000
output[23] = 00001000
output[24] = 40BC5000
output[25] = 00001000
output[26] = 40BC6000
output[27] = 00001000
output[28] = 40BC7000
output[29] = 00001000
output[30] = 40BC8000
output[31] = 00001000
output[32] = 9F109F10
output[33] = 9F109F10
output[34] = 9F109F10
output[35] = 9F109F10
output[36] = 9F109F10
output[37] = 9F109F10
output[38] = 9F109F10
output[39] = 9F109F10
output[40] = 40D4E000
output[41] = 00021000
output[42] = 40D6F000
output[43] = 00001000
output[44] = 40D70000
output[45] = 00011000
output[46] = 40D81000
output[47] = 00011000
output[48] = 40D92000
output[49] = 00001000
output[50] = 40D93000
output[51] = 00001000
output[52] = 40D94000
output[53] = 00001000
output[54] = 40D95000
output[55] = 00001000
output[56] = 40D96000
output[57] = 00001000
output[58] = 40D97000
output[59] = 00001000
output[60] = 40D98000
output[61] = 00001000
output[62] = 40D99000
output[63] = 00002000
I've bolded the two sections where the output is actually correct. The rest of it just seems to be garbage.
Any ideas?
Thank you!