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
26auto scale(const Mat4f& matrix, const Vec3f& scale) -> Mat4f;
27
28template <typename T>
29inline auto ortho(T left, T right, T bottom, T top) {
30 Mat4f res = Mat4f::identity();
31 // TODO Test these
32 res.at(0, 0) = static_cast<T>(2) / (right - left);
33 res.at(1, 1) = static_cast<T>(2) / (top - bottom);
34 res.at(2, 2) = -static_cast<T>(1);
35 res.at(3, 0) = -(right + left) / (right - left);
36 res.at(3, 1) = -(top + bottom) / (top - bottom);
37
38 return res;
39}
40
41template <typename T>
42inline auto ortho(T left, T right, T bottom, T top, T near, T far) {
43 Mat4f res = Mat4f::identity();
44
45 res.at(0, 0) = static_cast<T>(2) / (right - left);
46 res.at(1, 1) = static_cast<T>(2) / (bottom - top);
47 res.at(2, 2) = static_cast<T>(1) / (near - far);
48 res.at(3, 0) = -(right + left) / (right - left);
49 res.at(3, 1) = -(bottom + top) / (bottom - top);
50 res.at(3, 2) = near / (near - far);
51 return res;
52}
53
54/*
55 @brief Create a perspective projection matrix
56 @param fov_y Field of view in radians;
57*/
58template <typename T>
59inline auto perspective(T fov_y, T aspect, T z_near, T z_far) {
60 const float focal_length = 1.0f / std::tan(fov_y / 2.0f);
61
62 const float x = focal_length / aspect;
63 const float y = -focal_length;
64 const float a = z_near / (z_far - z_near);
65 const float b = z_far * a;
66
67 Mat4f p{};
68
69 p.at(0, 0) = x;
70 p.at(1, 1) = y;
71 p.at(2, 2) = a;
72 p.at(2, 3) = -1.0;
73 p.at(3, 2) = b;
74
75 return p;
76}
77
78} // 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 scale(const Mat4f &matrix, const Vec3f &scale) -> Mat4f
Definition geometry.cpp:49
auto perspective(T fov_y, T aspect, T z_near, T z_far)
Definition geometry.hpp:59
auto radians(float degrees) -> float
Definition geometry.hpp:14
auto ortho(T left, T right, T bottom, T top)
Definition geometry.hpp:29
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:59
Definition matrix.hpp:86