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"输出一个值不正确的矩阵


基础教程推荐
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01