1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/small3dlib.git synced 2024-11-21 20:39:57 +01:00

Use mat transposition for inverse

This commit is contained in:
Miloslav Číž 2019-06-02 18:23:45 +02:00
parent 09479b7fa1
commit 596b9a48bf

View file

@ -398,6 +398,8 @@ typedef S3L_Unit S3L_Mat4[4][4]; /**< 4x4 matrix, used mostly for 3D
/** Initializes a 4x4 matrix to identity. */ /** Initializes a 4x4 matrix to identity. */
static inline void S3L_initMat4(S3L_Mat4 *m); static inline void S3L_initMat4(S3L_Mat4 *m);
void S3L_transposeMat4(S3L_Mat4 *m);
void S3L_makeTranslationMat( void S3L_makeTranslationMat(
S3L_Unit offsetX, S3L_Unit offsetX,
S3L_Unit offsetY, S3L_Unit offsetY,
@ -419,12 +421,6 @@ void S3L_makeRotationMatrixZXY(
S3L_Unit byZ, S3L_Unit byZ,
S3L_Mat4 *m); S3L_Mat4 *m);
void S3L_makeRotationMatrixYXZ(
S3L_Unit byX,
S3L_Unit byY,
S3L_Unit byZ,
S3L_Mat4 *m);
void S3L_makeWorldMatrix(S3L_Transform3D worldTransform, S3L_Mat4 *m); void S3L_makeWorldMatrix(S3L_Transform3D worldTransform, S3L_Mat4 *m);
void S3L_makeCameraMatrix(S3L_Transform3D cameraTransform, S3L_Mat4 *m); void S3L_makeCameraMatrix(S3L_Transform3D cameraTransform, S3L_Mat4 *m);
@ -956,47 +952,6 @@ void S3L_makeRotationMatrixZXY(
#undef S #undef S
} }
void S3L_makeRotationMatrixYXZ(
S3L_Unit byX,
S3L_Unit byY,
S3L_Unit byZ,
S3L_Mat4 *m)
{
S3L_Unit sx = S3L_sin(byX);
S3L_Unit sy = S3L_sin(byY);
S3L_Unit sz = S3L_sin(byZ);
S3L_Unit cx = S3L_cos(byX);
S3L_Unit cy = S3L_cos(byY);
S3L_Unit cz = S3L_cos(byZ);
#define M(x,y) (*m)[x][y]
#define S S3L_FRACTIONS_PER_UNIT
M(0,0) = (cy * cz) / S - (sy * sx * sz) / (S * S);
M(1,0) = (cy * sz) / S + (sx * sy * cz) / (S * S);
M(2,0) = -1 * (cx * sy) / S;
M(3,0) = 0;
M(0,1) = -1 * (sz * cx) / S;
M(1,1) = (cz * cx) / S;
M(2,1) = sx;
M(3,1) = 0;
M(0,2) = (cz * sy) / S + (sz * sx * cy) / (S * S);
M(1,2) = (sz * sy) / S - (cz * sx * cy) / (S * S);
M(2,2) = (cx * cy) / S;
M(3,2) = 0;
M(0,3) = 0;
M(1,3) = 0;
M(2,3) = 0;
M(3,3) = S3L_FRACTIONS_PER_UNIT;
#undef M
#undef S
}
void S3L_initTransoform3D(S3L_Transform3D *t) void S3L_initTransoform3D(S3L_Transform3D *t)
{ {
S3L_initVec4(&(t->translation)); S3L_initVec4(&(t->translation));
@ -1691,6 +1646,19 @@ void S3L_makeWorldMatrix(S3L_Transform3D worldTransform, S3L_Mat4 *m)
S3L_mat4Xmat4(m,&t); S3L_mat4Xmat4(m,&t);
} }
void S3L_transposeMat4(S3L_Mat4 *m)
{
S3L_Unit tmp;
for (uint8_t y = 0; y < 3; ++y)
for (uint8_t x = 1 + y; x < 4; ++x)
{
tmp = (*m)[x][y];
(*m)[x][y] = (*m)[y][x];
(*m)[y][x] = tmp;
}
}
void S3L_makeCameraMatrix(S3L_Transform3D cameraTransform, S3L_Mat4 *m) void S3L_makeCameraMatrix(S3L_Transform3D cameraTransform, S3L_Mat4 *m)
{ {
S3L_makeTranslationMat( S3L_makeTranslationMat(
@ -1701,12 +1669,14 @@ void S3L_makeCameraMatrix(S3L_Transform3D cameraTransform, S3L_Mat4 *m)
S3L_Mat4 r; S3L_Mat4 r;
S3L_makeRotationMatrixYXZ( S3L_makeRotationMatrixZXY(
cameraTransform.rotation.x, -1 * cameraTransform.rotation.x,
cameraTransform.rotation.y, -1 * cameraTransform.rotation.y,
cameraTransform.rotation.z, -1 * cameraTransform.rotation.z,
&r); &r);
S3L_transposeMat4(&r); // transposing creates an inverse transform
S3L_mat4Xmat4(m,&r); S3L_mat4Xmat4(m,&r);
} }