Search code examples
c++glm-math

Problems with Glm functions


Why does this code compile

    [[nodiscard]] glm::mat4 rotationX(double theta)
    {
        return glm::rotate(glm::mat4(1.0), static_cast<float>(theta), glm::vec3(1.0, 0.0, 0.0));
    }

and this one not

    [[nodiscard]] glm::mat4 rotationX(double theta)
    {
        return glm::rotate(glm::mat4(1.0), theta, glm::vec3(1.0, 0.0, 0.0));
    }

it results in error C2672 no matching overloaded function found and Error C2782: Template parameter T is ambigous. what type should theta be?

Also this code

    [[nodiscard]] glm::mat4 shearing(double xy, double xz, double yx, double yz, double zx, double zy)
    {
        auto sh = glm::mat4(1.0);

        sh[1][0] = xy;
        sh[2][0] = xz;
        sh[2][1] = yz;

        sh[0][1] = yx;
        sh[0][2] = zx;
        sh[1][2] = zy;

        return sh;
    }

produces the following warning:
warning C4244: 'argument' : conversion from 'double' to 'T', possible loss of data

I can't understand what exactly the error is, because everything else seems to work.

PS, I include

#include <glm/fwd.hpp> 

in .hpp and

#include <glm/glm.hpp>  
#include <glm/gtc/matrix_transform.hpp>

in .cpp.


Solution

  • When using glm::rotate with vec3 and mat4, the type of theta must be float, because the element type of mat4 and vec3 is float:

    typedef mat<4, 4, f32, defaultp> mat4;
    
    typedef vec<3, float, defaultp> vec3;
    

    The corresponding double precision data types are damt4 and dvec3:

    typedef mat<4, 4, f64, defaultp> dmat4;
    
    typedef vec<3, f64, defaultp> dvec3; 
    

    The names of the types are the same as for the corresponding GLSL data types (Data Type (GLSL)). In glsl, the matrix and vector data types can be constructed from any basic data type.