-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexbuffer.go
57 lines (44 loc) · 1.02 KB
/
indexbuffer.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
package mesh
import "github.com/ungerik/go3d/float64/vec3"
type Face [3]uint16
type IndexBuffer struct {
Vertices []vec3.T
Faces []Face
}
func (this *IndexBuffer) NumTriangles() int {
return len(this.Faces)
}
func (this *IndexBuffer) read() <-chan Triangle {
triChan := make(chan Triangle)
go func() {
for _, face := range this.Faces {
triChan <- Triangle{
this.Vertices[face[0]],
this.Vertices[face[1]],
this.Vertices[face[2]],
}
}
close(triChan)
}()
return triChan
}
func (this *IndexBuffer) ConvertFrom(mesh Mesh) {
this.Vertices = make([]vec3.T, 0)
this.Faces = make([]Face, 0, mesh.NumTriangles())
uniqueVertices := make(map[vec3.T]uint16)
var currIndex uint16
var face Face
for tri := range mesh.read() {
for i, vert := range tri[:] {
index, exists := uniqueVertices[vert]
if !exists {
this.Vertices = append(this.Vertices, vert)
uniqueVertices[vert] = currIndex
index = currIndex
currIndex++
}
face[i] = index
}
this.Faces = append(this.Faces, face)
}
}