Solved

Picking/Selection not working, Using Ortho2D

Posted on 2007-11-18
9
705 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now