wren
Vulkan-based game engine
Loading...
Searching...
No Matches
geometry.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <numbers>
4
5#include "matrix.hpp"
6#include "vector.hpp"
7
8namespace wren::math {
9
10inline auto degrees(float radian) -> float {
11 return radian * static_cast<float>(180 / std::numbers::pi);
12}
13
14inline auto radians(float degrees) -> float {
15 return degrees * static_cast<float>(std::numbers::pi / 180);
16}
17
18auto translate(Mat4f mat, Vec4f offset) -> Mat4f;
19auto translate(Mat4f mat, Vec3f offset) -> Mat4f;
20
21auto look_at(const Vec3f& position, const Vec3f& target, const Vec3f& world_up)
22 -> Mat4f;
23
24auto rotate(const Mat4f& matrix, float rotation, const Vec3f& axis) -> Mat4f;
25
26template <typename T>
27inline auto ortho(T left, T right, T bottom, T top) {
28 Mat4f res = Mat4f::identity();
29 // TODO Test these
30 res.at(0, 0) = static_cast<T>(2) / (right - left);
31 res.at(1, 1) = static_cast<T>(2) / (top - bottom);
32 res.at(2, 2) = -static_cast<T>(1);
33 res.at(3, 0) = -(right + left) / (right - left);
34 res.at(3, 1) = -(top + bottom) / (top - bottom);
35
36 return res;
37}
38
39template <typename T>
40inline auto ortho(T left, T right, T bottom, T top, T z_near, T z_far) {
41 Mat4f res = Mat4f::identity();
42 // TODO test this
43 res.at(0, 0) = static_cast<T>(2) / (right - left);
44 res.at(1, 1) = static_cast<T>(2) / (top - bottom);
45 res.at(2, 2) = static_cast<T>(1) / (z_far - z_near);
46 res.at(3, 0) = -(right + left) / (right - left);
47 res.at(3, 1) = -(top + bottom) / (top - bottom);
48 res.at(3, 2) = -z_near / (z_far - z_near);
49 return res;
50}
51
52/*
53 @brief Create a perspective projection matrix
54 @param fov_y Field of view in radians;
55*/
56template <typename T>
57inline auto perspective(T fov_y, T aspect, T z_near, T z_far) {
58 const float focal_length = 1.0f / std::tan(fov_y / 2.0f);
59
60 const float x = focal_length / aspect;
61 const float y = -focal_length;
62 const float a = z_near / (z_far - z_near);
63 const float b = z_far * a;
64
65 Mat4f p{};
66
67 p.at(0, 0) = x;
68 p.at(1, 1) = y;
69 p.at(2, 2) = a;
70 p.at(2, 3) = -1.0;
71 p.at(3, 2) = b;
72
73 return p;
74}
75
76} // namespace wren::math
static constexpr auto identity()
Definition matrix.hpp:18
auto at(std::size_t col, std::size_t row) -> T &
Definition matrix.hpp:69
Definition geometry.hpp:8
auto degrees(float radian) -> float
Definition geometry.hpp:10
auto perspective(T fov_y, T aspect, T z_near, T z_far)
Definition geometry.hpp:57
auto radians(float degrees) -> float
Definition geometry.hpp:14
auto ortho(T left, T right, T bottom, T top)
Definition geometry.hpp:27
auto look_at(const Vec3f &position, const Vec3f &target, const Vec3f &world_up) -> Mat4f
Definition geometry.cpp:10
auto translate(Mat4f mat, Vec4f offset) -> Mat4f
Definition geometry.cpp:34
auto rotate(const Mat4f &matrix, float rotation, const Vec3f &axis) -> Mat4f
Definition geometry.cpp:49
Definition matrix.hpp:86