Calculating 3d Point
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
Are you using OpenGL ? If so you can use gluUnproject(). There is a first rate example of how to use this here
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
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
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
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?
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?
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:
In short, to use this little function, just do this:
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);
}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
okay this is the code that i have
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 ]
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 ]
Looks like the error is caused by the ox, oy, oz parameters to gluUnProject(). How are they defined?
i have tried them as float mouse3DPositions[ 3 ] and GLfloat mouse3DPositions[ 3 ]
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);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 ] ];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 ] ];
Two things:
gluUnProject() is defined like this...
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:
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 );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);
yea removing the &'s fixed it, now i have it in 3d space and now the math begins...
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| calculating X and Y coordinates w/ an angle and distance | ferum | 13 | 12,672 |
Jun 25, 2008 10:53 PM Last Post: rosenth |
|
| Calculating relative transformations | TomorrowPlusX | 3 | 2,535 |
Jul 11, 2007 06:15 PM Last Post: Skorche |
|
| Calculating direction of car from normal vector and angle | spinner | 1 | 2,778 |
Oct 21, 2006 10:43 AM Last Post: imikedaman |
|
| Trouble Calculating Height Field Normals | Nick | 15 | 5,411 |
Jul 21, 2005 07:27 AM Last Post: Nick |
|
| Am I calculating the dot-product correctly? | sealfin | 8 | 4,027 |
Jul 13, 2005 04:02 PM Last Post: Skorche |
|

