使用glm将vec3与mat4相乘
Multiply vec3 with mat4 using glm
此时,我将vec3
转换为vec4
,将其与矩阵相乘,然后将其投射回vec3
代码:
glm::vec3 transformed = glm::vec3(matrix * glm::vec4(point, 0.0))
它有效,但我认为这不是计算它的好方法。是否可以在vec3
上应用mat4
,而不将其强制转换为vec4
并返回?
使用OpenGL时,坚持使用齐次坐标是非常明智的。对于3D空间,这些是4D矢量,其中第四元素通常等于1。当你这样做的时候,所有的计算都在4维空间中,所以任何地方都不需要转换。
还要注意,在您的示例中,您实际上会丢失转换矩阵中可能记录的任何翻译。如果你想保持这一点,你需要使用1作为第四个元素,而不是0。
红皮书的附录F描述了在OpenGL中使用齐次坐标的方式和原因。
mat4是一个4乘4的矩阵,因此需要一个4维向量来乘以它。
第4维对于3D数学非常有用,可以区分3D空间点(1)和3D矢量(0)
这里我错过了很多接线员,但我希望你能明白。
class point3
{
public:
vec4 p;
public:
point3();
point3(const point3& rhs);
point3(const point3& rhs);
point3(float x, float y, float z);
point3& operator=(const point3&rhs);
vector3 operator-(const point3&rhs);
point3 operator+(const vector3&rhs);
point3& operator+=(const vector3&rhs);
};
point3::point3():p.x(0), p.y(0), p.z(0), p.w(1)
{
}
point3::point3(const point3& rhs):p(rhs.p)
{
}
point3::point3(const point3& rhs):p(rhs.p)
{
}
point3::point3(float x, float y, float z):p.x(x), p.y(y), p.z(z), p.w(1)
{
}
point3& point3::operator=(const point3&rhs)
{
return (p=rhs.p);
}
vector3 point3::operator-(const point3&rhs)
{
vector3 result;
result.p=(p-rhs.p);
return result;
}
point3 point3::operator+(const vector3&rhs)
{
point3 result;
result.p=(p+rhs.p);
return result;
}
point3& point3::operator+=(const vector3&rhs)
{
p=(p+rhs.p);
return p;
}
class vector3
{
public:
vec4 p;
public:
vector3();
vector3(const vector3& rhs);
vector3(const vector3& rhs);
vector3(float x, float y, float z);
vector3& operator=(const vector3&rhs);
vector3 operator-(const vector3&rhs);
point3 operator-(const point3&rhs);
point3 operator+(const point3&rhs);
vector3 operator+(const vector3&rhs);
vector3& operator+=(const vector3&rhs);
};
vector3::vector3():p.x(0), p.y(0), p.z(0), p.w(0)
{
}
vector3::vector3(const vector3& rhs):p(rhs.p)
{
}
vector3::vector3(const vector3& rhs):p(rhs.p)
{
}
vector3::vector3(float x, float y, float z):p.x(x), p.y(y), p.z(z), p.w(0)
{
}
vector3& vector3::operator=(const vector3&rhs)
{
p=rhs.p;
return p;
}
vector3 vector3::operator-(const vector3&rhs)
{
vector3 result;
result.p=(p-rhs.p);
return result;
}
point3 vector3::operator-(const point3&rhs)
{
point3 result;
result.p=(p-rhs.p);
return result;
}
point3 vector3::operator+(const point3&rhs)
{
point3 result;
result.p=(p+rhs.p);
return result;
}
vector3 vector3::operator+(const vector3&rhs)
{
vector3 result;
result.p=(p+rhs.p);
return result;
}
vector3& vector3::operator+=(const vector3&rhs)
{
p=(p+rhs.p);
return p;
}
相关文章:
- 将执行、作业和WinAPI相乘
- 如何在 glRotatef 中使用 glm::mat4
- 有没有办法在C++中将字符串和整数相乘?
- 将 glm.mat4 从 Python 传回 C++ (glm::mat4)
- 将字节与异或相乘
- 变量按什么顺序相乘
- 将最多 50 位数字的 2 个数字相乘
- 通过零参数构造函数创建的 glm::mat4 应该包含哪些值?
- 在 c++ 中不使用 * 运算符将十进制值相乘
- chrono::duration_cast在比率相乘过程中的问题
- 如何将整数数组相乘得到一个数字?
- 在不使用 * 和 / 运算符的情况下将两个浮点数相乘和除以
- 如何获得将两种不同类型的相乘的结果类型?
- Visual studio 2013 和 g++ 7.1 中将 int 和 long 类型相乘时的 c++ 差异行为
- 模板化函数无法将值与 -1 相乘
- 将数字与文本文件中的行相乘
- CV :: MAT如何从/转换为GLM :: MAT4
- (也许是NP-Hard)求一个集合的子集总数,使得每个子集在与其所有元素相乘时的值都大于X
- C++将存储在链表中的两个大数字相乘
- 使用glm将vec3与mat4相乘