summaryrefslogtreecommitdiff
path: root/gl-vfog/geometry.hh
diff options
context:
space:
mode:
authorHunter Kvalevog <hunter@kvog.sh>2025-11-20 15:37:28 -0600
committerHunter Kvalevog <hunter@kvog.sh>2025-11-21 20:49:11 -0600
commit2890cf45c1ad62bf8496f15dfb4796f9c71c02eb (patch)
tree1b677376e962fb446588858f2a24cd3196b6bd3c /gl-vfog/geometry.hh
parent8a077ca7c421dcea92c9485aa284c6e636f2ddad (diff)
shaders
Diffstat (limited to 'gl-vfog/geometry.hh')
-rw-r--r--gl-vfog/geometry.hh108
1 files changed, 0 insertions, 108 deletions
diff --git a/gl-vfog/geometry.hh b/gl-vfog/geometry.hh
deleted file mode 100644
index c48248a..0000000
--- a/gl-vfog/geometry.hh
+++ /dev/null
@@ -1,108 +0,0 @@
-#ifndef _GEOMETRY_HH_
-#define _GEOMETRY_HH_
-
-#include <cmath>
-
-static inline float Radians(float degrees)
-{
- return degrees * M_PI / 180.0f;
-}
-
-class Vec3
-{
-public:
- float x;
- float y;
- float z;
-public:
- Vec3(float x, float y, float z)
- : x(x), y(y), z(z) { }
-
- inline Vec3 operator+(const Vec3& rhs) const { return Vec3(x + rhs.x, y + rhs.y, z + rhs.z); }
- inline Vec3 operator-(const Vec3& rhs) const { return Vec3(x - rhs.x, y - rhs.y, z - rhs.z); }
-
- inline float* Base() { return &x; }
-
- inline float Length() { return std::sqrtf(x * x + y * y + z * z); }
-
- inline Vec3 Normalize()
- {
- float l = Length();
- if (l < 0.0f) { l = 1.0f; }
- return Vec3(x / l, y / l, z / l);
- }
-
- inline float Dot(const Vec3& rhs)
- {
- return x * rhs.x + y * rhs.y + z * rhs.z;
- }
-
- inline Vec3 Cross(const Vec3& rhs)
- {
- return Vec3(
- y * rhs.z - z * rhs.y,
- z * rhs.x - x * rhs.z,
- x * rhs.y - y * rhs.x
- );
- }
-};
-
-class Mat4x4
-{
-private:
- float m[4 * 4];
-public:
- inline float& operator[](std::size_t s) { return m[s]; }
- inline float operator[](std::size_t s) const { return m[s]; }
-
- inline float* Base() { return m; }
-
- inline Mat4x4 operator*(const Mat4x4& rhs)
- {
- Mat4x4 r = { };
- for (int col = 0; col < 4; ++col) {
- for (int row = 0; row < 4; ++row) {
- r[col * 4 + row] =
- m[0 * 4 + row] * rhs[col * 4 + 0] +
- m[1 * 4 + row] * rhs[col * 4 + 1] +
- m[2 * 4 + row] * rhs[col * 4 + 2] +
- m[3 * 4 + row] * rhs[col * 4 + 3];
- }
- }
- return r;
- }
-public:
- static Mat4x4 LookAt(const Vec3& eye, const Vec3& at, const Vec3& up)
- {
- Vec3 f = (at - eye).Normalize();
- Vec3 s = f.Cross(up).Normalize();
- Vec3 u = s.Cross(f);
- Vec3 t = Vec3(-s.Dot(eye), -u.Dot(eye), f.Dot(eye));
-
- Mat4x4 m;
- m[ 0] = s.x; m[ 1] = u.x; m[ 2] = -f.x; m[ 3] = 0.0f;
- m[ 4] = s.y; m[ 5] = u.y; m[ 6] = -f.y; m[ 7] = 0.0f;
- m[ 8] = s.z; m[ 9] = u.z; m[10] = -f.z; m[11] = 0.0f;
- m[12] = t.x; m[13] = t.y; m[14] = t.z; m[15] = 1.0f;
-
- return m;
- }
-
- static Mat4x4 Perspective(float fov_deg, float aspect, float z_near, float z_far)
- {
- const float fov_cot = 1.0f / std::tanf(Radians(fov_deg) / 2.0f);
-
- Mat4x4 m = { };
-
- m[0*4+0] = fov_cot / aspect;
- m[1*4+1] = fov_cot;
- m[2*4+3] = -1.0f;
-
- m[2*4+2] = (z_far + z_near) / (z_near - z_far);
- m[3*4+2] = (2.0f * z_near * z_far) / (z_near - z_far);
-
- return m;
- }
-};
-
-#endif // _GEOMETRY_HH_