diff options
| author | Markus Mittendrein <maxmitti@maxmitti.tk> | 2022-02-20 18:19:29 +0100 |
|---|---|---|
| committer | Markus Mittendrein <maxmitti@maxmitti.tk> | 2022-02-20 18:19:29 +0100 |
| commit | 4a7ce9ade25b0ec98866e41824c509b65f673330 (patch) | |
| tree | cd061a1044bd28fc1188834d88f306a852e7a55c /DTTransform.c | |
| parent | 1f3327795d7d47a3058a0a72efd5f163441eaf7b (diff) | |
| download | System.c4g-4a7ce9ade25b0ec98866e41824c509b65f673330.tar.gz System.c4g-4a7ce9ade25b0ec98866e41824c509b65f673330.zip | |
Cleanup DTTransform and remove wrong 3D stuff
Diffstat (limited to 'DTTransform.c')
| -rw-r--r-- | DTTransform.c | 179 |
1 files changed, 83 insertions, 96 deletions
diff --git a/DTTransform.c b/DTTransform.c index baf9e8e..92d4788 100644 --- a/DTTransform.c +++ b/DTTransform.c @@ -1,11 +1,41 @@ -#strict 2 +#strict 3 + +/* + // Examples: + obj->SetTransform([Rotate(90)]); // rotates graphics of obj around center by 90° clock-wise; array brackets necessary! + obj->SetTransform([Rotate(90), TranslateY(5)]); // rotates graphics of obj around center by 90° clock-wise and translates it down by 5 pixel + // order matters! + obj->SetTransform([TranslateY(5), Rotate(90)]); // translates graphics of obj down by 5 pixel and and rotates it around the point 5 pixel above center by 90° clock-wise; effectively the same as rotating around center by 90° and moving to the left by 5 pixel + + // obj->ResetTransform(); // resets transformation + + // Translate* and Rotate have a precision parameter, which defaults to 1: + obj->SetTransform([TranslateY(500, 1000)]); // translates graphics of obj down by 0.5 pixel + obj->SetTransform([TranslateY(1, 2)]); // also translates graphics of obj down by 0.5 pixel + + // Working with overlays: + // base graphic is overlay 0 as usual + obj->SetTransform([...], 1); // applies to overlay 1 + obj->SetTransform([...], -10); // applies to overlays 0 through 10 (inclusive) + obj->SetTransform([...], [1, 5]); // applies to overlays 1 and 5 + obj->SetTransform([...], [2, -4, 10, -11, 42]); // applies to overlays 2,3,4 and 10,11 and 42 + obj->ResetTransform(overlays); // resets transformations of overlays as specified in SetTransform +*/ + +// All matrices are stored linearly in row major order global func ResetTransform(overlays) { for(var overlay in TransformOverlays(overlays)) { - if(!overlay) SetObjDrawTransform(0, 0, 0, 0, 0, 0, this, overlay); - else SetObjDrawTransform(1000, 0, 0, 0, 1000, 0, this, overlay); + if(overlay) + { + SetObjDrawTransform(1000, 0, 0, 0, 1000, 0, this, overlay); + } + else + { + SetObjDrawTransform(); + } } } @@ -24,14 +54,15 @@ global func AddTransform(array transforms, overlays) { for(var transform in transforms) { - SetObjDrawTransform2(transform[0][0], transform[0][1], transform[0][2], transform[1][0], transform[1][1], transform[1][2], transform[2][0], transform[2][1], transform[2][2], overlay); + SetObjDrawTransform2(transform[0], transform[1], transform[2], transform[3], transform[4], transform[5], transform[6], transform[7], transform[8], overlay); } } } global func TransformOverlays(overlays) { - if(!overlays || GetType(overlays) == C4V_Int) + overlays ??= 0; + if(GetType(overlays) == C4V_Int) { if(overlays >= 0) return [overlays]; else @@ -85,123 +116,89 @@ global func TransformOverlays(overlays) } } -global func Rotate() { return RotateZ(...); } - -global func RotateX(int phi, int prec) +global func Rotate(int phi, int prec) { - prec = prec || 1; + prec ??= 1; var cos = Cos(phi, 1000, prec); var sin = Sin(phi, 1000, prec); - return - [ - [1000, 0, 0], - [ 0, cos, -sin], - [ 0, sin, cos] + return [ + cos, -sin, 0, + sin, cos, 0, + 0, 0, 1000 ]; } -global func RotateY(int phi, int prec) +global func ScaleXY(int x, int y) { - prec = prec || 1; - - var cos = Cos(phi, 1000, prec); - var sin = Sin(phi, 1000, prec); - - return - [ - [ cos, 0, sin], - [ 0, 1000, 0], - [-sin, 0, cos] + return [ + x, 0, 0, + 0, y, 0, + 0, 0, 1000 ]; } -global func RotateZ(int phi, int prec) +global func ScaleX(int f) { return ScaleXY(f, 1000); } +global func ScaleY(int f) { return ScaleXY(1000, f); } +global func Scale(int f) { - prec = prec || 1; - - var cos = Cos(phi, 1000, prec); - var sin = Sin(phi, 1000, prec); - - return - [ - [cos, -sin, 0], - [sin, cos, 0], - [ 0, 0, 1000] - ]; -} - -global func Scale3D(int x, int y, int z) -{ - return - [ - [x, 0, 0], - [0, y, 0], - [0, 0, z] + return [ + 1000, 0, 0, + 0, 1000, 0, + 0, 0, 1000000/f ]; } -global func Scale2D(int x, int y) { return Scale3D(x, y, 1000); } - -global func ScaleX(int f) { return Scale3D(f, 1000, 1000); } -global func ScaleY(int f) { return Scale3D(1000, f, 1000); } -global func ScaleZ(int f) { return Scale3D(1000, 1000, f); } - -global func Translate2D(int x, int y, int prec) +global func TranslateXY(int x, int y, int prec) { - prec = prec || 1; - return - [ - [1000, 0, x * 1000 / prec], - [ 0, 1000, y * 1000 / prec], - [ 0, 0, 1000] + prec ??= 1; + return [ + 1000, 0, x * 1000 / prec, + 0, 1000, y * 1000 / prec, + 0, 0, 1000 ]; } -global func TranslateX(int x, int prec) { return Translate2D(x, 0, prec); } -global func TranslateY(int y, int prec) { return Translate2D(0, y, prec); } +global func TranslateX(int x, int prec) { return TranslateXY(x, 0, prec); } +global func TranslateY(int y, int prec) { return TranslateXY(0, y, prec); } global func MirrorX() { - return - [ - [-1000, 0, 0], - [ 0, 1000, 0], - [ 0, 0, 1000] + return [ + -1000, 0, 0, + 0, 1000, 0, + 0, 0, 1000 ]; } global func MirrorY() { - return - [ - [1000, 0, 0], - [ 0, -1000, 0], - [ 0, 0, 1000] + return [ + 1000, 0, 0, + 0, -1000, 0, + 0, 0, 1000 ]; } -global func Skew2D(int x, int y) +global func SkewXY(int x, int y) { - return - [ - [1000, x, 0], - [ y, 1000, 0], - [ 0, 0, 1000] + return [ + 1000, x, 0, + y, 1000, 0, + 0, 0, 1000 ]; } -global func SkewX(int x) { return Skew2D(x, 0); } -global func SkewY(int y) { return Skew2D(0, y); } +global func SkewX(int x) { return SkewXY(x, 0); } +global func SkewY(int y) { return SkewXY(0, y); } global func Matrix3x3Multiply(array a, array b, int precision) { - return - [ - [(a[2][0] * b[0][2] + a[1][0] * b[0][1] + a[0][0] * b[0][0]) / precision, (a[2][1] * b[0][2] + a[1][1] * b[0][1] + a[0][1] * b[0][0]) / precision, (a[2][2] * b[0][2] + a[1][2] * b[0][1] + a[0][2] * b[0][0]) / precision], - [(a[2][0] * b[1][2] + a[1][0] * b[1][1] + a[0][0] * b[1][0]) / precision, (a[2][1] * b[1][2] + a[1][1] * b[1][1] + a[0][1] * b[1][0]) / precision, (a[2][2] * b[1][2] + a[1][2] * b[1][1] + a[0][2] * b[1][0]) / precision], - [(a[2][0] * b[2][2] + a[1][0] * b[2][1] + a[0][0] * b[2][0]) / precision, (a[2][1] * b[2][2] + a[1][1] * b[2][1] + a[0][1] * b[2][0]) / precision, (a[2][2] * b[2][2] + a[1][2] * b[2][1] + a[0][2] * b[2][0]) / precision] + return [ + (a[6] * b[2] + a[3] * b[1] + a[0] * b[0]) / precision, (a[7] * b[2] + a[4] * b[1] + a[1] * b[0]) / precision, (a[8] * b[2] + a[5] * b[1] + a[2] * b[0]) / precision, + (a[6] * b[5] + a[3] * b[4] + a[0] * b[3]) / precision, (a[7] * b[5] + a[4] * b[4] + a[1] * b[3]) / precision, (a[8] * b[5] + a[5] * b[4] + a[2] * b[3]) / precision, + (a[6] * b[8] + a[3] * b[7] + a[0] * b[6]) / precision, (a[7] * b[8] + a[4] * b[7] + a[1] * b[6]) / precision, (a[8] * b[8] + a[5] * b[7] + a[2] * b[6]) / precision ]; } @@ -209,7 +206,7 @@ global func Matrix3x3Multiply(array a, array b, int precision) // May be used to cache transforms. For direct application use SetTransform instead, which implicitly uses the native engine implementation of matrix multiplication. global func PreTransform(array transforms) { - if(GetLength(transforms) == 0) + if(!transforms || GetLength(transforms) == 0) { FatalError("PreTransform called without transforms."); } @@ -222,14 +219,4 @@ global func PreTransform(array transforms) } return ret; } -} - -// Examples - -// Clonk with giant head: -// GetCursor(0)->SetTransform([TranslateY(8), RotateX(4), ScaleZ(400), TranslateY(-10)]) - -// 3D-Rotating Hut: -// Schedule("FindObject(HUT2)->SetTransform([TranslateY(-20), RotateZ(++Global(0)), TranslateY(0), RotateX(-1), TranslateY(20)])", 1, 10000) -// 3D-Rotating Clonk: -// Schedule("GetCursor(0)->SetTransform([TranslateY(-8), RotateZ(++Global(0)), TranslateY(0), RotateX(-2), TranslateY(8)])", 1, 10000)
\ No newline at end of file +}
\ No newline at end of file |
