简体   繁体   English

使用glm将vec3与mat4相乘

[英]Multiply vec3 with mat4 using glm

At the moment, I convert the vec3 to vec4 , multiply it with the matrix, and then cast it back to vec3 此刻,我将vec3转换为vec4 ,将其与矩阵相乘,然后将其转换回vec3

Code: 码:

glm::vec3 transformed = glm::vec3(matrix * glm::vec4(point, 0.0))

It works, but I think it is not the good way to calculate it. 它有效,但我认为这不是计算它的好方法。 Is it possible to apply a mat4 on a vec3 , without casting it to vec4 and back? 是否可以在vec3上应用mat4 ,而不将其转换为vec4并返回?

When working with OpenGL it is very wise to stick with homogeneous coordinates . 使用OpenGL时,坚持使用齐次坐标是非常明智的。 For 3D space these are 4D vectors where normally the fourth element equals 1. When you do this all your calculations are in 4 dimensional space, so no conversions needed anywhere. 对于3D空间,这些是4D向量,通常第四个元素等于1.当您执行此操作时,所有计算都在4维空间中,因此任何地方都不需要转换。

Also note that in your example, you will actually lose any translation that may have been recorded in the transformation matrix. 另请注意,在您的示例中,您实际上将丢失可能已在转换矩阵中记录的任何转换。 If you want to keep this you'll need to use 1 for the 4th element, not 0. 如果你想保留它,你需要使用1代表第4个元素,而不是0。

Appendix F of the Red Book describes how and why homogeneous coordinates are used within OpenGL. 红皮书的附录F描述了在OpenGL中如何以及为何使用齐次坐标。

mat4 is a 4 by 4 matrix, so you need a 4 dimension vector to multiply it. mat4是一个4乘4的矩阵,所以你需要一个4维向量来乘以它。

That 4th dimension is really useful for 3D math, to distinguish between 3D space points (1) and 3D vectors (0) 第四维对于3D数学非常有用,可以区分3D空间点(1)和3D矢量(0)

Here I missing lots of operator, but I hope that you get the idea. 在这里,我错过了许多操作员,但我希望你能得到这个想法。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM