-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.go
106 lines (98 loc) · 3.49 KB
/
model.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
package model
import (
"bytes"
"reflect"
"strings"
"fmt"
"github.com/lquesada/cavernal/lib/g3n/engine/loader/obj"
"github.com/lquesada/cavernal/lib/g3n/engine/math32"
)
var (
MeterScaleFactor float32 = 1/1.26
GlobalScaleFactor float32 = 0.01
X2 = &Transform{Scale: &math32.Vector3{2, 2, 2}}
X3 = &Transform{Scale: &math32.Vector3{3, 3, 3}}
X4 = &Transform{Scale: &math32.Vector3{4, 4, 4}}
X5 = &Transform{Scale: &math32.Vector3{5, 5, 5}}
X6 = &Transform{Scale: &math32.Vector3{6, 6, 6}}
X7 = &Transform{Scale: &math32.Vector3{7, 7, 7}}
X8 = &Transform{Scale: &math32.Vector3{8, 8, 8}}
Xhalf = &Transform{Scale: &math32.Vector3{0.5, 0.5, 0.5}}
Xthird = &Transform{Scale: &math32.Vector3{0.333, 0.333, 0.333}}
Xquarter = &Transform{Scale: &math32.Vector3{0.25, 0.25, 0.25}}
)
func DirOf(i interface{}) string {
return strings.TrimPrefix(reflect.TypeOf(i).PkgPath(), "github.com/lquesada/cavernal/")
}
func load(filepath, baseFilename string, files map[string][]byte) (*obj.Decoder, error) {
objFilename := fmt.Sprintf("%s/%s.obj", filepath, baseFilename)
mtlFilename := fmt.Sprintf("%s/%s.mtl", filepath, baseFilename)
objData, ok := files[objFilename]
if !ok {
return nil, fmt.Errorf("obj file %s/%s data not found", filepath, baseFilename)
}
mtlData, ok := files[mtlFilename]
if !ok {
return nil, fmt.Errorf("mtl file %s/%s data not found", filepath, baseFilename)
}
dec, err := obj.DecodeReader(bytes.NewReader(objData), bytes.NewReader(mtlData), filepath, files)
if err != nil {
return nil, fmt.Errorf("couldn't decode object: %s", err)
}
_, err = dec.NewGroup()
if err != nil {
return nil, fmt.Errorf("couldn't get group for %s/%s{.obj/.mtl/.png}: %s", filepath, baseFilename, err)
}
return dec, nil
}
func Load(filepath, baseFilename string, files map[string][]byte) *obj.Decoder {
model, err := load(filepath, baseFilename, files)
if err != nil {
panic(fmt.Sprintf("Critical error. Failed to load %s/%s{.obj/.mtl/.png}: %s", filepath, baseFilename, err))
}
_, err = model.NewGroup()
if err != nil {
panic(fmt.Sprintf("Critical error. Failed to get test group for %s/%s{.obj/.mtl/.png}: %s", filepath, baseFilename, err))
}
return model
}
type NodeSpec struct {
Decoder *obj.Decoder
Transform *Transform
RGBA *math32.Color4
}
func (m *NodeSpec) Build() *node {
n, err := m.Decoder.NewGroup()
if err != nil {
panic(fmt.Sprintf("Critical error. Failed to get group: %s", err))
}
position := &math32.Vector3{}
if m.Transform != nil && m.Transform.Position != nil {
position.Copy(m.Transform.Position)
position.MultiplyScalar(-GlobalScaleFactor)
if m.Transform != nil && m.Transform.Scale != nil && m.Transform.Scale.Length() != 0 {
position.Multiply(m.Transform.Scale)
}
}
rotation := &math32.Vector3{}
if m.Transform != nil && m.Transform.Rotation != nil {
rotation.Copy(m.Transform.Rotation)
}
scale := &math32.Vector3{GlobalScaleFactor, GlobalScaleFactor, GlobalScaleFactor}
if m.Transform != nil && m.Transform.Scale != nil && m.Transform.Scale.Length() != 0 {
scale.Multiply(m.Transform.Scale)
}
rgba := &math32.Color4{1, 1, 1, 1}
if m.RGBA != nil {
rgba.R, rgba.G, rgba.B, rgba.A = m.RGBA.R, m.RGBA.G, m.RGBA.B, m.RGBA.A
}
return &node{
nodes: []INode{&modelNode{node: n}},
transform: &Transform{
Position: position,
Rotation: rotation,
Scale: scale,
},
rgba: rgba,
}
}