Mouse woes

Started by
38 comments, last by BeyondDeath 22 years, 2 months ago
Hey, I was right !

I read in the OpenGL red book :

quote:Each hit record consists of four items, in order.

- The number of names on the name stack when the hit occurred.

- Both the minimum and maximum window-coordinate z values of all vertices of the primitives that intersected the viewing volume since the last recorded hit. These two values, which lie in the range [0,1], are each multiplied by 2^32-1 and rounded to the nearest unsigned integer.

- The contents of the name stack at the time of the hit, with the bottommost element first.


I''d recomment using that function to see what happens (from Red Book too) :
  void processHits (GLint hits, GLuint buffer[]){   unsigned int i, j;   GLuint names, *ptr;   printf ("hits = %d\n", hits);   ptr = (GLuint *) buffer;   for (i = 0; i < hits; i++) { /*  for each hit  */      names = *ptr;      printf (" number of names for hit = %d\n", names); ptr++;      printf("  z1 is %g;", (float) *ptr/0x7fffffff); ptr++;      printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++;      printf ("   the name is ");      for (j = 0; j < names; j++) {     /*  for each name */         printf ("%d ", *ptr); ptr++;      }      printf ("\n");   }}  


and call this function with the number of hits as first argument (the number of hits is the integer return by glRenderMode(GL_RENDER)) and the selection buffer as second argument.


FYI, you can find the book online at http://ask.ii.uib.no/ebt-bin/nph-dweb/dynaweb/SGI_Developer/OpenGL_PG/
I got the selection buffer information in "Chapter 13 Selection and Feedback".
Advertisement
Still the same thing. If I actually click the terrain it doesnt work (most of the time) and if i click below it, it works sometimes. If you want to see the code or a build lemme know Im gonna put a bullet thru my computer now
why not taking a look at it ?
you can send me a zipped email of your code, but I won''t guarantee I will read it until tomorrow since it''s 1:00am here and I gotta go to sleep
Ok, Ive done some work with it, and it is working SORT OF

when I am above the terrain I am able to click it and it figures out the square that i clicked ... only thing is that it isnt the one i clicked it is another about 10 squares off ... not in any regular direction. If you want I can email the new source. (Its all oop now so its not as yucky )

Hopefully we are on the verge of fixing this stupid thing

thanks
Not that I want to say it was an easy problem, but it was a... simple problem
Took me around 10mins to figure it out (even though I did not answered 10mins after you posted the message above)
In fact, it costs me more time to post the answer than to find it !

Well *hum* jokes apart. You did not make any error drawing the scene or pushing/popping the names.


You have (had?) two *other* mistakes :


First of all, you use wrong mouse coordinates for the cursor.
In picking, mouse coordinates are used as (m.x, viewport[3]-m.y) because of OpenGL''s mathematical representation of coordinates in 3D which uses a y axis up for positive values and down for negative values (in mouse coordinates, up result in _lower_ value, and down result in _higher_ values.

But you don''t use MS Windows'' mouse cursor : you use your own cursor whose coordinates are exploited in the OpenGL transformation matrices (modelview and projection).
So, you also have to "invert" your cursor position.
Instead of calling glTranslatef(m.x, m.y), I''d recommend using glTranslatef(m.x, window_height-m.y);

Without that, for me the mouse up/down axis is reversed, which is quite frustrating.



Secondly, and more important, you don''t use the good projection matrix.
For selection rendering, you use this perspective :
gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]), 0.1f, 100.0f);

But for your "normal" rendering, you use this perspective :
gluPerspective(45.0f,(GLfloat)xres/(GLfloat)yres,0.1f,2000.0f);

And there''s a problem because with the first of those perspectives, your far plane is at 100.0f which is not enough for your scene : because terrain is drawn "behind" this far plane 100.0f, the polygons are clipped out of the viewing frustum, result in non hitting the selection buffer.

To correct it, the simple solution is to use the same gluPerspective arguments in BOTH cases.

Otherwise, you can ask OpenGL to "synchronize" this matrix for you.
Instead of doing :

  glMatrixMode(GL_PROJECTION); // Selects The Projection MatrixglPushMatrix(); // Push The Projection MatrixglLoadIdentity(); // Resets The Matrix// This Creates A Matrix That Will Zoom Up To A Small Portion Of The Screen, Where The Mouse Is.gluPickMatrix((GLdouble) m.x, (GLdouble) (viewport[3]-m.y), 1.0f, 1.0f, viewport);// Apply The Perspective MatrixgluPerspective(...);glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix...  


you can do :
  GLfloat projection[16]; // The Current Projection MatrixglGetFloatv(GL_PROJECTION_MATRIX, projection);glMatrixMode(GL_PROJECTION); // Selects The Projection MatrixglPushMatrix(); // Push The Projection MatrixglLoadIdentity(); // Resets The Matrix// This Creates A Matrix That Will Zoom Up To A Small Portion Of The Screen, Where The Mouse Is.gluPickMatrix((GLdouble) m.x, (GLdouble) (viewport[3]-m.y), 1.0f, 1.0f, viewport);// Apply The Perspective MatrixglMultMatrixf(projection);glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix...  


athough I''d not recommend that method since it''s a bit slower. Just keep this in mind if you can''t control the exact projection matrix yourself.



Also, one last thing I''d like to say : do not use a picking region of (1.0f, 1.0f). Sometimes, objects will be difficult to select on a single pixel exactitude. I''d rather recommend using a greater region (but still small of course) like (5.0f, 5.0f), eg :
gluPickMatrix((GLdouble) m.x, (GLdouble) (viewport[3]-m.y), 5.0f, 5.0f, viewport);
If both projection calls are to be the same, why make 2 calls?
www.EberKain.comThere it is, Television, Look Listen Kneel Pray.
2 calls ? where ?
please show the lines of code that do bother you
... ACK it works... in one strait line and only from under the terrain!!! ... 1 line on Z that is like into / out of screen...
ACK
THANKs again.. I modified the code tho since then so you may not be looking at the same code. also, I had updated the perspective thing about an hour after i sent that copy. and the mouse thing I knew about I was just a lil'' lazy to change
OMFG IT IS WORKING... PROPERLY!!!
All i had to do was translate in the click button cause it wasnt taking into accound the change caused by my input. THANKS SOMUCH FOR THE HELP VINCOOF!
You''re more than welcome

Glad you feel the mighty power of selection buffer

This topic is closed to new replies.

Advertisement