Skip to content

Commit

Permalink
Merge pull request #1347 from vsg-dev/CameraSampler
Browse files Browse the repository at this point in the history
vsg::CoordinateFrame, vsg::CameraSampler & long double support.
  • Loading branch information
robertosfield authored Dec 16, 2024
2 parents e07a9a7 + a12c1a9 commit 308d7c9
Show file tree
Hide file tree
Showing 17 changed files with 682 additions and 109 deletions.
3 changes: 2 additions & 1 deletion cmake/cppcheck-suppression-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ duplInheritedMember:*/src/vsg/nodes/Node.cpp
// suppress unhelpful warning of shadowFunction
shadowFunction:*/include/vsg/maths/transform.h
shadowFunction:*/src/io/Path.cpp
shadowFunction:*/src/vsg/animation/CameraAnimation.cpp
shadowFunction:*/src/vsg/animation/CameraAnimationHandler.cpp
shadowFunction:*/src/vsg/animation/CameraSampler.cpp
shadowFunction:*/src/vsg/animation/TransformSampler.cpp
shadowFunction:*/src/vsg/io/tile.cpp
shadowFunction:*/src/vsg/io/FileSystem.cpp
Expand Down
4 changes: 3 additions & 1 deletion include/vsg/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/animation/Animation.h>
#include <vsg/animation/AnimationGroup.h>
#include <vsg/animation/AnimationManager.h>
#include <vsg/animation/CameraAnimation.h>
#include <vsg/animation/CameraAnimationHandler.h>
#include <vsg/animation/CameraSampler.h>
#include <vsg/animation/FindAnimations.h>
#include <vsg/animation/Joint.h>
#include <vsg/animation/JointSampler.h>
#include <vsg/animation/MorphSampler.h>
#include <vsg/animation/TransformSampler.h>
#include <vsg/animation/time_value.h>

// Lighting header files
#include <vsg/lighting/AmbientLight.h>
Expand Down
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 @@ -21,11 +21,12 @@ namespace vsg
{

/// event handler for controlling the playing and recording of camera animation paths
class VSG_DECLSPEC CameraAnimation : public Inherit<Visitor, CameraAnimation>
class VSG_DECLSPEC CameraAnimationHandler : public Inherit<Visitor, CameraAnimationHandler>
{
public:
explicit CameraAnimation(ref_ptr<Object> in_object, const Path& in_filename = "saved_animation.vsgt", ref_ptr<Options> in_options = {});
CameraAnimation(ref_ptr<Object> in_object, ref_ptr<Animation> in_animation, const Path& in_filename = "saved_animation.vsgt", ref_ptr<Options> in_options = {});
CameraAnimationHandler();
CameraAnimationHandler(ref_ptr<Object> in_object, ref_ptr<Animation> in_animation, const Path& in_filename = "saved_animation.vsgt", ref_ptr<Options> in_options = {});
explicit CameraAnimationHandler(ref_ptr<Object> in_object, const Path& in_filename = "saved_animation.vsgt", ref_ptr<Options> in_options = {});

/// object to track/modify
ref_ptr<Object> object;
Expand All @@ -37,8 +38,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 All @@ -61,6 +62,9 @@ namespace vsg

protected:
};
VSG_type_name(vsg::CameraAnimation);
VSG_type_name(vsg::CameraAnimationHandler);

// fallback for naming prior to VulkanSceneGraph-1.1.9.
using CameraAnimation = vsg::CameraAnimationHandler;

} // namespace vsg
147 changes: 147 additions & 0 deletions include/vsg/animation/CameraSampler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#pragma once

/* <editor-fold desc="MIT License">
Copyright(c) 2024 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */

#include <vsg/animation/TransformSampler.h>
#include <vsg/app/ViewMatrix.h>
#include <vsg/maths/transform.h>

namespace vsg
{

using time_path = time_value<RefObjectPath>;

class VSG_DECLSPEC CameraKeyframes : public Inherit<Object, CameraKeyframes>
{
public:
CameraKeyframes();

/// name of node
std::string name;

// object tracking key frames
std::vector<time_path> tracking;

/// position key frames
std::vector<time_dvec3> origins;

/// position key frames
std::vector<time_dvec3> positions;

/// rotation key frames
std::vector<time_dquat> rotations;

/// field of view key frames
std::vector<time_double> fieldOfViews;

/// near/far key frames
std::vector<time_dvec2> nearFars;

void clear()
{
origins.clear();
positions.clear();
rotations.clear();
fieldOfViews.clear();
nearFars.clear();
}

void add(double time, const dvec3& origin, const dvec3& position, const dquat& rotation, double fov, const dvec2& nearFar)
{
origins.push_back(VectorKey{time, origin});
positions.push_back(VectorKey{time, position});
rotations.push_back(QuatKey{time, rotation});
fieldOfViews.push_back(time_double{time, fov});
nearFars.push_back(time_dvec2{time, nearFar});
}

void add(double time, const dvec3& origin, const dvec3& position, const dquat& rotation, double fov)
{
origins.push_back(VectorKey{time, origin});
positions.push_back(VectorKey{time, position});
rotations.push_back(QuatKey{time, rotation});
fieldOfViews.push_back(time_double{time, fov});
}

void add(double time, const dvec3& position, const dquat& rotation, double fov, const dvec2& nearFar)
{
positions.push_back(VectorKey{time, position});
rotations.push_back(QuatKey{time, rotation});
fieldOfViews.push_back(time_double{time, fov});
nearFars.push_back(time_dvec2{time, nearFar});
}

void add(double time, const dvec3& position, const dquat& rotation, double fov)
{
positions.push_back(VectorKey{time, position});
rotations.push_back(QuatKey{time, rotation});
fieldOfViews.push_back(time_double{time, fov});
}

void add(double time, const dvec3& origin, const dvec3& position, const dquat& rotation)
{
origins.push_back(VectorKey{time, origin});
positions.push_back(VectorKey{time, position});
rotations.push_back(QuatKey{time, rotation});
}

void add(double time, const dvec3& position, const dquat& rotation)
{
positions.push_back(VectorKey{time, position});
rotations.push_back(QuatKey{time, rotation});
}

void read(Input& input) override;
void write(Output& output) const override;
};
VSG_type_name(vsg::CameraKeyframes);

/// Animation sampler for sampling position, rotation and scale keyframes for setting camera view and project matrices.
class VSG_DECLSPEC CameraSampler : public Inherit<AnimationSampler, CameraSampler>
{
public:
CameraSampler();
CameraSampler(const CameraSampler& rhs, const CopyOp& copyop = {});

ref_ptr<CameraKeyframes> keyframes;
ref_ptr<Object> object;

// updated using keyFrames
dvec3 origin;
dvec3 position;
dquat rotation;
double fieldOfView;
dvec2 nearFar;

void update(double time) override;
double maxTime() const override;

inline dmat4 transform() const { return translate(position) * vsg::rotate(rotation); }

public:
ref_ptr<Object> clone(const CopyOp& copyop = {}) const override { return CameraSampler::create(*this, copyop); }
int compare(const Object& rhs) const override;

void read(Input& input) override;
void write(Output& output) const override;

void apply(mat4Value& mat) override;
void apply(dmat4Value& mat) override;
void apply(LookAt& lookAt) override;
void apply(LookDirection& lookDirection) override;
void apply(Perspective& perspective) override;
void apply(Camera& camera) override;
};
VSG_type_name(vsg::CameraSampler);

} // namespace vsg
18 changes: 3 additions & 15 deletions include/vsg/animation/TransformSampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,15 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
</editor-fold> */

#include <vsg/animation/Animation.h>
#include <vsg/animation/time_value.h>
#include <vsg/app/ViewMatrix.h>
#include <vsg/maths/transform.h>

namespace vsg
{

struct VectorKey
{
double time;
dvec3 value;

bool operator<(const VectorKey& rhs) const { return time < rhs.time; }
};

struct QuatKey
{
double time;
dquat value;

bool operator<(const QuatKey& rhs) const { return time < rhs.time; }
};
using VectorKey = time_dvec3;
using QuatKey = time_dquat;

class VSG_DECLSPEC TransformKeyframes : public Inherit<Object, TransformKeyframes>
{
Expand Down
82 changes: 82 additions & 0 deletions include/vsg/animation/time_value.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#pragma once

/* <editor-fold desc="MIT License">
Copyright(c) 2024 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */

#include <vsg/animation/Animation.h>
#include <vsg/app/ViewMatrix.h>
#include <vsg/maths/transform.h>

namespace vsg
{

template<typename T>
struct time_value
{
using value_type = T;
double time;
value_type value;

bool operator<(const time_value& rhs) const { return time < rhs.time; }
};

using time_double = time_value<double>;
using time_dvec2 = time_value<dvec2>;
using time_dvec3 = time_value<dvec3>;
using time_dvec4 = time_value<dvec4>;
using time_dquat = time_value<dquat>;

template<typename T, typename V>
bool sample(double time, const T& values, V& value)
{
if (values.size() == 0) return false;

if (values.size() == 1)
{
value = values.front().value;
return true;
}

auto pos_itr = values.begin();
if (time <= pos_itr->time)
{
value = pos_itr->value;
return true;
}
else
{
using value_type = typename T::value_type;
pos_itr = std::lower_bound(values.begin(), values.end(), time, [](const value_type& elem, double t) -> bool { return elem.time < t; });

if (pos_itr == values.begin())
{
value = values.front().value;
return true;
}

if (pos_itr == values.end())
{
value = values.back().value;
return true;
}

auto before_pos_itr = pos_itr - 1;
double delta_time = (pos_itr->time - before_pos_itr->time);
double r = delta_time != 0.0 ? (time - before_pos_itr->time) / delta_time : 0.5;

value = mix(before_pos_itr->value, pos_itr->value, r);

return true;
}
}

} // namespace vsg
4 changes: 4 additions & 0 deletions include/vsg/core/ConstVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace vsg
class JointSampler;
class MorphSampler;
class TransformSampler;
class CameraSampler;
class Joint;

// forward declare vulkan classes
Expand Down Expand Up @@ -157,6 +158,7 @@ namespace vsg
class Viewer;
class ViewMatrix;
class LookAt;
class LookDirection;
class RelativeViewMatrix;
class TrackingViewMatrix;
class ProjectionMatrix;
Expand Down Expand Up @@ -355,6 +357,7 @@ namespace vsg
virtual void apply(const JointSampler&);
virtual void apply(const MorphSampler&);
virtual void apply(const TransformSampler&);
virtual void apply(const CameraSampler&);
virtual void apply(const Joint&);

// Vulkan nodes
Expand Down Expand Up @@ -446,6 +449,7 @@ namespace vsg
virtual void apply(const Viewer&);
virtual void apply(const ViewMatrix&);
virtual void apply(const LookAt&);
virtual void apply(const LookDirection&);
virtual void apply(const RelativeViewMatrix&);
virtual void apply(const TrackingViewMatrix&);
virtual void apply(const ProjectionMatrix&);
Expand Down
4 changes: 4 additions & 0 deletions include/vsg/core/Visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ namespace vsg
class AnimationGroup;
class AnimationSampler;
class TransformSampler;
class CameraSampler;
class MorphSampler;
class JointSampler;
class Joint;
Expand Down Expand Up @@ -157,6 +158,7 @@ namespace vsg
class Viewer;
class ViewMatrix;
class LookAt;
class LookDirection;
class RelativeViewMatrix;
class TrackingViewMatrix;
class ProjectionMatrix;
Expand Down Expand Up @@ -355,6 +357,7 @@ namespace vsg
virtual void apply(JointSampler&);
virtual void apply(MorphSampler&);
virtual void apply(TransformSampler&);
virtual void apply(CameraSampler&);
virtual void apply(Joint&);

// Vulkan nodes
Expand Down Expand Up @@ -446,6 +449,7 @@ namespace vsg
virtual void apply(Viewer&);
virtual void apply(ViewMatrix&);
virtual void apply(LookAt&);
virtual void apply(LookDirection&);
virtual void apply(RelativeViewMatrix&);
virtual void apply(TrackingViewMatrix&);
virtual void apply(ProjectionMatrix&);
Expand Down
Loading

0 comments on commit 308d7c9

Please sign in to comment.