mirror of
https://github.com/minetest/minetest.git
synced 2024-11-20 14:43:45 +01:00
Refactor matrix4.h
Sets the surprising row-major conventions used here straight. Renames rotateVect to rotateAndScaleVect: If the matrix also scales, that is applied as well by the method. Obsolete rotateVect variants are removed. The inverseRotateVect method is also renamed accordingly. Note that this applies the transpose of the product of the scale and rotation matrices, which inverts just the rotation.
This commit is contained in:
parent
c8f1efebea
commit
7e4919c6ed
@ -24,7 +24,12 @@ namespace core
|
||||
{
|
||||
|
||||
//! 4x4 matrix. Mostly used as transformation matrix for 3d calculations.
|
||||
/** The matrix is a D3D style matrix, row major with translations in the 4th row. */
|
||||
/** Conventions: Matrices are considered to be in row-major order.
|
||||
* Multiplication of a matrix A with a row vector v is the premultiplication vA.
|
||||
* Translations are thus in the 4th row.
|
||||
* The matrix product AB yields a matrix C such that vC = (vB)A:
|
||||
* B is applied first, then A.
|
||||
*/
|
||||
template <class T>
|
||||
class CMatrix4
|
||||
{
|
||||
@ -242,17 +247,11 @@ public:
|
||||
//! Translate a vector by the inverse of the translation part of this matrix.
|
||||
void inverseTranslateVect(vector3df &vect) const;
|
||||
|
||||
//! Rotate a vector by the inverse of the rotation part of this matrix.
|
||||
void inverseRotateVect(vector3df &vect) const;
|
||||
//! Scale a vector, then rotate by the inverse of the rotation part of this matrix.
|
||||
[[nodiscard]] vector3d<T> scaleThenInvRotVect(const vector3d<T> &vect) const;
|
||||
|
||||
//! Rotate a vector by the rotation part of this matrix.
|
||||
void rotateVect(vector3df &vect) const;
|
||||
|
||||
//! An alternate transform vector method, writing into a second vector
|
||||
void rotateVect(core::vector3df &out, const core::vector3df &in) const;
|
||||
|
||||
//! An alternate transform vector method, writing into an array of 3 floats
|
||||
void rotateVect(T *out, const core::vector3df &in) const;
|
||||
//! Rotate and scale a vector. Applies both rotation & scale part of the matrix.
|
||||
[[nodiscard]] vector3d<T> rotateAndScaleVect(const vector3d<T> &vect) const;
|
||||
|
||||
//! Transforms the vector by this matrix
|
||||
/** This operation is performed as if the vector was 4d with the 4th component =1 */
|
||||
@ -1154,39 +1153,23 @@ inline bool CMatrix4<T>::isIdentity_integer_base() const
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void CMatrix4<T>::rotateVect(vector3df &vect) const
|
||||
inline vector3d<T> CMatrix4<T>::rotateAndScaleVect(const vector3d<T> &v) const
|
||||
{
|
||||
vector3d<T> tmp(static_cast<T>(vect.X), static_cast<T>(vect.Y), static_cast<T>(vect.Z));
|
||||
vect.X = static_cast<f32>(tmp.X * M[0] + tmp.Y * M[4] + tmp.Z * M[8]);
|
||||
vect.Y = static_cast<f32>(tmp.X * M[1] + tmp.Y * M[5] + tmp.Z * M[9]);
|
||||
vect.Z = static_cast<f32>(tmp.X * M[2] + tmp.Y * M[6] + tmp.Z * M[10]);
|
||||
}
|
||||
|
||||
//! An alternate transform vector method, writing into a second vector
|
||||
template <class T>
|
||||
inline void CMatrix4<T>::rotateVect(core::vector3df &out, const core::vector3df &in) const
|
||||
{
|
||||
out.X = in.X * M[0] + in.Y * M[4] + in.Z * M[8];
|
||||
out.Y = in.X * M[1] + in.Y * M[5] + in.Z * M[9];
|
||||
out.Z = in.X * M[2] + in.Y * M[6] + in.Z * M[10];
|
||||
}
|
||||
|
||||
//! An alternate transform vector method, writing into an array of 3 floats
|
||||
template <class T>
|
||||
inline void CMatrix4<T>::rotateVect(T *out, const core::vector3df &in) const
|
||||
{
|
||||
out[0] = in.X * M[0] + in.Y * M[4] + in.Z * M[8];
|
||||
out[1] = in.X * M[1] + in.Y * M[5] + in.Z * M[9];
|
||||
out[2] = in.X * M[2] + in.Y * M[6] + in.Z * M[10];
|
||||
return {
|
||||
v.X * M[0] + v.Y * M[4] + v.Z * M[8],
|
||||
v.X * M[1] + v.Y * M[5] + v.Z * M[9],
|
||||
v.X * M[2] + v.Y * M[6] + v.Z * M[10]
|
||||
};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void CMatrix4<T>::inverseRotateVect(vector3df &vect) const
|
||||
inline vector3d<T> CMatrix4<T>::scaleThenInvRotVect(const vector3d<T> &v) const
|
||||
{
|
||||
vector3d<T> tmp(static_cast<T>(vect.X), static_cast<T>(vect.Y), static_cast<T>(vect.Z));
|
||||
vect.X = static_cast<f32>(tmp.X * M[0] + tmp.Y * M[1] + tmp.Z * M[2]);
|
||||
vect.Y = static_cast<f32>(tmp.X * M[4] + tmp.Y * M[5] + tmp.Z * M[6]);
|
||||
vect.Z = static_cast<f32>(tmp.X * M[8] + tmp.Y * M[9] + tmp.Z * M[10]);
|
||||
return {
|
||||
v.X * M[0] + v.Y * M[1] + v.Z * M[2],
|
||||
v.X * M[4] + v.Y * M[5] + v.Z * M[6],
|
||||
v.X * M[8] + v.Y * M[9] + v.Z * M[10]
|
||||
};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
@ -1247,8 +1230,7 @@ inline void CMatrix4<T>::transformPlane(core::plane3d<f32> &plane) const
|
||||
|
||||
// Transform the normal by the transposed inverse of the matrix
|
||||
CMatrix4<T> transposedInverse(*this, EM4CONST_INVERSE_TRANSPOSED);
|
||||
vector3df normal = plane.Normal;
|
||||
transposedInverse.rotateVect(normal);
|
||||
vector3df normal = transposedInverse.rotateAndScaleVect(plane.Normal);
|
||||
plane.setPlane(member, normal.normalize());
|
||||
}
|
||||
|
||||
|
@ -389,7 +389,7 @@ bool CB3DMeshFileLoader::readChunkVRTS(CSkinnedMesh::SJoint *inJoint)
|
||||
|
||||
// Transform the Vertex position by nested node...
|
||||
inJoint->GlobalMatrix.transformVect(Vertex.Pos);
|
||||
inJoint->GlobalMatrix.rotateVect(Vertex.Normal);
|
||||
Vertex.Normal = inJoint->GlobalMatrix.rotateAndScaleVect(Vertex.Normal);
|
||||
|
||||
// Add it...
|
||||
BaseVertices.push_back(Vertex);
|
||||
|
@ -354,8 +354,7 @@ static void transformVertices(std::vector<video::S3DVertex> &vertices, const cor
|
||||
// Apply scaling, rotation and rotation (in that order) to the position.
|
||||
transform.transformVect(vertex.Pos);
|
||||
// For the normal, we do not want to apply the translation.
|
||||
// TODO note that this also applies scaling; the Irrlicht method is misnamed.
|
||||
transform.rotateVect(vertex.Normal);
|
||||
vertex.Normal = transform.rotateAndScaleVect(vertex.Normal);
|
||||
// Renormalize (length might have been affected by scaling).
|
||||
vertex.Normal.normalize();
|
||||
}
|
||||
|
@ -511,7 +511,7 @@ void CSkinnedMesh::skinJoint(SJoint *joint, SJoint *parentJoint)
|
||||
jointVertexPull.transformVect(thisVertexMove, weight.StaticPos);
|
||||
|
||||
if (AnimateNormals)
|
||||
jointVertexPull.rotateVect(thisNormalMove, weight.StaticNormal);
|
||||
thisNormalMove = jointVertexPull.rotateAndScaleVect(weight.StaticNormal);
|
||||
|
||||
if (!(*(weight.Moved))) {
|
||||
*(weight.Moved) = true;
|
||||
|
@ -405,10 +405,11 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 tool_reload_ratio)
|
||||
|
||||
// Compute absolute camera position and target
|
||||
m_headnode->getAbsoluteTransformation().transformVect(m_camera_position, rel_cam_pos);
|
||||
m_headnode->getAbsoluteTransformation().rotateVect(m_camera_direction, rel_cam_target - rel_cam_pos);
|
||||
m_camera_direction = m_headnode->getAbsoluteTransformation()
|
||||
.rotateAndScaleVect(rel_cam_target - rel_cam_pos);
|
||||
|
||||
v3f abs_cam_up;
|
||||
m_headnode->getAbsoluteTransformation().rotateVect(abs_cam_up, rel_cam_up);
|
||||
v3f abs_cam_up = m_headnode->getAbsoluteTransformation()
|
||||
.rotateAndScaleVect(rel_cam_up);
|
||||
|
||||
// Separate camera position for calculation
|
||||
v3f my_cp = m_camera_position;
|
||||
|
@ -1015,8 +1015,7 @@ int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
|
||||
v3f z_dir = z_directions[i];
|
||||
core::CMatrix4<f32> a;
|
||||
a.buildRotateFromTo(v3f(0,1,0), z_dir);
|
||||
v3f dir = m_camera_direction;
|
||||
a.rotateVect(dir);
|
||||
v3f dir = a.rotateAndScaleVect(m_camera_direction);
|
||||
int br = 0;
|
||||
float step = BS*1.5;
|
||||
if(max_d > 35*BS)
|
||||
|
@ -536,9 +536,9 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
|
||||
return; // Avoid zero divides
|
||||
|
||||
// Angle according to camera view
|
||||
v3f fore(0.f, 0.f, 1.f);
|
||||
scene::ICameraSceneNode *cam = client->getSceneManager()->getActiveCamera();
|
||||
cam->getAbsoluteTransformation().rotateVect(fore);
|
||||
v3f fore = cam->getAbsoluteTransformation()
|
||||
.rotateAndScaleVect(v3f(0.f, 0.f, 1.f));
|
||||
int angle = - fore.getHorizontalAngle().Y;
|
||||
|
||||
// Limit angle and ajust with given offset
|
||||
|
@ -357,16 +357,18 @@ void ParticleSpawner::spawnParticle(ClientEnvironment *env, float radius,
|
||||
|
||||
if (attached_absolute_pos_rot_matrix) {
|
||||
// Apply attachment rotation
|
||||
attached_absolute_pos_rot_matrix->rotateVect(pp.vel);
|
||||
attached_absolute_pos_rot_matrix->rotateVect(pp.acc);
|
||||
pp.vel = attached_absolute_pos_rot_matrix->rotateAndScaleVect(pp.vel);
|
||||
pp.acc = attached_absolute_pos_rot_matrix->rotateAndScaleVect(pp.acc);
|
||||
}
|
||||
|
||||
if (attractor_obj)
|
||||
attractor_origin += attractor_obj->getPosition() / BS;
|
||||
if (attractor_direction_obj) {
|
||||
auto *attractor_absolute_pos_rot_matrix = attractor_direction_obj->getAbsolutePosRotMatrix();
|
||||
if (attractor_absolute_pos_rot_matrix)
|
||||
attractor_absolute_pos_rot_matrix->rotateVect(attractor_direction);
|
||||
if (attractor_absolute_pos_rot_matrix) {
|
||||
attractor_direction = attractor_absolute_pos_rot_matrix
|
||||
->rotateAndScaleVect(attractor_direction);
|
||||
}
|
||||
}
|
||||
|
||||
pp.expirationtime = r_exp.pickWithin();
|
||||
|
@ -137,8 +137,8 @@ void DirectionalLight::update_frustum(const Camera *cam, Client *client, bool fo
|
||||
// when camera offset changes, adjust the current frustum view matrix to avoid flicker
|
||||
v3s16 cam_offset = cam->getOffset();
|
||||
if (cam_offset != shadow_frustum.camera_offset) {
|
||||
v3f rotated_offset;
|
||||
shadow_frustum.ViewMat.rotateVect(rotated_offset, intToFloat(cam_offset - shadow_frustum.camera_offset, BS));
|
||||
v3f rotated_offset = shadow_frustum.ViewMat.rotateAndScaleVect(
|
||||
intToFloat(cam_offset - shadow_frustum.camera_offset, BS));
|
||||
shadow_frustum.ViewMat.setTranslation(shadow_frustum.ViewMat.getTranslation() + rotated_offset);
|
||||
shadow_frustum.player += intToFloat(shadow_frustum.camera_offset - cam->getOffset(), BS);
|
||||
shadow_frustum.camera_offset = cam_offset;
|
||||
|
@ -838,14 +838,10 @@ void Sky::updateStars()
|
||||
);
|
||||
core::CMatrix4<f32> a;
|
||||
a.buildRotateFromTo(v3f(0, 1, 0), r);
|
||||
v3f p = v3f(-d, 1, -d);
|
||||
v3f p1 = v3f(d, 1, -d);
|
||||
v3f p2 = v3f(d, 1, d);
|
||||
v3f p3 = v3f(-d, 1, d);
|
||||
a.rotateVect(p);
|
||||
a.rotateVect(p1);
|
||||
a.rotateVect(p2);
|
||||
a.rotateVect(p3);
|
||||
v3f p = a.rotateAndScaleVect(v3f(-d, 1, -d));
|
||||
v3f p1 = a.rotateAndScaleVect(v3f(d, 1, -d));
|
||||
v3f p2 = a.rotateAndScaleVect(v3f(d, 1, d));
|
||||
v3f p3 = a.rotateAndScaleVect(v3f(-d, 1, d));
|
||||
vertices.push_back(video::S3DVertex(p, {}, {}, {}));
|
||||
vertices.push_back(video::S3DVertex(p1, {}, {}, {}));
|
||||
vertices.push_back(video::S3DVertex(p2, {}, {}, {}));
|
||||
|
@ -225,8 +225,7 @@ void GUIScene::setCameraRotation(v3f rot)
|
||||
core::matrix4 mat;
|
||||
mat.setRotationDegrees(rot);
|
||||
|
||||
m_cam_pos = v3f(0.f, 0.f, m_cam_distance);
|
||||
mat.rotateVect(m_cam_pos);
|
||||
m_cam_pos = mat.rotateAndScaleVect(v3f(0.f, 0.f, m_cam_distance));
|
||||
|
||||
m_cam_pos += m_target_pos;
|
||||
m_cam->setPosition(m_cam_pos);
|
||||
|
Loading…
Reference in New Issue
Block a user