Login
Username:

Password:


Lost Password?

Register now!
Main Menu
H3D.org Feeds
H3D.org Forum Index
   Programming Issues
     mouse to world
Register To Post

Threaded | Newest First Previous Topic | Next Topic | Bottom
Poster Thread
erik
Posted on: 2006/7/13 4:55
Not too shy to talk
Joined: 2005/3/15
From: Uppsala
Posts: 12
mouse to world
I am struggling with mapping of the mouse position to world coordinates by using the gluUnproject function. First I obtain the modelview matrix, the projection matrix, and the viewport. Then I use glReadPixels to obtain the depth value at the current position. This information and the mouse position is passed on to gluUnproject in order to obtain the corresponding local position. I don't get it to work properly, someone have any hints?
karlu
Posted on: 2006/7/13 14:03
Guru
Joined: 2004/12/2
From: Sweden
Posts: 586
Re: mouse to world
I never understood how to use gluUnproject, so I implemented my own. I explain how in this thread, but it might not be very clear. Not using Unproject you have to invert a matrix or two, that's a major downside.

Here is the code I use to extract the position, x, of the mouse at the near plane and the vector, v, pointing directly into the screen at that position. It is simple to get from there to a position at a specific depth. I use my own little linear algebra package, but it should be clear enough what's being done. If not, don't hesitate to kick me hard to teach me to be more clear.

No guarantees that this is the simplest and most effective way of performing the tast!

  template< class T, class M >
  template< class S >
  bool a3d::Vector3<T,M>::fromScreenPosition( S sx, S sy,
                                              a3d::Vector3<T,M> &x,
                                              a3d::Vector3<T,M> &v ){
    a3d::Matrix4f Mm;
    glGetFloatv( GL_MODELVIEW_MATRIX, (float*)Mm );
    a3d::Matrix4f Mmi = Mm.inverse();
    
    GLint size[4];
    glGetIntegerv( GL_VIEWPORT, size );
    int width = size[2]-size[0];
    int height = size[3]-size[1];
    if( width <= 0 || height <= 0 ){
      return false; }
    
    a3d::Matrix4f Mp;
    glGetFloatv( GL_PROJECTION_MATRIX, (float*)Mp );
    if( fabsf( Mp[10] -1.0f ) < std::numeric_limits<float>::epsilon() ||
        fabsf( Mp[10] +1.0f ) < std::numeric_limits<float>::epsilon() ){
      return false; }
    
    float n = Mp[14]/( Mp[10] -1.0f );
    float f = Mp[14]/( Mp[10] +1.0f );
    
    sy = sy < 0 ? sy = -sy : height-sy;
    
    a3d::Matrix4f Mpi = Mp.inverse();
    
    a3d::Vector4f v0_4( 2.0f*sx/width -1.0f,
                        2.0f*sy/height -1.0f,
                        -1.0f, 1.0f );
    v0_4 = n * v0_4;
    v0_4 = Mpi * v0_4;
    v0_4 = Mmi * v0_4;
    
    a3d::Vector4f v1_4( 2.0f * sx/width -1.0f,
                        2.0f * sy/height -1.0f,
                        +1.0f, 1.0f );
    v1_4 = f * v1_4;
    v1_4 = Mpi * v1_4;
    v1_4 = Mmi * v1_4;
    
    x = a3d::Vector3<T,M>( v0_4[0], v0_4[1], v0_4[2] );
    v = a3d::Vector3<T,M>( v1_4[0] - v0_4[0],
                           v1_4[1] - v0_4[1],
                           v1_4[2] - v0_4[2] ).normalized();


----------------
KJ Lundin Palmerius
Norrköping Visualization and Interaction Studio
Linköping University, Sweden

erik
Posted on: 2006/7/14 11:28
Not too shy to talk
Joined: 2005/3/15
From: Uppsala
Posts: 12
Re: mouse to world
Thanks KJ, think I got it to work now. With this approach you of course have to implement some raycasting to compute intersection with your objects and in my case that is very simple.

/ Erik
karlu
Posted on: 2006/7/17 3:43
Guru
Joined: 2004/12/2
From: Sweden
Posts: 586
Re: mouse to world
Not at all! You may use the Z-value of the depth buffer. The Z-value describes the depth as a function of the near and far plane, so its position in global coordinates can easily be determined. Then all you have to do is a plane intersection, which is trivial. But then again, if you can make readily available tools work, such as the gluUnproject function, then that is always easier than to implement the tool yourself ;-)


----------------
KJ Lundin Palmerius<br />Norrköping Visualization and Interaction Studio<br />Linköping University, Sweden

mjavaid
Posted on: 2009/12/19 3:05
Not too shy to talk
Joined: 2008/12/23
From:
Posts: 15
Re: mouse to world
Hi KJ
Do you know about how to make such a conversion from window to world coordinates while working in Python. As I'm afraid one may not use OpenGl variables in Python.

Thanks
Maria
karlu
Posted on: 2010/1/3 20:20
Guru
Joined: 2004/12/2
From: Sweden
Posts: 586
Re: mouse to world
You will need to know the values of several OpenGL states to use in the calculations, so I'm afraid you need to use OpenGL anyhow. I'm not sure if calling OpenGL from a Python script would work but you could try it. The coordinate converting code is available here.


----------------
KJ Lundin Palmerius<br />Norrköping Visualization and Interaction Studio<br />Linköping University, Sweden

Threaded | Newest First Previous Topic | Next Topic | Top

Register To Post
 



(C) 2004 SenseGraphics AB    ---    Powered by XOOPS