-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
float.go
62 lines (55 loc) · 1.67 KB
/
float.go
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
package protocol
import (
"bytes"
"encoding/binary"
"github.com/go-gl/mathgl/mgl32"
"math"
)
// Float32 reads a float32 from Buffer src, setting the result to the pointer to a float32 passed.
func Float32(src *bytes.Buffer, x *float32) error {
var bits uint32
if err := binary.Read(src, binary.LittleEndian, &bits); err != nil {
return wrap(err)
}
*x = math.Float32frombits(bits)
return nil
}
// WriteFloat32 writes a float32 to Buffer dst, by first converting it to a uint32.
func WriteFloat32(dst *bytes.Buffer, x float32) error {
if err := binary.Write(dst, binary.LittleEndian, math.Float32bits(x)); err != nil {
return wrap(err)
}
return nil
}
// Vec3 reads an mgl32.Vec3 (float32 vector) from Buffer src, setting the result to the pointer to an
// mgl32.Vec3 passed.
func Vec3(src *bytes.Buffer, x *mgl32.Vec3) error {
return chainErr(
Float32(src, &(*x)[0]),
Float32(src, &(*x)[1]),
Float32(src, &(*x)[2]),
)
}
// WriteVec3 writes an mgl32.Vec3 (float32 vector) to Buffer dst, writing each of the float32s separately.
func WriteVec3(dst *bytes.Buffer, x mgl32.Vec3) error {
return chainErr(
WriteFloat32(dst, x[0]),
WriteFloat32(dst, x[1]),
WriteFloat32(dst, x[2]),
)
}
// Vec2 reads an mgl32.Vec2 (float32 vector) from Buffer src, setting the result to the pointer to an
// mgl32.Vec2 passed.
func Vec2(src *bytes.Buffer, x *mgl32.Vec2) error {
return chainErr(
Float32(src, &(*x)[0]),
Float32(src, &(*x)[1]),
)
}
// WriteVec2 writes an mgl32.Vec2 (float32 vector) to Buffer dst, writing each of the float32s separately.
func WriteVec2(dst *bytes.Buffer, x mgl32.Vec2) error {
return chainErr(
WriteFloat32(dst, x[0]),
WriteFloat32(dst, x[1]),
)
}