   Little problem with gluUnproject.
  Im creating map editor and have some problems with gluUnproject.Well getting co-ordinates from my scene works fine but still with X,Y co-ordinates i have little problem.Click on url to see my problem.http://img30.imageshack.us/i/problemgluun.jpg/Game map is 256*256*8. Im getting X,Y,Z coordinates for each block (64*64) on map.Example block on screenshot has Y = 4 if i click on the one side of the block. Value is correct.But if i click another side of block, then Y increases by one. So Y becomes 5.But this is not correct, it must be also 4, since block still stays on Y 4.Why it adds 1 to Y co-ordinates? And just on the specific sides?I have same problem with X at one side. Adds one to X, i don't understand why.Im using Delphi and my code is:// just 3D point with X,Y,Z coords.type TPoint3D=record    Z, X, Y: Double;  end; viewport:   array [1..4]  of Integer;modelview:  array [1..16] of Double;projection: array [1..16] of Double;  winZ: Single;  Tmp:record    X,Y,Z: Double;    end;begin  glGetDoublev(GL_MODELVIEW_MATRIX,@modelview);  glGetDoublev(GL_PROJECTION_MATRIX,@projection);  glGetIntegerv(GL_VIEWPORT,@viewport);  if(Y=0)then Y:=1;    glReadPixels(X, (viewport[4]-y-1), 1,1, GL_DEPTH_COMPONENT, GL_FLOAT,@winz);  gluUnProject(X, (viewport[4]-y-1), winZ, @modelview, @projection, @viewport,    tmp.X,    tmp.Z,    tmp.Y);Here is a calculation how i get the X,Y:X = X /64 * 64Y = Y /64 * 64Does somebody have a idea what im doing wrong?It seems like very little bug, but i don't know why it adds 1 to my X,Y?I have this problem only with sides.EDIT: if i remove -1 fromgluUnProject(X, (viewport[4]-y)then this solves problem with one side. The other still remains.And if i remove -1 from glReadPixels(X, (viewport[4]-y) thenthe previously solved bug comes back.Strange.Problem with glReadPixels??[Edited by - BytePtr on June 16, 2009 8:15:17 AM]
  I didn't thoroughly read your code but it might be a precision problem. The right side might be well within the range for Y so Y = Y /64 * 64 would give 4 (e.g. Y is between 16384.0 and 20480.0). On the right side Y seems to be at the edge between 4 and 5 (maybe slightly bigger than 20480.0) and thus the result is 5.   ;  So this is problem with calculations in my code?The whole scene moves around also: zoom, rotate, top down, but it doesn't change anything.What should i check then? Any ideas?   ;  I just spotted an error in my post: the range for Y = 4 would be [256.0,320.0[, correct?The problem is that with blocks and glReadPixels you generally only pick on the boundary of a cube. So your points would end up on the plane between two blocks and thus it would be hard to decide which side to assign the point to (which would depend on the scene, rotation etc.).However, since you'd normally only be able to pick blocks if they face the camera a trick might work: for unprojection add a small offset to the pixel depth so that the unprojected position is moved slightly into the block. It won't work correctly on the edges (depending on how big you choose that offset) but it should in the middle of the blocks' faces.Another option might be to use OpenGL selection mode and use appropriate object names.Or you might do it all yourself, i.e. shoot a ray into the world and see which block it intersects first. This should work well as long as the blocks contain - well, blocks - since then you'd only have to find the first non-empty block that is crossed.   