Skip to content

Commit

Permalink
Added use of CameraSampler to CameraAnimation.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertosfield committed Dec 12, 2024
1 parent d96d1ae commit 12f8c15
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 39 deletions.
6 changes: 3 additions & 3 deletions include/vsg/animation/CameraAnimation.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
</editor-fold> */

#include <vsg/animation/TransformSampler.h>
#include <vsg/animation/CameraSampler.h>
#include <vsg/core/Inherit.h>
#include <vsg/maths/quat.h>
#include <vsg/ui/KeyEvent.h>
Expand All @@ -37,8 +37,8 @@ namespace vsg
// animation to play/record to
ref_ptr<Animation> animation;

// transformSampler to play/record to
ref_ptr<TransformSampler> transformSampler;
// CameraSampler to play/record to
ref_ptr<CameraSampler> cameraSampler;

KeySymbol toggleRecordingKey = KEY_r;
KeySymbol togglePlaybackKey = KEY_p;
Expand Down
71 changes: 45 additions & 26 deletions src/vsg/animation/CameraAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
</editor-fold> */

#include <vsg/animation/CameraAnimation.h>
#include <vsg/animation/TransformSampler.h>
#include <vsg/app/Camera.h>
#include <vsg/io/Logger.h>
#include <vsg/io/Options.h>
Expand All @@ -35,28 +36,46 @@ CameraAnimation::CameraAnimation(ref_ptr<Object> in_object, const Path& in_filen
{
for (auto sampler : animation->samplers)
{
if (auto ts = sampler.cast<TransformSampler>())
if (auto cs = sampler.cast<CameraSampler>())
{
transformSampler = ts;
cameraSampler = cs;
break;
}
}
}
else if ((transformSampler = read_object.cast<TransformSampler>()))
else if ((cameraSampler = read_object.cast<CameraSampler>()))
{
animation = Animation::create();
animation->samplers.push_back(transformSampler);
animation->samplers.push_back(cameraSampler);
}
else if (auto ts = read_object.cast<TransformSampler>())
{
auto tkf = ts->keyframes;

// convert TransformSampler to CameraSampler
cameraSampler = CameraSampler::create();
cameraSampler->name = ts->name;
auto& ckf = cameraSampler->keyframes = CameraKeyframes::create();

ckf->positions = tkf->positions;
ckf->rotations = tkf->rotations;

animation = Animation::create();
animation->samplers.push_back(cameraSampler);
}
else if (auto keyframes = read_object.cast<TransformKeyframes>())
{
transformSampler = TransformSampler::create();
transformSampler->keyframes = keyframes;
cameraSampler = CameraSampler::create();
auto& ckf = cameraSampler->keyframes = CameraKeyframes::create();

ckf->positions = keyframes->positions;
ckf->rotations = keyframes->rotations;

animation = Animation::create();
animation->samplers.push_back(transformSampler);
animation->samplers.push_back(cameraSampler);
}
}
if (object && transformSampler) transformSampler->object = object;
if (object && cameraSampler) cameraSampler->object = object;
}
else
{
Expand All @@ -74,10 +93,10 @@ CameraAnimation::CameraAnimation(ref_ptr<Object> in_object, ref_ptr<Animation> i
{
for (auto& sampler : animation->samplers)
{
if (auto ts = sampler.cast<TransformSampler>())
if (auto ts = sampler.cast<CameraSampler>())
{
transformSampler = ts;
transformSampler->object = object;
cameraSampler = ts;
cameraSampler->object = object;
break;
}
}
Expand All @@ -86,33 +105,33 @@ CameraAnimation::CameraAnimation(ref_ptr<Object> in_object, ref_ptr<Animation> i

void CameraAnimation::apply(Camera& camera)
{
if (transformSampler)
if (cameraSampler)
{
auto& keyframes = transformSampler->keyframes;
if (!keyframes) keyframes = TransformKeyframes::create();
auto& keyframes = cameraSampler->keyframes;
if (!keyframes) keyframes = CameraKeyframes::create();

dvec3 position, scale;
dquat orientation;
auto matrix = camera.viewMatrix->inverse();
if (decompose(matrix, position, orientation, scale))
{
keyframes->add(simulationTime - startTime, position, orientation, scale);
keyframes->add(simulationTime - startTime, position, orientation);
}
}
}

void CameraAnimation::apply(MatrixTransform& transform)
{
if (transformSampler)
if (cameraSampler)
{
auto& keyframes = transformSampler->keyframes;
if (!keyframes) keyframes = TransformKeyframes::create();
auto& keyframes = cameraSampler->keyframes;
if (!keyframes) keyframes = CameraKeyframes::create();

dvec3 position, scale;
dquat orientation;
if (decompose(transform.matrix, position, orientation, scale))
{
keyframes->add(simulationTime - startTime, position, orientation, scale);
keyframes->add(simulationTime - startTime, position, orientation);
}
}
}
Expand All @@ -137,21 +156,21 @@ void CameraAnimation::record()
{
animation = Animation::create();
}
if (!transformSampler)
if (!cameraSampler)
{
transformSampler = TransformSampler::create();
transformSampler->object = object;
cameraSampler = CameraSampler::create();
cameraSampler->object = object;

animation->samplers.push_back(transformSampler);
animation->samplers.push_back(cameraSampler);
}

if (transformSampler->keyframes)
if (cameraSampler->keyframes)
{
transformSampler->keyframes->clear();
cameraSampler->keyframes->clear();
}
else
{
transformSampler->keyframes = TransformKeyframes::create();
cameraSampler->keyframes = CameraKeyframes::create();
}
}

Expand Down
35 changes: 25 additions & 10 deletions src/vsg/animation/CameraSampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void CameraKeyframes::read(Input& input)
projections.resize(num_projections);
for (auto& scale : projections)
{
input.matchPropertyName("scale");
input.matchPropertyName("projection");
input.read(1, &scale.time);
input.read(1, &scale.value);
}
Expand Down Expand Up @@ -97,7 +97,7 @@ void CameraKeyframes::write(Output& output) const
output.writeValue<uint32_t>("projections", projections.size());
for (const auto& scale : projections)
{
output.writePropertyName("scale");
output.writePropertyName("projection");
output.write(1, &scale.time);
output.write(1, &scale.value);
output.writeEndOfLine();
Expand Down Expand Up @@ -219,22 +219,37 @@ void CameraSampler::apply(dmat4Value& matrix)

void CameraSampler::apply(LookAt& lookAt)
{
lookAt.origin = origin;
lookAt.set(transform());
if (keyframes)
{
vsg::info("CameraSampler::apply(LookAt&)");
if (!keyframes->origins.empty()) lookAt.origin = origin;
if (!keyframes->positions.empty() || !keyframes->rotations.empty())
{
lookAt.set(transform());
}
}
}

void CameraSampler::apply(LookDirection& lookDirection)
{
lookDirection.origin = origin;
lookDirection.position = origin;
lookDirection.rotation = rotation;
if (keyframes)
{
vsg::info("CameraSampler::apply(LookDirection&)");
if (!keyframes->origins.empty()) lookDirection.origin = origin;
if (!keyframes->positions.empty()) lookDirection.position = position;
if (!keyframes->rotations.empty()) lookDirection.rotation = rotation;
}
}

void CameraSampler::apply(Perspective& perspective)
{
perspective.fieldOfViewY = projection.x;
perspective.nearDistance = projection.y;
perspective.farDistance = projection.z;
if (keyframes && !keyframes->projections.empty())
{
vsg::info("CameraSampler::apply(Perspective&)");
perspective.fieldOfViewY = projection.x;
perspective.nearDistance = projection.y;
perspective.farDistance = projection.z;
}
}

void CameraSampler::apply(Camera& camera)
Expand Down
2 changes: 2 additions & 0 deletions src/vsg/io/ObjectFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ ObjectFactory::ObjectFactory()
// animation
add<vsg::TransformKeyframes>();
add<vsg::TransformSampler>();
add<vsg::CameraKeyframes>();
add<vsg::CameraSampler>();
add<vsg::MorphKeyframes>();
add<vsg::MorphSampler>();
add<vsg::JointSampler>();
Expand Down

0 comments on commit 12f8c15

Please sign in to comment.