PDA

View Full Version : [Picking] hit-value returning all Objects



blue5teel
07-02-2006, 07:52 AM
I cant get picking working in my application. The Hit-Value returns all Objects on screen.
Here some excerpts from my code:


void OglCode::draw(){

glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glShadeModel (GL_SMOOTH);


glLightfv( GL_LIGHT0, GL_POSITION, g_LightPosition);
glLightfv( GL_LIGHT0, GL_DIFFUSE, g_LightDiffuse);
glLightfv( GL_LIGHT0, GL_AMBIENT, g_LightAmbient);


glLightfv( GL_LIGHT1, GL_POSITION,g1_LightPosition);
glLightfv( GL_LIGHT1, GL_DIFFUSE, g1_LightDiffuse);
glLightfv( GL_LIGHT1, GL_AMBIENT, g1_LightAmbient);

glPushMatrix();
glRotatef(view_rotx, 1.0f, 0.0f,0.0f);
glRotatef(view_roty, 0.0f, 1.0f, 0.0f);
glRotatef(view_rotz, 0.0f, 0.0f, 1.0f);

if(mode == GL_SELECT){

for(int i = 0;i<Listcount;i++){
glPushMatrix();
glLoadName(i);
glCallList(i);
glPopMatrix();
}

}

else{
drawCoordinateGizmo();
drawGrid();

for(int i =0;i<POPSIZE;i++){
drawApex(Population[i]);
}

for(int i = 0;i<Listcount;i++){
glPushMatrix();
glCallList(i);
glPopMatrix();
}

}

glPopMatrix();
this->swapBuffers();
}
void OglCode::processHits(int x, int y){

const int buffsize= POPSIZE;
GLuint select_buf[buffsize]; //selection buffer
int hits; //number of hits
glSelectBuffer(buffsize, select_buf);

//set up view matrix here - ie do whatever transformations
glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

gluPickMatrix((GLdouble)mousex,
(GLdouble)(viewport[3]-mousey),
1, 1,
viewport);
glFrustum(-verhaeltnis, verhaeltnis, -1.0, 1.0, 4.0, 15.0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
this->swapBuffers();
draw();

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glLoadIdentity();

glFlush(); //flush the pipeline
hits = glRenderMode(GL_RENDER); //switch back to RENDER mode
mode = GL_RENDER;
}Maybe i should mention that i use QT and QGLWidget....

Flavious
07-02-2006, 09:24 PM
When posting code in the hope that others will debug it for you, you should try to post a minimal complete, compilable reproduction case, so that interested parties can simply paste it into an empty project and tinker with it, without having to guess at the missing code (a fruitless endeavour at best).

Posting incomplete snippets, particularly for more complex operations, is of little value, as it leaves the reader with little else to do but pour over the code, line-by-line, looking for errors, or speculate on the numerous pitfalls you may have encountered.

Short of reducing your app to one postable here, consider starting with some documentation and a working demo.
http://www.rush3d.com/reference/opengl-redbook-1.1/chapter12.html
http://www.opengl.org/resources/faq/technical/selection.htm
http://www.lighthouse3d.com/opengl/picking/

QT (QuickTime?) and QGLWidget have nothing to do with OpenGL picking. I would advise you to debug your app outside that environment if you suspect it to be at fault. You could use GLUT, for example, which is very well tested and stable on a variety of platforms.
http://www.xmission.com/~nate/glut.html

I hope this helps.