-
Notifications
You must be signed in to change notification settings - Fork 69
/
3ds.go
182 lines (172 loc) · 4.01 KB
/
3ds.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
170
171
172
173
174
175
176
177
178
179
180
181
182
package fauxgl
import (
"encoding/binary"
"io"
"os"
)
func Load3DS(filename string) (*Mesh, error) {
type ChunkHeader struct {
ChunkID uint16
Length uint32
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var vertices []Vector
var faces []*Triangle
var triangles []*Triangle
for {
header := ChunkHeader{}
if err := binary.Read(file, binary.LittleEndian, &header); err != nil {
if err == io.EOF {
break
}
return nil, err
}
switch header.ChunkID {
case 0x4D4D:
case 0x3D3D:
case 0x4000:
_, err := readNullTerminatedString(file)
if err != nil {
return nil, err
}
case 0x4100:
case 0x4110:
v, err := readVertexList(file)
if err != nil {
return nil, err
}
vertices = v
case 0x4120:
f, err := readFaceList(file, vertices)
if err != nil {
return nil, err
}
faces = f
triangles = append(triangles, faces...)
case 0x4150:
err := readSmoothingGroups(file, faces)
if err != nil {
return nil, err
}
// case 0x4160:
// matrix, err := readLocalAxis(file)
// if err != nil {
// return nil, err
// }
// for i, v := range vertices {
// vertices[i] = matrix.MulPosition(v)
// }
default:
file.Seek(int64(header.Length-6), 1)
}
}
return NewTriangleMesh(triangles), nil
}
func readSmoothingGroups(file *os.File, triangles []*Triangle) error {
groups := make([]uint32, len(triangles))
if err := binary.Read(file, binary.LittleEndian, &groups); err != nil {
return err
}
var tables [32]map[Vector][]Vector
for i := 0; i < 32; i++ {
tables[i] = make(map[Vector][]Vector)
}
for i, g := range groups {
t := triangles[i]
n := t.Normal()
for j := 0; j < 32; j++ {
if g&1 == 1 {
tables[j][t.V1.Position] = append(tables[j][t.V1.Position], n)
tables[j][t.V2.Position] = append(tables[j][t.V2.Position], n)
tables[j][t.V3.Position] = append(tables[j][t.V3.Position], n)
}
g >>= 1
}
}
for i, g := range groups {
t := triangles[i]
var n1, n2, n3 Vector
for j := 0; j < 32; j++ {
if g&1 == 1 {
for _, v := range tables[j][t.V1.Position] {
n1 = n1.Add(v)
}
for _, v := range tables[j][t.V2.Position] {
n2 = n2.Add(v)
}
for _, v := range tables[j][t.V3.Position] {
n3 = n3.Add(v)
}
}
g >>= 1
}
t.V1.Normal = n1.Normalize()
t.V2.Normal = n2.Normalize()
t.V3.Normal = n3.Normalize()
}
return nil
}
func readLocalAxis(file *os.File) (Matrix, error) {
var m [4][3]float32
if err := binary.Read(file, binary.LittleEndian, &m); err != nil {
return Matrix{}, err
}
matrix := Matrix{
float64(m[0][0]), float64(m[0][1]), float64(m[0][2]), float64(m[3][0]),
float64(m[1][0]), float64(m[1][1]), float64(m[1][2]), float64(m[3][1]),
float64(m[2][0]), float64(m[2][1]), float64(m[2][2]), float64(m[3][2]),
0, 0, 0, 1,
}
return matrix, nil
}
func readVertexList(file *os.File) ([]Vector, error) {
var count uint16
if err := binary.Read(file, binary.LittleEndian, &count); err != nil {
return nil, err
}
result := make([]Vector, count)
for i := range result {
var v [3]float32
if err := binary.Read(file, binary.LittleEndian, &v); err != nil {
return nil, err
}
result[i] = Vector{float64(v[0]), float64(v[1]), float64(v[2])}
}
return result, nil
}
func readFaceList(file *os.File, vertices []Vector) ([]*Triangle, error) {
var count uint16
if err := binary.Read(file, binary.LittleEndian, &count); err != nil {
return nil, err
}
result := make([]*Triangle, count)
for i := range result {
var v [4]uint16
if err := binary.Read(file, binary.LittleEndian, &v); err != nil {
return nil, err
}
result[i] = NewTriangleForPoints(
vertices[v[0]], vertices[v[1]], vertices[v[2]])
}
return result, nil
}
func readNullTerminatedString(file *os.File) (string, error) {
var bytes []byte
buf := make([]byte, 1)
for {
n, err := file.Read(buf)
if err != nil {
return "", err
} else if n == 1 {
if buf[0] == 0 {
break
}
bytes = append(bytes, buf[0])
}
}
return string(bytes), nil
}