Skip to content

Commit 12f8c15

Browse files
committed
Added use of CameraSampler to CameraAnimation.
1 parent d96d1ae commit 12f8c15

File tree

4 files changed

+75
-39
lines changed

4 files changed

+75
-39
lines changed

include/vsg/animation/CameraAnimation.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
1212
1313
</editor-fold> */
1414

15-
#include <vsg/animation/TransformSampler.h>
15+
#include <vsg/animation/CameraSampler.h>
1616
#include <vsg/core/Inherit.h>
1717
#include <vsg/maths/quat.h>
1818
#include <vsg/ui/KeyEvent.h>
@@ -37,8 +37,8 @@ namespace vsg
3737
// animation to play/record to
3838
ref_ptr<Animation> animation;
3939

40-
// transformSampler to play/record to
41-
ref_ptr<TransformSampler> transformSampler;
40+
// CameraSampler to play/record to
41+
ref_ptr<CameraSampler> cameraSampler;
4242

4343
KeySymbol toggleRecordingKey = KEY_r;
4444
KeySymbol togglePlaybackKey = KEY_p;

src/vsg/animation/CameraAnimation.cpp

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
1111
</editor-fold> */
1212

1313
#include <vsg/animation/CameraAnimation.h>
14+
#include <vsg/animation/TransformSampler.h>
1415
#include <vsg/app/Camera.h>
1516
#include <vsg/io/Logger.h>
1617
#include <vsg/io/Options.h>
@@ -35,28 +36,46 @@ CameraAnimation::CameraAnimation(ref_ptr<Object> in_object, const Path& in_filen
3536
{
3637
for (auto sampler : animation->samplers)
3738
{
38-
if (auto ts = sampler.cast<TransformSampler>())
39+
if (auto cs = sampler.cast<CameraSampler>())
3940
{
40-
transformSampler = ts;
41+
cameraSampler = cs;
4142
break;
4243
}
4344
}
4445
}
45-
else if ((transformSampler = read_object.cast<TransformSampler>()))
46+
else if ((cameraSampler = read_object.cast<CameraSampler>()))
4647
{
4748
animation = Animation::create();
48-
animation->samplers.push_back(transformSampler);
49+
animation->samplers.push_back(cameraSampler);
50+
}
51+
else if (auto ts = read_object.cast<TransformSampler>())
52+
{
53+
auto tkf = ts->keyframes;
54+
55+
// convert TransformSampler to CameraSampler
56+
cameraSampler = CameraSampler::create();
57+
cameraSampler->name = ts->name;
58+
auto& ckf = cameraSampler->keyframes = CameraKeyframes::create();
59+
60+
ckf->positions = tkf->positions;
61+
ckf->rotations = tkf->rotations;
62+
63+
animation = Animation::create();
64+
animation->samplers.push_back(cameraSampler);
4965
}
5066
else if (auto keyframes = read_object.cast<TransformKeyframes>())
5167
{
52-
transformSampler = TransformSampler::create();
53-
transformSampler->keyframes = keyframes;
68+
cameraSampler = CameraSampler::create();
69+
auto& ckf = cameraSampler->keyframes = CameraKeyframes::create();
70+
71+
ckf->positions = keyframes->positions;
72+
ckf->rotations = keyframes->rotations;
5473

5574
animation = Animation::create();
56-
animation->samplers.push_back(transformSampler);
75+
animation->samplers.push_back(cameraSampler);
5776
}
5877
}
59-
if (object && transformSampler) transformSampler->object = object;
78+
if (object && cameraSampler) cameraSampler->object = object;
6079
}
6180
else
6281
{
@@ -74,10 +93,10 @@ CameraAnimation::CameraAnimation(ref_ptr<Object> in_object, ref_ptr<Animation> i
7493
{
7594
for (auto& sampler : animation->samplers)
7695
{
77-
if (auto ts = sampler.cast<TransformSampler>())
96+
if (auto ts = sampler.cast<CameraSampler>())
7897
{
79-
transformSampler = ts;
80-
transformSampler->object = object;
98+
cameraSampler = ts;
99+
cameraSampler->object = object;
81100
break;
82101
}
83102
}
@@ -86,33 +105,33 @@ CameraAnimation::CameraAnimation(ref_ptr<Object> in_object, ref_ptr<Animation> i
86105

87106
void CameraAnimation::apply(Camera& camera)
88107
{
89-
if (transformSampler)
108+
if (cameraSampler)
90109
{
91-
auto& keyframes = transformSampler->keyframes;
92-
if (!keyframes) keyframes = TransformKeyframes::create();
110+
auto& keyframes = cameraSampler->keyframes;
111+
if (!keyframes) keyframes = CameraKeyframes::create();
93112

94113
dvec3 position, scale;
95114
dquat orientation;
96115
auto matrix = camera.viewMatrix->inverse();
97116
if (decompose(matrix, position, orientation, scale))
98117
{
99-
keyframes->add(simulationTime - startTime, position, orientation, scale);
118+
keyframes->add(simulationTime - startTime, position, orientation);
100119
}
101120
}
102121
}
103122

104123
void CameraAnimation::apply(MatrixTransform& transform)
105124
{
106-
if (transformSampler)
125+
if (cameraSampler)
107126
{
108-
auto& keyframes = transformSampler->keyframes;
109-
if (!keyframes) keyframes = TransformKeyframes::create();
127+
auto& keyframes = cameraSampler->keyframes;
128+
if (!keyframes) keyframes = CameraKeyframes::create();
110129

111130
dvec3 position, scale;
112131
dquat orientation;
113132
if (decompose(transform.matrix, position, orientation, scale))
114133
{
115-
keyframes->add(simulationTime - startTime, position, orientation, scale);
134+
keyframes->add(simulationTime - startTime, position, orientation);
116135
}
117136
}
118137
}
@@ -137,21 +156,21 @@ void CameraAnimation::record()
137156
{
138157
animation = Animation::create();
139158
}
140-
if (!transformSampler)
159+
if (!cameraSampler)
141160
{
142-
transformSampler = TransformSampler::create();
143-
transformSampler->object = object;
161+
cameraSampler = CameraSampler::create();
162+
cameraSampler->object = object;
144163

145-
animation->samplers.push_back(transformSampler);
164+
animation->samplers.push_back(cameraSampler);
146165
}
147166

148-
if (transformSampler->keyframes)
167+
if (cameraSampler->keyframes)
149168
{
150-
transformSampler->keyframes->clear();
169+
cameraSampler->keyframes->clear();
151170
}
152171
else
153172
{
154-
transformSampler->keyframes = TransformKeyframes::create();
173+
cameraSampler->keyframes = CameraKeyframes::create();
155174
}
156175
}
157176

src/vsg/animation/CameraSampler.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void CameraKeyframes::read(Input& input)
6161
projections.resize(num_projections);
6262
for (auto& scale : projections)
6363
{
64-
input.matchPropertyName("scale");
64+
input.matchPropertyName("projection");
6565
input.read(1, &scale.time);
6666
input.read(1, &scale.value);
6767
}
@@ -97,7 +97,7 @@ void CameraKeyframes::write(Output& output) const
9797
output.writeValue<uint32_t>("projections", projections.size());
9898
for (const auto& scale : projections)
9999
{
100-
output.writePropertyName("scale");
100+
output.writePropertyName("projection");
101101
output.write(1, &scale.time);
102102
output.write(1, &scale.value);
103103
output.writeEndOfLine();
@@ -219,22 +219,37 @@ void CameraSampler::apply(dmat4Value& matrix)
219219

220220
void CameraSampler::apply(LookAt& lookAt)
221221
{
222-
lookAt.origin = origin;
223-
lookAt.set(transform());
222+
if (keyframes)
223+
{
224+
vsg::info("CameraSampler::apply(LookAt&)");
225+
if (!keyframes->origins.empty()) lookAt.origin = origin;
226+
if (!keyframes->positions.empty() || !keyframes->rotations.empty())
227+
{
228+
lookAt.set(transform());
229+
}
230+
}
224231
}
225232

226233
void CameraSampler::apply(LookDirection& lookDirection)
227234
{
228-
lookDirection.origin = origin;
229-
lookDirection.position = origin;
230-
lookDirection.rotation = rotation;
235+
if (keyframes)
236+
{
237+
vsg::info("CameraSampler::apply(LookDirection&)");
238+
if (!keyframes->origins.empty()) lookDirection.origin = origin;
239+
if (!keyframes->positions.empty()) lookDirection.position = position;
240+
if (!keyframes->rotations.empty()) lookDirection.rotation = rotation;
241+
}
231242
}
232243

233244
void CameraSampler::apply(Perspective& perspective)
234245
{
235-
perspective.fieldOfViewY = projection.x;
236-
perspective.nearDistance = projection.y;
237-
perspective.farDistance = projection.z;
246+
if (keyframes && !keyframes->projections.empty())
247+
{
248+
vsg::info("CameraSampler::apply(Perspective&)");
249+
perspective.fieldOfViewY = projection.x;
250+
perspective.nearDistance = projection.y;
251+
perspective.farDistance = projection.z;
252+
}
238253
}
239254

240255
void CameraSampler::apply(Camera& camera)

src/vsg/io/ObjectFactory.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ ObjectFactory::ObjectFactory()
300300
// animation
301301
add<vsg::TransformKeyframes>();
302302
add<vsg::TransformSampler>();
303+
add<vsg::CameraKeyframes>();
304+
add<vsg::CameraSampler>();
303305
add<vsg::MorphKeyframes>();
304306
add<vsg::MorphSampler>();
305307
add<vsg::JointSampler>();

0 commit comments

Comments
 (0)