diff options
[WGL/P] Minor style changes and fixed level loading
4 files changed, 43 insertions, 21 deletions
diff --git a/Year 2/Computer Graphics with WebGL/Project/Project.html b/Year 2/Computer Graphics with WebGL/Project/Project.html index 5708803..5379b6d 100644 --- a/Year 2/Computer Graphics with WebGL/Project/Project.html +++ b/Year 2/Computer Graphics with WebGL/Project/Project.html @@ -57,7 +57,8 @@ h2 { top: 0;
padding: 0.5em;
max-width: 24em;
- max-height: calc(100vh - 0.5em);
+ max-height: calc(100vh - 2em);
+ overflow: auto;
}
#coordinates {
left: 0;
@@ -130,18 +131,21 @@ h2 { window.onresize = resizeglcanvas;
var pauseAnimations = false;
- var startT = -1;
+ var t = 0;
function drawFrame()
{
- if (startT == -1) startT = Date.now();
- var t = Math.floor(Date.now() - startT) / 10; // tenths of a second
+ if (startT == -1) { startT = Date.now(); t = 0; }
+ var candidat = Math.floor(Date.now() - startT) / 10; // tenths of a second
+ if (pauseAnimations) startT += (candidat - t)*10;
+ else t = candidat;
+
gl.clear(gl.COLOR_BUFFER_BIT+gl.DEPTH_BUFFER_BIT);
updateCamera();
gl.uniform3fv(uEye, pos);
skybox.draw();
- if (!pauseAnimations) animations.forEach((a) => a(t));
+ if (!pauseAnimations || t == 0) animations.forEach((a) => a(t));
stars.forEach((s) => s.draw());
requestAnimationFrame(drawFrame);
diff --git a/Year 2/Computer Graphics with WebGL/Project/blackhole.js b/Year 2/Computer Graphics with WebGL/Project/blackhole.js index 28252d1..0bd0ff5 100644 --- a/Year 2/Computer Graphics with WebGL/Project/blackhole.js +++ b/Year 2/Computer Graphics with WebGL/Project/blackhole.js @@ -1,14 +1,14 @@ -blackHolesBuf = []; - -BlackHole = function(center, size) { +BlackHole = function(center, size, blackHolesBuf) { if (blackHolesBuf.length == BLACKHOLE_MAX_COUNT) throw 'Error: cannot add more black holes, limit of ' + BLACKHOLE_MAX_COUNT + ' reached!' this.rs = size / 3; this.index = blackHolesBuf.push([]) - 1; - this.moveTo(center); + this.center = center; + + blackHolesBuf[this.index] = center.concat(this.rs); } -BlackHole.prototype.moveTo = function(newCenter) { +BlackHole.prototype.moveTo = function(newCenter, blackHolesBuf) { blackHolesBuf[this.index] = newCenter.concat(this.rs); gl.uniform4fv(gl.getUniformLocation(glprog, 'uBH[' + this.index + ']'), blackHolesBuf[this.index]); } 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); } diff --git a/Year 2/Computer Graphics with WebGL/Project/star.js b/Year 2/Computer Graphics with WebGL/Project/star.js index de1e9e9..8aad803 100644 --- a/Year 2/Computer Graphics with WebGL/Project/star.js +++ b/Year 2/Computer Graphics with WebGL/Project/star.js @@ -1,15 +1,16 @@ -starsBuf = []; - -Star = function(center, size) { +Star = function(center, size, starsBuf) { if (starsBuf.length == STAR_MAX_COUNT) throw 'Error: cannot add more stars, limit of ' + STAR_MAX_COUNT + ' reached!' this.sphere = new Sphere(center, size); this.sphere.color = [0.956,0.5,0.215]; this.index = starsBuf.push([]) - 1; - this.moveTo(center); + this.center = center; + + this.sphere.center = center; + starsBuf[this.index] = center.concat(this.sphere.size); } -Star.prototype.moveTo = function(newCenter) { +Star.prototype.moveTo = function(newCenter, starsBuf) { this.sphere.center = newCenter; starsBuf[this.index] = newCenter.concat(this.sphere.size); gl.uniform4fv(gl.getUniformLocation(glprog, 'uStars[' + this.index + ']'), starsBuf[this.index]); |
