0

I'm following tutorial for picking in opengl. This is my function for picking,

int size = 25;
GLuint buffer[size];

glSelectBuffer(size, buffer);
glRenderMode(GL_SELECT);
glPushMatrix();
glLoadIdentity();

glGetIntegerv(GL_VIEWPORT, viewport);
gluPickMatrix(x, viewport[3] - y, 1000, 1000, viewport);

gluPerspective(60, ratio, 0.001, 1000);
glMatrixMode(GL_MODELVIEW);
glInitNames();
//give dummy name
glPushName(0);

draw_scene(scene_root, true);

glPopName();
//stop picking
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glFlush();

GLint hits = glRenderMode(GL_RENDER);

I'm using 1000,1000 for gluPickMatrix just to get a hit

draw_scene boils down to

glLoadName(1); 
mProgram.setUniformValue(mMvpMatrixLocation, getCameraMatrix() * transform);
glDrawArrays(GL_TRIANGLES, 40, 40 + v->sphereSize/3);

Get getCameraMatrix() returns a QMatrix4x4 that represents my camera projection, and transform is QMatrix4x4` that represents my model coordinates.

My shader.vert looks like this

in vec3 vert;
uniform mat4 mvpMatrix;

void main()
{
   gl_Position = mvpMatrix * vec4(vert, 1.0);
}

Note mvpMatrix is getCameraMatrix() * transform

I'm loading '1' temporally since I only have one object.

My issue is that it is not generating any hits despite the viewport being 1000x1000

edit: The issue appears to that glRendmode(GL_SELECT) does change the render mode. I created another question for this,

Community
  • 1
  • 1
Sahar Rabinoviz
  • 1,939
  • 2
  • 17
  • 28
  • 1
    Based on the fact that you're setting a uniform, it looks like you're using a shader. Is your shader using the fixed function projection matrix? Because that's what you're setting with `gluPickMatrix()` and `gluProjection()`. – Reto Koradi Feb 24 '15 at 04:52
  • @RetoKoradi I edited question to include shader code, Yes my shader is using a projection matrix, because of this I dont need to use `gluPickMatrix()`? – Sahar Rabinoviz Feb 24 '15 at 15:03
  • As @RetoKoradi points out, [gluPickMatrix](https://www.opengl.org/sdk/docs/man2/xhtml/gluPickMatrix.xml) applies a transform to the currently active fixed function matrix, but you don't appear to use it. You could [extract the `GL_PROJECTION_MATRIX` matrix](http://stackoverflow.com/q/766127/1888983), build it yourself without gluPickMatrix, or go back to the [older GL](http://stackoverflow.com/a/26009113/1888983) shaders and use the builtin `gl_ProjectionMatrix`. – jozxyqk Feb 24 '15 at 16:03
  • @jozxyqk So what I should do is extract the GL_PROJECTION_MATRIX, then pass this matrix along with model matrix to the shader – Sahar Rabinoviz Feb 24 '15 at 17:07
  • yes, but if you're already handling your own matrix stuff, [constructing your own based of this](http://stackoverflow.com/q/8660454/1888983) might be cleaner. also be careful of which matrix is active (in your code, a `glMatrixMode(GL_PROJECTION)` is missing before `glPushMatrix()`. – jozxyqk Feb 24 '15 at 18:03
  • I'm still having trouble was I was to generate my viewport matrix, I verified it is moving to the chaging the viewport but redrawing on click.,yet for some reason it is not registering as a hit. – Sahar Rabinoviz Feb 24 '15 at 21:10

0 Answers0