“glm::translate"输出一个值不正确的矩阵 [英] "glm::translate" outputs a matrix with incorrect values

查看:32
本文介绍了“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"输出一个值不正确的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆