-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
146 lines (124 loc) · 2.86 KB
/
node.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
package opengl
import (
"time"
uuid "github.com/satori/go.uuid"
)
type node struct {
id *uuid.UUID
pos Position
orientation Orientation
content NodeContent
children []Node
}
func createNode(
id *uuid.UUID,
pos Position,
orientation Orientation,
) Node {
return createNodeInternally(id, pos, orientation, nil, nil)
}
func createNodeWithContent(
id *uuid.UUID,
pos Position,
orientation Orientation,
content NodeContent,
) Node {
return createNodeInternally(id, pos, orientation, content, nil)
}
func createNodeWithChildren(
id *uuid.UUID,
pos Position,
orientation Orientation,
children []Node,
) Node {
return createNodeInternally(id, pos, orientation, nil, children)
}
func createNodeWithContentAndChildren(
id *uuid.UUID,
pos Position,
orientation Orientation,
content NodeContent,
children []Node,
) Node {
return createNodeInternally(id, pos, orientation, content, children)
}
func createNodeInternally(
id *uuid.UUID,
pos Position,
orientation Orientation,
content NodeContent,
children []Node,
) Node {
out := node{
id: id,
pos: pos,
orientation: orientation,
content: content,
children: children,
}
return &out
}
// ID returns the id
func (obj *node) ID() *uuid.UUID {
return obj.id
}
// Position returns the position
func (obj *node) Position() Position {
return obj.pos
}
// Orientation returns the orientation
func (obj *node) Orientation() Orientation {
return obj.orientation
}
// HasContent returns true if there is content, false otherwise
func (obj *node) HasContent() bool {
return obj.content != nil
}
// Content returns the content, if any
func (obj *node) Content() NodeContent {
return obj.content
}
// HasChildren returns true if there is children, false otherwise
func (obj *node) HasChildren() bool {
return obj.children != nil
}
// Children returns the children, if any
func (obj *node) Children() []Node {
return obj.children
}
// Render renders the node:
func (obj *node) Render(
delta time.Duration,
activeCamera WorldCamera,
activeScene Scene,
) error {
// render the model of the node, if any:
if obj.HasContent() {
content := obj.Content()
if content.IsModel() {
model := content.Model()
err := model.Render(delta, activeCamera, activeScene)
if err != nil {
return err
}
}
}
// render the children nodes, if any:
if obj.HasChildren() {
children := obj.Children()
for _, oneChildNode := range children {
// add the relative position:
pos := obj.Position().Add(oneChildNode.Position())
// add the relative orientation:
orientation := obj.Orientation().Add(oneChildNode.Orientation())
// update the camera:
childActiveCamera := activeCamera.Update(pos, orientation)
// render the child node:
err := oneChildNode.Render(delta, childActiveCamera, activeScene)
if err != nil {
return err
}
}
}
return nil
}