From 46ee648fff31004221d307d0af9cabc0c4250a4c Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 27 Aug 2023 12:01:37 +0300 Subject: [WGL/P] Minor formatting and naming changes --- .../Computer Graphics with WebGL/Project/Project.html | 18 +++++------------- .../Computer Graphics with WebGL/Project/blackhole.js | 4 ++-- Year 2/Computer Graphics with WebGL/Project/shaders.js | 8 ++++++-- .../Computer Graphics with WebGL/Project/spaceship.js | 8 ++++---- Year 2/Computer Graphics with WebGL/Project/star.js | 4 ++-- 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/Year 2/Computer Graphics with WebGL/Project/Project.html b/Year 2/Computer Graphics with WebGL/Project/Project.html index 4c88553..6133dba 100644 --- a/Year 2/Computer Graphics with WebGL/Project/Project.html +++ b/Year 2/Computer Graphics with WebGL/Project/Project.html @@ -70,7 +70,7 @@ h2 { function start() { - canvas = document.getElementById("picasso"); + canvas = document.getElementById('picasso'); canvas.addEventListener('webglcontextlost',function(event){event.preventDefault();},false); canvas.addEventListener('webglcontextrestored',function(){init();},false); @@ -83,7 +83,7 @@ h2 { function init() { - gl = getContext("picasso"); + gl = getContext('picasso'); glprog = getProgram(vShader,fShader); getVariables(); @@ -104,7 +104,7 @@ h2 { skybox = new Skybox(10000, [0,0,0], undefined); stars = [ - new Star([1100,0,1000], 10), + new Star([1200,0,1000], 10), // new Star([1000,0,900], 30), // new Star([950,0,1100], 40), ]; @@ -132,24 +132,16 @@ h2 { { gl.clear(gl.COLOR_BUFFER_BIT+gl.DEPTH_BUFFER_BIT); - lookUpdate(); + updateCamera(); gl.uniform3fv(uEye, pos); stars[0].moveTo(vecSum(stars[0].sphere.center, [-0.5,0,0])); + skybox.draw(); stars.forEach((s) => s.draw()); requestAnimationFrame(drawFrame); } - document.addEventListener('keypress', (e) => { - if (e.key == 'u') { - console.log(w); - console.log(w*gl.canvas.height / gl.canvas.width); - console.log(forward.toCartesian()); - console.log(left.toCartesian()); - console.log(up.toCartesian()); - } - }); diff --git a/Year 2/Computer Graphics with WebGL/Project/blackhole.js b/Year 2/Computer Graphics with WebGL/Project/blackhole.js index 674093b..28252d1 100644 --- a/Year 2/Computer Graphics with WebGL/Project/blackhole.js +++ b/Year 2/Computer Graphics with WebGL/Project/blackhole.js @@ -2,7 +2,7 @@ blackHolesBuf = []; BlackHole = function(center, size) { if (blackHolesBuf.length == BLACKHOLE_MAX_COUNT) - throw "Error: cannot add more black holes, limit of " + BLACKHOLE_MAX_COUNT + " reached!" + throw 'Error: cannot add more black holes, limit of ' + BLACKHOLE_MAX_COUNT + ' reached!' this.rs = size / 3; this.index = blackHolesBuf.push([]) - 1; @@ -10,7 +10,7 @@ BlackHole = function(center, size) { } BlackHole.prototype.moveTo = function(newCenter) { blackHolesBuf[this.index] = newCenter.concat(this.rs); - gl.uniform4fv(gl.getUniformLocation(glprog, "uBH[" + this.index + "]"), blackHolesBuf[this.index]); + gl.uniform4fv(gl.getUniformLocation(glprog, 'uBH[' + this.index + ']'), blackHolesBuf[this.index]); } BlackHole.prototype.draw = function() { // Black holes are not "drawn" diff --git a/Year 2/Computer Graphics with WebGL/Project/shaders.js b/Year 2/Computer Graphics with WebGL/Project/shaders.js index 88e57c8..1657f5b 100644 --- a/Year 2/Computer Graphics with WebGL/Project/shaders.js +++ b/Year 2/Computer Graphics with WebGL/Project/shaders.js @@ -38,7 +38,7 @@ var vShader =` var fShader = ` precision highp float; - #define MORE_MAGIC 0.01 + #define MAGIC_STEP 0.01 #define STAR_COLOR vec3(0.95,0.5,0.215) varying vec3 vColor; @@ -102,7 +102,7 @@ var fShader = ` // https://github.com/rantonels/starless/blob/7a0c3c4efdc0666f53dcfa48d310fb2164be2d1a/tracer.py // and // https://github.com/dbrant/BlackHoleRaytracer/blob/master/Equation/SchwarzschildBlackHoleEquation.cs - step += normalize(ray - BH.xyz) * -1.5 * (h*h) * MORE_MAGIC / (dist*dist*dist*dist*dist); + step += normalize(ray - BH.xyz) * -1.5 * (h*h) * MAGIC_STEP / (dist*dist*dist*dist*dist); ray += step; } return 1; // orbits inside @@ -128,7 +128,10 @@ var fShader = ` return vec4(color, 1); } + // I think this slightly differs than the norm, but ray is the current position of my ray + // and step is a direction vector, so moving the ray forward is as simple as ray += step vec4 rayMarch(vec3 ray, vec3 step) { + // Does the ray pass through any blackholes int bhStatus = 2; for (int i = 0; i < 5; i++) { if (linePointDistance(ray, step, uBH[i].xyz) < uBH[i].a * 3.0) { @@ -140,6 +143,7 @@ var fShader = ` return vec4(0,1,0,1); } + // Does the ray hit a star for (int i = 0; i < 5; i++) { if (linePointDistance(ray, step, uStars[i].xyz) < uStars[i].a) { return calculateColor(bhStatus == 2, uStars[i], ray, step); diff --git a/Year 2/Computer Graphics with WebGL/Project/spaceship.js b/Year 2/Computer Graphics with WebGL/Project/spaceship.js index 3f88b69..cb3b548 100644 --- a/Year 2/Computer Graphics with WebGL/Project/spaceship.js +++ b/Year 2/Computer Graphics with WebGL/Project/spaceship.js @@ -15,6 +15,7 @@ SphericalCoord.prototype.recalculateCartesian = function() { Math.sin(this.theta) * Math.cos(this.phi)]; } SphericalCoord.prototype.addAngles = function(theta, phi) { + // I figured this out on my own :) // Theta is in the range [0, pi), however in a sphere we have 2pi degrees. // We keep a "full" theta, which is in the range [0, 2pi), and then adjust normal theta // accordingly. With just normal theta, after a positive value is passed (for example) @@ -25,7 +26,6 @@ SphericalCoord.prototype.addAngles = function(theta, phi) { // pi and 2pi, you need to rotate phi by 180 degrees (2pi), so theta is acting on the other // hemisphere, and then we need to remove from 2pi the "leftover" amount, since our theta // would signify the "reverse" angle than the one we want. - // Special thanks for interactive applet: https://mathinsight.org/spherical_coordinates var before = this.fulltheta <= Math.PI; this.fulltheta = (this.fulltheta + theta) % (2 * Math.PI); @@ -84,7 +84,7 @@ var keyAction = [ () => { pos = vecSum(pos, left.toCartesian()); }, // 'a' () => { pos = vecSub(pos, left.toCartesian()); }, // 'd' ]; -document.addEventListener("keydown", (event) => { +document.addEventListener('keydown', (event) => { if (isPaused) return; switch (event.key) { @@ -96,7 +96,7 @@ document.addEventListener("keydown", (event) => { case 'd' : pressedKeys[5] = true; break; } }); -document.addEventListener("keyup", (event) => { +document.addEventListener('keyup', (event) => { if (isPaused) return; switch (event.key) { @@ -144,7 +144,7 @@ function mouseInit() { /* * On every draw */ -function lookUpdate() { +function updateCamera() { for (var i = 0; i < pressedKeys.length; i++) if (pressedKeys[i]) keyAction[i](); lookAt(pos, vecSum(pos, forward.toCartesian()), up.toCartesian()); diff --git a/Year 2/Computer Graphics with WebGL/Project/star.js b/Year 2/Computer Graphics with WebGL/Project/star.js index 1931c7f..de1e9e9 100644 --- a/Year 2/Computer Graphics with WebGL/Project/star.js +++ b/Year 2/Computer Graphics with WebGL/Project/star.js @@ -2,7 +2,7 @@ starsBuf = []; Star = function(center, size) { if (starsBuf.length == STAR_MAX_COUNT) - throw "Error: cannot add more stars, limit of " + STAR_MAX_COUNT + " reached!" + 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]; @@ -12,7 +12,7 @@ Star = function(center, size) { Star.prototype.moveTo = function(newCenter) { this.sphere.center = newCenter; starsBuf[this.index] = newCenter.concat(this.sphere.size); - gl.uniform4fv(gl.getUniformLocation(glprog, "uStars[" + this.index + "]"), starsBuf[this.index]); + gl.uniform4fv(gl.getUniformLocation(glprog, 'uStars[' + this.index + ']'), starsBuf[this.index]); } Star.prototype.draw = function() { this.sphere.draw(); -- cgit v1.3.1-26-gb76a