DGL
https://delphigl.com/forum/

gluUnProject and FreePascal
https://delphigl.com/forum/viewtopic.php?f=19&t=3151
Seite 1 von 1

Autor:  BTierens [ Fr Aug 13, 2004 17:51 ]
Betreff des Beitrags:  gluUnProject and FreePascal

Hi,

I'm using the FreePascal compiler (http://www.freepascal.org/). I have a tile based landscape with objects and now I want to know at wich tile/object the user clicks. First I use gluUnProject to get the 3D coordinates and then, I use a tree to know on wich tile/object the user clicks. But the gluUnProject function has strange output.
This is the code:

Code:
  1.  
  2. //************************************************************************************************
  3. //Get the tile wich is selected
  4. //************************************************************************************************
  5. function GetOGLPos(X, Y: Integer): T3D_Point;
  6. var
  7.   viewport: TViewPortArray;
  8.   modelview:  T16dArray;
  9.   projection: T16dArray;
  10.   winZ: Single;
  11.   resPoint: T3D_Point;
  12. begin
  13.   glGetDoublev(GL_MODELVIEW_MATRIX, @modelview );
  14.   glGetDoublev(GL_PROJECTION_MATRIX, @projection );
  15.   glGetIntegerv(GL_VIEWPORT, @viewport );
  16.  
  17.   // In Delphi A Y Value Of 0 Returns An Unknown Value
  18.   // I Discovered This While I Was Testing A Crosshair
  19.   if(Y = 0) then Y := 1;
  20.   glReadPixels(X,-Y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,@winZ);
  21.   gluUnProject(X,viewport[4]-Y,winZ,modelview,projection,viewport,@resPoint[0],@resPoint[1],@resPoint[2]);
  22.   writeLn(floatToStr(resPoint[0]) + ' , ' + floatToStr(resPoint[1]) + ' , ' + floatToStr(resPoint[2]));
  23.   Result := resPoint;
  24. end;
  25.  


I got results like 0.01268 for x, 1085,36 for y and -0,099 for z. All my tiles are 1x1 and the result doesn't change to much. The y-value (1085,36) is much too high.
Does somebody knows what the problem is?

Autor:  Sascha Willems [ Fr Aug 13, 2004 18:00 ]
Betreff des Beitrags: 

How does T3D_Point look? You must declare the return values for gluUnproject as doubles, or else you will get bogus results. I guess there lies your problem, as your T3D_Point most likely consists of singles. So change that to doubles and try again.

Autor:  BTierens [ Fr Aug 13, 2004 18:08 ]
Betreff des Beitrags: 

No, T3D_Point is an array of gldouble. I get the same result if I change gldouble by double.


Zitat:
T3D_Point = Array[0..2] of gldouble;

Autor:  Sascha Willems [ Fr Aug 13, 2004 18:17 ]
Betreff des Beitrags: 

I just saw this (didn't look close before my last post) :

Code:
  1. glReadPixels(X,-Y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,@winZ);


The -Y is wrong in there, and should just be Y.

Autor:  BTierens [ Fr Aug 13, 2004 18:26 ]
Betreff des Beitrags: 

Now I get other output but now, most of the times the y value is 2170779,42944. I based my code on Delphi code from http://nehe.gamedev.net/data/articles/article.asp?article=13 wich was based on C++ code on the same page.

Autor:  BTierens [ Fr Aug 13, 2004 18:59 ]
Betreff des Beitrags: 

I changed the code a bit so the code is more similar to the C++ code.

Code:
  1.  
  2. function GetOGLPos(X, Y: integer): T3D_Point;
  3. var
  4.   viewport: TViewPortArray;
  5.   modelview:  T16dArray;
  6.   projection: T16dArray;
  7.   winZ: single;
  8.   resPoint: T3D_Point;
  9. begin
  10.   glGetDoublev(GL_MODELVIEW_MATRIX, @modelview );
  11.   glGetDoublev(GL_PROJECTION_MATRIX, @projection );
  12.   glGetIntegerv(GL_VIEWPORT, @viewport );
  13.  
  14.   // In Delphi A Y Value Of 0 Returns An Unknown Value
  15.   // I Discovered This While I Was Testing A Crosshair
  16.   if(Y = 0) then Y := 1;
  17.   glReadPixels(X,round(viewport[3]-Y),1,1,GL_DEPTH_COMPONENT,GL_FLOAT,@winZ);
  18.   gluUnProject(X,viewport[3]-Y,winZ,modelview,projection,viewport,@resPoint[0],@resPoint[1],@resPoint[2]);
  19.   writeLn(floatToStr(resPoint[0]) + ' , ' + floatToStr(resPoint[1]) + ' , ' + floatToStr(resPoint[2]));
  20.   Result := resPoint;
  21. end;
  22.  


I don't get values that are too high anymore but I don't think the values are correct. I've attached a screenshot to show the problem.

Dateianhänge:
Dateikommentar: You can see tree cursors with the value of the getOGLPos function. In the center, you see the size of one tile (1x1).
screenshot.jpg
screenshot.jpg [ 82.99 KiB | 6193-mal betrachtet ]

Autor:  Sascha Willems [ Fr Aug 13, 2004 20:11 ]
Betreff des Beitrags: 

Code:
  1. glReadPixels(X,round(viewport[3]-Y),1,1,GL_DEPTH_COMPONENT,GL_FLOAT,@winZ)


Why that? glReadPixels(X, Y, ...) should be correct. In opposition to gluUnproject, die Origin for glReadPixels lies in the upper/left corner, so you don't need to subtract Y from the bottom of the viewport.

Autor:  BTierens [ Sa Aug 14, 2004 07:29 ]
Betreff des Beitrags: 

The reference page from msdn.microsoft.com says this:
Zitat:
x, y
The window coordinates of the first pixel that is read from the framebuffer. This location is the lower-left corner of a rectangular block of pixels.

Autor:  BTierens [ Sa Aug 14, 2004 08:57 ]
Betreff des Beitrags: 

It seems to work now. Thanks for your help.

Autor:  Phobeus [ Sa Aug 14, 2004 13:54 ]
Betreff des Beitrags: 

btw: Interesting to see that someone is using freepascal for a project. Please feel free to keep us informed about the progress. I will be interested in ;)

Autor:  BTierens [ Sa Aug 14, 2004 15:40 ]
Betreff des Beitrags: 

I will do that.

Seite 1 von 1 Alle Zeiten sind UTC + 1 Stunde
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/