atcBrambo
30-11-2010, 02:16 PM
Hi.
I have found code like this also under many C# C++ (and C-- ahh ahh)
---------------------------------------
function GetOpenGLPosFromMouse(X, Y: Integer): TGLVectord3;
var
viewport: TGLVectori4;
modelview: TGLMatrixd4;
projection: TGLMatrixd4;
winZ,winY: Single;
begin
glGetDoublev(GL_MODELVIEW_MATRIX, @modelview ); // Modelview
glGetDoublev(GL_PROJECTION_MATRIX, @projection ); // Projection
glGetIntegerv(GL_VIEWPORT, @viewport ); // Viewport
winY := viewport[3] - y; //Change from Win32 to OpenGL coordinate system
glReadPixels(X, Round(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, @winZ );
gluUnProject(X, winY, winZ,
modelview, projection, viewport,
@Result[0], @Result[1], @Result[2]);
end;---------------------------------------
But it doesn't work; in fact I tested with this
---------------------------------------------
OpenGLMousePosition:=GetOpenGLPosFromMouse(DelphiM ousePosition.X,DelphiMousePosition.Y);
glDisable(GL_DEPTH_TEST);
Main_Form.Caption:= 'Mouse Position is '+
'x..:'+IntToStr(DelphiMousePosition.X)+' '+
'y..:'+IntToStr(DelphiMousePosition.y)+' '+
' OpenGl Mouse '+
'x..:'+FormatFloat('#,##0.0;-#,##0.0;0.0',OpenGLMousePosition[0])+' '+
'y..:'+FormatFloat('#,##0.0;-#,##0.0;0.0',OpenGLMousePosition[1])+' '+
'z..:'+FormatFloat('#,##0.0;-#,##0.0;0.0',OpenGLMousePosition[2])+' ';
glColor3d(1.0,1.0,0.0);
glLineWidth(3);
glBegin(GL_LINES);
glVertex3d( 0, 0, 0);
glVertex3d( OpenGLMousePosition[0],OpenGLMousePosition[1],OpenGLMousePosition[2]);
glEnd();
------------------------------------------------------------------------------------
while moving the mouse the line goes somewhere else. Occasionally near (maybe about 40 pixels away) the mouse.
Did someone already faced the headache ?
JPG sample cannot be upload .... too large.
I will be happy to send it.
Giovanni.
Italy.
I have found code like this also under many C# C++ (and C-- ahh ahh)
---------------------------------------
function GetOpenGLPosFromMouse(X, Y: Integer): TGLVectord3;
var
viewport: TGLVectori4;
modelview: TGLMatrixd4;
projection: TGLMatrixd4;
winZ,winY: Single;
begin
glGetDoublev(GL_MODELVIEW_MATRIX, @modelview ); // Modelview
glGetDoublev(GL_PROJECTION_MATRIX, @projection ); // Projection
glGetIntegerv(GL_VIEWPORT, @viewport ); // Viewport
winY := viewport[3] - y; //Change from Win32 to OpenGL coordinate system
glReadPixels(X, Round(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, @winZ );
gluUnProject(X, winY, winZ,
modelview, projection, viewport,
@Result[0], @Result[1], @Result[2]);
end;---------------------------------------
But it doesn't work; in fact I tested with this
---------------------------------------------
OpenGLMousePosition:=GetOpenGLPosFromMouse(DelphiM ousePosition.X,DelphiMousePosition.Y);
glDisable(GL_DEPTH_TEST);
Main_Form.Caption:= 'Mouse Position is '+
'x..:'+IntToStr(DelphiMousePosition.X)+' '+
'y..:'+IntToStr(DelphiMousePosition.y)+' '+
' OpenGl Mouse '+
'x..:'+FormatFloat('#,##0.0;-#,##0.0;0.0',OpenGLMousePosition[0])+' '+
'y..:'+FormatFloat('#,##0.0;-#,##0.0;0.0',OpenGLMousePosition[1])+' '+
'z..:'+FormatFloat('#,##0.0;-#,##0.0;0.0',OpenGLMousePosition[2])+' ';
glColor3d(1.0,1.0,0.0);
glLineWidth(3);
glBegin(GL_LINES);
glVertex3d( 0, 0, 0);
glVertex3d( OpenGLMousePosition[0],OpenGLMousePosition[1],OpenGLMousePosition[2]);
glEnd();
------------------------------------------------------------------------------------
while moving the mouse the line goes somewhere else. Occasionally near (maybe about 40 pixels away) the mouse.
Did someone already faced the headache ?
JPG sample cannot be upload .... too large.
I will be happy to send it.
Giovanni.
Italy.