Solved

Picking/Selection not working, Using Ortho2D

Posted on 2007-11-18
9
709 Views
Last Modified: 2013-12-06
I have the following code:
void doSelect(int x, int y)
{
      GLuint buff[64] = {0};
      GLint hits, view[4];
      int id;
      int i;
      GLuint *names = buff;
      
      //set buffer
      glSelectBuffer(64, buff);
      glGetIntegerv(GL_VIEWPORT, view);
      
      //enter select mode
      glRenderMode(GL_SELECT);
      glPushName(0);
      
      glMatrixMode(GL_PROJECTION);
      glPushMatrix();
      glLoadIdentity();
            //Restrict area to around mouse
            gluPickMatrix(x-iw/2, y+ih/2, 5, 5, view);
            glOrtho(-iw/2, iw/2, -ih/2, ih/2, -1, 1);
            //DEBUG:
            fprintf(stdout, "Picking: Mouse at %d, %d +++ Picking Coords: %d, %d\n", x, y,  x, view[3]-y);
      glMatrixMode(GL_MODELVIEW);
      glInitNames();
      doRender();
      
      //restore normal views
      glMatrixMode(GL_PROJECTION);
      glPopMatrix();
      
      hits = glRenderMode(GL_RENDER);
      
            printf("%d hits:\n", hits);
       
       for (i = 0; i < hits; i++)
             fprintf(stdout,      "Number: %d\n"
                         "Min Z: %d\n"
                         "Max Z: %d\n"
                         "Name on stack: %d\n",
                         (GLubyte)names[i * 4],
                         (GLubyte)names[i * 4 + 1],
                         (GLubyte)names[i * 4 + 2],
                         (GLubyte)names[i * 4 + 3]
                         );
       
       fprintf(stdout, "\n");
      
      //restore to model view to render to screen
      glMatrixMode(GL_MODELVIEW);
}

Notice how the OpenGL coordnites is set to -Width/2 to Width/2 and -Height/2 to height/2, the mouse x,y are translated correctly, but somehow I am not registering any hits?

Am I doing something wrong?

Thanks in advanced
0
Comment
Question by:sakuya_su
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 20

Accepted Solution

by:
ikework earned 500 total points
ID: 20318177
hi sakuya_su,

glPushName(0) has to be after glInitNames(), since glInitNames initialzes the name-stack and glPushName(0) pushes one element to the stack. did you put glLoadName into the render function for each rendered object?

here is a short example how to setup the object-picking:

http://gpwiki.org/index.php/OpenGL:Tutorials:Picking


ike
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 20318309
Yes, I have the names pushed in in the render function.

The thing is, if i did a full screen selection, that is:
gluPickMatrix(x-iw/2, y+ih/2, iw, ih, view);

I get all the names, meaning its working, but as soon as I limit to a 5 pixel region, I don't get any names anymore.
0
 
LVL 20

Expert Comment

by:ikework
ID: 20318909
>> The thing is, if i did a full screen selection, that is:
>> gluPickMatrix(x-iw/2, y+ih/2, iw, ih, view);
>> I get all the names, meaning its working, but as soon as I limit to a 5 pixel region, I don't get
>> any names anymore.

are you sure? did you really check, which names were returned ..  because the first two params of gluPickMatrix look wrong, try:

  gluPickMatrix(x, y, 5, 5, view);

or if y-axis is mirrored:

  gluPickMatrix(x, view[3]-y, 5, 5, view);


ike
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Author Comment

by:sakuya_su
ID: 20322482
if i did something like gluPickMatrix(0, 0, 1024, 768, view);

I get all names returned to me. I checked
0
 
LVL 20

Expert Comment

by:ikework
ID: 20322505
so all objects are rendered at window-position 0,0 ??
0
 
LVL 20

Expert Comment

by:ikework
ID: 20322509
can you show the full code?
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 20322555
gimme an hour or so, I'm working on something else.. Im using bounding box atm for checking.

Thanks for staying with me though :)
0
 
LVL 20

Expert Comment

by:ikework
ID: 20323133
:)
0
 
LVL 10

Author Comment

by:sakuya_su
ID: 20324001
Hmm I actually found the problem, up on looking closely, I found out that hits were registered when Render returned but no name was found, and it seems that glLoadName doesnt work at all, once I switched to glPushName it started working :S

Thank you very much for your help, I give you the points
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question