/
EntityPreview.cpp
181 lines (141 loc) · 4.39 KB
/
EntityPreview.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
#include "EntityPreview.h"
#include "i18n.h"
#include "ieclass.h"
#include "ifilter.h"
#include "scene/BasicRootNode.h"
#include "../dialog/MessageBox.h"
#include "scene/PrefabBoundsAccumulator.h"
#include "string/convert.h"
namespace wxutil
{
EntityPreview::EntityPreview(wxWindow* parent) :
RenderPreview(parent, false),
_sceneIsReady(false),
_defaultCamDistanceFactor(2.8f)
{}
void EntityPreview::setDefaultCamDistanceFactor(float factor)
{
_defaultCamDistanceFactor = factor;
}
const IEntityNodePtr& EntityPreview::getEntity()
{
return _entity;
}
void EntityPreview::setEntity(const IEntityNodePtr& entity)
{
if (_entity == entity) return;
if (_entity)
{
// Use getScene to access the root node to ensure it's created
getScene()->root()->removeChildNode(_entity);
}
_entity = entity;
if (_entity)
{
getScene()->root()->addChildNode(_entity);
// Remember the entity bounds including children, but without light or speaker radii
scene::PrefabBoundsAccumulator accumulator;
accumulator.visit(_entity);
_entity->foreachNode([&](const scene::INodePtr& node)
{
accumulator.visit(node);
return true;
});
_untransformedEntityBounds = accumulator.getBounds();
}
else
{
_untransformedEntityBounds = AABB({ 0,0,0 }, { 64,64,64 });
}
queueSceneUpdate();
}
void EntityPreview::setupSceneGraph()
{
RenderPreview::setupSceneGraph();
try
{
_rootNode = std::make_shared<scene::BasicRootNode>();
// This entity is acting as our root node in the scene
getScene()->setRoot(_rootNode);
// Set up the light
_light = GlobalEntityModule().createEntity(
GlobalEntityClassManager().findClass("light"));
Node_getEntity(_light)->setKeyValue("light_radius", "600 600 600");
Node_getEntity(_light)->setKeyValue("origin", "0 0 300");
_rootNode->addChildNode(_light);
}
catch (std::runtime_error&)
{
Messagebox::ShowError(_("Unable to setup the preview,\ncould not find the entity class 'light'"));
}
}
AABB EntityPreview::getSceneBounds()
{
return _untransformedEntityBounds;
}
void EntityPreview::prepareScene()
{
if (_sceneIsReady) return;
// Clear the flag
_sceneIsReady = true;
// Reset the model rotation
resetModelRotation();
if (_entity)
{
// Reset the default view, facing down to the model from diagonally above the bounding box
double distance = getSceneBounds().getRadius() * _defaultCamDistanceFactor;
setViewOrigin(Vector3(1, 1, 1) * distance);
setViewAngles(Vector3(34, 135, 0));
}
// Trigger an initial update of the subgraph
GlobalFilterSystem().updateSubgraph(getScene()->root());
}
void EntityPreview::queueSceneUpdate()
{
_sceneIsReady = false;
queueDraw();
}
bool EntityPreview::onPreRender()
{
if (!_sceneIsReady)
{
prepareScene();
}
if (_light)
{
Vector3 lightOrigin = _viewOrigin + Vector3(0, 0, 20);
// Position the light just above the camera
Node_getEntity(_light)->setKeyValue("origin", string::to_string(lightOrigin));
// Let the light encompass the object
float radius = (getSceneBounds().getOrigin() - lightOrigin).getLength() * 2.0f;
radius = std::max(radius, 200.f);
std::ostringstream value;
value << radius << ' ' << radius << ' ' << radius;
Node_getEntity(_light)->setKeyValue("light_radius", value.str());
Node_getEntity(_light)->setKeyValue("_color", "0.6 0.6 0.6");
}
return _entity != nullptr;
}
void EntityPreview::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());
}
}
RenderStateFlags EntityPreview::getRenderFlagsFill()
{
return RenderPreview::getRenderFlagsFill() | RENDER_DEPTHWRITE | RENDER_DEPTHTEST;
}
}