-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneGraph.h
More file actions
99 lines (82 loc) · 2.64 KB
/
SceneGraph.h
File metadata and controls
99 lines (82 loc) · 2.64 KB
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
#pragma once
// SceneGraph is a data structure for managing the spatial representation of a
// scene. It has a hierarchical, tree-like structure where nodes represent
// objects together with their transforms, colour, and child nodes. Each node's
// world transform is the product of its parent's world transform and its own
// local transform.
#include "matrix.h"
#include "vec3.h"
#include "vec4.h"
#include "Mesh.h"
#include <vector>
// Convenience aliases mapping the generic library types to the names used here.
using Vector3 = vec3<float>;
using Vector4 = vec4<float>;
using Matrix4 = matrix<float, 4, 4>;
class SceneNode
{
public:
explicit SceneNode(Mesh* mesh = nullptr,
Vector4 colour = Vector4(1, 1, 1, 1))
: parent(nullptr),
mesh(mesh),
worldTransform(Matrix4::identity()),
transform(Matrix4::identity()),
modelScale(1, 1, 1),
colour(colour)
{
}
virtual ~SceneNode()
{
for (SceneNode* child : children)
delete child;
}
// Non-copyable: nodes own their children via raw pointers.
SceneNode(const SceneNode&) = delete;
SceneNode& operator=(const SceneNode&) = delete;
void SetTransform(const Matrix4& m) { transform = m; }
const Matrix4& GetTransform() const { return transform; }
const Matrix4& GetWorldTransform() const { return worldTransform; }
Vector4 GetColour() const { return colour; }
void SetColour(Vector4 c) { colour = c; }
Vector3 GetModelScale() const { return modelScale; }
void SetModelScale(Vector3 s) { modelScale = s; }
Mesh* GetMesh() const { return mesh; }
void SetMesh(Mesh* m) { mesh = m; }
void AddChild(SceneNode* child)
{
children.push_back(child);
child->parent = this;
}
std::vector<SceneNode*>::const_iterator GetChildIteratorStart() const
{
return children.begin();
}
std::vector<SceneNode*>::const_iterator GetChildIteratorEnd() const
{
return children.end();
}
virtual void Update(float msec)
{
if (parent)
worldTransform = parent->worldTransform * transform;
else
worldTransform = transform; // root: world transform is the local transform
for (SceneNode* child : children)
child->Update(msec);
}
virtual void Draw(const OGLRenderer& r)
{
(void)r; // renderer unused in this stub
if (mesh)
mesh->Draw();
}
protected:
SceneNode* parent;
Mesh* mesh;
Matrix4 worldTransform;
Matrix4 transform;
Vector3 modelScale;
Vector4 colour;
std::vector<SceneNode*> children;
};