From ad2776375379600364882b9515d049509509903b Mon Sep 17 00:00:00 2001 From: Syndamia Date: Mon, 28 Aug 2023 10:08:33 +0300 Subject: [WGL/P] Minor style changes and fixed level loading --- .../Computer Graphics with WebGL/Project/level.js | 29 +++++++++++++++++----- 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'Year 2/Computer Graphics with WebGL/Project/level.js') 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); } -- cgit v1.3.1-26-gb76a