2
votes

When I use NIM GLM to create a projection matrix and attempt to pass that to a shader with glUniformMatrix4fv, I get a type mismatch issue. The matrix pointer is of type ptr float64 and I assume that glUniformMatrix4fv needs ptr float32.

I'll be damned if I can figure out how to convert this or ensure the projection matrix starts off at the right level of precision.

Interestingly all other matrcies I have created - such as model and view - pass to the shaders without a hitch.

Here is the code...

var
  projection = perspective(PI/2, aspectRatio, 0.1f, 100.0f)
  projectionMatrixLocation: int32
    
#... later in the main loop
    
projectionMatrixLocation = glGetUniformLocation(shaderProgram,"projection")

#...

while not window.windowShouldClose:
  #...
  glUniformMatrix4fv(projectionMatrixLocation, 1, false, projection.caddr)  #here the error comes
      
#...

# in the shader
uniform mat4 projection;
2

2 Answers

2
votes

perspective will return a Mat4[T], based on the type of its first parameter. you can see this for yourself:

import glm,math
var 
  proj64 = perspective(PI / 2, 1.33, 0.1, 100)
  proj32 = perspective(PI.float32 / 2, 1.33, 0.1, 100)
echo typeof proj32 #Mat4[float32]
echo typeof proj64 #Mat4[float], 64 bit on every architecture

note that the subsequent parameters can be ints and they'll be converted.

DO NOT cast a float64 to a float32, you're just converting it to zero!

echo proj32.caddr[] #0.75
echo proj64.caddr[] #0.7500000000000002
echo cast[ptr float32](proj64.caddr)[] #2.802596928649634e-45
-1
votes

Okay. I have been doing some black magic with my pointers. I'm not sure this is the solution given that I can't see anything rendered on the screen at the moment. It's tricky to debug with my GSL shaders created using in-line in strings, but at least the thing is compiling now.

Replacing...

glUniformMatrix4fv(projectionMatrixLocation, 1, false, projection.caddr)

With...

glUniformMatrix4fv(projectionMatrixLocation, 1, false, cast[ptr float32] (projection.caddr))

Please let me know if this is a dangerous approach!