| 1 |
{%MainUnit castlevectors.pas} |
| 2 |
{ |
| 3 |
Copyright 2020-2024 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ TTransformation record. } |
| 18 |
|
| 19 |
{$ifdef read_interface} |
| 20 |
|
| 21 |
type |
| 22 |
{ Describe transformation in a way comfortable to apply it in both ways. } |
| 23 |
TTransformation = record |
| 24 |
{ Transformation from local to outside coordinate space. } |
| 25 |
Transform: TMatrix4; |
| 26 |
|
| 27 |
{ Inverted @link(Transform) matrix. |
| 28 |
|
| 29 |
Note that any scale with zero component along the way will make this |
| 30 |
partially invalid (we'll substitute identity in place of inverted scaling |
| 31 |
matrix). This is unavoidable, there's no reverse matrix for scaling |
| 32 |
with zero factor, since one resulting point may correpond to infinitely many |
| 33 |
source points (i.e., it's natural that such scaling function cannot be |
| 34 |
reversed). } |
| 35 |
InverseTransform: TMatrix4; |
| 36 |
|
| 37 |
{ A uniform scale of the matrix @link(Transform). If the matrix |
| 38 |
causes non-uniform scaling, this value represents an average scale. } |
| 39 |
Scale: Single; |
| 40 |
|
| 41 |
{ Is the underlying scale uniform (which means that @link(Scale) |
| 42 |
accurately represents the scaling, is not only approximate). } |
| 43 |
UniformScale: Boolean; |
| 44 |
|
| 45 |
{ Make no transformation (identity matrix, scale 1). } |
| 46 |
procedure Init; |
| 47 |
|
| 48 |
{ Modify transformation by an additional translation, rotation, scaling. |
| 49 |
|
| 50 |
Multiplies at the same time transformation matrix in @link(Transform), |
| 51 |
and it's inverse in @link(InverseTransform). |
| 52 |
The precise meaning of Center, Translation and other parameters |
| 53 |
follows exactly the X3D Transform node definition (see |
| 54 |
http://www.web3d.org/files/specifications/19775-1/V3.2/Part01/components/group.html#Transform ). |
| 55 |
|
| 56 |
@param(Rotation Rotation is expressed as a 4D vector, |
| 57 |
in which the first 3 components |
| 58 |
specify the rotation axis (does not need to be normalized, but must be non-zero), |
| 59 |
and the last component is the rotation angle @italic(in radians).) |
| 60 |
} |
| 61 |
procedure Multiply( |
| 62 |
const Center: TVector3; |
| 63 |
const Rotation: TVector4; |
| 64 |
const ScaleVector: TVector3; |
| 65 |
const ScaleOrientation: TVector4; |
| 66 |
const Translation: TVector3); overload; |
| 67 |
procedure Multiply( |
| 68 |
const Rotation: TVector4; |
| 69 |
const ScaleVector: TVector3; |
| 70 |
const Translation: TVector3); overload; |
| 71 |
|
| 72 |
{ Modify transformation by an additional transformation matrix. |
| 73 |
|
| 74 |
Using this is @bold(very discouraged), as we need to assume UniformScale=false, |
| 75 |
which in turn e.g. makes sphere collisions worse (at some transformations, |
| 76 |
they have to treat sphere as something larger). |
| 77 |
That is because matrix can contain any transformation, |
| 78 |
and even using MatrixDecompose and then analyzing whether resulting scale has X=Y=Z |
| 79 |
would be unreliable. Testcases: |
| 80 |
|
| 81 |
- Rendering ghost teeth in castle-game. |
| 82 |
|
| 83 |
This shows that we cannot just replace matrix with translation+rotation+scale |
| 84 |
by MatrixDecompose, matrix can contain other transformations like shear. |
| 85 |
So we need to account whole matrix. |
| 86 |
Replacing MultiplyMatrix(matrix) with MatrixDecompose + Multiply(vectors) |
| 87 |
makes wrong rendering. |
| 88 |
|
| 89 |
- Graves on "The Gate" level in castle-game. |
| 90 |
|
| 91 |
This shows that even calculating UniformScale based on scale resulting |
| 92 |
from MatrixDecompose doesn't work reliably. |
| 93 |
Replacing inside MultiplyMatrix the UniformScale detection, |
| 94 |
to assume true when scale (from MatrixDecompose) has X=Y=Z, |
| 95 |
results in blocked movement. |
| 96 |
MatrixDecompose cannot account fo arbitrary matrix that can contain anything. |
| 97 |
|
| 98 |
- Beginning position (blocked movement) on "castle hall" level in castle-game. |
| 99 |
|
| 100 |
Same as above. |
| 101 |
Shows that we cannot detect UniformScale smartly. |
| 102 |
|
| 103 |
Use the @link(Multiply) instead to provide separate translation, |
| 104 |
rotation etc. vectors. |
| 105 |
Use this method only when you are forced to do so, |
| 106 |
i.e. your input is already a matrix. |
| 107 |
} |
| 108 |
procedure MultiplyMatrix(const Matrix: TMatrix4); |
| 109 |
|
| 110 |
{ Modify transformation by an additional translation. } |
| 111 |
procedure Translate(const Translation: TVector3); |
| 112 |
|
| 113 |
{ Set Scale expressed by this transformation to identify, |
| 114 |
preserving the translation and rotation. } |
| 115 |
procedure ScaleToIdentity; |
| 116 |
end; |
| 117 |
|
| 118 |
PTransformation = ^TTransformation; |
| 119 |
|
| 120 |
TTransformationList = class({$ifdef FPC}specialize{$endif} TStructList<TTransformation>) |
| 121 |
end; |
| 122 |
|
| 123 |
{$endif read_interface} |
| 124 |
|
| 125 |
{$ifdef read_implementation} |
| 126 |
|
| 127 |
{ TTransformation ---------------------------------------------------- } |
| 128 |
|
| 129 |
procedure TTransformation.Init; |
| 130 |
begin |
| 131 |
Transform := TMatrix4.Identity; |
| 132 |
InverseTransform := TMatrix4.Identity; |
| 133 |
Scale := 1; |
| 134 |
UniformScale := true; |
| 135 |
end; |
| 136 |
|
| 137 |
procedure TTransformation.Translate(const Translation: TVector3); |
| 138 |
begin |
| 139 |
MultMatricesTranslation(Transform, InverseTransform, Translation); |
| 140 |
end; |
| 141 |
|
| 142 |
procedure TTransformation.Multiply( |
| 143 |
const Center: TVector3; |
| 144 |
const Rotation: TVector4; |
| 145 |
const ScaleVector: TVector3; |
| 146 |
const ScaleOrientation: TVector4; |
| 147 |
const Translation: TVector3); |
| 148 |
var |
| 149 |
M, IM: TMatrix4; |
| 150 |
MRotateScaleOrient, IMRotateScaleOrient: TMatrix4; |
| 151 |
begin |
| 152 |
{ To make InverseTransform, we multiply inverted matrices in inverted order |
| 153 |
below. } |
| 154 |
|
| 155 |
MultMatricesTranslation(Transform, InverseTransform, Translation + Center); |
| 156 |
|
| 157 |
{ We avoid using RotationMatricesRad when angle = 0, since this |
| 158 |
is often the case, and it makes TransformState much faster |
| 159 |
(which is important --- TransformState is important for traversing state). } |
| 160 |
if Rotation.W <> 0 then |
| 161 |
begin |
| 162 |
{ Note that even rotation Axis = zero is OK, both M and IM will be |
| 163 |
identity in this case. } |
| 164 |
RotationMatricesRad(Rotation, M, IM); |
| 165 |
Transform := Transform * M; |
| 166 |
InverseTransform := IM * InverseTransform; |
| 167 |
end; |
| 168 |
|
| 169 |
if (ScaleVector.X <> 1) or |
| 170 |
(ScaleVector.Y <> 1) or |
| 171 |
(ScaleVector.Z <> 1) then |
| 172 |
begin |
| 173 |
if ScaleOrientation.W <> 0 then |
| 174 |
begin |
| 175 |
RotationMatricesRad(ScaleOrientation, MRotateScaleOrient, IMRotateScaleOrient); |
| 176 |
Transform := Transform * MRotateScaleOrient; |
| 177 |
InverseTransform := IMRotateScaleOrient * InverseTransform; |
| 178 |
end; |
| 179 |
|
| 180 |
{ For scaling, we explicitly request that if ScalingFactor contains |
| 181 |
zero, IM will be forced to be identity (the 2nd param to ScalingMatrices |
| 182 |
is "true"). That's because X3D allows |
| 183 |
scaling factor to have 0 components (we need InverseTransform only |
| 184 |
for special tricks). } |
| 185 |
|
| 186 |
ScalingMatrices(ScaleVector, true, M, IM); |
| 187 |
Transform := Transform * M; |
| 188 |
InverseTransform := IM * InverseTransform; |
| 189 |
|
| 190 |
if ScaleOrientation.W <> 0 then |
| 191 |
begin |
| 192 |
{ That's right, we reuse MRotateScaleOrient and IMRotateScaleOrient |
| 193 |
matrices below. Since we want to reverse them now, so normal |
| 194 |
Transform is multiplied by IM and InverseTransform is multiplied by M. } |
| 195 |
Transform := Transform * IMRotateScaleOrient; |
| 196 |
InverseTransform := MRotateScaleOrient * InverseTransform; |
| 197 |
end; |
| 198 |
|
| 199 |
Scale := Scale * Approximate3DScale(ScaleVector); |
| 200 |
UniformScale := UniformScale and |
| 201 |
(ScaleVector.X = ScaleVector.Y) and |
| 202 |
(ScaleVector.Y = ScaleVector.Z); |
| 203 |
end; |
| 204 |
|
| 205 |
MultMatricesTranslation(Transform, InverseTransform, -Center); |
| 206 |
end; |
| 207 |
|
| 208 |
procedure TTransformation.Multiply( |
| 209 |
const Rotation: TVector4; |
| 210 |
const ScaleVector: TVector3; |
| 211 |
const Translation: TVector3); |
| 212 |
var |
| 213 |
M, IM: TMatrix4; |
| 214 |
begin |
| 215 |
{ To make InverseTransform, we multiply inverted matrices in inverted order |
| 216 |
below. } |
| 217 |
|
| 218 |
MultMatricesTranslation(Transform, InverseTransform, Translation); |
| 219 |
|
| 220 |
{ We avoid using RotationMatricesRad when angle = 0, since this |
| 221 |
is often the case, and it makes TransformState much faster |
| 222 |
(which is important --- TransformState is important for traversing state). } |
| 223 |
if Rotation.W <> 0 then |
| 224 |
begin |
| 225 |
{ Note that even rotation Axis = zero is OK, both M and IM will be |
| 226 |
identity in this case. } |
| 227 |
RotationMatricesRad(Rotation, M, IM); |
| 228 |
Transform := Transform * M; |
| 229 |
InverseTransform := IM * InverseTransform; |
| 230 |
end; |
| 231 |
|
| 232 |
if (ScaleVector.X <> 1) or |
| 233 |
(ScaleVector.Y <> 1) or |
| 234 |
(ScaleVector.Z <> 1) then |
| 235 |
begin |
| 236 |
{ For scaling, we explicitly request that if ScalingFactor contains |
| 237 |
zero, IM will be forced to be identity (the 2nd param to ScalingMatrices |
| 238 |
is "true"). That's because X3D allows |
| 239 |
scaling factor to have 0 components (we need InverseTransform only |
| 240 |
for special tricks). } |
| 241 |
|
| 242 |
ScalingMatrices(ScaleVector, true, M, IM); |
| 243 |
Transform := Transform * M; |
| 244 |
InverseTransform := IM * InverseTransform; |
| 245 |
|
| 246 |
Scale := Scale * Approximate3DScale(ScaleVector); |
| 247 |
UniformScale := UniformScale and |
| 248 |
(ScaleVector.X = ScaleVector.Y) and |
| 249 |
(ScaleVector.Y = ScaleVector.Z); |
| 250 |
end; |
| 251 |
end; |
| 252 |
|
| 253 |
procedure TTransformation.MultiplyMatrix(const Matrix: TMatrix4); |
| 254 |
var |
| 255 |
InvertedMatrix: TMatrix4; |
| 256 |
begin |
| 257 |
Transform := Transform * Matrix; |
| 258 |
|
| 259 |
if TryInverseHarder(Matrix, InvertedMatrix) then |
| 260 |
begin |
| 261 |
InverseTransform := InvertedMatrix * InverseTransform; |
| 262 |
end else |
| 263 |
begin |
| 264 |
WritelnLogMultiline('Matrix', |
| 265 |
'Cannot invert matrix:' + NL + Matrix.ToRawString(' ')); |
| 266 |
|
| 267 |
{ When determinant is zero, we treat inverted matrix |
| 268 |
like identity (like in TMatrixTransformNode_1.Transformation). |
| 269 |
So no need to multiply InverseTransform by anything. } |
| 270 |
end; |
| 271 |
|
| 272 |
Scale := Scale * Approximate3DScale(Matrix); |
| 273 |
|
| 274 |
// we have to assume non-uniform matrix, see comments in the interface |
| 275 |
UniformScale := false; |
| 276 |
end; |
| 277 |
|
| 278 |
procedure TTransformation.ScaleToIdentity; |
| 279 |
var |
| 280 |
Translation, ScaleVec: TVector3; |
| 281 |
Rotation: TVector4; |
| 282 |
begin |
| 283 |
// early exit if we don't need to do anything |
| 284 |
if UniformScale and SameValue(Scale, 1) then |
| 285 |
Exit; |
| 286 |
|
| 287 |
MatrixDecompose(Transform, Translation, Rotation, ScaleVec); |
| 288 |
Init; |
| 289 |
Multiply(Rotation, NoScale, Translation); |
| 290 |
end; |
| 291 |
|
| 292 |
{$endif read_implementation} |