PDA

View Full Version : gluProject issues



IlovePascal
21-02-2007, 12:09 AM
Hello, I used the following procedure to get the screen coordinates of a point in the 3D matrix (with OpenGL)


// 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 : integer; 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;

But for some reason, it bugs on the last two lines. I don't understand why, since all we're doing is changing the values of the variables! I thought it might have to do with the rounding or something in there, so I changed those two lines to

screenX:=0;
screenY:=0;
and it still bugged...
Then I just commented them out and it worked fine (except that I don't get the correct output values, of course).
I don't understand what's going on...

Oh, and also, when objx,objy and objz are anything other than integers, i get the error
floating point overflow Isn't that weird, since coordinates should be reals or even GLdoubles ?

Cheers :wink:

Nitrogen
21-02-2007, 08:40 AM
I was just about to give you my tutorial on gluUnproject because of the title of the post!

IlovePascal
21-02-2007, 10:00 AM
I was just about to give you my tutorial on gluUnproject because of the title of the post!

Oops! sorry about that! Can WILL or any other staff change that to gluProject? Cheers ;)

And Nitrogen, I have already read and used your gluUnproject tutorial :wink: :thumbup:

grudzio
21-02-2007, 12:58 PM
I have looked in glu.pas and there is different ordering of parameters for gluProject. First should go modelview matrix (model) then projection matrix (proj). Probably this wrong ordering of matrices is causing errors.

IlovePascal
22-02-2007, 02:54 AM
I have looked in glu.pas and there is different ordering of parameters for gluProject. First should go modelview matrix (model) then projection matrix (proj). Probably this wrong ordering of matrices is causing errors.
Interesting! Because even swaping them around I get the same result!

But still, my main problem is with assigning the variables at the end of the procedure. I don't understand why it bugs there. It all goes well when those two last lines are commented out, but freezes the game when they are not!

Any ideas???

grudzio
22-02-2007, 11:50 AM
Can you post code showing how you use Matrix_To_Win function?

IlovePascal
23-02-2007, 10:11 PM
Can you post code showing how you use Matrix_To_Win function?
Ok, here is part of the procedure I call it in:


Procedure Where_Mouse;
var i, a,b,c : integer;
Winx,Winy, Winx2,Winy2 : integer;
s,t : string;
begin
SetMatrix_Edge; //Sets matrix at the top left corner of the board

a:=0; b:=0; c:=0;

for i:= 1 to 4 do //to find the coordinates of a vertex around the mouse
begin
Matrix_To_Win(a,0,b, WinX,WinY); //the coordinates of the vertex we're at

Matrix_To_Win(a+1,0,b, WinX2,WinY2); //the coordinates of the 'reference' vertex (to tell direction)
if AbsR(mpos.x-WinX)>AbsR(mpos.x-WinX2) then c:=1 //if mouse is closer to vertex a+1 than a,
else c:=-1;
if not (WinX=mpos.x) then //if mouse is at that point, then don't do anything
Repeat
a += c ; //else increase or decrease Xvertex depending on position of mouse relative to vertex
Matrix_To_Win(a,0,b, WinX,WinY);
Matrix_To_Win(a+1,0,b, WinX2,WinY2);
Until &#40;&#40;mpos.x<WinX>=WinX2&#41;&#41; or &#40;&#40;mpos.x>=WinX&#41;and&#40;mpos.x <WinX2>100&#41; ;

When grudzio asked about the procedure I call it in, I checked everything and added the last line

or &#40;Abs&#40;a&#41;>100&#41; ;
It made sense to me that the way it was would not make it an infinite loop, but because now I can write text to the screen =)) I used it to display the value of WinX and WinY for the origin

Matrix_To_Win&#40;0,0,0, WinX,WinY&#41;;
and I got WinX = 1786941367 and WinY = -10800336608 !

So now I understand that the problem was not in the Matrix_To_Win procedure, but rather in this loop. However, it only does that because the procedure Matrix_to_Win returns such ridiculous values.
Why does it return such values?

EDIT: The code above didn't show up the way it should have...it looks like there was some bug here :scratch:

grudzio
23-02-2007, 11:48 PM
OK, the enums passed to glGetDoublev in Matrix_To_win procedure are wrong. They should be GL_MODELVIEW_MATRIX and GL_PROJECTION_MATRIX.

IlovePascal
25-02-2007, 07:00 AM
OK, the enums passed to glGetDoublev in Matrix_To_win procedure are wrong. They should be GL_MODELVIEW_MATRIX and GL_PROJECTION_MATRIX.
Ok, I changed it. I get different values for WinX and WinY now, but it's still ridiculous ones... :(

here is a copy of the source, if it helps you help me :) :
http://www.uploadtemple.com/view.php/1172386230.zip

(Both the procedure Matrix_To_Win and Where_Mouse are located in MainProc.pas; the procedure SetMatrix_Edge used in there is at the bottom of SmallProc.pas :wink:)

grudzio
28-02-2007, 08:34 PM
Check variable declarations in Matrix_to_win. Winx, Winy and Winz should be declared as glDouble. Also procedure parameters objx, objy objz should be declared as glDoubles (just in case :D ).

IlovePascal
03-03-2007, 10:17 PM
Grudzio...Thanks!
I still don't get what i expected, but now the numbers make more sense, so i'll figure it out frm here :wink: