Solved

Picking/Selection not working, Using Ortho2D

Posted on 2007-11-18
9
708 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How many Python RTS servers on one host? 1 302
Sudoku - eraseable white board 2 292
Any clean Java Swing double buffering advice? 10 251
scores100 challenge 3 121
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…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

789 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