This procedure does not work because OpenGL functions do not allocate memory to store results. Although the specs says that glGetDoublev expects PGLDouble as a parameter (PGDouble - pointer to a double type) but actually it expects an array of doubles big enough to store the result of the query.
Pascal versions of glu(Un)Projects expects specific data types which are defined in glu.pas.
Here is a corrected version of your Matrix_to_win procedure
Code:
// objx,objy,objz - 3D location of a point
// screenX, screenY - will hold screen coordinates of (objx,objy,objz)
procedure Matrix_To_Win(objx,objy,objz : gldouble; var screenX : integer, var screenY : integer);
var
model, proj : T16dArray; //type defined in glu.pas
view : TViewportArray; //also defined in glu.pas
winx,winy,winz : glDouble;
begin
glGetDoublev(GL_PROJECTION, @proj);
glGetDoublev(GL_MODELVIEW, @model);
glGetIntegerv(GL_VIEWPORT,@view);
gluProject(objx,objy,objz, proj, model, view, @winx, @winy, @winz);
screenX := Round(winx);
//since OpenGL treats lower left corner as a (0,0) winy must be
//transformed to normal window coordinates with (0,0) at upper left
//corner
screenY := ScreenHeight - Round(winy);
end;
The z coordinate is a depth value. It is put into depth buffer and is used for depth sorting.
I think that gluProject may return coordinates that are outside the screen.
P. S. If you use FPC and gluProject crashes your application, look at this thread http://www.pascalgamedevelopment.com...pic.php?t=3636
Bookmarks