-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscene.go
133 lines (106 loc) · 2.75 KB
/
scene.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
package main
import (
"encoding/json"
"fmt"
"image/color"
"log"
"os"
"path"
)
type SceneMeshData struct {
ID string `json:"id"`
ObjFile string `json:"objFile"`
Texture string `json:"texture"`
TextureScale float32 `json:"textureScale"`
}
type SceneObjectData struct {
MeshID string `json:"meshID"`
Position [3]float32 `json:"position"`
Rotation [3]float32 `json:"rotation"`
Scale [3]float32 `json:"scale"`
}
type SceneData struct {
Name string `json:"name"`
Meshes []SceneMeshData `json:"meshes"`
Objects []SceneObjectData `json:"objects"`
}
type Scene struct {
Objects []*Object
}
func (s *Scene) NumObjects() int {
return len(s.Objects)
}
func (s *Scene) NumVertices() (n int) {
for _, obj := range s.Objects {
n += len(obj.Mesh.Vertices)
}
return n
}
func (s *Scene) NumTriangles() (n int) {
for _, obj := range s.Objects {
n += len(obj.Mesh.Faces)
}
return n
}
func LoadSceneFile(filename string) (*Scene, error) {
f, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("failed to open level file: %w", err)
}
defer func() {
_ = f.Close()
}()
decoder := json.NewDecoder(f)
sceneData := SceneData{}
if err := decoder.Decode(&sceneData); err != nil {
return nil, fmt.Errorf("failed to read scene manifest: %w", err)
}
defaultColor := color.RGBA{200, 200, 200, 255}
defaultTexture := NewColorTexture(defaultColor)
var (
rootDir = path.Dir(filename)
meshes = make(map[string]*Mesh)
objects []*Object
)
for _, meshData := range sceneData.Meshes {
loadedMeshes, err := LoadMeshFile(path.Join(rootDir, meshData.ObjFile), true)
if err != nil {
return nil, fmt.Errorf("failed to load mesh '%s': %w", meshData.ID, err)
}
mesh := loadedMeshes[0]
if meshData.Texture != "" {
texture, err := LoadTextureFile(path.Join(rootDir, meshData.Texture))
if err != nil {
return nil, fmt.Errorf("failed to load texture %s: %w", meshData.ID, err)
}
if meshData.TextureScale != 0 {
texture.SetScale(meshData.TextureScale)
}
for i := range mesh.Faces {
mesh.Faces[i].Texture = texture
}
} else {
for i := range mesh.Faces {
mesh.Faces[i].Texture = defaultTexture
}
}
meshes[meshData.ID] = mesh
}
for _, objData := range sceneData.Objects {
mesh, ok := meshes[objData.MeshID]
if !ok {
return nil, fmt.Errorf("mesh id not found: %s", objData.MeshID)
}
if objData.Scale == [3]float32{0, 0, 0} {
log.Printf("[WARN] object scale is zero: %s", objData.MeshID)
}
obj := NewObject(mesh)
obj.Scale = Vec3FromArray(objData.Scale)
obj.Rotation = Vec3FromArray(objData.Rotation).ToRadians()
obj.Translation = Vec3FromArray(objData.Position)
objects = append(objects, obj)
}
return &Scene{
Objects: objects,
}, err
}