Solved

Picking/Selection not working, Using Ortho2D

Posted on 2007-11-18
9
710 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
 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

 
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

 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

Question has a verified solution.

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

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 …
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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