I am trying to implement some simple object picking in my application, but running into problems from the start. For testing I am trying to use what is given in this tutorial: OpenGL Programming/Object selection
In my code I now do the following:
GLbyte color[4];
GLfloat depth;
GLuint index;
glReadPixels(click->x, WINDOW_HEIGHT - click->y - 1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, color);
glReadPixels(click->x, WINDOW_HEIGHT - click->y - 1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
glReadPixels(click->x, WINDOW_HEIGHT - click->y - 1, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_INT, &index);
printf("Clicked on pixel %d, %d, color %02hhx%02hhx%02hhx%02hhx, depth %f, stencil index %u\n", click->x, click->y, color[0], color[1], color[2], color[3], depth, index);
which give me the follwing output:

No values for RGBA or stencil index. I only have 1 object where I have put glStencilFunc(GL_ALWAYS, 99, -1);, so I am not expecting to see a stencil index for most places. For the colors however I did not think anything extra would be required?
For debugging here are some core parts of my OGL initialization code:
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_MULTISAMPLE);
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
and SDL initialization code:
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
window = SDL_CreateWindow(windowTitle.c_str(), SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
From this information is it possible to extract why my glReadPixels is not reporting for example RGBA values?
Found the solution to this problem…
It was swapping the buffer before running the
glReadPixels.