This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.go
executable file
·169 lines (131 loc) · 2.75 KB
/
read.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
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
package main
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"runtime/debug"
)
var (
errMissingMagic = errors.New("magic VOX string not found")
errBadVersion = errors.New("only version 150 supported")
errMissingMain = errors.New("missing MAIN chunk")
)
func newVox(r io.ReadSeeker) (*vox, error) {
br := breader{r, nil}
buf := make([]byte, 4)
br.Read(buf)
if !bytes.Equal(bVOX, buf) {
return nil, errMissingMagic
}
br.Read(buf)
if !bytes.Equal(bVER, buf) {
return nil, errBadVersion
}
br.Read(buf)
if !bytes.Equal(bMAIN, buf) {
return nil, errMissingMain
}
// there's an implicit `chunk` quality to main,
// but it is the 'container' of other chunks,
// so it isn't represented here inside of a *vox
mc := chunk{}
br.ReadV(&mc.sz)
br.ReadV(&mc.szChildren)
v := &vox{}
for br.err == nil {
c := chunk{}
br.Read(c.id[:])
if br.err != nil {
break
}
br.ReadV(&c.sz)
br.ReadV(&c.szChildren)
copy(buf, c.id[:])
log.Printf("\t\tchunk(%s):\tsz=%d\tszc=%d", c.id[:], c.sz, c.szChildren)
switch {
case bytes.Equal(bPACK, buf):
p := &pack{}
br.ReadV(&p.numModels)
v.pack = p
case bytes.Equal(bSIZE, buf):
sz := size{}
br.ReadV(&sz.x)
br.ReadV(&sz.y)
br.ReadV(&sz.z)
v.sizes = append(v.sizes, sz)
case bytes.Equal(bXYZI, buf):
vx := xyzi{}
br.ReadV(&vx.szVoxels)
vx.voxels = make([]voxel, int(vx.szVoxels))
for i := 0; i < int(vx.szVoxels); i++ {
br.Read(buf)
vx.voxels[i].x = buf[0]
vx.voxels[i].y = buf[1]
vx.voxels[i].z = buf[2]
vx.voxels[i].colorIndex = buf[3]
}
v.voxels = append(v.voxels, vx)
case bytes.Equal(bRGBA, buf):
pal := &rgba{}
for i := 0; i < 254; i++ {
br.ReadV(&pal.colors[i+1])
}
v.palette = pal
// skip 4
br.Seek(16, io.SeekCurrent)
case bytes.Equal(bMATT, buf):
// for now, we're skipping this entire section.
/*
m := material{}
br.ReadV(&m.id)
br.ReadV(&m.typ)
br.ReadV(&m.weight)
br.ReadV(&m.properties)
*/
br.Seek(int64(c.sz+c.szChildren), io.SeekCurrent)
}
}
if br.err == io.EOF {
return v, nil
}
return v, br.err
}
type breader struct {
r io.ReadSeeker
err error
}
func (b *breader) ReadV(v interface{}) {
if b.err != nil {
return
}
err := binary.Read(b.r, binary.LittleEndian, v)
b.setErr(err)
}
func (b *breader) Read(dst []byte) {
if b.err != nil {
return
}
_, err := b.r.Read(dst)
b.setErr(err)
}
func (b *breader) Seek(pos int64, dir int) {
if b.err != nil {
return
}
_, err := b.r.Seek(pos, dir)
b.setErr(err)
}
func (b *breader) setErr(err error) {
if err != nil {
if err == io.EOF {
b.err = err
return
}
debug.PrintStack()
cur, _ := b.r.Seek(0, io.SeekCurrent)
b.err = fmt.Errorf("%s (pos: %d)", err, cur)
}
}