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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
#ifndef _SOFTSHADEROOM_MATH_H_
#define _SOFTSHADEROOM_MATH_H_
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include "structs.h"
#define PI 3.141592653f
#define RAD2DEG 57.295779523f /*180.f/PI*/
#define DEG2RAG 0.0174532925f /*PI/180.f*/
#define EPSILON 1e-4f
extern char printbuffer[1024];
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define clamp(v, l, h) ((v) > (l) ? ((v) < (h) ? (v) : (h)) : (l))
#define absf(v) ((v )> 0 ? (v ): -(v))
#define radians(angle) (angle * DEG2RAG)
#define degree(rad) (rad * RAD2DEG)
#define compare(v1, v2) (absf((v1) - (v2)) < EPSILON)
#define swapi(a, b) {int temp = a; a = b; b = temp;}
#define swapf(a, b) {float temp = a; a = b; b = temp;}
float rsqrt(float n);
float lerp(float from, float to, float t);
#define MAT(M, r, c) (M->m[c][r])
/************************************************************************/
/* Vec */
/************************************************************************/
void internal_vec2_scale(Vec2* v, float k, Vec2* out);
void internal_vec2_plus(Vec2* v1, Vec2* v2, Vec2* out);
void internal_vec2_minus(Vec2* v1, Vec2* v2, Vec2* out);
void internal_vec2_offset(Vec2* v, float offset, Vec2* out);
void internal_vec2_rotate(Vec2* v, float angle, Vec2* out);
float internal_vec2_dot(Vec2* v1, Vec2* v2);
void internal_vec2_tostring(Vec2* v, char buf[]);
void internal_vec2_print(Vec2* v);
#define internal_vec3_xy(v) (v->xy)
extern Vec3 vec3forward; /*(0,0,1)*/
extern Vec3 vec3up; /*(0,1,0)*/
extern Vec3 vec3left;/*(1,0,0)*/
extern Vec3 vec3zero; /*(0,0,0)*/
extern Vec2 vec2zero; /*(0,0,0)*/
extern Vec4 vec4zero; /*(0,0,0)*/
#define zerovec2 {0, 0}
#define zerovec3 {0, 0, 0}
#define zerovec4 {0, 0, 0, 0}
void internal_vec3_tostring(Vec3* v, char buf[]);
void internal_vec3_print(Vec3* v);
Vec3 internal_vec3_make(float x, float y, float z);
float internal_vec3_intersection(Vec3* v1, Vec3* v2); /*夹角*/
void internal_vec3_projection(Vec3* v1, Vec3* v2, Vec3* out);/*v1在v2上的投影*/
void internal_vec3_scale(Vec3* v, float k, Vec3* out);
void internal_vec3_scale3(Vec3* v, Vec3* scalar, Vec3* out);
void internal_vec3_plus(Vec3* v1, Vec3* v2, Vec3* out);
void internal_vec3_offset(Vec3* v, float offset, Vec3* out);
void internal_vec3_normalize(Vec3* v, Vec3* out);
void internal_vec3_vec4(float w, Vec4* out);
void internal_vec3_minus(Vec3* v1, Vec3* v2, Vec3* out);
float internal_vec3_dot(Vec3* v1, Vec3* v2);
void internal_vec3_cross(Vec3* v1, Vec3* v2, Vec3* out);
void internal_vec3_multiply(Vec3* v1, Vec3* v2, Quat* out);// 向量的乘法,st=sxt-s*t,结果是一个四元数
float internal_vec3_magnitude(Vec3* v1);
float internal_vec3_magnitude2(Vec3* v1);
void internal_vec3_lerp(Vec3* v1, Vec3* v2, float t, Vec3* out);
void internal_vec3_slerp(Vec3* v1, Vec3* v2, float t, Vec3* out);
void internal_vec4_dividew(Vec4* v, Vec3* out);
void internal_vec4_dividewnoz(Vec4* v, Vec4* out);
void internal_vec4_tostring(Vec4* v, char buf[]);
void internal_vec4_print(Vec4* v);
void internal_vec4_lerp(Vec4* a, Vec4* b, float t, Vec4* out);
void internal_vec4_scale(Vec4* v, float t, Vec4* out);
void internal_vec4_add(Vec4* v1, Vec4* v2, Vec4* out);
void internal_vec4_minus(Vec4* v1, Vec4* v2, Vec4* out);
void internal_vec4_plus(Vec4* v1, Vec4* v2, Vec4* out);
/************************************************************************/
/* Matrix */
/************************************************************************/
extern Mat4 mat4identity;
void internal_mat4_tostring(Mat4* m, char str[]);
void internal_mat4_print(Mat4* m);
void internal_mat4_zero(Mat4* out);
void internal_mat4_setidentity(Mat4* out);
void internal_mat4_setfrustum(float l, float r, float b, float t, float n, float f, Mat4* out);
void internal_mat4_setperspective(float fov, float aspect, float near, float far, Mat4* out);
void internal_mat4_setortho(float l, float r, float b, float t, float n, float f, Mat4* out);
void internal_mat4_setscale(float kx, float ky, float kz, Mat4* out);
void internal_mat4_setposition(float x, float y, float z, Mat4* out);
void internal_mat4_setrotatez(float angle, Mat4* out);
void internal_mat4_setrotatex(float angle, Mat4* out);
void internal_mat4_setrotatey(float angle, Mat4* out);
void internal_mat4_setrotate(float angleX, float angleY, float angleZ, Mat4* out);/*RyRxRz*/
void internal_mat4_setaxisangle(Vec3* axis, float angle, Mat4* out);
bool internal_mat4_setlookrotation(Vec3* view, Vec3* up, Mat4* out);
void internal_mat4_setorthonormalbias(Vec3* x, Vec3* y, Vec3* z, Mat4* out); /*正交的三个轴*/
void internal_mat4_orthogonalize(Mat4* in, Mat4* out); /*解决矩阵蠕变,对左上角3x3矩阵进行正交化,结果是右手系的正交矩阵*/
bool internal_mat4_isorthogonal(Mat4* m); /*判断是不是正交矩阵*/
bool internal_mat4_isidentity(Mat4* m);
void internal_mat4_settr(Vec3* pos, Quat* rot, Mat4* out); /*用旋转和平移初始化mat4*/
void internal_mat4_settrs(Vec3* pos, Quat* rot, Vec3* scale, Mat4* out);
void internal_mat4_settrinverse(Vec3* pos, Quat* rot, Mat4* out);
void internal_mat4_multiply(Mat4* m1, Mat4* m2, Mat4* out); /* m1的行乘m2的列,意义是用m1变换m2 */
void internal_mat4_transpose(Mat4* m, Mat4* out);
void internal_mat4_scale(Mat4* m, Vec3* scale, Mat4* out);/* 后乘post-multiply scale */
void internal_mat4_translate(Mat4* m, Vec3* pos, Mat4* out); /* 后乘post-multiply translate */
void internal_mat4_rotate(Mat4*m, float angle, Vec3* rot, Mat4* out);/*后乘绕任意轴向量旋转矩阵*/
bool internal_mat4_invertfull(Mat4* in, Mat4* out); /* 并不是所有矩阵都能求逆 */
bool internal_mat4_invertgeneral3d(Mat4* in, Mat4* out); /* 对scale rotate translate求逆 */
void internal_mat4_invertscale(Mat4* scale, Mat4* out); /* 对缩放矩阵求逆 */
void internal_mat4_invertrot(Mat4* rot, Mat4* out); /* 对旋转矩阵求逆 */
void internal_mat4_invertpos(Mat4* pos, Mat4* out); /* 对平移矩阵求逆 */
void internal_mat4_decomposetrs(Mat4* src, Vec3* pos, Quat* quat, Vec3* scale); /*分解trs矩阵*/
void internal_mat4_mulvec4(Mat4* m, Vec4* v, Vec4* out);
void internal_mat4_mulvec3(Mat4* m, Vec3* v, Vec3* out);
bool internal_mat4_toeuler(Mat4* in, Euler* out); /* 计算YXZ旋转矩阵的欧拉角 */
void internal_mat4_toquat(Mat4* in, Quat* out); /*in是正交矩阵*/
#define ROWMAT(A, ...)\
Mat4 A={__VA_ARGS__};internal_mat4_transpose(&A, &A);
void internal_mat3_multvec3(Mat3* m, Vec3* v, Vec3* out);
void internal_mat3_multmat3(Mat3* m1, Mat3* m2, Mat3* out);
void internal_mat23_applytovec3(Mat23* m, Vec3* v, Vec2* out);
void internal_mat43_applytovec3(Mat43* m, Vec3* v, Vec4* out);
bool internal_mat3_isidentity(Mat3* m);
/************************************************************************/
/* Quat */
/************************************************************************/
void internal_quat_tostring(Quat* q, char str[]);
void internal_quat_print(Quat* q);
Quat internal_quat_make(float rx, float ry, float rz);
void internal_euler_toquat(Euler* e, Quat* out);
void internal_euler_deg2rad(Euler* in, Euler* out);
void internal_euler_rad2deg(Euler* in, Euler* out);
void internal_euler_tostring(Euler* v, char buf[]);
void internal_euler_print(Euler* v);
void internal_quat_fromaxisangle(Vec3* axis, float angle, Quat* out); /*轴角转四元数*/
void internal_quat_fromeuler(Euler* euler, Quat* out); /*按照zxy顺序*/
void internal_quat_tomat4(Quat* q, Mat4* out);
void internal_quat_toeuler(Quat*q, Euler* out);
void internal_quat_normalize(Quat* q, Quat* out); /*解决蠕变,保持四元数合法*/
void internal_quat_scale(Quat* q, float scale, Quat* out);
void internal_quat_rotate();
void internal_quat_minus(Quat* q1, Quat* q2, Quat* out);
void internal_quat_slerp(Quat* start, Quat* end, float t, Quat* out);
void internal_quat_lerp(Quat* start, Quat* end, float t, Quat* out);
void internal_quat_translate(Quat* q, Vec4* v, Vec4* out);
void internal_quat_invert(Quat* q, Quat* out);
float internal_quat_dot(Quat* q1, Quat* q2);
void internal_quat_multiply(Quat* q1, Quat* q2, Quat* out);
void internal_quat_devide(Quat* q, float k, Quat* out);
void internal_quat_negtive(Quat* in, Quat* out);
bool internal_quat_isidentity(Quat* q);
void internal_quat_applytovec3(Quat* q, Vec3* v, Vec3* out); /*用四元数直接旋转向量*/
void internal_quat_conjugate(Quat* in, Quat* out);
bool internal_quat_setlookrotation(Vec3* view, Vec3* up, Quat* out);
float internal_quat_magnitude(Quat* q);
float internal_quat_magnitude2(Quat* q);
#endif
|