-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaj_helpers.h
69 lines (56 loc) · 2.02 KB
/
caj_helpers.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
#ifndef CAJ_HELPERS_H
#define CAJ_HELPERS_H
#include "caj_types.h"
// WARNING: this assumes float is 32-bit and same endianness as int, etc.
static inline void caj_uint32_to_bin_le(unsigned char *buf, uint32_t val) {
buf[0] = val & 0xff;
buf[1] = (val >> 8) & 0xff;
buf[2] = (val >> 16) & 0xff;
buf[3] = (val >> 24) & 0xff;
}
static inline uint32_t caj_bin_to_uint32_le(unsigned char *buf) {
return buf[0] | ((uint32_t)buf[1] << 8) | ((uint32_t)buf[2] << 16) |
((uint32_t)buf[3] << 24);
}
static inline void caj_float_to_bin_le(unsigned char *buf, float val) {
union { uint32_t i; float f; } u;
u.f = val; caj_uint32_to_bin_le(buf, u.i);
}
static inline float caj_bin_to_float_le(unsigned char *buf) {
union { uint32_t i; float f; } u;
u.i = caj_bin_to_uint32_le(buf); return u.f;
}
static inline void caj_vect3_to_bin_le(unsigned char *buf, const caj_vector3 *v) {
caj_float_to_bin_le(buf+0, v->x);
caj_float_to_bin_le(buf+4, v->y);
caj_float_to_bin_le(buf+8, v->z);
}
static inline void caj_quat_to_bin4_le(unsigned char *buf, const caj_quat *v) {
caj_float_to_bin_le(buf+0, v->x);
caj_float_to_bin_le(buf+4, v->y);
caj_float_to_bin_le(buf+8, v->z);
caj_float_to_bin_le(buf+12, v->w);
}
static inline void caj_quat_to_bin3_le(unsigned char *buf, const caj_quat *v) {
caj_float_to_bin_le(buf+0, v->x);
caj_float_to_bin_le(buf+4, v->y);
caj_float_to_bin_le(buf+8, v->z);
}
static inline void caj_vect4_to_bin_le(unsigned char *buf, const caj_vector4 *v) {
caj_float_to_bin_le(buf+0, v->x);
caj_float_to_bin_le(buf+4, v->y);
caj_float_to_bin_le(buf+8, v->z);
caj_float_to_bin_le(buf+12, v->w);
}
static inline void caj_bin_to_vect3_le(caj_vector3 *v, unsigned char *buf) {
v->x = caj_bin_to_float_le(buf+0);
v->y = caj_bin_to_float_le(buf+4);
v->z = caj_bin_to_float_le(buf+8);
}
static inline void caj_bin3_to_quat_le(caj_quat *v, unsigned char *buf) {
v->x = caj_bin_to_float_le(buf+0);
v->y = caj_bin_to_float_le(buf+4);
v->z = caj_bin_to_float_le(buf+8);
caj_expand_quat(v);
}
#endif