/
AnimationPreview.cpp
183 lines (144 loc) · 3.93 KB
/
AnimationPreview.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "AnimationPreview.h"
#include "i18n.h"
#include "imodel.h"
#include "ientity.h"
#include "ieclass.h"
#include "imd5anim.h"
#include "itextstream.h"
#include "math/AABB.h"
#include "scene/BasicRootNode.h"
#include "wxutil/GLWidget.h"
namespace ui
{
namespace
{
const char* const FUNC_STATIC_CLASS = "func_static";
}
AnimationPreview::AnimationPreview(wxWindow* parent) :
wxutil::RenderPreview(parent, true)
{}
void AnimationPreview::clearModel()
{
if (_model)
{
if (_entity)
{
_entity->removeChildNode(_model);
}
_model.reset();
}
}
// Set the model, this also resets the camera
void AnimationPreview::setModelNode(const scene::INodePtr& node)
{
// Remove the old model from the scene, if any
clearModel();
// Ensure that this is an MD5 model node
model::ModelNodePtr model = Node_getModel(node);
if (!model)
{
rError() << "AnimationPreview::setModelNode: node is not a model." << std::endl;
stopPlayback();
return;
}
// greebo: Call getScene() to trigger a scene setup if necessary
getScene();
try
{
dynamic_cast<const md5::IMD5Model&>(model->getIModel()).getAnim();
}
catch (std::bad_cast&)
{
rError() << "AnimationPreview::setModelNode: modelnode doesn't contain an MD5 model." << std::endl;
stopPlayback();
return;
}
_model = node;
// Set the animation to play
dynamic_cast<md5::IMD5Model&>(model->getIModel()).setAnim(_anim);
// AddChildNode also tells the model which renderentity it is attached to
_entity->addChildNode(_model);
// Reset preview time
stopPlayback();
if (_model != nullptr)
{
// Reset the model rotation
resetModelRotation();
// Use AABB to adjust camera distance
const AABB& bounds = _model->localAABB();
if (bounds.isValid())
{
// Reset the default view, facing down to the model from diagonally above the bounding box
double distance = bounds.getRadius() * 3.0f;
setViewOrigin(Vector3(1, 1, 1) * distance);
}
else
{
// Bounds not valid, fall back to default
setViewOrigin(Vector3(1, 1, 1) * 40.0f);
}
setViewAngles(Vector3(23, 135, 0));
// Start playback when switching particles
startPlayback();
}
// Redraw
queueDraw();
}
AABB AnimationPreview::getSceneBounds()
{
if (!_model) return RenderPreview::getSceneBounds();
return _model->localAABB();
}
bool AnimationPreview::onPreRender()
{
if (!_model) return false;
// Set the animation to play
model::ModelNodePtr model = Node_getModel(_model);
dynamic_cast<md5::IMD5Model&>(model->getIModel()).updateAnim(_renderSystem->getTime());
return true;
}
RenderStateFlags AnimationPreview::getRenderFlagsFill()
{
return RenderPreview::getRenderFlagsFill() | RENDER_DEPTHWRITE | RENDER_DEPTHTEST;
}
void AnimationPreview::setAnim(const md5::IMD5AnimPtr& anim)
{
_anim = anim;
if (!_model)
{
return;
}
// Set the animation to play
model::ModelNodePtr model = Node_getModel(_model);
dynamic_cast<md5::IMD5Model&>(model->getIModel()).setAnim(_anim);
}
void AnimationPreview::setupSceneGraph()
{
RenderPreview::setupSceneGraph();
_root = std::make_shared<scene::BasicRootNode>();
_entity = GlobalEntityCreator().createEntity(
GlobalEntityClassManager().findClass(FUNC_STATIC_CLASS)
);
_root->addChildNode(_entity);
// This entity is acting as our root node in the scene
getScene()->setRoot(_root);
}
void AnimationPreview::onModelRotationChanged()
{
if (_entity)
{
// Update the model rotation on the entity
std::ostringstream value;
value << _modelRotation.xx() << ' '
<< _modelRotation.xy() << ' '
<< _modelRotation.xz() << ' '
<< _modelRotation.yx() << ' '
<< _modelRotation.yy() << ' '
<< _modelRotation.yz() << ' '
<< _modelRotation.zx() << ' '
<< _modelRotation.zy() << ' '
<< _modelRotation.zz();
Node_getEntity(_entity)->setKeyValue("rotation", value.str());
}
}
} // namespace