opengl - Ray picking with gluUnProject() -
i have quad on y = -50 plane. @ moment, want obtain coordinates of mouse click on quad. i've managed limited extent. problem transformations applied when drawing quad aren't accounted for. can add in constants , make work, let user rotate scene x , y axes glrotatef(), coordinates messed rotation happens.
here's i'm doing now:
i call gluunproject() twice, once z = 0, , once z = 1.
gluunproject( mousex, window_height - mousey, 0, modelview, projection, viewport, &x1, &y1, &z1); gluunproject( mousex, window_height - mousey, 1, modelview, projection, viewport, &x2, &y2, &z2);
normalized ray vector:
x = x2 - x1; y = y2 - y1; z = z2 - z1; mag = sqrt(x*x + y*y + z*z); x /= mag; y /= mag; z /= mag;
parametric equation:
float t = -(camy) / y; planex = camx + t*x; planey = camy + t*y; planez = camz + t*z;
where (camx, camy, camz) camera position passed glulookat().
i want planex, planey, , planez coordinates of click on quad, in same coordinate system used draw quad. how can achieve this?
you not supposed pass in explicit z-depth of choosing. in order find world coordinate, need pass in depth buffer value @ particular mouse coordinate.
glfloat depth; glreadpixels(x, y, 1, 1, gl_depth_component, gl_float, &depth);
passing gluunproject should yield values looking for. plus, genpfault said in comment, make sure grabbing model view matrix data @ right moment. otherwise, have wrong matrix.
Comments
Post a Comment