Results 1 to 7 of 7

Thread: Screen to World Coordinates

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Screen to World Coordinates

    Hi.
    I have found code like this also under many C# C++ (and C-- ahh ahh)
    ---------------------------------------
    Code:
    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
    ---------------------------------------------
    Code:
    OpenGLMousePosition:=GetOpenGLPosFromMouse(DelphiMousePosition.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.
    Last edited by WILL; 30-11-2010 at 04:29 PM. Reason: Please use code tags to post code...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •