Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Computer Graphics with WebGL / Project / level.js
diff options
context:
space:
mode:
Commitad2776375379600364882b9515d049509509903b (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-08-28 10:08:33 +0300
Committer
 Syndamia <kamen@syndamia.com>
2023-08-28 10:08:33 +0300
Filesd94f5b432be8b670826dbefbbdcb40d0ec81e624 /Year 2/Computer Graphics with WebGL/Project/level.js
uni-projects-ad2776375379600364882b9515d049509509903b.tar
uni-projects-ad2776375379600364882b9515d049509509903b.tar.gz
uni-projects-ad2776375379600364882b9515d049509509903b.zip
Parent43d4296dc06b1d696fe4b236e614a04cb0705b04 (diff)
[WGL/P] Minor style changes and fixed level loading
1 files changed, 23 insertions, 6 deletions
-rw-r--r--Year 2/Computer Graphics with WebGL/Project/level.js29
diff --git a/Year 2/Computer Graphics with WebGL/Project/level.js b/Year 2/Computer Graphics with WebGL/Project/level.js
index 0172175..6d193a3 100644
--- a/Year 2/Computer Graphics with WebGL/Project/level.js
+++ b/Year 2/Computer Graphics with WebGL/Project/level.js
@@ -1,8 +1,11 @@
stars = [];
blackHoles = [];
animations = [];
+starsBuf = [];
+blackHolesBuf = [];
levels = [];
+var startT = -1;
Level = function(name, description, cameraCoords) {
this.name = name;
@@ -11,23 +14,37 @@ Level = function(name, description, 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);
+ var star = new Star(center, size, this.starsBuf);
this.stars.push(star);
- if (animation) this.animations.push((t) => star.moveTo(animation(t, 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);
+ var bh = new BlackHole(center, size, this.blackHolesBuf);
this.blackHoles.push(bh);
- if (animation) this.animations.push((t) => bh.moveTo(animation(t, 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() {
@@ -35,13 +52,13 @@ function createLevels() {
"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) => [1100 -100 * (1 + Math.sin(t/300 + 1.5 * Math.PI)),0,1000]);
+ l1.addStar([1100,0,1000], 10, (t, star) => vecSum(star.center, [-100 * (1 + Math.sin(t/300 + 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([1100,0,1000], 10, (t, star) => vecSum(star.sphere.center, [-1,0,0]));
+ l2.addStar([1070,0,1000], 10, (t, star) => vecSum(star.center, [-t/8,0,0]));
l2.addBlackHole([1000,0,900], 30, undefined);
}