-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinearAlgebra.h
executable file
·90 lines (74 loc) · 1.96 KB
/
LinearAlgebra.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#pragma once
#include "types.h"
namespace dawn
{
static mat4f rotation(double angle, char axis)
{
mat4f r;
double cov = cos(angle);
double siv = sin(angle);
switch (axis)
{
case 'x':
r << 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, cov, -siv, 0.0f,
0.0f, siv, cov, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f;
break;
case 'y':
r << cov, 0.0f, siv, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
-siv, 0.0f, cov, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f;
break;
case 'z':
r << cov, -siv, 0.0f, 0.0f,
siv, cov, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f;
break;
}
return r;
}
static mat4f translation(float x, float y, float z)
{
mat4f t;
t << 1.0f, 0.0f, 0.0f, x,
0.0f, 1.0f, 0.0f, y,
0.0f, 0.0f, 1.0f, z,
0.0f, 0.0f, 0.0f, 1.0f;
return t;
}
static mat4f scaling(float x, float y, float z)
{
mat4f s;
s << x , 0.0f, 0.0f, 0.0f,
0.0f, y , 0.0f, 0.0f,
0.0f, 0.0f, z , 0.0f,
0.0f, 0.0f, 0.0f, 1.0f;
return s;
}
static mat4f ortho(float left, float right, float bottom, float top, float near, float far)
{
mat4f o = mat4f::Zero();
o(0,0) = 2.0/(right - left); o(0,3) = -(right + left)/(right - left);
o(1,1) = 2.0/(top - bottom); o(1,3) = -(top + bottom)/(top - bottom);
o(2,2) = -2.0/(far - near); o(2,3) = -(far + near)/(far - near);
o(3,3) = 1.0;
return o;
}
static mat4f perspective(float fovy, float aspect, float near, float far)
{
mat4f p = mat4f::Zero();
float f = 1.0f / tan(fovy / 2.0f);
p(0,0) = f / aspect;
p(1,1) = f;
p(2,2) = (far + near) / (near - far);
p(2,3) = -1.0f;
p(3,2) = (2.0f * far * near) / (near - far);
return p;
}
static float lerp(float v0, float v1, float t) {
return v0 + t * (v1 - v0);
}
}