Skip to content

Commit f41d9ca

Browse files
committed
Added vsg::LookDirection ViewMatrix implementation.
1 parent 7e6c315 commit f41d9ca

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

include/vsg/app/ViewMatrix.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ namespace vsg
8888

8989
void set(const dmat4& matrix);
9090

91-
dmat4 transform(const vsg::dvec3& offset={}) const override;
91+
dmat4 transform(const dvec3& offset={}) const override;
9292

9393
void read(Input& input) override;
9494
void write(Output& output) const override;
@@ -99,8 +99,38 @@ namespace vsg
9999
};
100100
VSG_type_name(vsg::LookAt);
101101

102+
/// LookDirection is a ViewMatrix that uses a position and rotation to set the view matrix.
103+
class VSG_DECLSPEC LookDirection : public vsg::Inherit<ViewMatrix, LookDirection>
104+
{
105+
public:
106+
107+
LookDirection() :
108+
position(0.0, 0.0, 0.0),
109+
rotation()
110+
{
111+
}
112+
113+
LookDirection(const LookDirection& view, const CopyOp& copyop = {}) :
114+
Inherit(view, copyop),
115+
position(view.position),
116+
rotation(view.rotation)
117+
{
118+
}
119+
120+
ref_ptr<Object> clone(const CopyOp& copyop = {}) const override { return LookDirection::create(*this, copyop); }
121+
122+
dvec3 position;
123+
dquat rotation;
124+
125+
void set(const dmat4& matrix);
126+
127+
dmat4 transform(const dvec3& offset={}) const override;
128+
};
129+
VSG_type_name(vsg::LookDirection);
130+
131+
102132
/// RelativeViewMatrix is a ViewMatrix that decorates another ViewMatrix and pre-multiplies its transform matrix to give a relative view matrix.
103-
class RelativeViewMatrix : public Inherit<ViewMatrix, RelativeViewMatrix>
133+
class VSG_DECLSPEC RelativeViewMatrix : public Inherit<ViewMatrix, RelativeViewMatrix>
104134
{
105135
public:
106136
RelativeViewMatrix(const dmat4& m, ref_ptr<ViewMatrix> vm) :

src/vsg/app/ViewMatrix.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,34 @@ void LookAt::set(const dmat4& matrix)
4848
eye = matrix * dvec3(0.0, 0.0, 0.0);
4949
}
5050

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

57-
dmat4 RelativeViewMatrix::transform(const vsg::dvec3& offset) const
57+
void LookDirection::set(const dmat4& matrix)
58+
{
59+
dvec3 scale;
60+
vsg::decompose(matrix, position, rotation, scale);
61+
}
62+
63+
dmat4 LookDirection::transform(const dvec3& offset) const
64+
{
65+
return vsg::rotate(-rotation) * vsg::translate(offset-origin-position);
66+
}
67+
68+
dmat4 RelativeViewMatrix::transform(const dvec3& offset) const
5869
{
5970
return matrix * viewMatrix->transform(offset);
6071
}
6172

62-
dmat4 TrackingViewMatrix::transform(const vsg::dvec3& offset) const
73+
dmat4 TrackingViewMatrix::transform(const dvec3& offset) const
6374
{
6475
return matrix * vsg::translate(offset-origin) * vsg::inverse(computeTransform(objectPath));
6576
}
6677

67-
dmat4 TrackingViewMatrix::inverse(const vsg::dvec3& offset) const
78+
dmat4 TrackingViewMatrix::inverse(const dvec3& offset) const
6879
{
69-
return computeTransform(objectPath) * vsg::translate(origin - offset) * vsg::inverse(matrix);
80+
return vsg::computeTransform(objectPath) * vsg::translate(origin - offset) * vsg::inverse(matrix);
7081
}

0 commit comments

Comments
 (0)