Browse Source

finished exercise 07 & startet 08

master
fdai7303 1 year ago
parent
commit
b14ebd2949
  1. 70
      src/07/matrix.ts
  2. 29
      src/08/setup-scenegraph.ts

70
src/07/matrix.ts

@ -64,9 +64,13 @@ export default class Matrix {
* @return The resulting translation matrix * @return The resulting translation matrix
*/ */
static translation(translation: Vector): Matrix { static translation(translation: Vector): Matrix {
// TODO: Return a new Matrix that is translated according to the given Vector // 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: (1, 0, 0, 0) to specify the x-axis
// TODO: (0, 1, 0, 0) to specify the y-axis // TODO: (0, 1, 0, 0) to specify the y-axis
// TODO: (0, 0, 1, 0) to specify the z-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 { static scaling(scale: Vector): Matrix {
// TODO: Return a new scaling Matrix with the scaling components of the Vector scale // 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 { mul(other: Matrix): Matrix {
// TODO: Return a new Matrix mat with mat = this * other // 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 { mulVec(other: Vector): Vector {
// TODO: Return a new Vector vec with vec = this * other // 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]);
} }
/** /**

29
src/08/setup-scenegraph.ts

@ -3,8 +3,9 @@ import 'bootstrap/scss/bootstrap.scss';
import Vector from '../05/vector'; import Vector from '../05/vector';
import { GroupNode, SphereNode } from './nodes'; import { GroupNode, SphereNode } from './nodes';
import { Rotation, Scaling, Translation } from './transformation';
import { MatrixTransformation, Rotation, Scaling, Translation } from './transformation';
import { raytracePhong } from './raytracing'; import { raytracePhong } from './raytracing';
import Matrix from '../07/matrix';
window.addEventListener('load', () => { window.addEventListener('load', () => {
@ -35,15 +36,31 @@ window.addEventListener('load', () => {
// TODO: o root GroupNode with Translation (0, 0, -5) // TODO: o root GroupNode with Translation (0, 0, -5)
// TODO: / \ // TODO: / \
// 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: \
// 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: \
// TODO: o SphereNode
// TODO: o SphereNode: five
// TODO: // 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 animationHandle: number;
let lastTimestamp = 0; let lastTimestamp = 0;

Loading…
Cancel
Save