PDA

View Full Version : gluUnproject



{MSX}
23-04-2005, 02:36 PM
Hi, i've spotted this code around:

{------------------------------------------------------------------}
{ Get the vector for the current mouse position }
{------------------------------------------------------------------}
function GetOpenGLPos(X, Y: Integer): TVector;
var
viewport: TViewPortArray;
modelview: T16dArray;
projection: T16dArray;
winZ,winY: Single;
begin
glGetDoublev(GL_MODELVIEW_MATRIX, @modelview[0] ); // Get the Current Modelview matrix
glGetDoublev(GL_PROJECTION_MATRIX, @projection[0] ); // Get the Current Projection Matrix
glGetIntegerv(GL_VIEWPORT, @viewport[0] ); // Get the Current Viewport

winY := viewport[3] - y; //Change from Window to OpenGL coordinate system

glReadPixels(X, Round(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, @winZ );//Read the Depth value at the current X and Y position
gluUnProject(X, winY, winz, modelview, projection, viewport, // Get the vector for the current mouse position
@(Result.x), @(Result.y), @(Result.z)); // And Return it from the function
end;


But it gives AV when i run it..
Any of you is using gluUnproject ? Thanks.
This picking stuff is driving me mad :D

savage
23-04-2005, 06:06 PM
Where does the AV happen? Can you post a simple project that shows the AV?

Bart
24-04-2005, 06:24 AM
This is how I use gluUnproject:

type
T3D_Point = Array[0..2] of gldouble;
function GetWorldPos(X,Y: integer):T3D_Point;
var
viewport: TViewPortArray;
modelview: T16dArray;
projection: T16dArray;
winZ: single;
resPoint: T3D_Point;
begin
glGetDoublev(GL_MODELVIEW_MATRIX,@modelview);
glGetDoublev(GL_PROJECTION_MATRIX,@projection);
glGetIntegerv(GL_VIEWPORT,@viewport);

if(Y = 0) then Y := 1;
glReadPixels(X,round(viewport[3]-Y),1,1,GL_DEPTH_COMPONENT,GL_FLOAT,@winZ);
gluUnProject(X,viewport[3]-Y,winZ,modelview,projection,viewport,@resPoint[0],@resPoint[1],@resPoint[2]);
Result := resPoint;
end;