aboutsummaryrefslogtreecommitdiff
path: root/engines/bladerunner/fog.cpp
diff options
context:
space:
mode:
authorPeter Kohaut2019-04-18 22:14:06 +0200
committerPeter Kohaut2019-04-18 22:14:43 +0200
commitd0edb79a101427c57689dc84eb3875aed0eed29e (patch)
tree35f616e805ef8847bc7e9b72eb1ed030ac64778a /engines/bladerunner/fog.cpp
parentbdf5f1596ac4e5a21828272fcf295430bb28babb (diff)
downloadscummvm-rg350-d0edb79a101427c57689dc84eb3875aed0eed29e.tar.gz
scummvm-rg350-d0edb79a101427c57689dc84eb3875aed0eed29e.tar.bz2
scummvm-rg350-d0edb79a101427c57689dc84eb3875aed0eed29e.zip
BLADERUNNER: Clarification of some math
Diffstat (limited to 'engines/bladerunner/fog.cpp')
-rw-r--r--engines/bladerunner/fog.cpp17
1 files changed, 12 insertions, 5 deletions
diff --git a/engines/bladerunner/fog.cpp b/engines/bladerunner/fog.cpp
index 4856f1d3b3..e6b603702e 100644
--- a/engines/bladerunner/fog.cpp
+++ b/engines/bladerunner/fog.cpp
@@ -123,18 +123,25 @@ void FogSphere::read(Common::ReadStream *stream, int frameCount) {
void FogSphere::calculateCoeficient(Vector3 position, Vector3 viewPosition, float *coeficient) {
*coeficient = 0.0f;
- // ray - sphere intersection, where sphere center is always at 0, 0, 0 as everything else tranformed by the fog matrix
+ // Ray - sphere intersection, where sphere center is always at 0, 0, 0 as everything else tranformed by the fog matrix.
+ // Quadratic formula can and was simplified becasue rayDirection is normalized and hence a = 1.
+ // Explained on wikipedia https://en.wikipedia.org/wiki/Line%E2%80%93sphere_intersection
+
+ // There is also alternative approach which will end-up with this formula where plane is created from ray origin, ray destination
+ // and sphere center, then there is only need to solve two right triangles.
+ // Explained in book Andrew S. Glassner (1995), Graphics Gems I (p. 388-389)
+
Vector3 rayOrigin = _matrix * position;
Vector3 rayDestination = _matrix * viewPosition;
Vector3 rayDirection = (rayDestination - rayOrigin).normalize();
float b = Vector3::dot(rayDirection, rayOrigin);
float c = Vector3::dot(rayOrigin, rayOrigin) - (_radius * _radius);
- float t = b * b - c;
+ float d = b * b - c;
- if (t >= 0.0f) { // there is an interstection between ray and the sphere
- Vector3 intersection1 = rayOrigin + (-b - sqrt(t)) * rayDirection;
- Vector3 intersection2 = rayOrigin + (-b + sqrt(t)) * rayDirection;
+ if (d >= 0.0f) { // there is an interstection between ray and the sphere
+ Vector3 intersection1 = rayOrigin + (-b - sqrt(d)) * rayDirection;
+ Vector3 intersection2 = rayOrigin + (-b + sqrt(d)) * rayDirection;
Vector3 intersection1World = _inverted * intersection1;
Vector3 intersection2World = _inverted * intersection2;