From 14526cb38d986bd190b9308ed0695c3b614c5ace Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 1 Aug 2024 08:24:30 +0300 Subject: (blog/tpbhs) Changed post from a general article about the project to only about the physics --- ...tric-in-Schwarzschild-coordinates-wikipedia.png | Bin 3423 -> 0 bytes .../img/Schwarzschild-radius-wikipedia.png | Bin 1054 -> 0 bytes .../img/algorithm.png | Bin 28039 -> 0 bytes .../img/antonelli-formula-expanded.png | Bin 11619 -> 0 bytes .../img/isco-wikipedia.png | Bin 1614 -> 0 bytes .../img/normalize-vector-wikipedia.png | Bin 625 -> 0 bytes .../img/photon-sphere-wikipedia.png | Bin 1472 -> 0 bytes ...chwarzschild-black-hole-force-field-formula.png | Bin 2265 -> 0 bytes .../img/specific-angular-momentum-wikipedia.png | Bin 529 -> 0 bytes .../img/variables-expanded.png | Bin 9318 -> 0 bytes .../index.md | 544 --------------------- ...tric-in-Schwarzschild-coordinates-wikipedia.png | Bin 0 -> 3423 bytes .../img/Schwarzschild-radius-wikipedia.png | Bin 0 -> 1054 bytes .../img/algorithm.png | Bin 0 -> 28039 bytes .../img/antonelli-formula-expanded.png | Bin 0 -> 11619 bytes .../img/isco-wikipedia.png | Bin 0 -> 1614 bytes .../img/normalize-vector-wikipedia.png | Bin 0 -> 625 bytes .../img/photon-sphere-wikipedia.png | Bin 0 -> 1472 bytes ...chwarzschild-black-hole-force-field-formula.png | Bin 0 -> 2265 bytes .../img/specific-angular-momentum-wikipedia.png | Bin 0 -> 529 bytes .../img/variables-expanded.png | Bin 0 -> 9318 bytes .../index.md | 544 +++++++++++++++++++++ 22 files changed, 544 insertions(+), 544 deletions(-) delete mode 100644 src/blog/difficulties-of-making-a-black-hole-shader/img/Schwarzschild-metric-in-Schwarzschild-coordinates-wikipedia.png delete mode 100644 src/blog/difficulties-of-making-a-black-hole-shader/img/Schwarzschild-radius-wikipedia.png delete mode 100644 src/blog/difficulties-of-making-a-black-hole-shader/img/algorithm.png delete mode 100644 src/blog/difficulties-of-making-a-black-hole-shader/img/antonelli-formula-expanded.png delete mode 100644 src/blog/difficulties-of-making-a-black-hole-shader/img/isco-wikipedia.png delete mode 100644 src/blog/difficulties-of-making-a-black-hole-shader/img/normalize-vector-wikipedia.png delete mode 100644 src/blog/difficulties-of-making-a-black-hole-shader/img/photon-sphere-wikipedia.png delete mode 100644 src/blog/difficulties-of-making-a-black-hole-shader/img/riccardo-antonelli-schwarzschild-black-hole-force-field-formula.png delete mode 100644 src/blog/difficulties-of-making-a-black-hole-shader/img/specific-angular-momentum-wikipedia.png delete mode 100644 src/blog/difficulties-of-making-a-black-hole-shader/img/variables-expanded.png delete mode 100644 src/blog/difficulties-of-making-a-black-hole-shader/index.md create mode 100644 src/blog/the-physics-behind-a-black-hole-shader/img/Schwarzschild-metric-in-Schwarzschild-coordinates-wikipedia.png create mode 100644 src/blog/the-physics-behind-a-black-hole-shader/img/Schwarzschild-radius-wikipedia.png create mode 100644 src/blog/the-physics-behind-a-black-hole-shader/img/algorithm.png create mode 100644 src/blog/the-physics-behind-a-black-hole-shader/img/antonelli-formula-expanded.png create mode 100644 src/blog/the-physics-behind-a-black-hole-shader/img/isco-wikipedia.png create mode 100644 src/blog/the-physics-behind-a-black-hole-shader/img/normalize-vector-wikipedia.png create mode 100644 src/blog/the-physics-behind-a-black-hole-shader/img/photon-sphere-wikipedia.png create mode 100644 src/blog/the-physics-behind-a-black-hole-shader/img/riccardo-antonelli-schwarzschild-black-hole-force-field-formula.png create mode 100644 src/blog/the-physics-behind-a-black-hole-shader/img/specific-angular-momentum-wikipedia.png create mode 100644 src/blog/the-physics-behind-a-black-hole-shader/img/variables-expanded.png create mode 100644 src/blog/the-physics-behind-a-black-hole-shader/index.md (limited to 'src') diff --git a/src/blog/difficulties-of-making-a-black-hole-shader/img/Schwarzschild-metric-in-Schwarzschild-coordinates-wikipedia.png b/src/blog/difficulties-of-making-a-black-hole-shader/img/Schwarzschild-metric-in-Schwarzschild-coordinates-wikipedia.png deleted file mode 100644 index 3964615..0000000 Binary files a/src/blog/difficulties-of-making-a-black-hole-shader/img/Schwarzschild-metric-in-Schwarzschild-coordinates-wikipedia.png and /dev/null differ diff --git a/src/blog/difficulties-of-making-a-black-hole-shader/img/Schwarzschild-radius-wikipedia.png b/src/blog/difficulties-of-making-a-black-hole-shader/img/Schwarzschild-radius-wikipedia.png deleted file mode 100644 index fccf54f..0000000 Binary files a/src/blog/difficulties-of-making-a-black-hole-shader/img/Schwarzschild-radius-wikipedia.png and /dev/null differ diff --git a/src/blog/difficulties-of-making-a-black-hole-shader/img/algorithm.png b/src/blog/difficulties-of-making-a-black-hole-shader/img/algorithm.png deleted file mode 100644 index 40d4c86..0000000 Binary files a/src/blog/difficulties-of-making-a-black-hole-shader/img/algorithm.png and /dev/null differ diff --git a/src/blog/difficulties-of-making-a-black-hole-shader/img/antonelli-formula-expanded.png b/src/blog/difficulties-of-making-a-black-hole-shader/img/antonelli-formula-expanded.png deleted file mode 100644 index 48eb7d4..0000000 Binary files a/src/blog/difficulties-of-making-a-black-hole-shader/img/antonelli-formula-expanded.png and /dev/null differ diff --git a/src/blog/difficulties-of-making-a-black-hole-shader/img/isco-wikipedia.png b/src/blog/difficulties-of-making-a-black-hole-shader/img/isco-wikipedia.png deleted file mode 100644 index 2b01135..0000000 Binary files a/src/blog/difficulties-of-making-a-black-hole-shader/img/isco-wikipedia.png and /dev/null differ diff --git a/src/blog/difficulties-of-making-a-black-hole-shader/img/normalize-vector-wikipedia.png b/src/blog/difficulties-of-making-a-black-hole-shader/img/normalize-vector-wikipedia.png deleted file mode 100644 index 517e142..0000000 Binary files a/src/blog/difficulties-of-making-a-black-hole-shader/img/normalize-vector-wikipedia.png and /dev/null differ diff --git a/src/blog/difficulties-of-making-a-black-hole-shader/img/photon-sphere-wikipedia.png b/src/blog/difficulties-of-making-a-black-hole-shader/img/photon-sphere-wikipedia.png deleted file mode 100644 index 840572c..0000000 Binary files a/src/blog/difficulties-of-making-a-black-hole-shader/img/photon-sphere-wikipedia.png and /dev/null differ diff --git a/src/blog/difficulties-of-making-a-black-hole-shader/img/riccardo-antonelli-schwarzschild-black-hole-force-field-formula.png b/src/blog/difficulties-of-making-a-black-hole-shader/img/riccardo-antonelli-schwarzschild-black-hole-force-field-formula.png deleted file mode 100644 index 846b0d3..0000000 Binary files a/src/blog/difficulties-of-making-a-black-hole-shader/img/riccardo-antonelli-schwarzschild-black-hole-force-field-formula.png and /dev/null differ diff --git a/src/blog/difficulties-of-making-a-black-hole-shader/img/specific-angular-momentum-wikipedia.png b/src/blog/difficulties-of-making-a-black-hole-shader/img/specific-angular-momentum-wikipedia.png deleted file mode 100644 index 61b3383..0000000 Binary files a/src/blog/difficulties-of-making-a-black-hole-shader/img/specific-angular-momentum-wikipedia.png and /dev/null differ diff --git a/src/blog/difficulties-of-making-a-black-hole-shader/img/variables-expanded.png b/src/blog/difficulties-of-making-a-black-hole-shader/img/variables-expanded.png deleted file mode 100644 index 484e2f1..0000000 Binary files a/src/blog/difficulties-of-making-a-black-hole-shader/img/variables-expanded.png and /dev/null differ diff --git a/src/blog/difficulties-of-making-a-black-hole-shader/index.md b/src/blog/difficulties-of-making-a-black-hole-shader/index.md deleted file mode 100644 index 555744a..0000000 --- a/src/blog/difficulties-of-making-a-black-hole-shader/index.md +++ /dev/null @@ -1,544 +0,0 @@ -# Difficulties of making a black hole shader - -## Contents - -1. [Preface](#preface) - 1. [Brief introduction to computer graphics](#brief-introduction-to-computer-graphics) - 2. [Brief introduction to WebGL](#brief-introduction-to-webgl) -2. [Theoretical and algorithmic background](#theoretical-and-algorithmic-background) - 1. [What is a black hole](#what-is-a-black-hole) - 2. [Geodesics, gravity and gravitational lensing](#geodesics-gravity-and-gravitational-lensing) - 3. [Finding a formula](#finding-a-formula) - 4. [Applying the formula to "bend" light's path](#applying-the-formula-to-bend-lights-path) - 5. [Properties of a black hole](#properties-of-a-black-hole) - 6. [Putting everything together](#putting-everything-together) -3. [WebGL preparation](#webgl-preparation) -4. [Shader](#shader) -5. [Appendix](#appendix) - -Back in August I had to submit a project for an elective WebGL programming course in university. -I chose to create an interractive and animated black hole demo. -The primary idea was to show how the gravitational pull of black holes "bends" light and how the result looks from different points of view. - -During development I found a lot of hurdles, primarily with the actual mathematics behind it. -Information online is difficult to find and understand without having a physics background, so I'll try my best to lay out everything I know. - -I'll assume you don't know much about physics, but are familiar with mathematics and at the very least vectors. - -## Preface - -### Brief introduction to computer graphics - -Image and video files store visual information in a "fixed" way: you get a huge array of colors, each connected to a particular pixel on your screen. -The objects in those mediums cannot be modified easily, and additions or subtractions are even harder to do, usually requiring an expert using professional tools for hours, days or weeks on end. - -Computer graphics aims to offload a lot of the creator's workload by "generating" the images on the fly using a set of descriptions for our objects. -The idea is to define objects with some primary properties of simpler geometric figures, like the points on a triangle, then give those to the computer, which will in turn have to calculate what the precise color of every pixel in the resultant image should be. - -For example, we can represent a tin can (without a label) as a simple cyllinder. -Changing it's height or width is trivial: just change the corresponding property you pass to the computer, and it will handle the rest. - -Of course this system has it's limitations: the more detail you add to an object, the more difficult it is to crunch up the math. -And adding lighting with shadows, bloom, refractions and so on becomes even more demanding for the computer. - -Expanding on out tin can example, if you want an image with a tin can up close, with a bright light shining on it, showing the hunderds or thousands of small scratches, bumps and kicks from manufacturing and usage, alongside reflections and shadows, you'll swiftly find that computer graphics are not the correct tool for the job. - -![](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a8/Empty_tin_can2009-01-19.jpg/514px-Empty_tin_can2009-01-19.jpg) -*Image by Sun Ladder on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=5746428)* - -Such an image contains way way too much detail for the average computer to handle in reasonable time (some small fraction of a second). -However, computer generated graphics are still a very useful tools. -Thats why modern machines come with a graphics card handling *only* graphical tasks (alongside a processor, handling all other tasks like running programs, managing internet traffic, etc.). - -A graphics card is a combination of a special processor (GPU), designed to be able to *only* compute stuff related to graphics, with the upside of doing so much much faster than a general purpose processor (CPU) could, and some memory for use exclusively by that GPU. - -Since the GPU is different on the hardware level, it accepts significantly different instructions than a normal processor does. -For this reason programs written for a graphics processing unit are called "shaders", rather than just "programs". - -Of course, higher-level programming languages have been designed, which can then be transformed into the GPU-specific instructions. -But, contrary to normal software development, we don't really have standalone "shader compilers", but graphics library which do the job, alongside TODO - -### Brief introduction to WebGL - -One of the most popular such libraries is [OpenGL](TODO) with it's associated shader programming languages [GLSL](TODO) and [GLSL ES](TODO). -In the 2000s people decided it would be a good idea to add a graphics API/library to web browsers, which later morphed into what we call today [WebGL](TODO). - -It serves as a "layer above" OpenGL: you write JavaScript code with which the browser tells OpenGL what to do. -You would get a DOM object in which the graphics will be rendered, and a "context" object to which you'll pass down your shaders, shader parameters and change graphics settings. - -## Theoretical and algorithmic background - -&&important I haven't formally studied physics, so don't fully trust anything here. - There are most definetly errors, steming from my lackluster understanding. - -Using [Einstein's field equations](TODO), or to simplify our life, [Schwarzschild's metric](TODO), we can precisely calculate the curvatures in space(time) at every point around a Schwarzschild black hole. -Unfortunetly, the computation would still be too laborious, so we'll need to make an approximation using [celestial mechanics](TODO). - -That is the overall gist of the underlying physics, but I promised an explanation, so let's back up a bit. - -### What is a black hole - -Before we even begin to talk about gravity and light, we need to understand how atoms work. -Imagine a set of marbles, scattered on a table. - -![](https://upload.wikimedia.org/wikipedia/commons/thumb/0/0e/Marbles_from_Indonesia.jpg/1280px-Marbles_from_Indonesia.jpg) -*Image from By Okkisafire on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=30338734)* - -Now, start pushing on all of them from the edges of the table to it's center. -In the beginning, you'll mostly roll them, until they all get in contact with each other. -At that point (assuming they all stay flat on the table), the glass will keep the marbles together, handling your force. - -By increasing the force with which you push on them, after a certain threshold, the glass will break, turning the marbles into shards. -Those shards will still have some fight in them, so after increasing your force by another level, you'll turn all of those shards into tiny glass granules. - -A similar occurence happens with atoms. -Normally atoms are spread apart, by pushing them together, at a certain point (ignoring the orbitin electrons) all nuclei will be "touching" and the [nuclar force](TODO) will keep them together. -By increasing your pushing force you can overwhelm the nuclear force and "shatter" the nuclei into their subatomic particles. - -Assuming an enormus explosion doesn't happen, and you can keep increasing your force, it's also possible to overwhelm the "subatomic forces" that keep those subatomic particles "together"[^matter-together]. -However, those subatomic particles are more commonly referred to as [elementary particles](https://en.wikipedia.org/wiki/Elementary_particle), meaning there are no "granules" into which the elementary particles can break down. -It doesn't make sense if subatomic particles wouldn't be "breakable", since that would require dealing with potentially infinite amounts of force and such an assumption would destroy all of humanity's physics. - -Then that leaves us with only one solution: laws of matter break. -If nothing can keep the elementary particles "together", so keep matter "together", then the result is a sort of "collapse", all of that matter just "deflates" into a single, infinetly small point (but the amount of mass isn't infinite, it's the sum of mass from the particles that collapsed). -That point is called a **black hole**. - -This object is very bizzare, generally it doesn't have too much properties, but the ones it has are really unique. -In my case of computer graphics, the only important part is it's effect on light. - -### Geodesics, gravity and gravitational lensing - -To understand how black holes affect light, we first need to understand how black holes affect gravity. - -Think of a flat surface, like a table. -What is the shortest distance between two points? -Obviously, that's a straight line segment. -If you then extend that segment, so it has an infinite length, it becomes a line. - -However, what if our surface isn't flat? -Imagine two points on a sphere, what is the shortest distance between them? -Obviously, it's an arc of a circle and extending it to it's maximum possible length, we get a [great circle](https://en.wikipedia.org/wiki/Great_circle). - -![](https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/Illustration_of_great-circle_distance.svg/480px-Illustration_of_great-circle_distance.svg.png) -*Image by CheCheDaWaff on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=48187293)* - -Now, what does it mean to go "forwards"? -On a flat surface, we can define it as "following a (straight) line segment", on our sphere we can say "following an arc of a great circle". -But what we really need is a more generalized definition, which handles both of our cases (and more). - -That's why in geometry there is the notion of [geodesics](TODO), the generalized curve on any surface which (roughly speaking) is the shortest[^geodesics-are-local] path between any two points. -On a flat surface, that's the line segment, on the sphere it's the great circle arc. -It's important to note that a general geodesic cannot have most properties you might be used to from "the shortest path between two points". -For example, on a flat surface, we can have parallel lines, but on our sphere, great circles will always intersect[^parallels-of-latitude]. - -And there are a lot more complicated surfaces, imagine a flat surface but with half-spherical "trophs" in it, on the flat segments your geodesic would be the line segment, but on those trophs it will be an arc (of a great circle). -Actually, you don't need to imagine: the entire universe is a 3D surface, which is mostly flat, but has sherical-ish "trophs" in it. - -That's one of the primary results from the General theory of relativity, normally the universe's surface is nice and flat, but objects with mass "produce" gravity, which some sort of a force[^gravity-isnt-a-force] that bends our surface and creates those trophs, changing the local geodesics. -The resultant effect is that if you travel perfectly forwards in a spaceship and pass by an object with mass (like a planet) from a close enough distance, your path will be curved, without you steering your ship. - -Here comes the meat of this section: if you have no outside references (if all planets and stars are invisible), you, in your ship, *wouldn't* really be able to tell your path is curved. -But to a (stationary) outside observer, like another spaceship, it *would* look like you've turned. -In fact, while "turning" it will look like you've slowed down, since an arc is always longer than a straight line segment, but you're travelling at the same speed. - -For the cherry on top, light is also affected by gravity, and we know it's speed is constant and always travels forwards, therefore gravity "bends" light. - - - -The amount by which and the size of the area which is affected depend on that object's mass and size. -Generally, if two objects have the same mass, the bigger one will affect a larger portion of space, but will bend it less, compared to the smaller one, which will affect a smaller portion, which will be bend a lot. - -Since a black hole is a single infinetly small point, then all of it's mass is concentrated on that point, making it bend a relatively small are of space **a lot**. -Strong enough in fact, that if you get too close, you will not be able to overwhelm gravity and "merge" into that infinitessimal point (the white lines are our "geodesics"): - -![](https://upload.wikimedia.org/wikipedia/commons/thumb/9/91/CNX_UPhysics_13_07_spacetime.png/800px-CNX_UPhysics_13_07_spacetime.png?20171124170318) -*Image by OpenStax University Physics on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=64307901)* - -An interesting effect from gravity is that light (which always travells "forwards", so along a geodesic) also gets "bent", allowing an observer to view otherwise obstructed objects. -We call that [graviational lensing](TODO). -Though not unique to black holes, thats when the effect is strongest: - -![](https://upload.wikimedia.org/wikipedia/commons/0/02/Gravitational_lens-full.jpg) -*(notice how the gray arrows show the path light travels from a galaxy that is behind (obstructed by) the black hole)* - -I'll abstrain from explaining light, but you may think of it as a small particle, "similar" to an electron, travelling through space. -In fact, Newton understood light to be just that, and called the underlying particle a "corpiscule". -For our purpopses, this approximation will serve us well. - -The distinction that gravity doesn't change the direction or speed of an object "directly" is at upmost importance for physics, since this way we preserve two very important properties of light: it's constant speed and straight travelling path. -However, in our simulation, it would be significantly simpler to assume the opposite, since to an outside observer (like the "player") the difference is indistinguishable. - -Therefore, in our simulation, we'll have light rays, but when they approach a black hole, their speed and direction would change. -This will be determined by some physics equations. - -### Finding a formula - -Now, how do we calculate the exact curvature a black hole produces? -General theory of relativity provides us with the (really complicated) [Einstein field equations](https://en.wikipedia.org/wiki/Einstein_field_equations) for this exact problem. -[Schwartzschild](TODO) solved these equations, assuming that the body with mass doesn't spin[^schwm-assumptions]. -This solution is called the [Schwarzschild metric](https://en.wikipedia.org/wiki/Schwarzschild_metric), and a black hole for which this assumption is true is called a "Shwartzschild black hole". - -The Shwarzschild black hole is the symplest of it's type, so using the Schwarzschild metric should give us the least trouble. -Now, here is the metric in Schwartzschild coordinates: - -![](./img/Schwarzschild-metric-in-Schwarzschild-coordinates-wikipedia.png) - -**YOUCH!** - -The variables themselves aren't that bad: `c` is the speed of light, τ is time, Ω, `t` and `r` are Schwarzschild coordinates. - -However, as you can see, there are two big problems: we have to calculate derivatives (that's what the `d` signifies) and convert from Schwarzschild coordinates to normal (eucledian) coordinates, that the computer understands natively. -Both of those operations are **incredibly slow**, the average computer doesn't standa a chance at rendering anything in a reasonable amount of time. - -Thankfully [Riccardo Antonelli](http://rantonels.github.io/starless/) was able to do all of our coordinate conversions and simplify the living crap out of that monster (using some approximations with celestial mechanics), turning it into something digestible: - -![](./img/riccardo-antonelli-schwarzschild-black-hole-force-field-formula.png) - -I will not be trying to explain why or how the Schwarzschild metric and this formula work, that's way way beyond my elementary understanding and the scope of this post. - -Question now is: what do all of those letters represent? -This turned out to be a much harder problem than it should have, I actually had to refer [Ricardo Antonelli's](https://github.com/rantonels/starless/blob/7a0c3c4efdc0666f53dcfa48d310fb2164be2d1a/tracer.py) and [Dmitry Brant's](https://dmitrybrant.com/2018/12/11/ray-tracing-black-holes) implementation in their projects, alongside copus articles on wikipedia to fully grasp how to put it to work. - -### Applying the formula to "bend" light's path - -Our plan for simulating gravitational lensing is to move the corpiscule by a tiny amount in a certain direction by a certain amount, update the direction and amount according to gravity, rince and repeat. -The direction, alongside the amount by which the particle moves, can be represented with a single vector, which we'll call light's "velocity". - -Referring to the formula again: - -![](./img/riccardo-antonelli-schwarzschild-black-hole-force-field-formula.png) - -The F→ returns a force vector (`F` for "force" and → for "vector"): any force acting on an object has a strength (magnitude) and a direction. -Imagine you are pushing a box: you are applying some force to it in a "forward" direction. -If you pull the box, you'll apply some force in a "backward" direction. - -This force vector conveys how much and in what direction exactly the black hole is "pulling" on the corpiscule. - -Although the function takes in only one argument, `r`, that doesn't mean that `h` or r\^ aren't also arguments: - -- `r` is the distance from the center of the black hole (the singularity) to the **current** position of the corpiscule. - -- `h` is the magnitude of the ["specific angular momentum"](https://en.wikipedia.org/wiki/Specific_angular_momentum) of the light "particle". - - "Normal" linear momentum specifies how much "force" at what speed it takes to stop (or change) the speed and direction of an object in linear motion (that's why it's in kg\*m/s). - Imagine a big heavy train on a perfectly straight rails going at a fast speed. - How do you forcefully stop it? - - You can put a *really* heavy barrier on the tracks, in front of the train. - Since the train is moving at a really great speed, this barrier will have to be much much heavier (with much more mass) than the train itself. - - Alternatively, you can "launch" something against the train, meaning you can put a second train, moving in the opposite direction of the first train, so they hit head-on. - Since the second train would also be moving, if it's travelling at the same speed and has the same mass as the first train, upon collision they would both stop[^hitting-trains]. - If the second travells at twice the speed, then it can have half of the mass of the first to produce the same effect. - - The property with which we can "prove" why both a larger mass at no speed, and a smaller mass at a faster speed, can stop the train, is the momentum. - - Angular momentum specifies how much "force" at what speed it takes to stop (or change) the speed and directino of a rotating object. - Imagine the train scenario but on a circular track. - - The specific angular momentum is the angular momentum of an object, divided by it's mass. - It's magnitude kind of tells us which property is more prevalent in the momentum, the mass or the speed. - For the same angular momentum, a long (big magnitutde) *specific* angular momentum means our object is small and moves fast, while for a short (small magnitutde) *specific* angular momentum out object is large and moves slowly. - - In our case a light "particle" is being pulled by a stationary object of large mass (a Shwarzschild black hole), so rather than finding the angular momentum[^angular-momentum] and then dividing by the mass[^light-has-no-mass], we'll use another formula: - - ![](./img/specific-angular-momentum-wikipedia.png) - - where **in this specific formula only** `r` is the vector from the center of the big mass (the singularity of the black hole) to the light "particle" and `v` is the velocity (direction and amount of speed) the light is travelling with. - - The cross product returns a vector, so we'll need to take the magnitutde of the result and plug that into the formula. - It's also important that **we calculate it only once, in the beginning**. - So, we shouldn't be calculating the magnitude of the specific angular momentum every time the light "particle" is moved, rather we calculate it once, after it gets close enough to be affected by the black hole. - -- r^ is the normalized ([unit](https://en.wikipedia.org/wiki/Unit_vector), hence the `^` on top) vector from the center of the black hole to the light particle: - - ![](./img/normalize-vector-wikipedia.png) - -Finally, putting all needed variables together in one proper function, we get: - -![Refer to "Antonelli’s formula - explained" in the Appendix](./img/variables-expanded.png) - -As you may notice, this can be simplified, if we substitude r^: - -![Refer to "Antonelli’s formula - simplified" in the Appendix](./img/antonelli-formula-expanded.png) - -In effect, we pass the distance from the black hole to the corpiscule and the function returns how light's velocity changes. - -Finally, we know what to give the formula, and what it will return. -The task at hand is making an algorithm which utilises it. -For that we'll need to get even more familiar with black holes. - -### Properties of a black hole - -As stated, we'll be working with non-rotating stationary black holes, Schwarzschild black holes. -From the [no-hair conjecture](https://en.wikipedia.org/wiki/No-hair_theorem) we know that such a black hole has only three distinct properties: mass, electric charge and angular momentum. -The last one doesn't apply for our case, and the second is irrelevant. - -Meaning, from the mass alone, we can derive the gravitational "forces", alongside three radii, which will be the backbone of our black hole: the event horizon, the photon sphere and [ISCO](https://en.wikipedia.org/wiki/Innermost_stable_circular_orbit). - -The event horizon, also called the Schwarzschild radius, is the distance from the center of the black hole (singularity), which once passed, cannot be passed again (you cannot escape the black hole). -The formula for it is: - -![r_s = (2GM)/(c^2)](./img/Schwarzschild-radius-wikipedia.png) - -where `G` is the [gravitational constant](https://en.wikipedia.org/wiki/Gravitational_constant) and `M` is the mass of the black hole (in kg). - -The photon sphere is the distance from the singularity, in which photons start orbiting the black hole (and can either escape or pass the event horizon). -Formula: - -![r = r_s * 3/2](./img/photon-sphere-wikipedia.png) - -where `G` and `M` are the same as before, `c` is the speed of light in vacuum and rs is the afroementioned Schwarzschild radius. - -Finally, the innermost stable circular orbit (ISCO), formally it's the distance, before which you can orbit the black hole, but once passed, you'll get sucked in and either get shot out or be forever lost past the event horizon. -For our purposes, it's fine to approximate it as the maximum radius after which spacetime isn't bent[^isco] and objects aren't affected. -Formula: - -![](./img/isco-wikipedia.png) - -Putting them all together, we should expect light to travel like this: -![](https://upload.wikimedia.org/wikipedia/commons/2/27/Black_Hole_Shadow.gif) -*Animation by Hugo Spinelli on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=138168679)* - - -### Putting everything together - -Finally, now we have everything we need to divise an algorithm for our light. -For light we'll store two vectors: one for it's position and one for it's velocity. -For a black hole we'll store a vector and a quantity: the position of the black hole's center and it's mass. -For convinience, we could just store the Schwarzschild radius, since all of our radii depend only on it. - -Once the distance between the light and the center is less than or equal to the ISCO, we can begin augmenting light's velocity. -On small steps, we move the light forward a bit with the velocity and then update the velocity with the black hole's pull. - -If light enters the event horizon, then we know it cannot escape, and stop moving it. -If it passes the ISCO then it has left the black hole and will never approach it again. - -If light enters the photon sphere, then we'll have to "track" how long it's been there, if it decides to orbit in a stable manner, then it will stay there indefinetly. -The simplest way to "track" this is just to limit how many times we can move the light forward. - -An algorithm doing this would look a bit like this (in pseudocode, inspired by Python): - -![Refer to "Algorithm" section of the Appendix](./img/algorithm.png) - -as a reminder, here is the definiton of F→: - -![](./img/antonelli-formula-expanded.png) - -## WebGL preparation - -The course was focused on handmade solutions, so some of the logic uses a [custom handmade library](TODO). - -Since the primary component was the shader itself, I made sure to simplify my logic to the absolute minimum. -A **lot** of decisions were made to optimize development time, without considering how good they were. - -In a scene there really are only 3 distinct objects: the stars, the black holes and the skybox. -The latter is just a simple cube, inside which everything resides, if a ray doesn't hit anything of interest, it will hit the cube. -Both stars and black holes are simple spheres, the former with an orange-y color (#f4803b) and any radious. -Black holes aren't rendered and their underlying sphere has the same radius as the ISCO, so their purpose is for intersection checks (more on those when we get to the [shaders](#shaders)). - -Most JavaScript code isn't worth talking about, wherever possible I use the [course library](TODO) and where I can't it's mostly simple WebGL configurations, elementary object "classes" and trivial HTML handling. -Though, there are still two somewhat interesting systems: - -### Scenes - -I wanted to have multiple scenes, with different arrangements of objects and with animations (stars moving around). -For that I devised a dead-simple level loading and animation system. - -An object of type "Level" stores a name, description, location of camera (the viewer) and arrays for stars, black holes and animations. -Though, for creating new levels, you only pass the first three: - -```javascript -var level1 = new Level( - "Star passing behind black hole", // Level title - "The black hole warps the light", // Level description - [1000,0,650]); // Initial coordinates of camera -``` - -Adding stars or black holes requires going through two member functions: `addStar` and `addBlackHole`. -Both take the center of the object, as well as it's radius, alongside an animation function. -The latter has two parameters: current time in TODO(seconds,milliseconds,?) from the beginning and the current object that will be created. -This function is called on each frame with the appropriate arguments, so inside you'll have to "manually" modify the object's properties. - -For example, a simple star going forwards could be added like this (`vecSum` is just [componentwise vector summation](TODO)): - -```javascript -level1.addStar([1070,0,1000], // Coordinates of center - 10, // Radius - (t, star) => vecSum(star.center, [-t,0,0])); // Move from left to right -``` - -Or one which rotates around a point: - -```javascript -var speed = Math.PI / 20, rad = 40; -level1.addStar([1000+rad,0,900], - 3, - (t, star) => vecSum([1000,0,900], [Math.cos(t*speed) * rad, - Math.sin(t*speed) * 5, - Math.sin(t*speed) * rad])); -``` - -For black holes it's the same. -If you want the object to not move, you could just pass `undefined` as the function: - -```javascript -level1.addBlackHole([1000,0,900], 30, undefined); -``` - -### Movement - -It wouldn't be fun without the ability to move around in the scene, so I added a simple system for that too. -WASD moves the camera position forward, left, backward and right, shift moves down and space moves up, all relative to the current direction the camera is looking towards. -Those are all done with relatively simple analytic geometry: we store three vectors, one pointing up, one pointing forward and one pointing left (which can be calculated from the other two with a cross product). -To move forward, add to the position the forward vector times some constant, to move backwards subtract and so on. - -Looking around with the mouse was kinda tricky, since I decided to use [spherical coordinates](TODO). -You store two angles, the "up" and "left" angle, moving the mouse forwards adds to the "up" angle, moving it downwards subtracts and so on. -The tricky part was what to do after - -TODO - -## Shaders - -As a gentle reminder, in OpenGL we have a vertex shader and a fragment shader: - -- The vertex shader is ran on every point of every object in our scene -- The fragment shader is ran on every pixel of your screen, with the added bonus that depending on the pixel, certain values are passed down from the vertex shader. - - This includes interpolated points: a line is made up of two points, the vertex shader is ran on both of them, then the line is interpolated and the fragment shader is ran on each pixel, corresponding to a point on the line. - The vertex shader values are also passed down interpolated, so if one end of the line is the color red, while the other end is green, then the fragment shader corresponding to the pixel, which shows the middle of the line, will have the color yellow. - -### Vertex shader - -As already mentioned, black holes aren't rendered, so the only objects with special rendering are the stars. -To simplify my life, I chose to do a simple [diffusion reflection](https://learnopengl.com/Lighting/Basic-Lighting), with the light source emminating from the camera, something which was already made in lectures: - -![](TODO) - -The result isn't amazing, but you can clearly make out the sphere as a 3D object (not a flat circle). - -### Fragment shader - -This is the meat of our logic, this is where we'll implement our algorithm: - -![](./img/algorithm.png) - -In real life, every star emits light, which then travells some distance, until it reaches our eyes. -However, computing it this way is incredibly inefficient, for every star we'll have to create hundreds or thousands of light rays (corpiscules), most of which probably wont reach us. -We want performance, so this is a no-go. - -The alternative is to do the opposite, assume every pixel was hit by a "ray" and trace back it's steps, if it came out of a star then it has some specific color (it was an actual ray), but if it didn't then show it as black (no ray hit us). -Taking it further, how do we "trace back" those steps? -Well, we can find the precise coordinate of every pixel inside the 3D scene, and move each point in the direction we're looking (though this image is about raytracing, just look at the two top red vectors): - -![](https://upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Ray_trace_diagram.png/800px-Ray_trace_diagram.png?20080214101526) -*Image by Timrb on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=3553478)* - -In the context of computer graphics, this is called [ray marching](TODO). - -Now, how do we detect if the ray hit something? -The dumb way is to move the ray forward a bit and check with every object whether the ray intersects it. -However, since all of our objects are spheres, there is a very easy optimization and simplification - imagine the ray as an infinitely long line and just get the shortest (perpendicular) distance between it and the center of the sphere: - -TODO custom image #1 - -For this we can implement a simple distance function: - -![](https://wikimedia.org/api/rest_v1/media/math/render/svg/42c53741fd3d8765e56ea652369a512a2cd09eac) - -```glsl -float linePointDistance(vec3 a, vec3 n, vec3 p) { - return length((p - a) - dot(p - a, n) * n); -} -``` - -Where `a` is the coordinate of our ray (pixel), `n` is the direction it will be travelling in and `p` is the sphere center we're comparing with. - -However, there is a huge problem: our line extends in both directions, so a check like `linePointDistance(ray, step, center) <= radius` would also detect if the ray interects the point behind us (when ray is going in the opposite direction)! -A very simple solution is to just add a sign to our distance: if positive then the distance is in reference to a point in front, while negative means it's behind. -The simplest way to get this information is by taking the [dot product](TODO) between our "forwards" direction and the direction from our coordinates to the [projection](TODO) of our point on the line: - -TODO custom image #2 - -```glsl -float linePointDistance(vec3 a, vec3 n, vec3 p) { - vec3 projection = dot(p - a, n) * n; - float dist = length((p - a) - projection); - - if (dot(projection, n) < 0.0) return -dist; - return dist; -} -``` - -Since all of our objects are spheres, we can just iterate through all of them and do this check. - -## Appendix - -##### LaTeX source for equations - -The images for my custom LaTeX equations were created on [latexeditor.lagrida.com](https://latexeditor.lagrida.com/), by using Firefox's built-in ability to screenshot HTML nodes, in particular the [MathJax](https://www.mathjax.org/) node with the equation. - -###### Antonelli's formula - explained - -```latex -\begin{align*} - & \overrightarrow{F}(r_l,h) = -\frac{3}{2} \; h^2 \; \frac{\hat{r_l}}{\|r_l\|^5} \quad \text{, where}\\ - & h = \|r_l \cross v_l\|\\ - & \hat{r_l} = \frac{r_l}{\|r_l\|}\\ - & \|x\| = \text{magnitude of }x\\ - & r_l = \text{vector from black hole center to light "particle"}\\ - & v_l = \text{velocity of light "particle"} -\end{align*} -``` - -###### Antonelli's formula - simplified - -```latex -\begin{align*} - & \overrightarrow{F}(r_l,h) = -\frac{3}{2} \; h^2 \; \frac{r_l}{\|r_l\|^6} \quad \text{, where}\\ - & h = \|r_l \cross v_l\| \text{ (calculated right before being affected by black hole)}\\ - & \|x\| = \text{magnitude of }x\\ - & r_l = \text{vector from black hole center to light "particle"}\\ - & v_l = \text{velocity of light "particle"} -\end{align*} -``` - -###### Algorithm - -```latex -\begin{align*} - & p_{cbh} = \text{position of center of black hole}\\ - & r_s = \text{Schwarzchild distance from } p_{cbn} \\ - & r_{ps} = \frac{2}{3}r_s \quad \text{(photon sphere)}\\ - & r_{ISCO} = 3 r_s \\ - \\ - & p_l = \text{position of light "particle"}\\ - & v_l = \text{velocity of light "particle"}\\ - \\ - & r_l = \| p_l-p_{cbh} \| \quad \text{(distance between black hole and light)}\\ - & \text{If } r_l \leq r_{ISCO} : \\ - & \quad h = \| r_l \cross v_l \|\\ - & \quad \text{LightMovesLimit} = \infty \\ - & \quad \text{Repeat maximum LightMovesLimit times}:\\ - & \qquad \text{If } r_l \leq r_s : \\ - & \qquad \quad \text{return "Light never escapes"} \\ - & \qquad \text{If } r_l \leq r_{ps} : \\ - & \qquad \quad \text{LightMovesLimit} = 100 \\ - & \qquad \text{If } r_l > r_{ISCO} : \\ - & \qquad \quad \text{return "Light escaped black hole"} \\ - & \qquad p_l = p_l + v_l \quad \text{(move light)}\\ - & \qquad r_l = \| p_l-p_{cbh} \| \quad \text{(update distance to $p_{cbh}$)}\\ - & \qquad v_l = v_l + \overrightarrow{F}(r_l,h) \quad \text{(update light velocity)} \\ - & \quad \text{return "Light stuck in photon sphere, never escapes"} -\end{align*} -``` - -[^matter-together]: Technically, it's more accurate to say "matter apart", since after the collapse matter is as close together as physically possible, and the nuclear forces keep everything in order but also separated. However, considering the intuitive approach I'm striving for, "matter together" is simpler to understand. -[^parallels-of-latitude]: You might think about the latituginal lines on a globe, they are indeed parallel (equidistant), but if you take any two points on the same such line, the distance between them won't be the smallest. -[^geodesics-are-local]: This actually isn't completely true, a geodesic is the "locally shortest distance", I've skipped the details since they aren't really that important for our purpopses. More info can be found on [wikipedia](https://en.wikipedia.org/wiki/Geodesic#Introduction). -[^schwm-assumptions]: He also assumed the body didn't have an electric charge and the [cosmological constant](TODO) is zero. These are way outside of my understanding and our purposes, so I skiped them for simplicity. -[^isco]: It's not really true that gravity stopps affecting spacetime after the ISCO, even though things can orbit Earth that doesn't mean that Earth doesn't bend spacetime. However, for the purposes of a simple shader, this assumption produces perfectly useable results. -[^hitting-trains]: Of course, I'm asuming the trains are strong enough they won't break upon the collision. -[^angular-momentum]: Angular momentum is actually [defined using the cross product](https://en.wikipedia.org/wiki/Specific_angular_momentum#Definition) -[^light-has-no-mass]: Actually, light has no mass, so this version of the formula wouldn't work. You might think that makes no sense, since we cannot divide by zero, so the specific angular momentum shouldn't exist, but then with the cross product we find a value for it. You would be correct: Antonelli does some funky approximations, combining Newtonian physics with General theory of relativity, to make the formula that simple. Someone in physics will probably vomit looking at it, but for our purposes it's more than perfect. diff --git a/src/blog/the-physics-behind-a-black-hole-shader/img/Schwarzschild-metric-in-Schwarzschild-coordinates-wikipedia.png b/src/blog/the-physics-behind-a-black-hole-shader/img/Schwarzschild-metric-in-Schwarzschild-coordinates-wikipedia.png new file mode 100644 index 0000000..3964615 Binary files /dev/null and b/src/blog/the-physics-behind-a-black-hole-shader/img/Schwarzschild-metric-in-Schwarzschild-coordinates-wikipedia.png differ diff --git a/src/blog/the-physics-behind-a-black-hole-shader/img/Schwarzschild-radius-wikipedia.png b/src/blog/the-physics-behind-a-black-hole-shader/img/Schwarzschild-radius-wikipedia.png new file mode 100644 index 0000000..fccf54f Binary files /dev/null and b/src/blog/the-physics-behind-a-black-hole-shader/img/Schwarzschild-radius-wikipedia.png differ diff --git a/src/blog/the-physics-behind-a-black-hole-shader/img/algorithm.png b/src/blog/the-physics-behind-a-black-hole-shader/img/algorithm.png new file mode 100644 index 0000000..40d4c86 Binary files /dev/null and b/src/blog/the-physics-behind-a-black-hole-shader/img/algorithm.png differ diff --git a/src/blog/the-physics-behind-a-black-hole-shader/img/antonelli-formula-expanded.png b/src/blog/the-physics-behind-a-black-hole-shader/img/antonelli-formula-expanded.png new file mode 100644 index 0000000..48eb7d4 Binary files /dev/null and b/src/blog/the-physics-behind-a-black-hole-shader/img/antonelli-formula-expanded.png differ diff --git a/src/blog/the-physics-behind-a-black-hole-shader/img/isco-wikipedia.png b/src/blog/the-physics-behind-a-black-hole-shader/img/isco-wikipedia.png new file mode 100644 index 0000000..2b01135 Binary files /dev/null and b/src/blog/the-physics-behind-a-black-hole-shader/img/isco-wikipedia.png differ diff --git a/src/blog/the-physics-behind-a-black-hole-shader/img/normalize-vector-wikipedia.png b/src/blog/the-physics-behind-a-black-hole-shader/img/normalize-vector-wikipedia.png new file mode 100644 index 0000000..517e142 Binary files /dev/null and b/src/blog/the-physics-behind-a-black-hole-shader/img/normalize-vector-wikipedia.png differ diff --git a/src/blog/the-physics-behind-a-black-hole-shader/img/photon-sphere-wikipedia.png b/src/blog/the-physics-behind-a-black-hole-shader/img/photon-sphere-wikipedia.png new file mode 100644 index 0000000..840572c Binary files /dev/null and b/src/blog/the-physics-behind-a-black-hole-shader/img/photon-sphere-wikipedia.png differ diff --git a/src/blog/the-physics-behind-a-black-hole-shader/img/riccardo-antonelli-schwarzschild-black-hole-force-field-formula.png b/src/blog/the-physics-behind-a-black-hole-shader/img/riccardo-antonelli-schwarzschild-black-hole-force-field-formula.png new file mode 100644 index 0000000..846b0d3 Binary files /dev/null and b/src/blog/the-physics-behind-a-black-hole-shader/img/riccardo-antonelli-schwarzschild-black-hole-force-field-formula.png differ diff --git a/src/blog/the-physics-behind-a-black-hole-shader/img/specific-angular-momentum-wikipedia.png b/src/blog/the-physics-behind-a-black-hole-shader/img/specific-angular-momentum-wikipedia.png new file mode 100644 index 0000000..61b3383 Binary files /dev/null and b/src/blog/the-physics-behind-a-black-hole-shader/img/specific-angular-momentum-wikipedia.png differ diff --git a/src/blog/the-physics-behind-a-black-hole-shader/img/variables-expanded.png b/src/blog/the-physics-behind-a-black-hole-shader/img/variables-expanded.png new file mode 100644 index 0000000..484e2f1 Binary files /dev/null and b/src/blog/the-physics-behind-a-black-hole-shader/img/variables-expanded.png differ diff --git a/src/blog/the-physics-behind-a-black-hole-shader/index.md b/src/blog/the-physics-behind-a-black-hole-shader/index.md new file mode 100644 index 0000000..555744a --- /dev/null +++ b/src/blog/the-physics-behind-a-black-hole-shader/index.md @@ -0,0 +1,544 @@ +# Difficulties of making a black hole shader + +## Contents + +1. [Preface](#preface) + 1. [Brief introduction to computer graphics](#brief-introduction-to-computer-graphics) + 2. [Brief introduction to WebGL](#brief-introduction-to-webgl) +2. [Theoretical and algorithmic background](#theoretical-and-algorithmic-background) + 1. [What is a black hole](#what-is-a-black-hole) + 2. [Geodesics, gravity and gravitational lensing](#geodesics-gravity-and-gravitational-lensing) + 3. [Finding a formula](#finding-a-formula) + 4. [Applying the formula to "bend" light's path](#applying-the-formula-to-bend-lights-path) + 5. [Properties of a black hole](#properties-of-a-black-hole) + 6. [Putting everything together](#putting-everything-together) +3. [WebGL preparation](#webgl-preparation) +4. [Shader](#shader) +5. [Appendix](#appendix) + +Back in August I had to submit a project for an elective WebGL programming course in university. +I chose to create an interractive and animated black hole demo. +The primary idea was to show how the gravitational pull of black holes "bends" light and how the result looks from different points of view. + +During development I found a lot of hurdles, primarily with the actual mathematics behind it. +Information online is difficult to find and understand without having a physics background, so I'll try my best to lay out everything I know. + +I'll assume you don't know much about physics, but are familiar with mathematics and at the very least vectors. + +## Preface + +### Brief introduction to computer graphics + +Image and video files store visual information in a "fixed" way: you get a huge array of colors, each connected to a particular pixel on your screen. +The objects in those mediums cannot be modified easily, and additions or subtractions are even harder to do, usually requiring an expert using professional tools for hours, days or weeks on end. + +Computer graphics aims to offload a lot of the creator's workload by "generating" the images on the fly using a set of descriptions for our objects. +The idea is to define objects with some primary properties of simpler geometric figures, like the points on a triangle, then give those to the computer, which will in turn have to calculate what the precise color of every pixel in the resultant image should be. + +For example, we can represent a tin can (without a label) as a simple cyllinder. +Changing it's height or width is trivial: just change the corresponding property you pass to the computer, and it will handle the rest. + +Of course this system has it's limitations: the more detail you add to an object, the more difficult it is to crunch up the math. +And adding lighting with shadows, bloom, refractions and so on becomes even more demanding for the computer. + +Expanding on out tin can example, if you want an image with a tin can up close, with a bright light shining on it, showing the hunderds or thousands of small scratches, bumps and kicks from manufacturing and usage, alongside reflections and shadows, you'll swiftly find that computer graphics are not the correct tool for the job. + +![](https://upload.wikimedia.org/wikipedia/commons/thumb/a/a8/Empty_tin_can2009-01-19.jpg/514px-Empty_tin_can2009-01-19.jpg) +*Image by Sun Ladder on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=5746428)* + +Such an image contains way way too much detail for the average computer to handle in reasonable time (some small fraction of a second). +However, computer generated graphics are still a very useful tools. +Thats why modern machines come with a graphics card handling *only* graphical tasks (alongside a processor, handling all other tasks like running programs, managing internet traffic, etc.). + +A graphics card is a combination of a special processor (GPU), designed to be able to *only* compute stuff related to graphics, with the upside of doing so much much faster than a general purpose processor (CPU) could, and some memory for use exclusively by that GPU. + +Since the GPU is different on the hardware level, it accepts significantly different instructions than a normal processor does. +For this reason programs written for a graphics processing unit are called "shaders", rather than just "programs". + +Of course, higher-level programming languages have been designed, which can then be transformed into the GPU-specific instructions. +But, contrary to normal software development, we don't really have standalone "shader compilers", but graphics library which do the job, alongside TODO + +### Brief introduction to WebGL + +One of the most popular such libraries is [OpenGL](TODO) with it's associated shader programming languages [GLSL](TODO) and [GLSL ES](TODO). +In the 2000s people decided it would be a good idea to add a graphics API/library to web browsers, which later morphed into what we call today [WebGL](TODO). + +It serves as a "layer above" OpenGL: you write JavaScript code with which the browser tells OpenGL what to do. +You would get a DOM object in which the graphics will be rendered, and a "context" object to which you'll pass down your shaders, shader parameters and change graphics settings. + +## Theoretical and algorithmic background + +&&important I haven't formally studied physics, so don't fully trust anything here. + There are most definetly errors, steming from my lackluster understanding. + +Using [Einstein's field equations](TODO), or to simplify our life, [Schwarzschild's metric](TODO), we can precisely calculate the curvatures in space(time) at every point around a Schwarzschild black hole. +Unfortunetly, the computation would still be too laborious, so we'll need to make an approximation using [celestial mechanics](TODO). + +That is the overall gist of the underlying physics, but I promised an explanation, so let's back up a bit. + +### What is a black hole + +Before we even begin to talk about gravity and light, we need to understand how atoms work. +Imagine a set of marbles, scattered on a table. + +![](https://upload.wikimedia.org/wikipedia/commons/thumb/0/0e/Marbles_from_Indonesia.jpg/1280px-Marbles_from_Indonesia.jpg) +*Image from By Okkisafire on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=30338734)* + +Now, start pushing on all of them from the edges of the table to it's center. +In the beginning, you'll mostly roll them, until they all get in contact with each other. +At that point (assuming they all stay flat on the table), the glass will keep the marbles together, handling your force. + +By increasing the force with which you push on them, after a certain threshold, the glass will break, turning the marbles into shards. +Those shards will still have some fight in them, so after increasing your force by another level, you'll turn all of those shards into tiny glass granules. + +A similar occurence happens with atoms. +Normally atoms are spread apart, by pushing them together, at a certain point (ignoring the orbitin electrons) all nuclei will be "touching" and the [nuclar force](TODO) will keep them together. +By increasing your pushing force you can overwhelm the nuclear force and "shatter" the nuclei into their subatomic particles. + +Assuming an enormus explosion doesn't happen, and you can keep increasing your force, it's also possible to overwhelm the "subatomic forces" that keep those subatomic particles "together"[^matter-together]. +However, those subatomic particles are more commonly referred to as [elementary particles](https://en.wikipedia.org/wiki/Elementary_particle), meaning there are no "granules" into which the elementary particles can break down. +It doesn't make sense if subatomic particles wouldn't be "breakable", since that would require dealing with potentially infinite amounts of force and such an assumption would destroy all of humanity's physics. + +Then that leaves us with only one solution: laws of matter break. +If nothing can keep the elementary particles "together", so keep matter "together", then the result is a sort of "collapse", all of that matter just "deflates" into a single, infinetly small point (but the amount of mass isn't infinite, it's the sum of mass from the particles that collapsed). +That point is called a **black hole**. + +This object is very bizzare, generally it doesn't have too much properties, but the ones it has are really unique. +In my case of computer graphics, the only important part is it's effect on light. + +### Geodesics, gravity and gravitational lensing + +To understand how black holes affect light, we first need to understand how black holes affect gravity. + +Think of a flat surface, like a table. +What is the shortest distance between two points? +Obviously, that's a straight line segment. +If you then extend that segment, so it has an infinite length, it becomes a line. + +However, what if our surface isn't flat? +Imagine two points on a sphere, what is the shortest distance between them? +Obviously, it's an arc of a circle and extending it to it's maximum possible length, we get a [great circle](https://en.wikipedia.org/wiki/Great_circle). + +![](https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/Illustration_of_great-circle_distance.svg/480px-Illustration_of_great-circle_distance.svg.png) +*Image by CheCheDaWaff on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=48187293)* + +Now, what does it mean to go "forwards"? +On a flat surface, we can define it as "following a (straight) line segment", on our sphere we can say "following an arc of a great circle". +But what we really need is a more generalized definition, which handles both of our cases (and more). + +That's why in geometry there is the notion of [geodesics](TODO), the generalized curve on any surface which (roughly speaking) is the shortest[^geodesics-are-local] path between any two points. +On a flat surface, that's the line segment, on the sphere it's the great circle arc. +It's important to note that a general geodesic cannot have most properties you might be used to from "the shortest path between two points". +For example, on a flat surface, we can have parallel lines, but on our sphere, great circles will always intersect[^parallels-of-latitude]. + +And there are a lot more complicated surfaces, imagine a flat surface but with half-spherical "trophs" in it, on the flat segments your geodesic would be the line segment, but on those trophs it will be an arc (of a great circle). +Actually, you don't need to imagine: the entire universe is a 3D surface, which is mostly flat, but has sherical-ish "trophs" in it. + +That's one of the primary results from the General theory of relativity, normally the universe's surface is nice and flat, but objects with mass "produce" gravity, which some sort of a force[^gravity-isnt-a-force] that bends our surface and creates those trophs, changing the local geodesics. +The resultant effect is that if you travel perfectly forwards in a spaceship and pass by an object with mass (like a planet) from a close enough distance, your path will be curved, without you steering your ship. + +Here comes the meat of this section: if you have no outside references (if all planets and stars are invisible), you, in your ship, *wouldn't* really be able to tell your path is curved. +But to a (stationary) outside observer, like another spaceship, it *would* look like you've turned. +In fact, while "turning" it will look like you've slowed down, since an arc is always longer than a straight line segment, but you're travelling at the same speed. + +For the cherry on top, light is also affected by gravity, and we know it's speed is constant and always travels forwards, therefore gravity "bends" light. + + + +The amount by which and the size of the area which is affected depend on that object's mass and size. +Generally, if two objects have the same mass, the bigger one will affect a larger portion of space, but will bend it less, compared to the smaller one, which will affect a smaller portion, which will be bend a lot. + +Since a black hole is a single infinetly small point, then all of it's mass is concentrated on that point, making it bend a relatively small are of space **a lot**. +Strong enough in fact, that if you get too close, you will not be able to overwhelm gravity and "merge" into that infinitessimal point (the white lines are our "geodesics"): + +![](https://upload.wikimedia.org/wikipedia/commons/thumb/9/91/CNX_UPhysics_13_07_spacetime.png/800px-CNX_UPhysics_13_07_spacetime.png?20171124170318) +*Image by OpenStax University Physics on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=64307901)* + +An interesting effect from gravity is that light (which always travells "forwards", so along a geodesic) also gets "bent", allowing an observer to view otherwise obstructed objects. +We call that [graviational lensing](TODO). +Though not unique to black holes, thats when the effect is strongest: + +![](https://upload.wikimedia.org/wikipedia/commons/0/02/Gravitational_lens-full.jpg) +*(notice how the gray arrows show the path light travels from a galaxy that is behind (obstructed by) the black hole)* + +I'll abstrain from explaining light, but you may think of it as a small particle, "similar" to an electron, travelling through space. +In fact, Newton understood light to be just that, and called the underlying particle a "corpiscule". +For our purpopses, this approximation will serve us well. + +The distinction that gravity doesn't change the direction or speed of an object "directly" is at upmost importance for physics, since this way we preserve two very important properties of light: it's constant speed and straight travelling path. +However, in our simulation, it would be significantly simpler to assume the opposite, since to an outside observer (like the "player") the difference is indistinguishable. + +Therefore, in our simulation, we'll have light rays, but when they approach a black hole, their speed and direction would change. +This will be determined by some physics equations. + +### Finding a formula + +Now, how do we calculate the exact curvature a black hole produces? +General theory of relativity provides us with the (really complicated) [Einstein field equations](https://en.wikipedia.org/wiki/Einstein_field_equations) for this exact problem. +[Schwartzschild](TODO) solved these equations, assuming that the body with mass doesn't spin[^schwm-assumptions]. +This solution is called the [Schwarzschild metric](https://en.wikipedia.org/wiki/Schwarzschild_metric), and a black hole for which this assumption is true is called a "Shwartzschild black hole". + +The Shwarzschild black hole is the symplest of it's type, so using the Schwarzschild metric should give us the least trouble. +Now, here is the metric in Schwartzschild coordinates: + +![](./img/Schwarzschild-metric-in-Schwarzschild-coordinates-wikipedia.png) + +**YOUCH!** + +The variables themselves aren't that bad: `c` is the speed of light, τ is time, Ω, `t` and `r` are Schwarzschild coordinates. + +However, as you can see, there are two big problems: we have to calculate derivatives (that's what the `d` signifies) and convert from Schwarzschild coordinates to normal (eucledian) coordinates, that the computer understands natively. +Both of those operations are **incredibly slow**, the average computer doesn't standa a chance at rendering anything in a reasonable amount of time. + +Thankfully [Riccardo Antonelli](http://rantonels.github.io/starless/) was able to do all of our coordinate conversions and simplify the living crap out of that monster (using some approximations with celestial mechanics), turning it into something digestible: + +![](./img/riccardo-antonelli-schwarzschild-black-hole-force-field-formula.png) + +I will not be trying to explain why or how the Schwarzschild metric and this formula work, that's way way beyond my elementary understanding and the scope of this post. + +Question now is: what do all of those letters represent? +This turned out to be a much harder problem than it should have, I actually had to refer [Ricardo Antonelli's](https://github.com/rantonels/starless/blob/7a0c3c4efdc0666f53dcfa48d310fb2164be2d1a/tracer.py) and [Dmitry Brant's](https://dmitrybrant.com/2018/12/11/ray-tracing-black-holes) implementation in their projects, alongside copus articles on wikipedia to fully grasp how to put it to work. + +### Applying the formula to "bend" light's path + +Our plan for simulating gravitational lensing is to move the corpiscule by a tiny amount in a certain direction by a certain amount, update the direction and amount according to gravity, rince and repeat. +The direction, alongside the amount by which the particle moves, can be represented with a single vector, which we'll call light's "velocity". + +Referring to the formula again: + +![](./img/riccardo-antonelli-schwarzschild-black-hole-force-field-formula.png) + +The F→ returns a force vector (`F` for "force" and → for "vector"): any force acting on an object has a strength (magnitude) and a direction. +Imagine you are pushing a box: you are applying some force to it in a "forward" direction. +If you pull the box, you'll apply some force in a "backward" direction. + +This force vector conveys how much and in what direction exactly the black hole is "pulling" on the corpiscule. + +Although the function takes in only one argument, `r`, that doesn't mean that `h` or r\^ aren't also arguments: + +- `r` is the distance from the center of the black hole (the singularity) to the **current** position of the corpiscule. + +- `h` is the magnitude of the ["specific angular momentum"](https://en.wikipedia.org/wiki/Specific_angular_momentum) of the light "particle". + + "Normal" linear momentum specifies how much "force" at what speed it takes to stop (or change) the speed and direction of an object in linear motion (that's why it's in kg\*m/s). + Imagine a big heavy train on a perfectly straight rails going at a fast speed. + How do you forcefully stop it? + + You can put a *really* heavy barrier on the tracks, in front of the train. + Since the train is moving at a really great speed, this barrier will have to be much much heavier (with much more mass) than the train itself. + + Alternatively, you can "launch" something against the train, meaning you can put a second train, moving in the opposite direction of the first train, so they hit head-on. + Since the second train would also be moving, if it's travelling at the same speed and has the same mass as the first train, upon collision they would both stop[^hitting-trains]. + If the second travells at twice the speed, then it can have half of the mass of the first to produce the same effect. + + The property with which we can "prove" why both a larger mass at no speed, and a smaller mass at a faster speed, can stop the train, is the momentum. + + Angular momentum specifies how much "force" at what speed it takes to stop (or change) the speed and directino of a rotating object. + Imagine the train scenario but on a circular track. + + The specific angular momentum is the angular momentum of an object, divided by it's mass. + It's magnitude kind of tells us which property is more prevalent in the momentum, the mass or the speed. + For the same angular momentum, a long (big magnitutde) *specific* angular momentum means our object is small and moves fast, while for a short (small magnitutde) *specific* angular momentum out object is large and moves slowly. + + In our case a light "particle" is being pulled by a stationary object of large mass (a Shwarzschild black hole), so rather than finding the angular momentum[^angular-momentum] and then dividing by the mass[^light-has-no-mass], we'll use another formula: + + ![](./img/specific-angular-momentum-wikipedia.png) + + where **in this specific formula only** `r` is the vector from the center of the big mass (the singularity of the black hole) to the light "particle" and `v` is the velocity (direction and amount of speed) the light is travelling with. + + The cross product returns a vector, so we'll need to take the magnitutde of the result and plug that into the formula. + It's also important that **we calculate it only once, in the beginning**. + So, we shouldn't be calculating the magnitude of the specific angular momentum every time the light "particle" is moved, rather we calculate it once, after it gets close enough to be affected by the black hole. + +- r^ is the normalized ([unit](https://en.wikipedia.org/wiki/Unit_vector), hence the `^` on top) vector from the center of the black hole to the light particle: + + ![](./img/normalize-vector-wikipedia.png) + +Finally, putting all needed variables together in one proper function, we get: + +![Refer to "Antonelli’s formula - explained" in the Appendix](./img/variables-expanded.png) + +As you may notice, this can be simplified, if we substitude r^: + +![Refer to "Antonelli’s formula - simplified" in the Appendix](./img/antonelli-formula-expanded.png) + +In effect, we pass the distance from the black hole to the corpiscule and the function returns how light's velocity changes. + +Finally, we know what to give the formula, and what it will return. +The task at hand is making an algorithm which utilises it. +For that we'll need to get even more familiar with black holes. + +### Properties of a black hole + +As stated, we'll be working with non-rotating stationary black holes, Schwarzschild black holes. +From the [no-hair conjecture](https://en.wikipedia.org/wiki/No-hair_theorem) we know that such a black hole has only three distinct properties: mass, electric charge and angular momentum. +The last one doesn't apply for our case, and the second is irrelevant. + +Meaning, from the mass alone, we can derive the gravitational "forces", alongside three radii, which will be the backbone of our black hole: the event horizon, the photon sphere and [ISCO](https://en.wikipedia.org/wiki/Innermost_stable_circular_orbit). + +The event horizon, also called the Schwarzschild radius, is the distance from the center of the black hole (singularity), which once passed, cannot be passed again (you cannot escape the black hole). +The formula for it is: + +![r_s = (2GM)/(c^2)](./img/Schwarzschild-radius-wikipedia.png) + +where `G` is the [gravitational constant](https://en.wikipedia.org/wiki/Gravitational_constant) and `M` is the mass of the black hole (in kg). + +The photon sphere is the distance from the singularity, in which photons start orbiting the black hole (and can either escape or pass the event horizon). +Formula: + +![r = r_s * 3/2](./img/photon-sphere-wikipedia.png) + +where `G` and `M` are the same as before, `c` is the speed of light in vacuum and rs is the afroementioned Schwarzschild radius. + +Finally, the innermost stable circular orbit (ISCO), formally it's the distance, before which you can orbit the black hole, but once passed, you'll get sucked in and either get shot out or be forever lost past the event horizon. +For our purposes, it's fine to approximate it as the maximum radius after which spacetime isn't bent[^isco] and objects aren't affected. +Formula: + +![](./img/isco-wikipedia.png) + +Putting them all together, we should expect light to travel like this: +![](https://upload.wikimedia.org/wikipedia/commons/2/27/Black_Hole_Shadow.gif) +*Animation by Hugo Spinelli on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=138168679)* + + +### Putting everything together + +Finally, now we have everything we need to divise an algorithm for our light. +For light we'll store two vectors: one for it's position and one for it's velocity. +For a black hole we'll store a vector and a quantity: the position of the black hole's center and it's mass. +For convinience, we could just store the Schwarzschild radius, since all of our radii depend only on it. + +Once the distance between the light and the center is less than or equal to the ISCO, we can begin augmenting light's velocity. +On small steps, we move the light forward a bit with the velocity and then update the velocity with the black hole's pull. + +If light enters the event horizon, then we know it cannot escape, and stop moving it. +If it passes the ISCO then it has left the black hole and will never approach it again. + +If light enters the photon sphere, then we'll have to "track" how long it's been there, if it decides to orbit in a stable manner, then it will stay there indefinetly. +The simplest way to "track" this is just to limit how many times we can move the light forward. + +An algorithm doing this would look a bit like this (in pseudocode, inspired by Python): + +![Refer to "Algorithm" section of the Appendix](./img/algorithm.png) + +as a reminder, here is the definiton of F→: + +![](./img/antonelli-formula-expanded.png) + +## WebGL preparation + +The course was focused on handmade solutions, so some of the logic uses a [custom handmade library](TODO). + +Since the primary component was the shader itself, I made sure to simplify my logic to the absolute minimum. +A **lot** of decisions were made to optimize development time, without considering how good they were. + +In a scene there really are only 3 distinct objects: the stars, the black holes and the skybox. +The latter is just a simple cube, inside which everything resides, if a ray doesn't hit anything of interest, it will hit the cube. +Both stars and black holes are simple spheres, the former with an orange-y color (#f4803b) and any radious. +Black holes aren't rendered and their underlying sphere has the same radius as the ISCO, so their purpose is for intersection checks (more on those when we get to the [shaders](#shaders)). + +Most JavaScript code isn't worth talking about, wherever possible I use the [course library](TODO) and where I can't it's mostly simple WebGL configurations, elementary object "classes" and trivial HTML handling. +Though, there are still two somewhat interesting systems: + +### Scenes + +I wanted to have multiple scenes, with different arrangements of objects and with animations (stars moving around). +For that I devised a dead-simple level loading and animation system. + +An object of type "Level" stores a name, description, location of camera (the viewer) and arrays for stars, black holes and animations. +Though, for creating new levels, you only pass the first three: + +```javascript +var level1 = new Level( + "Star passing behind black hole", // Level title + "The black hole warps the light", // Level description + [1000,0,650]); // Initial coordinates of camera +``` + +Adding stars or black holes requires going through two member functions: `addStar` and `addBlackHole`. +Both take the center of the object, as well as it's radius, alongside an animation function. +The latter has two parameters: current time in TODO(seconds,milliseconds,?) from the beginning and the current object that will be created. +This function is called on each frame with the appropriate arguments, so inside you'll have to "manually" modify the object's properties. + +For example, a simple star going forwards could be added like this (`vecSum` is just [componentwise vector summation](TODO)): + +```javascript +level1.addStar([1070,0,1000], // Coordinates of center + 10, // Radius + (t, star) => vecSum(star.center, [-t,0,0])); // Move from left to right +``` + +Or one which rotates around a point: + +```javascript +var speed = Math.PI / 20, rad = 40; +level1.addStar([1000+rad,0,900], + 3, + (t, star) => vecSum([1000,0,900], [Math.cos(t*speed) * rad, + Math.sin(t*speed) * 5, + Math.sin(t*speed) * rad])); +``` + +For black holes it's the same. +If you want the object to not move, you could just pass `undefined` as the function: + +```javascript +level1.addBlackHole([1000,0,900], 30, undefined); +``` + +### Movement + +It wouldn't be fun without the ability to move around in the scene, so I added a simple system for that too. +WASD moves the camera position forward, left, backward and right, shift moves down and space moves up, all relative to the current direction the camera is looking towards. +Those are all done with relatively simple analytic geometry: we store three vectors, one pointing up, one pointing forward and one pointing left (which can be calculated from the other two with a cross product). +To move forward, add to the position the forward vector times some constant, to move backwards subtract and so on. + +Looking around with the mouse was kinda tricky, since I decided to use [spherical coordinates](TODO). +You store two angles, the "up" and "left" angle, moving the mouse forwards adds to the "up" angle, moving it downwards subtracts and so on. +The tricky part was what to do after + +TODO + +## Shaders + +As a gentle reminder, in OpenGL we have a vertex shader and a fragment shader: + +- The vertex shader is ran on every point of every object in our scene +- The fragment shader is ran on every pixel of your screen, with the added bonus that depending on the pixel, certain values are passed down from the vertex shader. + + This includes interpolated points: a line is made up of two points, the vertex shader is ran on both of them, then the line is interpolated and the fragment shader is ran on each pixel, corresponding to a point on the line. + The vertex shader values are also passed down interpolated, so if one end of the line is the color red, while the other end is green, then the fragment shader corresponding to the pixel, which shows the middle of the line, will have the color yellow. + +### Vertex shader + +As already mentioned, black holes aren't rendered, so the only objects with special rendering are the stars. +To simplify my life, I chose to do a simple [diffusion reflection](https://learnopengl.com/Lighting/Basic-Lighting), with the light source emminating from the camera, something which was already made in lectures: + +![](TODO) + +The result isn't amazing, but you can clearly make out the sphere as a 3D object (not a flat circle). + +### Fragment shader + +This is the meat of our logic, this is where we'll implement our algorithm: + +![](./img/algorithm.png) + +In real life, every star emits light, which then travells some distance, until it reaches our eyes. +However, computing it this way is incredibly inefficient, for every star we'll have to create hundreds or thousands of light rays (corpiscules), most of which probably wont reach us. +We want performance, so this is a no-go. + +The alternative is to do the opposite, assume every pixel was hit by a "ray" and trace back it's steps, if it came out of a star then it has some specific color (it was an actual ray), but if it didn't then show it as black (no ray hit us). +Taking it further, how do we "trace back" those steps? +Well, we can find the precise coordinate of every pixel inside the 3D scene, and move each point in the direction we're looking (though this image is about raytracing, just look at the two top red vectors): + +![](https://upload.wikimedia.org/wikipedia/commons/thumb/5/5c/Ray_trace_diagram.png/800px-Ray_trace_diagram.png?20080214101526) +*Image by Timrb on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=3553478)* + +In the context of computer graphics, this is called [ray marching](TODO). + +Now, how do we detect if the ray hit something? +The dumb way is to move the ray forward a bit and check with every object whether the ray intersects it. +However, since all of our objects are spheres, there is a very easy optimization and simplification - imagine the ray as an infinitely long line and just get the shortest (perpendicular) distance between it and the center of the sphere: + +TODO custom image #1 + +For this we can implement a simple distance function: + +![](https://wikimedia.org/api/rest_v1/media/math/render/svg/42c53741fd3d8765e56ea652369a512a2cd09eac) + +```glsl +float linePointDistance(vec3 a, vec3 n, vec3 p) { + return length((p - a) - dot(p - a, n) * n); +} +``` + +Where `a` is the coordinate of our ray (pixel), `n` is the direction it will be travelling in and `p` is the sphere center we're comparing with. + +However, there is a huge problem: our line extends in both directions, so a check like `linePointDistance(ray, step, center) <= radius` would also detect if the ray interects the point behind us (when ray is going in the opposite direction)! +A very simple solution is to just add a sign to our distance: if positive then the distance is in reference to a point in front, while negative means it's behind. +The simplest way to get this information is by taking the [dot product](TODO) between our "forwards" direction and the direction from our coordinates to the [projection](TODO) of our point on the line: + +TODO custom image #2 + +```glsl +float linePointDistance(vec3 a, vec3 n, vec3 p) { + vec3 projection = dot(p - a, n) * n; + float dist = length((p - a) - projection); + + if (dot(projection, n) < 0.0) return -dist; + return dist; +} +``` + +Since all of our objects are spheres, we can just iterate through all of them and do this check. + +## Appendix + +##### LaTeX source for equations + +The images for my custom LaTeX equations were created on [latexeditor.lagrida.com](https://latexeditor.lagrida.com/), by using Firefox's built-in ability to screenshot HTML nodes, in particular the [MathJax](https://www.mathjax.org/) node with the equation. + +###### Antonelli's formula - explained + +```latex +\begin{align*} + & \overrightarrow{F}(r_l,h) = -\frac{3}{2} \; h^2 \; \frac{\hat{r_l}}{\|r_l\|^5} \quad \text{, where}\\ + & h = \|r_l \cross v_l\|\\ + & \hat{r_l} = \frac{r_l}{\|r_l\|}\\ + & \|x\| = \text{magnitude of }x\\ + & r_l = \text{vector from black hole center to light "particle"}\\ + & v_l = \text{velocity of light "particle"} +\end{align*} +``` + +###### Antonelli's formula - simplified + +```latex +\begin{align*} + & \overrightarrow{F}(r_l,h) = -\frac{3}{2} \; h^2 \; \frac{r_l}{\|r_l\|^6} \quad \text{, where}\\ + & h = \|r_l \cross v_l\| \text{ (calculated right before being affected by black hole)}\\ + & \|x\| = \text{magnitude of }x\\ + & r_l = \text{vector from black hole center to light "particle"}\\ + & v_l = \text{velocity of light "particle"} +\end{align*} +``` + +###### Algorithm + +```latex +\begin{align*} + & p_{cbh} = \text{position of center of black hole}\\ + & r_s = \text{Schwarzchild distance from } p_{cbn} \\ + & r_{ps} = \frac{2}{3}r_s \quad \text{(photon sphere)}\\ + & r_{ISCO} = 3 r_s \\ + \\ + & p_l = \text{position of light "particle"}\\ + & v_l = \text{velocity of light "particle"}\\ + \\ + & r_l = \| p_l-p_{cbh} \| \quad \text{(distance between black hole and light)}\\ + & \text{If } r_l \leq r_{ISCO} : \\ + & \quad h = \| r_l \cross v_l \|\\ + & \quad \text{LightMovesLimit} = \infty \\ + & \quad \text{Repeat maximum LightMovesLimit times}:\\ + & \qquad \text{If } r_l \leq r_s : \\ + & \qquad \quad \text{return "Light never escapes"} \\ + & \qquad \text{If } r_l \leq r_{ps} : \\ + & \qquad \quad \text{LightMovesLimit} = 100 \\ + & \qquad \text{If } r_l > r_{ISCO} : \\ + & \qquad \quad \text{return "Light escaped black hole"} \\ + & \qquad p_l = p_l + v_l \quad \text{(move light)}\\ + & \qquad r_l = \| p_l-p_{cbh} \| \quad \text{(update distance to $p_{cbh}$)}\\ + & \qquad v_l = v_l + \overrightarrow{F}(r_l,h) \quad \text{(update light velocity)} \\ + & \quad \text{return "Light stuck in photon sphere, never escapes"} +\end{align*} +``` + +[^matter-together]: Technically, it's more accurate to say "matter apart", since after the collapse matter is as close together as physically possible, and the nuclear forces keep everything in order but also separated. However, considering the intuitive approach I'm striving for, "matter together" is simpler to understand. +[^parallels-of-latitude]: You might think about the latituginal lines on a globe, they are indeed parallel (equidistant), but if you take any two points on the same such line, the distance between them won't be the smallest. +[^geodesics-are-local]: This actually isn't completely true, a geodesic is the "locally shortest distance", I've skipped the details since they aren't really that important for our purpopses. More info can be found on [wikipedia](https://en.wikipedia.org/wiki/Geodesic#Introduction). +[^schwm-assumptions]: He also assumed the body didn't have an electric charge and the [cosmological constant](TODO) is zero. These are way outside of my understanding and our purposes, so I skiped them for simplicity. +[^isco]: It's not really true that gravity stopps affecting spacetime after the ISCO, even though things can orbit Earth that doesn't mean that Earth doesn't bend spacetime. However, for the purposes of a simple shader, this assumption produces perfectly useable results. +[^hitting-trains]: Of course, I'm asuming the trains are strong enough they won't break upon the collision. +[^angular-momentum]: Angular momentum is actually [defined using the cross product](https://en.wikipedia.org/wiki/Specific_angular_momentum#Definition) +[^light-has-no-mass]: Actually, light has no mass, so this version of the formula wouldn't work. You might think that makes no sense, since we cannot divide by zero, so the specific angular momentum shouldn't exist, but then with the cross product we find a value for it. You would be correct: Antonelli does some funky approximations, combining Newtonian physics with General theory of relativity, to make the formula that simple. Someone in physics will probably vomit looking at it, but for our purposes it's more than perfect. -- cgit v1.3.1-26-gb76a