【问题标题】:Problems with Glm functionsglm函数的问题
【发布时间】:2021-09-17 12:57:24
【问题描述】:

为什么这段代码会编译

    [[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));
    }

这个不是

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

它会导致错误C2672 no matching overloaded function found 和 错误C2782: Template parameter T is ambigous。 theta应该是什么类型?

还有这段代码

    [[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;
    }

产生以下警告:
warning C4244: 'argument' : conversion from 'double' to 'T', possible loss of data

我不明白究竟是什么错误,因为其他一切似乎都正常。

PS,我包括

#include <glm/fwd.hpp> 

在 .hpp 和

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

在 .cpp 中。

【问题讨论】:

    标签: c++ glm-math


    【解决方案1】:

    glm::rotatevec3mat4一起使用时,theta的类型必须是float,因为mat4vec3的元素类型是float

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

    对应的双精度数据类型为damt4dvec3

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

    类型的名称与对应的 GLSL 数据类型 (Data Type (GLSL)) 的名称相同。在 glsl 中,矩阵和向量数据类型可以由任何基本数据类型构成。

    【讨论】:

      【解决方案2】:
      [[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;
      }
      

      【讨论】:

        猜你喜欢
        • 2020-09-25
        • 1970-01-01
        • 2019-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-08
        • 1970-01-01
        • 2022-01-06
        相关资源
        最近更新 更多