wren
Vulkan-based game engine
Toggle main menu visibility
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
8
namespace
wren::math
{
9
10
inline
auto
degrees
(
float
radian) ->
float
{
11
return
radian *
static_cast<
float
>
(180 / std::numbers::pi);
12
}
13
14
inline
auto
radians
(
float
degrees
) ->
float
{
15
return
degrees
*
static_cast<
float
>
(std::numbers::pi / 180);
16
}
17
18
auto
translate
(Mat4f mat, Vec4f offset) -> Mat4f;
19
auto
translate
(Mat4f mat, Vec3f offset) -> Mat4f;
20
21
auto
look_at
(
const
Vec3f& position,
const
Vec3f& target,
const
Vec3f& world_up)
22
-> Mat4f;
23
24
auto
rotate
(
const
Mat4f& matrix,
float
rotation,
const
Vec3f& axis) -> Mat4f;
25
26
auto
scale
(
const
Mat4f& matrix,
const
Vec3f&
scale
) -> Mat4f;
27
28
template
<
typename
T>
29
inline
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
41
template
<
typename
T>
42
inline
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
*/
58
template
<
typename
T>
59
inline
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
wren::math::Mat< float, 4, 4 >::identity
static constexpr auto identity()
Definition
matrix.hpp:18
wren::math::Mat::at
auto at(std::size_t col, std::size_t row) -> T &
Definition
matrix.hpp:69
matrix.hpp
wren::math
Definition
geometry.hpp:8
wren::math::degrees
auto degrees(float radian) -> float
Definition
geometry.hpp:10
wren::math::scale
auto scale(const Mat4f &matrix, const Vec3f &scale) -> Mat4f
Definition
geometry.cpp:49
wren::math::perspective
auto perspective(T fov_y, T aspect, T z_near, T z_far)
Definition
geometry.hpp:59
wren::math::radians
auto radians(float degrees) -> float
Definition
geometry.hpp:14
wren::math::ortho
auto ortho(T left, T right, T bottom, T top)
Definition
geometry.hpp:29
wren::math::look_at
auto look_at(const Vec3f &position, const Vec3f &target, const Vec3f &world_up) -> Mat4f
Definition
geometry.cpp:10
wren::math::translate
auto translate(Mat4f mat, Vec4f offset) -> Mat4f
Definition
geometry.cpp:34
wren::math::rotate
auto rotate(const Mat4f &matrix, float rotation, const Vec3f &axis) -> Mat4f
Definition
geometry.cpp:59
wren::math::Mat4f
Definition
matrix.hpp:86
vector.hpp
wren_math
include
wren
math
geometry.hpp
Generated by
1.17.0