glm::vec3 和 glm::mat4 是如何初始化的?

How are glm::vec3 and glm::mat4 initialised?

这是关于了解 glm 源代码的。我想知道 glm 是否对它的 类 进行了零初始化并进行了尝试。是的,即使没有提供构造函数值,glm::vec3glm::mat4 也会被初始化。然后我想了解它是如何完成的并阅读glm::mat4模板的源代码。

有这个部分:

...
enum ctor{null};

// Constructors
GLM_FUNC_DECL tmat4x4();
GLM_FUNC_DECL tmat4x4(tmat4x4 const & m);
GLM_FUNC_DECL explicit tmat4x4(ctor Null);
...

我可以读到有一个 (void) 构造函数 (ctor) 但没有定义,所以没有 {...} 部分。并且有一个 explicit 构造函数,其中 0 作为来自类型 enum ctor 的第一个元素的参数,它当然得到索引值 0.


编辑: 浏览 GitHub 上的最新源文件,从 mat4x4.hppdetail/type_mat4x4.hpp 中找到一个 #includes type_mat4x4.inl 中的实施细节。在那里 ctor 行为变得可见。

  • "glm/detail/type_mat4x4.inl"
  • 之下
  • 它是一个什么都不做的构造函数,在0.9.6.3中它变成了enum ctor{uninitialize}
  • 调用默认构造函数

顺便说一句,我无法下载你的版本,这个答案是基于 0.9.6.3

glm::mat4x4 view{ 2 / (right - left), 0, 0, 0,
            0, 2 / (top - bottom), 0, 0,
            0, 0, -2 / (farplane - nearplane), 0,
            -((right + left) / (right - left)), -((top + bottom) / (top - bottom)), -((farplane + nearplane) / (farplane - nearplane)), 1
        };


glm::mat4 view{ 2 / (right - left), 0, 0, 0,
            0, 2 / (top - bottom), 0, 0,
            0, 0, -2 / (farplane - nearplane), 0,
            -((right + left) / (right - left)), -((top + bottom) / (top - bottom)), -((farplane + nearplane) / (farplane - nearplane)), 1
        };

glm::uvec4 ViewPort(0,0,0,0);

glm::vec3 Origin(0.0f, 0.0f, 0.0f);

它不再默认初始化。在包含之前定义 GLM_FORCE_CTOR_INIT 以强制初始化。 https://github.com/g-truc/glm/issues/809

#define GLM_FORCE_CTOR_INIT
#include <glm/glm.hpp>