From b14ebd29494694bdba1130488a3badf29525ea0c Mon Sep 17 00:00:00 2001 From: fdai7303 Date: Thu, 13 Jul 2023 09:32:00 +0200 Subject: [PATCH] finished exercise 07 & startet 08 --- src/07/matrix.ts | 70 ++++++++++++++++++++++++++++++++++---- src/08/setup-scenegraph.ts | 29 ++++++++++++---- 2 files changed, 87 insertions(+), 12 deletions(-) diff --git a/src/07/matrix.ts b/src/07/matrix.ts index 2ada48a..2eaa5c2 100644 --- a/src/07/matrix.ts +++ b/src/07/matrix.ts @@ -64,9 +64,13 @@ export default class Matrix { * @return The resulting translation matrix */ static translation(translation: Vector): Matrix { - // TODO: Return a new Matrix that is translated according to the given Vector - return Matrix.identity(); + return new Matrix([ + 1, 0, 0, translation.x, + 0, 1, 0, translation.y, + 0, 0, 1, translation.z, + 0, 0, 0, 1 + ]) } /** @@ -82,7 +86,32 @@ export default class Matrix { // TODO: (1, 0, 0, 0) to specify the x-axis // TODO: (0, 1, 0, 0) to specify the y-axis // TODO: (0, 0, 1, 0) to specify the z-axis - return Matrix.identity(); + if(axis.x == 1) { + return new Matrix ([ + 1, 0, 0, 0, + 0, Math.cos(angle), -1 * Math.sin(angle), 0, + 0, Math.sin(angle), Math.cos(angle), 0, + 0, 0, 0, 1 + ]); + } else + if(axis.y == 1) { + return new Matrix ([ + Math.cos(angle), 0, Math.sin(angle), 0, + 0, 1, 0, 0, + -1 * Math.sin(angle), 0, Math.cos(angle), 0, + 0, 0, 0, 1 + ]); + } else + if(axis.z == 1) { + return new Matrix ([ + Math.cos(angle), -1 * Math.sin(angle), 0, 0, + Math.sin(angle), Math.cos(angle), 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1 + ]); + } else { + return Matrix.identity(); + } } /** @@ -92,7 +121,12 @@ export default class Matrix { */ static scaling(scale: Vector): Matrix { // TODO: Return a new scaling Matrix with the scaling components of the Vector scale - return Matrix.identity(); + return new Matrix([ + scale.x, 0, 0, 0, + 0, scale.y, 0, 0, + 0, 0, scale.z, 0, + 0, 0, 0, 1 + ]) } @@ -116,7 +150,19 @@ export default class Matrix { */ mul(other: Matrix): Matrix { // TODO: Return a new Matrix mat with mat = this * other - return Matrix.identity(); + var aNumRows = 4, aNumCols = 4, + bNumRows = 4, bNumCols = 4, + m = new Matrix([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]); // initialize array of rows + for (var r = 0; r < aNumRows; ++r) { + for (var c = 0; c < bNumCols; ++c) { + let temp = 0; + for (var i = 0; i < aNumCols; ++i) { + temp += this.getVal(r, i) * other.getVal(i,c); + } + m.setVal(r, c, temp); + } + } + return m; } /** @@ -126,7 +172,19 @@ export default class Matrix { */ mulVec(other: Vector): Vector { // TODO: Return a new Vector vec with vec = this * other - return new Vector(0, 0, 0, 0); + var aNumRows = 4, aNumCols = 4, + bNumRows = 4, bNumCols = 4, + m = new Array(0,0,0,0); // initialize result vector + for (var r = 0; r < aNumRows; ++r) { + for (var c = 0; c < bNumCols; ++c) { + let temp = 0; + for (var i = 0; i < aNumCols; ++i) { + temp += this.getVal(r, i) * other.valueOf()[i]; + } + m[r] = temp; + } + } + return new Vector(m[0], m[1], m[2], m[3]); } /** diff --git a/src/08/setup-scenegraph.ts b/src/08/setup-scenegraph.ts index c3e9a73..eb72b54 100644 --- a/src/08/setup-scenegraph.ts +++ b/src/08/setup-scenegraph.ts @@ -3,8 +3,9 @@ import 'bootstrap/scss/bootstrap.scss'; import Vector from '../05/vector'; import { GroupNode, SphereNode } from './nodes'; -import { Rotation, Scaling, Translation } from './transformation'; +import { MatrixTransformation, Rotation, Scaling, Translation } from './transformation'; import { raytracePhong } from './raytracing'; +import Matrix from '../07/matrix'; window.addEventListener('load', () => { @@ -35,15 +36,31 @@ window.addEventListener('load', () => { // TODO: o root GroupNode with Translation (0, 0, -5) // TODO: / \ // TODO: / \ - // TODO: SphereNode o o GroupNode with moonRotation - // TODO: \ - // TODO: o GroupNode with Translation (2, 0, 0) + // TODO: SphereNode o o GroupNode with moonRotation: two + // TODO: one \ + // TODO: o GroupNode with Translation (2, 0, 0): three // TODO: \ - // TODO: o GroupNode with Scaling (0.2, 0.2, 0.2) + // TODO: o GroupNode with Scaling (0.2, 0.2, 0.2): four // TODO: \ - // TODO: o SphereNode + // TODO: o SphereNode: five // TODO: + //baumteile + let temp = Matrix.identity(); + let one = new SphereNode(new MatrixTransformation(temp, temp), new Vector(0,0,0,0)); //one + let two = new GroupNode(new MatrixTransformation(temp, temp)); //two + let three = new GroupNode(new MatrixTransformation(temp, temp)); //three + let four = new GroupNode(new MatrixTransformation(temp, temp)); //four + let five = new SphereNode(new MatrixTransformation(temp, temp), new Vector(0,0,0,0)); //five + + //baumstruktur + root.add(one); + root.add(two); + two.add(three); + three.add(four); + four.add(five); + + //animation let animationHandle: number; let lastTimestamp = 0;