From 87e9c9fa25b21bec3a658dcebc6c765a511d838e Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 27 Aug 2023 11:44:43 +0300 Subject: [WGL/P] Slight code reorganisation, moved buffer logic for black holes in BlackHole and exported count limits as global variables --- .../Project/Project.html | 7 +-- .../Project/blackhole.js | 10 +++- .../Project/shaders.js | 64 ++++++++++++---------- .../Computer Graphics with WebGL/Project/star.js | 3 + 4 files changed, 49 insertions(+), 35 deletions(-) diff --git a/Year 2/Computer Graphics with WebGL/Project/Project.html b/Year 2/Computer Graphics with WebGL/Project/Project.html index 41f4943..4c88553 100644 --- a/Year 2/Computer Graphics with WebGL/Project/Project.html +++ b/Year 2/Computer Graphics with WebGL/Project/Project.html @@ -112,10 +112,9 @@ h2 { // new Star([0,0,0], 100) // new Star([1000, 0, -30], 20); - blackHole = new BlackHole([1000,0,900], 30); - for (var i = 0; i < blackHolesBuf.length; i++) { - gl.uniform4fv(gl.getUniformLocation(glprog, "uBH[" + i + "]"), blackHolesBuf[i]); - } + blackHoles = [ + new BlackHole([1000,0,900], 30) + ]; resizeglcanvas(); } diff --git a/Year 2/Computer Graphics with WebGL/Project/blackhole.js b/Year 2/Computer Graphics with WebGL/Project/blackhole.js index 261a541..674093b 100644 --- a/Year 2/Computer Graphics with WebGL/Project/blackhole.js +++ b/Year 2/Computer Graphics with WebGL/Project/blackhole.js @@ -1,8 +1,16 @@ blackHolesBuf = []; BlackHole = function(center, size) { + if (blackHolesBuf.length == BLACKHOLE_MAX_COUNT) + throw "Error: cannot add more black holes, limit of " + BLACKHOLE_MAX_COUNT + " reached!" + this.rs = size / 3; - blackHolesBuf.push([center[0], center[1], center[2], this.rs]); + this.index = blackHolesBuf.push([]) - 1; + this.moveTo(center); +} +BlackHole.prototype.moveTo = function(newCenter) { + blackHolesBuf[this.index] = newCenter.concat(this.rs); + 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 3fe1e27..88e57c8 100644 --- a/Year 2/Computer Graphics with WebGL/Project/shaders.js +++ b/Year 2/Computer Graphics with WebGL/Project/shaders.js @@ -1,4 +1,7 @@ -var vShader =` +const STAR_MAX_COUNT = 5; +const BLACKHOLE_MAX_COUNT = 5; + +var vShader =` uniform mat4 uProjectionMatrix; uniform mat4 uViewMatrix; uniform mat4 uModelMatrix; @@ -36,7 +39,17 @@ var fShader = ` precision highp float; #define MORE_MAGIC 0.01 + #define STAR_COLOR vec3(0.95,0.5,0.215) + varying vec3 vColor; + varying vec3 vXYZ; + + // For ray marching + uniform vec4 uStars[` + STAR_MAX_COUNT + `]; + uniform vec4 uBH[` + BLACKHOLE_MAX_COUNT + `]; + uniform vec3 uEye; + + // For light recalculation uniform mat4 uProjectionMatrix; uniform mat4 uViewMatrix; uniform mat4 uModelMatrix; @@ -46,15 +59,7 @@ var fShader = ` uniform vec3 uAmbientColor; uniform vec3 uDiffuseColor; - uniform vec3 uLightDir; - - varying vec3 vColor; - varying vec3 vXYZ; - - uniform vec4 uStars[5]; - uniform vec4 uBH[5]; - - uniform vec3 uEye; + uniform vec3 uLightDir; // Thanks https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Vector_formulation float linePointDistance(vec3 linePoint, vec3 lineDir, vec3 point) { @@ -87,7 +92,7 @@ var fShader = ` return 1; } if (dist > BH.a * 3.0 + 1.0) { // if outside ISCO - return 2; + return 0; } // Gravity bends light (actually spacetime, which in turn changes it's path for outside observers). @@ -100,48 +105,47 @@ var fShader = ` step += normalize(ray - BH.xyz) * -1.5 * (h*h) * MORE_MAGIC / (dist*dist*dist*dist*dist); ray += step; } - return 0; // orbits inside + return 1; // orbits inside } vec4 calculateColor(bool notBent, vec4 star, vec3 ray, vec3 step) { if (notBent) { - return vec4(vColor,linePointDistance(ray, step, star.xyz) / star.a + 0.1); + return vec4(vColor,1); } - // vec3 anormal = spherePoint(ray, step, star) - star.xyz; - // - // mat4 mvMatrix = uViewMatrix * uModelMatrix; - // mat4 nMatrix = uUseNormalMatrix?uNormalMatrix:mvMatrix; - // vec3 color = uAmbientColor*vec3(0.95,0.5,0.215); - // vec3 light = normalize(-uLightDir); - // vec3 normal = vec3(normalize(nMatrix*vec4(anormal,0))); - // color += vec3(0.95,0.5,0.215)*uDiffuseColor*max(dot(normal,light),0.0); + // This is an exact copy from the vertex shader light calculations + // Since the ray was bent, we don't know what exactly it will hit, so we need to recalculate + // This probably isn't entirely correct, but I like how it looks + vec3 anormal = spherePoint(ray, step, star) - star.xyz; + mat4 mvMatrix = uViewMatrix * uModelMatrix; + mat4 nMatrix = uUseNormalMatrix?uNormalMatrix:mvMatrix; + + vec3 color = uAmbientColor*STAR_COLOR; + vec3 light = normalize(-uLightDir); + vec3 normal = vec3(normalize(nMatrix*vec4(anormal,0))); + color += STAR_COLOR*uDiffuseColor*max(dot(normal,light),0.0); - return vec4(0.95,0.5,0.215,1); + return vec4(color, 1); } vec4 rayMarch(vec3 ray, vec3 step) { - int bhStatus = 3; + int bhStatus = 2; for (int i = 0; i < 5; i++) { if (linePointDistance(ray, step, uBH[i].xyz) < uBH[i].a * 3.0) { bhStatus = rayMarchInBH(uBH[i], ray, step); } } - if (bhStatus == 0) return vec4(1,0,1,1); - if (bhStatus == 1) return vec4(0,1,0,1); - // if (bhStatus == 2) return vec4(1,1,0,1); - if (bhStatus < 2) { + if (bhStatus == 1) { return vec4(0,1,0,1); - // return vec4(1,0,0,1); } for (int i = 0; i < 5; i++) { if (linePointDistance(ray, step, uStars[i].xyz) < uStars[i].a) { - return calculateColor(bhStatus == 3, uStars[i], ray, step); + return calculateColor(bhStatus == 2, uStars[i], ray, step); } } - return vec4(1,0,0, 1); + return vec4(1,0,0,1); } void main() diff --git a/Year 2/Computer Graphics with WebGL/Project/star.js b/Year 2/Computer Graphics with WebGL/Project/star.js index a98c3cf..1931c7f 100644 --- a/Year 2/Computer Graphics with WebGL/Project/star.js +++ b/Year 2/Computer Graphics with WebGL/Project/star.js @@ -1,6 +1,9 @@ starsBuf = []; Star = function(center, size) { + 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; -- cgit v1.3.1-23-gd82d