OpenGL: Screen Coords to World Coods -Again
I am posting this message in here now cause it seems that people in the other board "DirectX, OpenGL, and Other APIs" Never quite answer my question,
I need some help with Converting Screen Coordinates to World Coords, I want a Object in 3D space to follow my mouse cursor (or basically be a 3D mouse cursor..basically give it the 2D screen Coords, and have it move to that spot in 3D space,
I am Having some issues on what to do, any help or pointers to references would be nice,
Thanx alot
- Someone else brought up gluUnProject, and wanted to know if there was any other way to do it..so I thought I would post his question also..is there any other way?
-Thanx alot
-Lucas
-SyntaxSoft@Home.com
-Lucas
As I see it..
Let''s say you are using the source code for movement with the mouse and such as defined in the tutorials..but have switched it over to keyboard so the mouse is now available.
well let''s say you start at 0,0,0 facing down the z-axis, positive x to the right.(screen size of 800,600). from here find out how far the x-axis extends to the right. (draw object a little down the z axis like (0,0,(float)-.01) so it shows up on the screen then just move it over till you have the x-value). Now check y value.. now you have the relative size of a plane that fits an 800x600 screen size if the plane is .01 from where you are standing. now just rotate, translate the object that you will use for the cursor as you would your (camera, person.. whatever) to move around your scene.. Just have to make sure you are drawing .01 in from of whatever direction you are looking..
That''s just a guess.. I''ve made a little graphical menu on the bottom of my application but never change the cursor..to the 3D coordinatres.. (always just specified screen coordinates for my menu)
Let''s say you are using the source code for movement with the mouse and such as defined in the tutorials..but have switched it over to keyboard so the mouse is now available.
well let''s say you start at 0,0,0 facing down the z-axis, positive x to the right.(screen size of 800,600). from here find out how far the x-axis extends to the right. (draw object a little down the z axis like (0,0,(float)-.01) so it shows up on the screen then just move it over till you have the x-value). Now check y value.. now you have the relative size of a plane that fits an 800x600 screen size if the plane is .01 from where you are standing. now just rotate, translate the object that you will use for the cursor as you would your (camera, person.. whatever) to move around your scene.. Just have to make sure you are drawing .01 in from of whatever direction you are looking..
That''s just a guess.. I''ve made a little graphical menu on the bottom of my application but never change the cursor..to the 3D coordinatres.. (always just specified screen coordinates for my menu)
Ok, what you''re asking is really two parts - 1) converting screen/window coordinates to world coordinates, and 2) movement handling of mouse movement to object_under_control movement. #2 is simple, and since your question is on OpenGL, I''ll put out some information for you on #1.
Glu 1.3 defines two functions for transforming coordinates between screen <-> world - gluProject and gluUnProject.
gluProject() will take provided world coordinates and return (via passed pointer arguments) screen coordinates.
gluUnProject() will take provided screen coordinates and return (via passed pointer arguments) world coordinates.
Now, both functions have arguments for ModelViewMatrix[], ProjectionMatrix[], and ViewPort[]. These you have to supply to the functions, and in order for your coordinate transforms to be accurate with the scene, naturally you want to provide the matrices that were/are used to render your scene. This is done via glGetv(GL_MODELVIEW_MATRIX, &mvm) (check that name, it might be not quite it =), glGetv(GL_PROJECTION_MATRIX, ±) (same as above), and glGeti(GL_VIEWPORT, &vp) (re). The ModelView matrix is defined as GLfloat [16], as is the Projection matrix. The Viewport is a GLint [4].
The trick to gluProject and gluUnProject is this - you need to call glGetx() to get the current Matrices... but glGetx() will retrieve them *from their respective stacks*, which means that right before you need to call glu(Un)Project, you need to call a glPushMatrix for both the ModelView and Projection matrices (the Viewport matrix is not on a stack as far as I know). After you push the current (and good) matrices to their stacks, call glGetx(), store the matrices returned, and call glPushMatrix, because you really don''t want to overflow the stack (eventually, not immediately). After that, you have all the things you need to use gluProject and gluUnProject successfully.
For more information, I suggest www.opengl.org, the searchable library there is fairly complete, but I highly recommend the Red Book or the SuperBible if you''re starting to learn OpenGL and want a good reference book.
If you''re curious, I had to learn how to use glu(Un)Project on my own, for a project of mine - http://www.planetunreal.com/mep/files/terrained.zip . It''s a heightmap terrain editor, with some nice features and interface, 100% OpenGL.
- Pfhoenix
the Mod Exemplar Project
http://www.planetunreal.com/mep
Glu 1.3 defines two functions for transforming coordinates between screen <-> world - gluProject and gluUnProject.
gluProject() will take provided world coordinates and return (via passed pointer arguments) screen coordinates.
gluUnProject() will take provided screen coordinates and return (via passed pointer arguments) world coordinates.
Now, both functions have arguments for ModelViewMatrix[], ProjectionMatrix[], and ViewPort[]. These you have to supply to the functions, and in order for your coordinate transforms to be accurate with the scene, naturally you want to provide the matrices that were/are used to render your scene. This is done via glGetv(GL_MODELVIEW_MATRIX, &mvm) (check that name, it might be not quite it =), glGetv(GL_PROJECTION_MATRIX, ±) (same as above), and glGeti(GL_VIEWPORT, &vp) (re). The ModelView matrix is defined as GLfloat [16], as is the Projection matrix. The Viewport is a GLint [4].
The trick to gluProject and gluUnProject is this - you need to call glGetx() to get the current Matrices... but glGetx() will retrieve them *from their respective stacks*, which means that right before you need to call glu(Un)Project, you need to call a glPushMatrix for both the ModelView and Projection matrices (the Viewport matrix is not on a stack as far as I know). After you push the current (and good) matrices to their stacks, call glGetx(), store the matrices returned, and call glPushMatrix, because you really don''t want to overflow the stack (eventually, not immediately). After that, you have all the things you need to use gluProject and gluUnProject successfully.
For more information, I suggest www.opengl.org, the searchable library there is fairly complete, but I highly recommend the Red Book or the SuperBible if you''re starting to learn OpenGL and want a good reference book.
If you''re curious, I had to learn how to use glu(Un)Project on my own, for a project of mine - http://www.planetunreal.com/mep/files/terrained.zip . It''s a heightmap terrain editor, with some nice features and interface, 100% OpenGL.
- Pfhoenix
the Mod Exemplar Project
http://www.planetunreal.com/mep
- Pfhoenix
I appreciate your help Pfhoenix..by far the most informing answer that I have ever heard..and I thank you..
Thanx alot.
-Lucas
-SyntaxSoft@Home.com
Thanx alot.
-Lucas
-SyntaxSoft@Home.com
-Lucas
I've never heard you needed to push or pop the modelview and projection matrices before calling the glGet* functions. glGet just gets the top of the stack - which is the one you're using anyway. In fact I'm using gluUnProject right now and don't need to do this.
I'm more confused than ever now about this whole thing.
I call gluUnProject twice to define an origin and an endpoint for my ray:
Now, I use this ray to test for triangle interesctions - all is well.
Ultimatly I want to convert my program to DirectX so I thought I could 'steal' (well at least use as inspiration
) the MESA source for this function, so I copied and pasted the source, changed it to use my globals and my matrix math classes and whatnot. Anyway, it doesn't work
The pickray is cast fine if you're near the middle of the viewport, but goes all over the place if you stray near the edges -- most distressing.
Even worse, I dumped my matrix library and used the functions in the MESA source and I got the same result. Anyone have this kinda thing working without using API specific code?
Thanks,
Paul.
Edited by - Pauly on November 30, 2000 11:46:59 AM
I'm more confused than ever now about this whole thing.
I call gluUnProject twice to define an origin and an endpoint for my ray:
gluUnProject ( x, vpHeight - y, 0.0, matModel, matProj, viewport,
&vPickRayOrig[X], &vPickRayOrig[Y], &vPickRayOrig[Z] );
gluUnProject ( x, vpHeight - y, 1.0, matModel, matProj, viewport,
&vPickRayEnd[X], &vPickRayEnd[Y], &vPickRayEnd[Z] );
Now, I use this ray to test for triangle interesctions - all is well.
Ultimatly I want to convert my program to DirectX so I thought I could 'steal' (well at least use as inspiration
![](smile.gif)
![](sad.gif)
Even worse, I dumped my matrix library and used the functions in the MESA source and I got the same result. Anyone have this kinda thing working without using API specific code?
Thanks,
Paul.
Edited by - Pauly on November 30, 2000 11:46:59 AM
Paul Grovespauls opengl page
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement