quot;glm::translatequot; outputs a matrix with incorrect values(“glm::translate输出一个值不正确的矩阵)
问题描述
我拿了一个示例代码来测试 glm::translate 函数:
I took a sample code to test the glm::translate function:
glm::vec4 vec(1.0f, 0.0f, 0.0f, 1.0f);
glm::mat4 trans;
trans = glm::translate(trans, glm::vec3(1.0f, 1.0f, 0.0f));
vec = trans * vec;
std::cout << vec.x << ", " << vec.y << ", " << vec.z << std::endl;
它输出以下内容:
-4.29497e+08, -4.29497e+08, -4.29497e+08
而不是预期的 2, 1, 0
可能的原因是什么,我该怎么办?
What is the possible cause and what can I do about it?
(我应该搜索这段代码之外的缺陷吗?)
(Should I search for the flaw outside this piece of code?)
推荐答案
-4.29497e+08
这看起来像是未初始化的内存,这会让我相信缺陷在于:
This looks like uninitialised memory, which would lead me to believe the flaw lies in:
glm::mat4 trans;
您尚未初始化矩阵,但已对其执行算术运算.你不能假设构造函数会初始化它的内存,所以改为:
You have not initialised the matrix, but have performed an arithmetic operation on it. You cannot assume that a constructor will initialise it's memory, so change to:
glm::mat4 trans(1.0f);
这应该可以解决问题.
这可能不会出现在所有开发环境中,例如,VS 中的调试模式有一些保护措施来防止这种情况,但它会出现在发布模式中.
This may not show up in all development environments, as, for example, debug mode in VS has some safeguards to prevent this, but it will show up in release mode.
简单地说:练习 RAII:资源获取就是初始化.至少,将内存归零,因为当重新分配内存时,它将被设置为上次释放时的值.
Simply put: Practice RAII: Resource Acquisition Is Initialisation. At the very least, zero the memory, as when the memory is reallocated, it will be set to whatever it's previous value was when it was last released.
这篇关于“glm::translate"输出一个值不正确的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“glm::translate"输出一个值不正确的矩阵
- Stroustrup 的 Simple_window.h 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- STL 中有 dereference_iterator 吗? 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- C++ 协变模板 2021-01-01
- 近似搜索的工作原理 2021-01-01
- 静态初始化顺序失败 2022-01-01