Search code examples
c#openglprojectionopentk

OpenTK orthographic projection with left top origin


How do I setup openTK so I get a orthographic projection where:

  • The origin is in the top left corner of the screen
  • I can use "normal" pixel coordinates, for instance: if my window is 500 X 400 then:
    • 0,0 is the top left corner
    • 500,0 is the top right corner
    • 500,400 is the bottom right corner
    • 0,400 is the bottom left corner

I currenly have this:

_projectionMatrix = Matrix4.CreateOrthographicOffCenter(
    ClientRectangle.X, ClientRectangle.Width,
    ClientRectangle.Y, ClientRectangle.Height, -1.0f, 1.0f);

I'm not fully able to understand what's happening but is seems the origin is now in the bottom left, also I don't know if the coordinates match with the pixels on screen.


Solution

  • The parameters to Matrix4.CreateOrthographicOffCenter are the left, right, bottom, top, near, and far of the cuboid view volume.

    If the origin of the view (ClientRectangle.Y) has to be at the top, then you've to swap the top and bottom parameter:

    _projectionMatrix = Matrix4.CreateOrthographicOffCenter(
        ClientRectangle.X, 
        ClientRectangle.X + ClientRectangle.Width,
        ClientRectangle.Y + ClientRectangle.Height, 
        ClientRectangle.Y,
        -1.0f, 1.0f);