Sign in to follow this  

obect picking in OpenGgl

Recommended Posts

Hi to all, Does anybody know how to pick an object and returning its pointer to rotate,translete etc. this object. I tried to pick an object using glPushName,glLoadName,but I thibk I'm missing something.Because I cant pick the objects, it always picks the same object. What's more I don't know how to access this selected object(or its pointer) later. Below is my code, can anybody see what's wrong? Thank you... void draw_all_objects(){ ShapeNode* current = shape.gethead()->next; float *array; for(int i=0;current!= shape.gettail();i++){ array=current->getvertexarray(); if(current->get_hide() == false){ glLoadIdentity(); //glMatrixMode(GL_MODELVIEW); glPushMatrix(); float center_x = current->getcx(); float center_y =current->getcy(); float center_z =current->getcz(); float angle = current->getangle(); float scal = current ->getscal(); glTranslated(center_x,center_y,center_z); glScaled(scal,scal,1); glRotated(angle,0.0,0.0,1.0); glTranslated(-center_x, -center_y, - center_z); glPushName(i); current->draw(); glPopName(); glPopMatrix(); } current=current->next; } } void myGlutDisplay( void ){ glClearColor( 1.0f, 1.0f, 1.0f, 1.0f ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode(GL_MODELVIEW); if (mode == SELECT) startPicking(); glLoadIdentity (); initTransformation(); draw_all_objects(); if(mode==SELECT) stopPicking(); else //glFlush(); glutSwapBuffers(); } void startPicking() { GLint viewport[4]; float ratio; glSelectBuffer(BUFSIZE,selectBuf); glGetIntegerv(GL_VIEWPORT,viewport); glRenderMode(GL_SELECT); glInitNames(); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPickMatrix(mouse_x,viewport[3]-mouse_y,1,1,viewport); //ratio = (viewport[2]+0.0) / viewport[3]; //gluPerspective(45,ratio,0.1,1000); glMatrixMode(GL_MODELVIEW); } /***************************** stop Picking ****************************/ void stopPicking() { glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); //glFlush(); hits = glRenderMode(GL_RENDER); if (hits != 0){ processHits2(hits,selectBuf,0); } mode = RENDER; } /*********** process hits2*************/ void processHits2 (GLint hits, GLuint buffer[], int sw) { GLint i, j, numberOfNames; GLuint names, *ptr, minZ,*ptrNames; ptr = (GLuint *) buffer; minZ = 0xffffffff; for (i = 0; i < hits; i++) { names = *ptr; ptr++; if (*ptr < minZ) { numberOfNames = names; minZ = *ptr; ptrNames = ptr+2; } ptr += names+2; } if (numberOfNames > 0) { printf ("You picked snowman "); ptr = ptrNames; for (j = 0; j < numberOfNames; j++,ptr++) { printf ("%d ", *ptr); } } else printf("You didn't click a snowman!"); printf ("\n"); }

Share this post

Link to post
Share on other sites
One thing that is possibly wrong is the order of your state calls in your startPicking function. Also, why is your perspective call commented out? Try changing your function to this:

// w = screen width
// h = screen height
void startPicking(int w, int h) {

GLint viewport[4];
float ratio;

ratio = (GLfloat)w / (GLfloat)h;





There may be other issues. Let me know if this doesn't correct them and I will look for more problems in your code.

Share this post

Link to post
Share on other sites
Hi JDaniel,
First of all,Thanks for your interest...
I tried what you've said...But unfortunately it didn't work again.
Why I commented out the perspective is that I'm a newbie in OpenGl and I tried what would change when I do it :)
If you really have interest in my code I really would appreciate it.
Because I'm really stuck and can't go any further :(
I sent the my code to your cs.utk email.(the reason why I didn't write here is my code is long)
Thanks again

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this