PDA

View Full Version : Selecting vs. gluUnProject for getting poly/vertex



Lightning
22-09-2005, 07:13 AM
I was wondering if there is another (fast) way of knowing on wich tile of a terrain i click with the mouse.
The problem is this, i have a Heightmap file from a game wich is let's say 200x150 i convert it to vertices and it shows fine in GL only i need to move/rotate it around, after all those operations i will have to calculate the original heightmap coordinate, i did manage to do this by using selection in GL but it's too slow on a 500 MHZ machine with an MX440 card, selection requires drawing one frame in selection mode and i have to assign some names(numbers) to every tile wich takes time and selection is not fast enough. I tried gluUnProject(wich is verry fast) but it gives me wrong coordinates, do you know how to obtain at least the initial tile coordinates i pass to GL ? I can calculate the exact tile in the heightmap.
You may wonder why do i need this, well it's for a terrain editor wich i made some time ago but it's too slow and working with it would be quite annoying, gluUnProject would be nice but it gives me the current coordinate and i need to know the exact tile/vertex i'm clicking on to be able to modify the terrain.
It would also be nice if i could know wich tile without drawing all tiles if the camera is not verry close and the user probably doesn't need exact coordinate.

Paulius
22-09-2005, 01:51 PM
Trace a ray from eye to mouse position and check collisions with planes created for all vertices in a way that a vertex is on the plane and the plane is perpendicular to eye position.

Quadrance:= Infinity;
Normal:= normalize(Ray.Origin - Vertex.Pos);
a:= dot(Ray.dir, Normal);
if (a < 0) then
begin
Dist:= Length(Ray.Origin - Vertex.Pos);
t:= ( -dot(Ray.origin, Normal) + Dist) / a;
PlaneHit:= Ray.Origin + Ray.Direction * t;
Quadrance:= DistanceSquared(Vertex.Pos, PlaneHit);
end;

The vertex whose position is closest to its plane hit point will be the one you want. You might also check for smallest t if that distance is similar among a couple of vertices, so the closest one to the viewer gats selected. Use gluUnProject twice with winz 1 and 0 and subract to get rays direction.