From ae1bd2c066291f5ef8fd421eea46373162148b8f Mon Sep 17 00:00:00 2001 From: Garux Date: Wed, 16 May 2018 14:16:43 +0300 Subject: [PATCH] matrix4_for_normal_transform, matrix4_transformed_normal, plane3_transformed_affine_full functions --- libs/math/matrix.h | 16 ++++++++++++++-- libs/math/plane.h | 6 ++++++ radiant/brush.h | 12 +----------- radiant/brush_primit.cpp | 24 +++++++----------------- 4 files changed, 28 insertions(+), 30 deletions(-) diff --git a/libs/math/matrix.h b/libs/math/matrix.h index 56ab757c..0b98c6a6 100644 --- a/libs/math/matrix.h +++ b/libs/math/matrix.h @@ -398,8 +398,8 @@ inline BasicVector3 matrix4_transformed_direction( const Matrix4& self, /// \brief Transforms \p direction by \p self in-place. template -inline void matrix4_transform_direction( const Matrix4& self, BasicVector3& normal ){ - normal = matrix4_transformed_direction( self, normal ); +inline void matrix4_transform_direction( const Matrix4& self, BasicVector3& direction ){ + direction = matrix4_transformed_direction( self, direction ); } /// \brief Returns \p vector4 transformed by \p self. @@ -591,6 +591,18 @@ inline void matrix4_full_invert( Matrix4& self ){ } +/// lets say M is a transformation matrix, then transforming vertex v is just M*v +/// but transforming the normal is M^(-T) n +inline Matrix4 matrix4_for_normal_transform( const Matrix4& matrix ){ + return matrix4_transposed( matrix4_affine_inverse( matrix ) ); +} + +template +inline BasicVector3 matrix4_transformed_normal( const Matrix4& matrix, const BasicVector3& normal ){ + return vector3_normalised( matrix4_transformed_direction( matrix4_for_normal_transform( matrix ), normal ) ); +} + + /// \brief Constructs a pure-translation matrix from \p translation. inline Matrix4 matrix4_translation_for_vec3( const Vector3& translation ){ return Matrix4( diff --git a/libs/math/plane.h b/libs/math/plane.h index 4027164f..10dca357 100644 --- a/libs/math/plane.h +++ b/libs/math/plane.h @@ -99,6 +99,12 @@ inline Plane3 plane3_inverse_transformed( const Plane3& plane, const Matrix4& tr ); } +inline Plane3 plane3_transformed_affine_full( const Plane3& plane, const Matrix4& transform ){ + const DoubleVector3 anchor( matrix4_transformed_point( transform, plane.normal() * plane.dist() ) ); + const DoubleVector3 normal( matrix4_transformed_normal( transform, plane.normal() ) ); + return Plane3( normal, vector3_dot( normal, anchor ) ); +} + inline Plane3 plane3_flipped( const Plane3& plane ){ return Plane3( vector3_negated( plane.normal() ), -plane.dist() ); } diff --git a/radiant/brush.h b/radiant/brush.h index a9b50d12..ae38c3c6 100644 --- a/radiant/brush.h +++ b/radiant/brush.h @@ -633,22 +633,12 @@ inline Plane3 Plane3_applyTranslation( const Plane3& plane, const Vector3& trans return Plane3( tmp.normal(), -tmp.dist() ); } -/// lets say M is a transformation matrix, then transforming vertex v is just M*v -/// but transforming the normal is M^(-T) n inline Plane3 Plane3_applyTransform( const Plane3& plane, const Matrix4& matrix ){ /* fails for scaling */ //Plane3 tmp( plane3_transformed( Plane3( plane.normal(), -plane.dist() ), matrix ) ); //return Plane3( tmp.normal(), -tmp.dist() ); /* ok */ -// Vector4 anchor = matrix4_transformed_vector4( matrix, Vector4( plane.normal() * plane.dist(), 1 ) ); -// Matrix4 mat = matrix4_transposed( matrix4_full_inverse( matrix ) ); -// Vector4 normal = matrix4_transformed_vector4( mat, Vector4( plane.normal(), 0 ) ); -// return plane3_normalised( Plane3( vector4_to_vector3( normal ), vector3_dot( vector4_to_vector3( normal ), vector4_to_vector3( anchor ) ) ) ); - - const DoubleVector3 anchor( matrix4_transformed_point( matrix, plane.normal() * plane.dist() ) ); - const Matrix4 mat( matrix4_transposed( matrix4_affine_inverse( matrix ) ) ); - const DoubleVector3 normal( vector3_normalised( matrix4_transformed_direction( mat, plane.normal() ) ) ); - return Plane3( normal, vector3_dot( normal, anchor ) ); + return plane3_transformed_affine_full( plane, matrix ); } class FacePlane diff --git a/radiant/brush_primit.cpp b/radiant/brush_primit.cpp index e79f4a28..1731ff1a 100644 --- a/radiant/brush_primit.cpp +++ b/radiant/brush_primit.cpp @@ -1494,13 +1494,10 @@ void Texdef_transformLocked_original( TextureProjection& projection, std::size_t //globalOutputStream() << "plane.normal(): " << plane.normal() << "\n"; #if 0 - Vector3 normalTransformed( matrix4_transformed_direction( identity2transformed, plane.normal() ) ); + const Vector3 normalTransformed( matrix4_transformed_direction( identity2transformed, plane.normal() ) ); #else //preserves scale in BP while scaling, but not shift //fixes QNAN - Matrix4 maa( matrix4_affine_inverse( identity2transformed ) ); - matrix4_transpose( maa ); - //Vector4 vec4 = matrix4_transformed_vector4( maa, Vector4( plane.normal(), 0 ) ); - //Vector3 normalTransformed( vector3_normalised( vector4_to_vector3( vec4 ) ) ); - Vector3 normalTransformed( vector3_normalised( matrix4_transformed_direction( maa, plane.normal() ) ) ); + const Matrix4 maa( matrix4_for_normal_transform( identity2transformed ) ); + const Vector3 normalTransformed( vector3_normalised( matrix4_transformed_direction( maa, plane.normal() ) ) ); #endif //globalOutputStream() << "normalTransformed: " << normalTransformed << "\n"; @@ -1707,9 +1704,7 @@ void Texdef_transformLocked( TextureProjection& projection, std::size_t width, s } #if 0 - Matrix4 maa( matrix4_affine_inverse( identity2transformed ) ); - matrix4_transpose( maa ); - const DoubleVector3 normalTransformed( vector3_normalised( matrix4_transformed_direction( maa, plane.normal() ) ) ); + const DoubleVector3 normalTransformed( matrix4_transformed_normal( identity2transformed, plane.normal() ) ); #else /* this is also handling scale = 0 case */ DoubleVector3 normalTransformed( plane3_for_points( points ).normal() ); @@ -1731,10 +1726,7 @@ void Texdef_transformLocked( TextureProjection& projection, std::size_t width, s const Vector3 offset = matrix4_transformed_point( identity2transformed, Vector3( 0, 0, 0 ) ); Vector3 newNormal = matrix4_transformed_point( identity2transformed, plane.normal() ) - offset; #elif 0 - const Matrix4 maa( matrix4_transposed( matrix4_affine_inverse( identity2transformed ) ) ); -// globalOutputStream() << "maa: " << maa << "\n"; - Vector3 newNormal( vector3_normalised( matrix4_transformed_direction( maa, plane.normal() ) ) ); -// globalOutputStream() << plane.normal() << newNormal << "\n"; + const Vector3 newNormal( matrix4_transformed_normal( identity2transformed, plane.normal() ) ); #elif 1 /* this is also handling scale = 0 case */ DoubleVector3 texX, texY; @@ -1855,10 +1847,8 @@ void Texdef_transformLocked( TextureProjection& projection, std::size_t width, s } //globalOutputStream() << "plane.normal(): " << plane.normal() << "\n"; - Matrix4 maa( matrix4_affine_inverse( identity2transformed ) ); - matrix4_transpose( maa ); - Vector3 normalTransformed( vector3_normalised( matrix4_transformed_direction( maa, plane.normal() ) ) ); - + const Matrix4 maa( matrix4_for_normal_transform( identity2transformed ) ); + const Vector3 normalTransformed( vector3_normalised( matrix4_transformed_direction( maa, plane.normal() ) ) ); //globalOutputStream() << "normalTransformed: " << normalTransformed << "\n"; // identity: identity space