Mouse Locations

Moderator
Posts: 613
Joined: 2004.09
Post: #1
Im pretty new to OpenGL, but i have been toying around with it trying to learn (the best way for me to learn something). I have read through most of NeHes stuff. I have been playing around with a 3d "engine" (I use engine losely). I want the player peice which is always centered in the middle of the screen (which i track with GluLookAt()) to be able to move where ever the mouse is clicked. I am doing this fine by getting the height and width of the window and doing some basic math to figure out where in the screen it needs to go. The problem I am having is that when you rotate the screen around the Z-axis it no longer knows which way is up. So when you are rotated 180 degrees it still moves the player up on the Y axis which is now down. Is there a way i can slove this problem, the easiest way i can think of is by transforming the click into a x/y grid in openGL. I dont need to worry about z yet for mouse locations maybe in a few weeks. Ive browsed through the forms and found some info on Ray Selection and Gl Selection but i cant really figure them out. Ive tried and play around with picking from NeHe's lesson 32 but cant make heads and tails of it. Is there someone out there who can point me in a good direction or take the time to explain things slowly to me =). I really appeciate it. Thanks All!!!
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #2
Hi kodex - welcome to iDG

Read up on gluProject and gluUnProject. You can use gluProject with your object's location to get back its 'z' depth. Then you can use gluUnProject with your mouse's x and y, and with the 'z' depth from gluProject, get back a new x, y, z location that represents where the object would be moved to in order to line it up to the mouse (while keeping its same 'z' depth)

Both use your model-view and projection matricies as well as your viewport so they take into account your 'camera' no matter how its oriented.

When I talk about 'z' depth - I dont mean its z location - but its depth away from the screen. And this number isnt even a straight measurement of depth but a logarithmic one - but you dont have to really care since both functions use it the same way.

so man gluProject and gluUnProject.

http://www.mevis.de/~uwe/opengl/gluUnProject.html
http://www.mevis.de/~uwe/opengl/gluProject.html

hth,
Codemattic
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #3
Could you use paragraphs the next time you post? I get dizzy from the onslaught of dense words Wink

Anyway, the gluUnProject and its companions are probably what you are looking for, unless you keep your matrices in software. You basically need to do some complicated stuff, projecting a line in the direction of the mouse pointer from your point-of-view, and then finding the intersection with your objects to determine what is pointed at.
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #4
Interesting, I was wondering something like this. How would I use gluUnProject in an NSOpenGLView?

Suppose I wanted to draw a logo on the top center of the screen.

"When you dream, there are no rules..."
Quote this message in a reply
Member
Posts: 233
Joined: 2003.05
Post: #5
Taxxodium... what you want to do is temporarily switch the projection matrix using glOrtho() and then just use screen x,y coordinates. Different problem than what these guys are talking about.

"Pay no attention to that man behind the curtain." - Wizard of Oz
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #6
Well, for example, how would I check if my polygon has reached the boundaries of a window? Keeping in mind that the polygon moves with the mouse.

"When you dream, there are no rules..."
Quote this message in a reply
Member
Posts: 233
Joined: 2003.05
Post: #7
Well, that sounds a lot different than, "Suppose I wanted to draw a logo on the top center of the screen." :/

It sort of depends what you need it for. If the polygon moves at the same speed as the mouse (like a pointer) than it should probably be drawn in 2D ortho in which case it is very easy to check. (Just check the vertices against the x and y limits of the screen/window.)

If you have a polygon spaceship or something that interacts with 3D elements, I'd guess you'd want a 3D boundary system, too. So, again, just use the 3D cooridinates.

The whole gluUnProject thing is about mixing the two. I can't think of a good reason to use it, except for find out where a 2D mouse click lands on a 3D terrain.

Kodex: why not just take the resulting vector of the click and rotate it around the z-axis to compensate for the rotation of the player.

In other words, you say that it all works fine when not rotated. Clicking up and to the left makes the player move in that direction. If you are rotated 45 just take the movement vector you get from the click to the upper left and rotate it 45 degrees. :/ Much easier, if I am understanding your description correctly.

That only works if you aren't worried about exact coordinates to move to (which you didn't seem to be interested in from your description.) For that you need either the picking described in the Nehe tutorial you described or the method codemattic is talking about.

"Pay no attention to that man behind the curtain." - Wizard of Oz
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #8
Is it possible to combine projections then? For example, draw my cursorpolygon in Ortho mode and draw other objects in Perspective mode?

How would I check for boundaries in Perspective mode? I'm thinking I have to take the fovy and the ratio into account, but how to do the convertion?

"When you dream, there are no rules..."
Quote this message in a reply
Member
Posts: 233
Joined: 2003.05
Post: #9
It is possible to "combine" projections, yes. Just switch your perspective and draw before clearing the buffer. As far as getting actual 3-D coordinates for your 2-D mouse position, that is where the picking (again, Nehe tutorial) or glUnProject comes in.

I've never really used either method, although I've read some about them. Smile I have used ortho and regular projections together though.

"Pay no attention to that man behind the curtain." - Wizard of Oz
Quote this message in a reply
Post Reply