-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.go
99 lines (85 loc) · 2.17 KB
/
mesh.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
package components
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"strings"
"sync"
)
const (
// TypeMesh represents a mesh component's type.
TypeMesh = "mesh"
// MeshSrcDir is the expected location of meshes
MeshSrcDir = "assets/models/"
)
// Mesh represents a component that holds the data representing a mesh.
type Mesh interface {
Component
// Data retrieves the mesh data.
Data() MeshData
// Set updates the mesh data with the new data passed in as a parameter.
Set(MeshData)
// Load loads the mesh data from file.
Load(string) error
}
// NewMesh creates a new Mesh component.
func NewMesh() Mesh {
m := mesh{}
return &m
}
type mesh struct {
data MeshData
dataLock sync.RWMutex
isLoaded bool
}
// Type retrieves the type of this component.
func (m *mesh) Type() string {
return TypeMesh
}
// Data retrieves the mesh data from the component.
func (m *mesh) Data() MeshData {
m.dataLock.RLock()
defer m.dataLock.RUnlock()
return m.data
}
// Set sets the MeshData to the new data.
func (m *mesh) Set(md MeshData) {
m.dataLock.Lock()
defer m.dataLock.Unlock()
m.data = md
}
// Load loads a mesh from file and returns an error if an error occurs while loading the file. If the mesh has already been loaded an "Already Loaded" error will be returned.
func (m *mesh) Load(fileName string) error {
m.dataLock.Lock()
defer m.dataLock.Unlock()
if m.isLoaded {
return errors.New("Already Loaded")
}
fullFileName := ""
if strings.Contains(fileName, MeshSrcDir) {
fullFileName = fileName
} else {
fullFileName = fmt.Sprintf("%s%s", MeshSrcDir, fileName)
}
data, err := ioutil.ReadFile(fullFileName)
if err != nil {
fmt.Println(err.Error())
return err
}
err = json.Unmarshal(data, &m.data)
if err == nil {
m.isLoaded = true
}
return err
}
// MeshData represents the the data needed to construct a 3d mesh.
type MeshData struct {
Indexed bool `json:"indexed"`
Verts []float32 `json:"verts"`
Indices []uint32 `json:"indices"`
VertSize int32 `json:"vertSize"`
TextureFile string `json:"textureFile"`
FragShaderFile string `json:"fragShaderFile"`
VertShaderFile string `json:"vertShaderFile"`
}