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

08-20 20:41