Skip to content

Commit

Permalink
Added vsg::LookDirection ViewMatrix implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertosfield committed Nov 15, 2024
1 parent 7e6c315 commit f41d9ca
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
34 changes: 32 additions & 2 deletions include/vsg/app/ViewMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace vsg

void set(const dmat4& matrix);

dmat4 transform(const vsg::dvec3& offset={}) const override;
dmat4 transform(const dvec3& offset={}) const override;

void read(Input& input) override;
void write(Output& output) const override;
Expand All @@ -99,8 +99,38 @@ namespace vsg
};
VSG_type_name(vsg::LookAt);

/// LookDirection is a ViewMatrix that uses a position and rotation to set the view matrix.
class VSG_DECLSPEC LookDirection : public vsg::Inherit<ViewMatrix, LookDirection>
{
public:

LookDirection() :
position(0.0, 0.0, 0.0),
rotation()
{
}

LookDirection(const LookDirection& view, const CopyOp& copyop = {}) :
Inherit(view, copyop),
position(view.position),
rotation(view.rotation)
{
}

ref_ptr<Object> clone(const CopyOp& copyop = {}) const override { return LookDirection::create(*this, copyop); }

dvec3 position;
dquat rotation;

void set(const dmat4& matrix);

dmat4 transform(const dvec3& offset={}) const override;
};
VSG_type_name(vsg::LookDirection);


/// RelativeViewMatrix is a ViewMatrix that decorates another ViewMatrix and pre-multiplies its transform matrix to give a relative view matrix.
class RelativeViewMatrix : public Inherit<ViewMatrix, RelativeViewMatrix>
class VSG_DECLSPEC RelativeViewMatrix : public Inherit<ViewMatrix, RelativeViewMatrix>
{
public:
RelativeViewMatrix(const dmat4& m, ref_ptr<ViewMatrix> vm) :
Expand Down
23 changes: 17 additions & 6 deletions src/vsg/app/ViewMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,34 @@ void LookAt::set(const dmat4& matrix)
eye = matrix * dvec3(0.0, 0.0, 0.0);
}

dmat4 LookAt::transform(const vsg::dvec3& offset) const
dmat4 LookAt::transform(const dvec3& offset) const
{
dvec3 delta = origin - offset;
return lookAt(eye + delta, center + delta, up);
return vsg::lookAt(eye + delta, center + delta, up);
}

dmat4 RelativeViewMatrix::transform(const vsg::dvec3& offset) const
void LookDirection::set(const dmat4& matrix)
{
dvec3 scale;
vsg::decompose(matrix, position, rotation, scale);
}

dmat4 LookDirection::transform(const dvec3& offset) const
{
return vsg::rotate(-rotation) * vsg::translate(offset-origin-position);
}

dmat4 RelativeViewMatrix::transform(const dvec3& offset) const
{
return matrix * viewMatrix->transform(offset);
}

dmat4 TrackingViewMatrix::transform(const vsg::dvec3& offset) const
dmat4 TrackingViewMatrix::transform(const dvec3& offset) const
{
return matrix * vsg::translate(offset-origin) * vsg::inverse(computeTransform(objectPath));
}

dmat4 TrackingViewMatrix::inverse(const vsg::dvec3& offset) const
dmat4 TrackingViewMatrix::inverse(const dvec3& offset) const
{
return computeTransform(objectPath) * vsg::translate(origin - offset) * vsg::inverse(matrix);
return vsg::computeTransform(objectPath) * vsg::translate(origin - offset) * vsg::inverse(matrix);
}

0 comments on commit f41d9ca

Please sign in to comment.