## 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
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
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
Apprentice
Posts: 7
Joined: 2007.02
Post: #4
You reveal nothing
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
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);
gluUnProject(wx,wy,wz,modelview,projection,viewport,&ox,&oy,&oz);

but what does each line do?
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);
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
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 );
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);
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 ]
Blake
Unregistered

Post: #8
anybody know whats wrong?
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
Blake
Unregistered

Post: #10
i have tried them as float mouse3DPositions[ 3 ] and GLfloat mouse3DPositions[ 3 ]
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
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);
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 ] ];
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 ), 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
Blake
Unregistered

Post: #14
yea removing the &'s fixed it, now i have it in 3d space and now the math begins...
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