I'm trying to develop an application using OpenGL 4.0 and Qt 5.3 and I want to implement color picking to select different models in a QGLWidget. So basically, when I detect a mouse click, I:
- Get the position of the mouse
- Render the scene
- Set a white background (
glClearColor
thenglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
- I bind my shader program
- I draw my models (each one with a different color and its own transformation matrix)
- Release my shader program
- Set a white background (
- Call
glFlush
andglFinish
to make sure I have finished rendering before callingglReadPixels
- Call
glReadPixels(mouse.x, window_height - mouse.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data)
data being aGLubyte
array of length 4
My program runs "well" but when I want to select an object, I have to click a little more up and right than where the model really is. I tried to swap buffers to check if the models are rendered at the right position and yes, they are...
I also tried to call glPixelStorei(GL_PACK_ALIGNMENT, x)
with x = 1, 2, 4, 8 before glReadPixels
but it doesn't seem to affect it.