Search code examples
c#openglmatrixgraphicsopentk

Proper method of creating a Side-On orthographic view matrix


I'm trying to create an Orthographic view matrix viewing the scene from the scene (90 degree angle from a top-down view).

This is my current code attempting that:

        ortho = Matrix4.CreateOrthographicOffCenter(-500f / side_zoom + (float)side_translate.X, 500f / side_zoom + (float)side_translate.X,
            500f / side_zoom + (float)side_translate.Y, -500f / side_zoom + (float)side_translate.Y, -100000, 100000) * Matrix4.CreateRotationX(90);

Basically, CreateOrthographicOffCenter() * RotationX(90)

This, at a glance, works as expected. But when you zoom in close or translate to the side, shapes horribly distort.

Here's the scene at a standard zoom, rendering as expected:

Scene

Here it is when you zoom in:

Scene Zoomed

As you can see, the tesselated rectangle has divided into a triangle and polygon, despite a static model being rendered. All other view angles of the scene remain unchanged. Something else worth noting is that all models stop at 1/4th of the way to the top. No amount of effort will bring them above the line, their lines always end right there. And it renders the line there, even though you'd expect the line to pass beyond the limit and stop rendering.

Also a final note, Ortho.inverted() * screen_coord_of_mouse returns the correct X-axis coordinate, but a horribly misplaced Y-axis coordinate. This same method works perfectly for the top-down view.


Solution

  • I FIGURED IT OUT.

    Basically... rotating an ortho matrix, as entertaining as that is, is not the correct method.

    The correct method looks more like this:

            Matrix4 view = Matrix4.LookAt(new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, -1));
            ortho = view * Matrix4.CreateOrthographicOffCenter(-500f / side_zoom + (float)side_translate.X, 500f / side_zoom + (float)side_translate.X,
                500f / side_zoom + (float)side_translate.Y, -500f / side_zoom + (float)side_translate.Y, -100000, 100000);
    

    Basically, multiply a lookAt view matrix behind the ortho matrix. Be warned that everything is flipped (Still working on that, but that's why my up vector is negative one rather than one)