vsg: include/vsg/app/ViewMatrix.h Source File

vsg 1.1.13 VulkanSceneGraph library

Loading...
Searching...
No Matches
ViewMatrix.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission 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:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE 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.
12
13</editor-fold> */
14
15#include <vsg/core/Inherit.h>
16#include <vsg/maths/transform.h>
17
18namespace vsg
19{
20
22 class VSG_DECLSPEC ViewMatrix : public Inherit<Object, ViewMatrix>
23 {
24 public:
25 ViewMatrix()
26 {
27 }
28
29 explicit ViewMatrix(const ViewMatrix& vm, const CopyOp& copyop = {}) :
30 Inherit(vm, copyop),
31 origin(vm.origin)
32 {
33 }
34
38 dvec3 origin;
39
40 virtual dmat4 transform(const dvec3& offset = {}) const = 0;
41
42 virtual dmat4 inverse(const dvec3& offset = {}) const
43 {
44 return vsg::inverse(transform(offset));
45 }
46
47 void read(Input& input) override;
48 void write(Output& output) const override;
49 };
50 VSG_type_name(vsg::ViewMatrix);
51
53 class VSG_DECLSPEC LookAt : public Inherit<ViewMatrix, LookAt>
54 {
55 public:
56 LookAt() :
57 eye(0.0, 0.0, 0.0),
58 center(0.0, 1.0, 0.0),
59 up(0.0, 0.0, 1.0)
60 {
61 }
62
63 LookAt(const LookAt& lookAt, const CopyOp& copyop = {}) :
64 Inherit(lookAt, copyop),
65 eye(lookAt.eye),
66 center(lookAt.center),
67 up(lookAt.up)
68 {
69 }
70
71 LookAt(const dvec3& in_eye, const dvec3& in_center, const dvec3& in_up) :
72 eye(in_eye),
73 center(in_center),
74 up(in_up)
75 {
76 dvec3 look = normalize(center - eye);
77 dvec3 side = normalize(cross(look, up));
78 up = normalize(cross(side, look));
79 }
80
81 LookAt& operator=(const LookAt& lookAt)
82 {
83 eye = lookAt.eye;
84 center = lookAt.center;
85 up = lookAt.up;
86 return *this;
87 }
88
89 ref_ptr<Object> clone(const CopyOp& copyop = {}) const override { return LookAt::create(*this, copyop); }
90
91 void transform(const dmat4& matrix);
92
93 void set(const dmat4& matrix);
94
95 dmat4 transform(const dvec3& offset = {}) const override;
96
97 void read(Input& input) override;
98 void write(Output& output) const override;
99
100 dvec3 eye;
101 dvec3 center;
102 dvec3 up;
103 };
104 VSG_type_name(vsg::LookAt);
105
107 class VSG_DECLSPEC LookDirection : public vsg::Inherit<ViewMatrix, LookDirection>
108 {
109 public:
110 LookDirection() :
111 position(0.0, 0.0, 0.0),
112 rotation()
113 {
114 }
115
116 LookDirection(const LookDirection& view, const CopyOp& copyop = {}) :
117 Inherit(view, copyop),
118 position(view.position),
119 rotation(view.rotation)
120 {
121 }
122
123 ref_ptr<Object> clone(const CopyOp& copyop = {}) const override { return LookDirection::create(*this, copyop); }
124
125 dvec3 position;
126 dquat rotation;
127
128 void set(const dmat4& matrix);
129
130 dmat4 transform(const dvec3& offset = {}) const override;
131 };
132 VSG_type_name(vsg::LookDirection);
133
135 class VSG_DECLSPEC RelativeViewMatrix : public Inherit<ViewMatrix, RelativeViewMatrix>
136 {
137 public:
138 RelativeViewMatrix(const dmat4& m, ref_ptr<ViewMatrix> vm) :
139 matrix(m),
140 viewMatrix(vm)
141 {
142 }
143
145 dmat4 transform(const dvec3& offset = {}) const override;
146
147 dmat4 matrix;
148 ref_ptr<ViewMatrix> viewMatrix;
149 };
150 VSG_type_name(vsg::RelativeViewMatrix);
151
155 class VSG_DECLSPEC TrackingViewMatrix : public Inherit<ViewMatrix, TrackingViewMatrix>
156 {
157 public:
158 template<typename T>
159 explicit TrackingViewMatrix(const dmat4& initial_matrix, const T& path) :
160 matrix(initial_matrix),
161 objectPath(path.begin(), path.end()) {}
162
163 template<typename T>
164 explicit TrackingViewMatrix(const T& path) :
165 objectPath(path.begin(), path.end()) {}
166
168 dmat4 transform(const dvec3& offset = {}) const override;
169 dmat4 inverse(const dvec3& offset = {}) const override;
170
171 dmat4 matrix;
172 RefObjectPath objectPath;
173 };
174 VSG_type_name(vsg::TrackingViewMatrix);
175
176} // namespace vsg
vsg::CopyOp
Definition Object.h:42
vsg::Inherit
Definition Inherit.h:28
vsg::LookAt::clone
ref_ptr< Object > clone(const CopyOp &copyop={}) const override
Definition ViewMatrix.h:89
vsg::LookDirection::clone
ref_ptr< Object > clone(const CopyOp &copyop={}) const override
Definition ViewMatrix.h:123
vsg::RelativeViewMatrix
RelativeViewMatrix is a ViewMatrix that decorates another ViewMatrix and pre-multiplies its transform...
Definition ViewMatrix.h:136
vsg::RelativeViewMatrix::transform
dmat4 transform(const dvec3 &offset={}) const override
returns matrix * viewMatrix->transform()
vsg::TrackingViewMatrix::transform
dmat4 transform(const dvec3 &offset={}) const override
returns matrix * computeTransfrom(objectPath)
vsg::ViewMatrix::origin
dvec3 origin
Definition ViewMatrix.h:38
vsg::ref_ptr
Definition ref_ptr.h:22