-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmatrix4.cpp
187 lines (147 loc) · 3.44 KB
/
matrix4.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
Copyright (c) 2018 Gombe.
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
*/
#include "matrix4.hpp"
Matrix4 loadPerspective(float fovy,float aspect,float zNear,float zFar,int width,int height){
Matrix4 m;
const float dz(zFar - zNear);
(void)width;
(void)height;
if (dz != 0.0f) {
m.set( 5 , 1.f / tanf(fovy*3.1415926535898f));
m.set( 0, m[5]*aspect );
m.set(10, -(zFar + zNear) / dz);
m.set(11, -1.f);
m.set(14, -2.f * zFar * zNear / dz);
}
return m;
}
Matrix4 lookat(fvector3 goal,fvector3 eye){
Matrix4 m,rm;
fvector3 t,r,s;
t=eye-goal;
r=cross(fvector3(0.f,-1.f,0.f),t);
s=cross(t,r);
float rrs,rss,rts;
rrs = 1.f/r.abs();
rss = 1.f/s.abs();
rts = 1.f/t.abs();
m.set(0, r.x*rrs);
m.set(4, r.y*rrs);
m.set(8, r.z*rrs);
m.set(1, s.x*rss);
m.set(5, s.y*rss);
m.set(9, s.z*rss);
m.set(2, t.x*rts);
m.set(6, t.y*rts);
m.set(10, t.z*rts);
// print(m);
return m*translation(-eye);
}
#include <iostream>
void Matrix4::print(){
for(int i=0;i<16;i++){
std::cout<<m[i]<< ",";
if(i%4==3)std::cout<<std::endl;
}
std::cout<<std::endl;
}
float sin16(uint16_t ang){
return sinf(ang*(2.f*3.14159265359f/65536.f));
}
float cos16(uint16_t ang){
return sin16(ang + 65536/4);
}
float tan16(uint16_t ang){
return sin16(ang) / cos16(ang);;
}
Matrix4 rotatex(uint16_t ang){
Matrix4 r;
float sint,cost;
sint = sin16(ang);
cost = cos16(ang);
return Matrix4(1.f, 0, 0, 0,
0, cost, -sint , 0,
0, sint, cost , 0,
0, 0, 0, 1.f);
}
Matrix4 rotatey(uint16_t ang){
Matrix4 r;
float sint,cost;
sint = sin16(ang);
cost = cos16(ang);
return Matrix4( cost, 0 , sint , 0,
0 , 1.f , 0 , 0,
-sint, 0 , cost , 0,
0 , 0 , 0 , 1.f);
}
Matrix4 rotatez(uint16_t ang){
float sint,cost;
sint = sin16(ang);
cost = cos16(ang);
return Matrix4(cost , -sint , 0 , 0,
sint , cost , 0 , 0,
0 , 0 , 1.f , 0,
0 , 0 , 0 , 1.f);
}
Matrix4 translation(fvector3 v){
return Matrix4(1.f,0,0,0,
0,1.f,0,0,
0,0,1.f,0,
v.x,v.y,v.z,1.f);
}
Matrix4 magnify(float n){
return Matrix4(1.f*n,0,0,0,
0,1.f*n,0,0,
0,0,1.f*n,0,
0,0,0,1.f);
}
Matrix4 magnify_y(float n){
return Matrix4(1.f,0,0,0,
0,n,0,0,
0,0,1.f,0,
0,0,0,1.f);
}
// assert that len of axis must be 1.
Matrix4 rotation_axis_and_cosv(fvector3 u,float cosv){
float s;
float c;
float oc;//one minus c
c = cosv;
s = sqrt(1-c*c);
oc = 1.f-c;
// return Matrix4(c+u.x*u.x*oc,
// u.y*u.x*oc+u.z*s,
// u.z*u.x*oc-u.y*s,
// 0.f,
// u.x*u.y*oc-u.z*s,
// c+u.y*u.y*oc,
// u.z*u.y*oc+u.x*s,
// 0.f,
// u.x*u.z*oc+u.y*s,
// u.y*u.z*oc-u.x*s,
// c+u.z*u.z*oc,
// 0.f,
// 0.f,0.f,0.f,1.f);
return Matrix4(c+u.x*u.x*oc,u.x*u.y*oc-u.z*s,u.x*u.z*oc+u.y*s,0.f,
u.y*u.x*oc+u.z*s,c+u.y*u.y*oc,u.y*u.z*oc-u.x*s,0.f,
u.z*u.x*oc-u.y*s,u.z*u.y*oc+u.x*s,c+u.z*u.z*oc,0.f,
0.f,0.f,0.f,1.f);
//TODO
}
// assert that len of axis must be 1.
Matrix4 rotation_axis_and_angle(fvector3 u,float angle){
float s;
float c;
float oc;//one minus c
s = sinf(-angle);
c = cosf(-angle);
oc = 1.f-c;
return Matrix4(c+u.x*u.x*oc,u.x*u.y*oc-u.z*s,u.x*u.z*oc+u.y*s,0.f,
u.y*u.x*oc+u.z*s,c+u.y*u.y*oc,u.y*u.z*oc-u.x*s,0.f,
u.z*u.x*oc-u.y*s,u.z*u.y*oc+u.x*s,c+u.z*u.z*oc,0.f,
0.f,0.f,0.f,1.f);
//TODO
}