Picking/Selection not working, Using Ortho2D

Posted on 2007-11-18
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
            //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);
            fprintf(stdout, "Picking: Mouse at %d, %d +++ Picking Coords: %d, %d\n", x, y,  x, view[3]-y);
      //restore normal views
      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

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
Question by:sakuya_su
  • 5
  • 4
LVL 20

Accepted Solution

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:

LVL 10

Author Comment

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.
LVL 20

Expert Comment

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);

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

LVL 10

Author Comment

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

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

Expert Comment

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

Expert Comment

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

Author Comment

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 :)
LVL 20

Expert Comment

ID: 20323133
LVL 10

Author Comment

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

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
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…
In a recent question ( here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

823 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