Rise
The Vieneo Province
Loading...
Searching...
No Matches
SunPositionOverride.h
Go to the documentation of this file.
1#pragma once
3#include <cmath>
4
6{
7 // Render coordinates have the opposite sign to the simulation position.
8 // Rotate the entire solar disc so its apparent size and facing are retained.
9 // The caller supplies the freshly computed orbital matrix every frame.
10 // 0..1 maps linearly from -90 to +90 degrees above the local tangent plane.
11 // Keep the natural solar azimuth; at zenith/nadir choose a stable tangent.
12 inline void Apply(float mode, const RiseMath::Vector3& outwardNormal,
13 RiseMath::Matrix& sunWorld, RiseMath::Vector3& incomingLight)
14 {
15 if (!std::isfinite(mode) || mode < 0.f || mode > 1.f) return;
16 RiseMath::Vector3 source(sunWorld._41, sunWorld._42, sunWorld._43);
17 const float distance = RiseMath::Vec3Length(&source);
18 if (distance <= 0.f || RiseMath::Vec3LengthSq(&outwardNormal) <= 0.f) return;
19 source /= distance;
20 RiseMath::Vector3 target;
21 RiseMath::Vec3Normalize(&target, &outwardNormal);
22 if (mode == 0.f) target = -target;
23 else if (mode != 1.f)
24 {
25 RiseMath::Vector3 tangent = source - target * RiseMath::Vec3Dot(&source, &target);
26 if (RiseMath::Vec3LengthSq(&tangent) < 1.e-10f)
27 {
28 const RiseMath::Vector3 reference = std::fabs(target.z) < .9f
29 ? RiseMath::Vector3(0, 0, 1) : RiseMath::Vector3(0, 1, 0);
30 tangent = reference - target * RiseMath::Vec3Dot(&reference, &target);
31 }
32 RiseMath::Vec3Normalize(&tangent, &tangent);
33 const float elevation = (mode - .5f) * RiseMath::Pi;
34 target = target * std::sin(elevation) + tangent * std::cos(elevation);
35 RiseMath::Vec3Normalize(&target, &target);
36 }
37
39 RiseMath::Vec3Cross(&axis, &source, &target);
40 const float dot = RiseMath::Vec3Dot(&source, &target);
41 RiseMath::Quaternion turn(axis.x, axis.y, axis.z, 1.f + dot);
42 if (dot < -0.999999f)
43 {
44 // At opposite directions the usual rotation axis is undefined.
45 const RiseMath::Vector3 reference = std::fabs(source.z) < .9f
46 ? RiseMath::Vector3(0, 0, 1) : RiseMath::Vector3(0, 1, 0);
47 RiseMath::Vec3Cross(&axis, &source, &reference);
48 RiseMath::Vec3Normalize(&axis, &axis);
49 turn = RiseMath::Quaternion(axis.x, axis.y, axis.z, 0);
50 }
52 RiseMath::Matrix rotation;
53 RiseMath::MatrixRotationQuaternion(&rotation, &turn);
54 RiseMath::MatrixMultiply(&sunWorld, &sunWorld, &rotation);
55 sunWorld._41 = target.x * distance;
56 sunWorld._42 = target.y * distance;
57 sunWorld._43 = target.z * distance;
58 incomingLight = -target;
59 }
60}
float Vec3Length(const Vector3 *a)
Definition ClientMath.h:95
constexpr float Pi
Definition ClientMath.h:12
Vector3 * Vec3Cross(Vector3 *out, const Vector3 *a, const Vector3 *b)
Definition ClientMath.h:105
Matrix * MatrixMultiply(Matrix *out, const Matrix *a, const Matrix *b)
Definition ClientMath.h:116
Matrix * MatrixRotationQuaternion(Matrix *out, const Quaternion *q)
Definition ClientMath.h:135
Quaternion * QuaternionNormalize(Quaternion *out, const Quaternion *q)
Definition ClientMath.h:165
float Vec3LengthSq(const Vector3 *a)
Definition ClientMath.h:94
float Vec3Dot(const Vector3 *a, const Vector3 *b)
Definition ClientMath.h:93
Vector3 * Vec3Normalize(Vector3 *out, const Vector3 *a)
Definition ClientMath.h:103
void Apply(float mode, const RiseMath::Vector3 &outwardNormal, RiseMath::Matrix &sunWorld, RiseMath::Vector3 &incomingLight)