Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Computer Graphics with WebGL / Project / level.js
blob: 24944ad0a7e93e00134fa3f99923e2bb28030586 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
stars = [];
blackHoles = [];
animations = [];
starsBuf = [];
blackHolesBuf = [];

levels = [];
var startT = -1;

Level = function(name, description, cameraCoords) {
	this.name = name;
	this.description = description;
	this.cameraCoords = cameraCoords;
	this.stars = [];
	this.blackHoles = [];
	this.animations = [];
	this.starsBuf = [];
	this.blackHolesBuf = [];
	levels.push(this);
}
Level.prototype.addStar = function(center, size, animation) {
	var star = new Star(center, size, this.starsBuf);
	this.stars.push(star);
	if (animation) this.animations.push((t) => star.moveTo(animation(t, star), this.starsBuf));
}
Level.prototype.addBlackHole = function(center, size, animation) {
	var bh = new BlackHole(center, size, this.blackHolesBuf);
	this.blackHoles.push(bh);
	if (animation) this.animations.push((t) => bh.moveTo(animation(t, bh), this.blackHolesBuf));
}
Level.prototype.load = function() {
	stars = this.stars;
	blackHoles = this.blackHoles;
	starsBuf = this.starsBuf;
	blackHolesBuf = this.blackHolesBuf;
	animations = this.animations;
	pos = this.cameraCoords;

	for (var i = 0; i < STAR_MAX_COUNT; i++) {
		var val = (i < starsBuf.length) ? starsBuf[i] : [0,0,0,0];
		gl.uniform4fv(gl.getUniformLocation(glprog, 'uStars[' + i + ']'), val);
	}
	for (var i = 0; i < BLACKHOLE_MAX_COUNT; i++) {
		var val = (i < blackHolesBuf.length) ? blackHolesBuf[i] : [0,0,0,0];
		gl.uniform4fv(gl.getUniformLocation(glprog, 'uBH[' + i + ']'), val);
	}
	startT = -1;
}

function createLevels() {
	var l1 = new Level(
		"Star passing behind black hole",
		"You can see how the black hole (correctly) warps the light of the star behind it when it passes",
		[1000,0,650]);
	l1.addStar([1100,0,1000], 10, (t, star) => vecSum(star.center, [-100 * (1 + Math.sin(t/30 + 1.5 * Math.PI)),0,0]));
	l1.addBlackHole([1000,0,900], 30, undefined);

	var l2 = new Level(
		"Star gets closer to event horizon",
		"As the star passes by behind the black hole, we can see it approaching the event horizon, however, it will never truly reach it and dissapear (at least until it's width becomes smaller than a pixel).",
		[1014,0,892]);
	l2.addStar([1070,0,1000], 10, (t, star) => vecSum(star.center, [-t,0,0]));
	l2.addBlackHole([1000,0,900], 30, undefined);
}