gluProject Converting 3D coordinates to 2D coordinates does not convert the 2D Y Coordinate correctly

前端 未结 2 1730
失恋的感觉
失恋的感觉 2021-01-02 11:02

After two hours of googling (here, here, here, here, and here, and a ton others which I am not bothered to find), I thought I had finally learnt the theory of turning 3D coo

相关标签:
2条回答
  • 2021-01-02 11:52

    I found an answer to my issue!

    I used

    font.drawString(drawx - offset, drawy, (sh.username + " || " + drawx + " | " + drawy), Color.orange);
    

    When it should have been

    font.drawString(drawx - offset, Display.getHeight() - drawy, (sh.username + " || " + drawx + " | " + drawy), Color.orange);
    
    0 讨论(0)
  • 2021-01-02 11:56

    You have to use the same projection matrix in gluProject that you use for rendering your ship. In your case the ship is rendered using a perspective projection, but when you call gluProject a orthographic projection is used.

    General theory about coordinate systems in OpenGL

    In most cases geometry of a model in your scene (e.g. the ship) is given in a model-coordinate system. This is the space where your vertex coordinates exist. When now placing the model in your scene we apply the model-matrix to each vertex to get the coordinates the ship has in the scene. This coordinate system is called world space. When viewing the scene from a given viewpoint and a viewing direction, again a transformation is needed that transforms the scene such that the viewpoint is located in the origin (0,0,0) and view-direction is along the negativ z-axis. This is the view coordinate system. The last step is to transform view-coordinates into ndc, which is done via a projection matrix.

    In total we get the transformation of a vertex to the screen as:

     v_screen = Projection * View * Model * v_model
    

    In ancient OpenGL (as you use it) View and Model are stored together in the ModelView matrix.

    (I skipped here some problems as perspective divide, but it should be sufficient to understand the problem.)

    Your problem

    You already have a position in world space (x,y,z) of your ship. Thus the transformation with Model has already happend. What is left is

    v_screen = Projection * View * v_worldspace
    

    For this we see, that in our case the ModelView matrix that gets entered to gluProject has to be exactly the View matrix.

    I can't tell you where you get the view matrix in your code, since I don't know this part of your code.

    0 讨论(0)
提交回复
热议问题