# Problem with gfm.math.matrix (some gamedevs out there ?)

drug drug2004 at bk.ru
Thu Sep 3 13:14:57 UTC 2020

```On 9/3/20 3:36 PM, Thomas wrote:
> Hi!
>
> I am following some examples in C++ about how OpenGL is working. There
> are great tutorials out there and most of it works also with D.
> For my code I am using gfm.math.matrix (Version 8.0.3) to be able to
> calculate the mouse position in a 3d world.
> Now I have some problems with the projection matrix by inverting it.
>
> My example code:
> ---------------------
> import std.stdio;
>
> int main()
> {
>
>      import gfm.math.matrix;
>
>      const int width = 800;
>      const int height = 600;
>
>      auto projectionMatrix = mat4!(float).identity();
>      auto ratio = cast(float)width / cast(float)height;
>
>      projectionMatrix = mat4!(float).perspective( 45.0f, ratio, 0.0f,
> 100.0f );
>
>      writeln("projectionMatrix: ", projectionMatrix );
>
>      auto inversedMatrix = mat4!(float).identity();
>      inversedMatrix = projectionMatrix.inverse();  // <-- why this does
> not work ?
>      writeln("inversedMatrix: ", inversedMatrix );
>
>      return 0;
> }
>
> The projection matrix will be calculated correctly with
> [1.34444, 0, 0, 0, 0, 1.79259, 0, 0, 0, 0, -1, -0, 0, 0, -1, 0] assuming
> that the screen size is 800x600.
>
> But using the .inverse() function in gfm returns only a matrix with
> following values:
> [-nan, -nan, -nan, -nan, -nan, -nan, -nan, -nan, -nan, -nan, -nan, -nan,
> -nan, -nan, inf, -inf]
>
> I don't know what I am doing wrong here:
>    - do I call the function wrong way ? (but there is no other way)
>    - is there a bug in the function ? (I do not believe that because the
> library is battle proved)
>
> So I looked a couple of hours for an answer, but did not find anything
> useful.
>
> Is somebody out there who could help me out ? Maybe one developer of the
> d gfm library ?
>
> Thank you for you time!
>
> Thomas

1. zNear should not be equal to zero, this makes your matrix singular
and inversion of singular matix is impossible so you get nans and infs.
Make zNear a little bit more than zero and your code will work

2. FOV in perspective should be expressed in radians, not degrees