Calculating 3d Point

Blake
Unregistered
 
Post: #1
hey all, im having trouble calculating the ground point that i click on by using the mouse x,y and my cameras x,y,z and rotation x. The cameras rotation x is 60 and the camera's y is 10, any help would be VERY appreciated
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2007.02
Post: #2
Are you using OpenGL ? If so you can use gluUnproject(). There is a first rate example of how to use this here
Quote this message in a reply
Blake
Unregistered
 
Post: #3
Jason Colman Wrote:Are you using OpenGL ? If so you can use gluUnproject(). There is a first rate example of how to use this here

thanks for the reply but im still lost Wink
Quote this message in a reply
Apprentice
Posts: 7
Joined: 2007.02
Post: #4
You reveal nothing Ninja
If indeed you are using OpenGL, you need look no further than the last 10 lines of that code sample.
You have much to learn young padawan etc Smile
Quote this message in a reply
Blake
Unregistered
 
Post: #5
okay i got this

GLdouble ox=0.0,oy=0.0,oz=0.0;
GLint viewport[4];
GLdouble modelview[16],projection[16];
GLfloat wx=x,wy,wz;

glGetIntegerv(GL_VIEWPORT,viewport);
y=viewport[3]-y;
wy=y;
glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
glGetDoublev(GL_PROJECTION_MATRIX,projection);
glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&wz);
gluUnProject(wx,wy,wz,modelview,projection,viewport,&ox,&oy,&oz);

but what does each line do? Sneaky
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #6
Let's work it backwards. You want to convert a click on-screen to a position in your 3D-world.

The last line, gluUnproject does this for you. However, it needs four things:
The click in 3D-space
The model view matrix
The projection matrix.
The viewport.

So, how the heck do we convert a click on the 2D screen into a click in 3-space? Well, we just take the depth buffer value at the point where you clicked. The glReadPixels line does that for you. Now, we have wx, wy, and wz, which represents the click in three dimensions. So far, so good.

Then, we need the viewport, model view matrix and the projection matrix. The glGetDoublev and glGetIntegerv calls does that for us. So, we have the ingredients for the magic formula. Pass them all to gluUnproject, and it'll spit out the 3D coordinates in your scene in the three last arguments, ox, oy, oz.

So, in total, this function becomes:
Code:
void Make2DPositionInto3DPosition (float x, float y, float *ox, float *oy, float *oz)
{
     GLint viewport[4];
     GLdouble modelview[16],projection[16];
     GLfloat wx=x,wy,wz;

     glGetIntegerv(GL_VIEWPORT,viewport);
     y=viewport[3]-y;
     wy=y;
     glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
     glGetDoublev(GL_PROJECTION_MATRIX,projection);
     glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&wz);
     gluUnProject(wx,wy,wz,modelview,projection,viewport,&ox,&oy,&oz);
}
In short, to use this little function, just do this:

Code:
Point mousePos;
float click3DPos [3];
GetMouseLoc (&mousePos);
Make2DPositionInto3DPosition (mousePos.x, mousePos.y, &click3DPos[0], &click3DPos[1], &click3DPos[2]);

// Use click3DPos to draw something at that position
Quote this message in a reply
Blake
Unregistered
 
Post: #7
okay this is the code that i have

Code:
NSPoint mouseLoc = [ self currentMousePosition ];
    [ self make2DPositionInto3DPosition:mouseLoc.x y:mouseLoc.y ox:&mouse3DPositions[ 0 ] oy:&mouse3DPositions[ 1 ] oz:&mouse3DPositions[ 2 ] ];
    glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
    glBegin( GL_QUADS );
    glVertex3d( 0, 0, 0 );
    glVertex3d( mouse3DPositions[ 0 ], 0, 0 );
    glVertex3d( mouse3DPositions[ 0 ], 0, mouse3DPositions[ 2 ] );
    glVertex3d( 0, 0, mouse3DPositions[ 2 ] );
    glEnd();

Code:
GLint viewport[4];
     GLdouble modelview[16],projection[16];
     GLfloat wx=x,wy,wz;

     glGetIntegerv(GL_VIEWPORT,viewport);
     y=viewport[3]-y;
     wy=y;
     glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
     glGetDoublev(GL_PROJECTION_MATRIX,projection);
     glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&wz);
     gluUnProject(wx,wy,wz,modelview,projection,viewport,&ox,&oy,&oz);

but i keep getting "Warning: pass agr 7-9 (7, 8 and 9) of 'gluUnProject' from incompatable pointer type" am i just missing something?

Edit: ive tried float and GLfloat for mouse3DPositions[ 3 ]
Quote this message in a reply
Blake
Unregistered
 
Post: #8
anybody know whats wrong?
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #9
Looks like the error is caused by the ox, oy, oz parameters to gluUnProject(). How are they defined?

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Blake
Unregistered
 
Post: #10
i have tried them as float mouse3DPositions[ 3 ] and GLfloat mouse3DPositions[ 3 ]
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #11
Blake Wrote:i have tried them as float mouse3DPositions[ 3 ] and GLfloat mouse3DPositions[ 3 ]
Eh? But your code says this:

Code:
gluUnProject(wx, wy, wz, modelview, projection, viewport, &ox, &oy, &oz);
Where are ox, oy, and oz coming from?

EDIT: I've just spotted this line, but I don't understand it:

Code:
[ self make2DPositionInto3DPosition:mouseLoc.x y:mouseLoc.y
    ox:&mouse3DPositions[ 0 ] oy:&mouse3DPositions[ 1 ] oz:&mouse3DPositions[ 2 ] ];
What does make2DPositionInto3DPosition look like?

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Blake
Unregistered
 
Post: #12
Code:
- (void) make2DPositionInto3DPosition:(float)x y:(float)y ox:(GLfloat *)ox oy:(GLfloat *)oy oz:(GLfloat *)oz
{
     GLint viewport[4];
     GLdouble modelview[16],projection[16];
     GLfloat wx=x,wy,wz;

     glGetIntegerv(GL_VIEWPORT,viewport);
     y=viewport[3]-y;
     wy=y;
     glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
     glGetDoublev(GL_PROJECTION_MATRIX,projection);
     glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&wz);
     gluUnProject(wx,wy,wz,modelview,projection,viewport,&ox,&oy,&oz);
}

Code:
[ self make2DPositionInto3DPosition:mouseLoc.x y:mouseLoc.y ox:&mouse3DPositions[ 0 ] oy:&mouse3DPositions[ 1 ] oz:&mouse3DPositions[ 2 ] ];
Quote this message in a reply
Moderator
Posts: 365
Joined: 2002.04
Post: #13
Two things:

gluUnProject() is defined like this...

Code:
int gluUnProject( GLdouble winx, GLdouble winy, GLdouble winz,
    const GLdouble modelMatrix[16], const GLdouble projMatrix[16], const GLint viewport[4],
    GLdouble *objx, GLdouble *objy, GLdouble *objz );
Notice that objx, objy and objz are GLdoubles, not GLfloats! You can implicitly cast a float to a double, but you can't make a double pointer point at a float - you'll need to pass actual GLdoubles for those parameters.

Secondly (and I could be wrong here because I'm not good at reading Objective-C), it looks like you're trying to pass the address of the ox, oy and oz pointers to gluUnProject(). Since ox, oy and oz are already pointers to floats (or doubles, rather Wink ), you don't need the ampersands:

Code:
gluUnProject(wx, wy, wz, modelview, projection, viewport, ox, oy, oz);

Neil Carter
Nether - Mac games and comic art
Quote this message in a reply
Blake
Unregistered
 
Post: #14
yea removing the &'s fixed it, now i have it in 3d space and now the math begins...
Quote this message in a reply
Blake
Unregistered
 
Post: #15
okay im stuck now, i have the positions of the mouse in the 3d world but now how do i convert this to the ground? the camera sits 10 above the ground and is angled at 60 degrees on its x axis

any advice?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  calculating X and Y coordinates w/ an angle and distance ferum 13 14,885 Jun 25, 2008 10:53 PM
Last Post: rosenth
  Calculating relative transformations TomorrowPlusX 3 2,800 Jul 11, 2007 06:15 PM
Last Post: Skorche
  Calculating direction of car from normal vector and angle spinner 1 3,236 Oct 21, 2006 10:43 AM
Last Post: imikedaman
  Trouble Calculating Height Field Normals Nick 15 6,066 Jul 21, 2005 07:27 AM
Last Post: Nick
  Am I calculating the dot-product correctly? sealfin 8 4,335 Jul 13, 2005 04:02 PM
Last Post: Skorche