From 70dc2d83a54fe43050c34d6f91ba5a71fdb35d65 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 27 Aug 2023 11:13:08 +0300 Subject: [WGL/P] Implemented correct ray rotation, following Riccardo Antonelli's formula --- .../Project/shaders.js | 30 ++++++++++++++-------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/Year 2/Computer Graphics with WebGL/Project/shaders.js b/Year 2/Computer Graphics with WebGL/Project/shaders.js index 152753a..3fe1e27 100644 --- a/Year 2/Computer Graphics with WebGL/Project/shaders.js +++ b/Year 2/Computer Graphics with WebGL/Project/shaders.js @@ -35,6 +35,8 @@ var fShader = ` precision highp float; + #define MORE_MAGIC 0.01 + uniform mat4 uProjectionMatrix; uniform mat4 uViewMatrix; uniform mat4 uModelMatrix; @@ -74,21 +76,28 @@ var fShader = ` int rayMarchInBH(vec4 BH, inout vec3 ray, inout vec3 step) { ray = spherePoint(ray, step, vec4(BH.xyz, BH.a * 3.0)); + + // h is used in the step changing calculation + float h = length(cross(ray, step)); + for (int i = 0; i < 100; i++) { - float dist = linePointDistance(ray, step, BH.xyz); + float dist = distance(ray, BH.xyz); + if (dist <= BH.a) { // if inside event horizon return 1; } - if (dist > BH.a * 3.0) { // if outside ISCO + if (dist > BH.a * 3.0 + 1.0) { // if outside ISCO return 2; } - // Used the help of https://en.wikipedia.org/wiki/Gravitational_lens#Explanation_in_terms_of_spacetime_curvature - vec3 rayStep = ray + step; - float theta = 2.0 * BH.a / length(BH.xyz - ray); - float alpha = -theta - angleBetweenVectors(-step, BH.xyz - rayStep); - step += normalize(BH.xyz - rayStep) * abs(sin(theta) / sin(alpha) * length(step)) / 100.0; - + // Gravity bends light (actually spacetime, which in turn changes it's path for outside observers). + // This is a more or less correct (as far as I can tell) implementation of Riccardo Antonelli's formula + // http://rantonels.github.io/starless/#MathJax-Element-29-Frame + // Though I found it difficult to understand, so I referred to some of the code from + // 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); ray += step; } return 0; // orbits inside @@ -119,11 +128,12 @@ var fShader = ` } } - // if (bhStatus == 0) return vec4(1,0,0,1); - // if (bhStatus == 1) return vec4(0,1,0,1); + 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) { return vec4(0,1,0,1); + // return vec4(1,0,0,1); } for (int i = 0; i < 5; i++) { -- cgit v1.3.1-26-gb76a