Calculating 3d Point

Apprentice
Posts: 7
Joined: 2007.02
Post: #16
The 3D coord you have got corresponds to the point where you click with the mouse. So if you click on the ground, the 3D coord will be on the ground too! And this will work however your camera is oriented!
Quote this message in a reply
Oldtimer
Posts: 832
Joined: 2002.09
Post: #17
And if you really need to snap that position down onto the ground, then just set the Y coordinate to whatever height the ground is at the 3D point's XZ position.
Quote this message in a reply
Blake
Unregistered
 
Post: #18
well it seems as if the cords that it is giving me is like its just putting the mouse right in front of the camera lens (so to speak)
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2009.01
Post: #19
What you need to do is trace a ray from the screen to the ground plane. This is what I'm doing in Beach Pong. (for full source see the uDevGame source thread)

Code:
ray_t getRayFromScreenCoords(int sx, int sy, int sw, int sh) {

    ray_t ray;
    GLdouble modelview[16], projection[16];
    GLdouble x, y, z;
    GLint viewport[4];

    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);
    glGetIntegerv(GL_VIEWPORT, viewport);

    gluUnProject(sx, sh - sy, 0.0,
              modelview, projection, viewport,
              &x, &y, &z);

    ray.origin = vvector( (float)x, (float)y, (float)z );

    gluUnProject(sx, sh - sy, 1.0,
              modelview, projection, viewport,
              &x, &y, &z);

    ray.direction.x = x - ray.origin.x;
    ray.direction.y = y - ray.origin.y;
    ray.direction.z = z - ray.origin.z;

    vnormalize(&ray.direction);

    return ray;
}

float intersectPlane(plane_t* p, ray_t* r) {
    float d = vdot(p->normal, r->direction);
    if (d != 0) {
        float t = vdot(p->position, p->normal) - vdot(r->origin, p->normal) / d;
        if (t > 0.0001f) return t;
    }
    return -1.0;
}


vec_t getPickingPoint(int x, int y) {
    vec_t picking;
    plane_t plane;
    float t;
    ray_t ray;
    
    plane.position = vvector(0, 0, 0);
    plane.normal = vvector(0, 1, 0);
    
    ray = getRayFromScreenCoords(x, y, getScreenWidth(), getScreenHeight());
    
    t = intersectPlane(&plane, &ray);
    picking.x = ray.origin.x + (ray.direction.x * t);
    
    picking.y = ray.origin.y + (ray.direction.y * t);
    picking.z = ray.origin.z + (ray.direction.z * t);
    
    return picking;
}


With this method you can easily pick spheres, triangles, etc too.

.johan
Quote this message in a reply
Oldtimer
Posts: 832
Joined: 2002.09
Post: #20
Quote:well it seems as if the cords that it is giving me is like its just putting the mouse right in front of the camera lens (so to speak)


Then you're misinterpreting the Z value... Hang on, I'll check my sources.

Code:
void bmMouse::GetClick3D (GLdouble *x, GLdouble *y, GLdouble *z)
{
    GLdouble modelView [16];
    GLdouble projection[16];
    GLint viewPort  [4];
    float outZ;
    
    glReadBuffer(GL_FRONT);

    glGetDoublev(GL_MODELVIEW_MATRIX, modelView);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);
    glGetIntegerv(GL_VIEWPORT, viewPort);
    
    glReadPixels(this->mouseX, this->mouseY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &outZ);
    gluUnProject(this->mouseX, this->mouseY, outZ, modelView, projection, viewPort, x, y, z);
}


Make sure that outZ is a float. Also, make sure you enable depth writes and all that. What is the Z value you get back from glReadPixels if you check it in the debugger?
Quote this message in a reply
Blake
Unregistered
 
Post: #21
Fenris Wrote:Then you're misinterpreting the Z value... Hang on, I'll check my sources.

Code:
void bmMouse::GetClick3D (GLdouble *x, GLdouble *y, GLdouble *z)
{
    GLdouble modelView [16];
    GLdouble projection[16];
    GLint viewPort  [4];
    float outZ;
    
    glReadBuffer(GL_FRONT);

    glGetDoublev(GL_MODELVIEW_MATRIX, modelView);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);
    glGetIntegerv(GL_VIEWPORT, viewPort);
    
    glReadPixels(this->mouseX, this->mouseY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &outZ);
    gluUnProject(this->mouseX, this->mouseY, outZ, modelView, projection, viewPort, x, y, z);
}


Make sure that outZ is a float. Also, make sure you enable depth writes and all that. What is the Z value you get back from glReadPixels if you check it in the debugger?

thanks a ton, i think that i wasnt doing glReadPixels and thats what was causing it to be all funny
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,457 Jun 25, 2008 10:53 PM
Last Post: rosenth
  Calculating relative transformations TomorrowPlusX 3 2,742 Jul 11, 2007 06:15 PM
Last Post: Skorche
  Calculating direction of car from normal vector and angle spinner 1 3,140 Oct 21, 2006 10:43 AM
Last Post: imikedaman
  Trouble Calculating Height Field Normals Nick 15 5,908 Jul 21, 2005 07:27 AM
Last Post: Nick
  Am I calculating the dot-product correctly? sealfin 8 4,260 Jul 13, 2005 04:02 PM
Last Post: Skorche