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

vsg 1.1.10 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 {
32 }
33
37 dvec3 origin;
38
39 virtual dmat4 transform(const dvec3& offset = {}) const = 0;
40
41 virtual dmat4 inverse(const dvec3& offset = {}) const
42 {
43 return vsg::inverse(transform(offset));
44 }
45
46 void read(Input& input) override;
47 void write(Output& output) const override;
48 };
49 VSG_type_name(vsg::ViewMatrix);
50
52 class VSG_DECLSPEC LookAt : public Inherit<ViewMatrix, LookAt>
53 {
54 public:
55 LookAt() :
56 eye(0.0, 0.0, 0.0),
57 center(0.0, 1.0, 0.0),
58 up(0.0, 0.0, 1.0)
59 {
60 }
61
62 LookAt(const LookAt& lookAt, const CopyOp& copyop = {}) :
63 Inherit(lookAt, copyop),
64 eye(lookAt.eye),
65 center(lookAt.center),
66 up(lookAt.up)
67 {
68 }
69
70 LookAt(const dvec3& in_eye, const dvec3& in_center, const dvec3& in_up) :
71 eye(in_eye),
72 center(in_center),
73 up(in_up)
74 {
75 dvec3 look = normalize(center - eye);
76 dvec3 side = normalize(cross(look, up));
77 up = normalize(cross(side, look));
78 }
79
80 LookAt& operator=(const LookAt& lookAt)
81 {
82 eye = lookAt.eye;
83 center = lookAt.center;
84 up = lookAt.up;
85 return *this;
86 }
87
88 ref_ptr<Object> clone(const CopyOp& copyop = {}) const override { return LookAt::create(*this, copyop); }
89
90 void transform(const dmat4& matrix);
91
92 void set(const dmat4& matrix);
93
94 dmat4 transform(const dvec3& offset = {}) const override;
95
96 void read(Input& input) override;
97 void write(Output& output) const override;
98
99 dvec3 eye;
100 dvec3 center;
101 dvec3 up;
102 };
103 VSG_type_name(vsg::LookAt);
104
106 class VSG_DECLSPEC LookDirection : public vsg::Inherit<ViewMatrix, LookDirection>
107 {
108 public:
109 LookDirection() :
110 position(0.0, 0.0, 0.0),
111 rotation()
112 {
113 }
114
115 LookDirection(const LookDirection& view, const CopyOp& copyop = {}) :
116 Inherit(view, copyop),
117 position(view.position),
118 rotation(view.rotation)
119 {
120 }
121
122 ref_ptr<Object> clone(const CopyOp& copyop = {}) const override { return LookDirection::create(*this, copyop); }
123
124 dvec3 position;
125 dquat rotation;
126
127 void set(const dmat4& matrix);
128
129 dmat4 transform(const dvec3& offset = {}) const override;
130 };
131 VSG_type_name(vsg::LookDirection);
132
134 class VSG_DECLSPEC RelativeViewMatrix : public Inherit<ViewMatrix, RelativeViewMatrix>
135 {
136 public:
137 RelativeViewMatrix(const dmat4& m, ref_ptr<ViewMatrix> vm) :
138 matrix(m),
139 viewMatrix(vm)
140 {
141 }
142
144 dmat4 transform(const dvec3& offset = {}) const override;
145
146 dmat4 matrix;
147 ref_ptr<ViewMatrix> viewMatrix;
148 };
149 VSG_type_name(vsg::RelativeViewMatrix);
150
154 class VSG_DECLSPEC TrackingViewMatrix : public Inherit<ViewMatrix, TrackingViewMatrix>
155 {
156 public:
157 template<typename T>
158 explicit TrackingViewMatrix(const dmat4& initial_matrix, const T& path) :
159 matrix(initial_matrix),
160 objectPath(path.begin(), path.end()) {}
161
162 template<typename T>
163 explicit TrackingViewMatrix(const T& path) :
164 objectPath(path.begin(), path.end()) {}
165
167 dmat4 transform(const dvec3& offset = {}) const override;
168 dmat4 inverse(const dvec3& offset = {}) const override;
169
170 dmat4 matrix;
171 RefObjectPath objectPath;
172 };
173 VSG_type_name(vsg::TrackingViewMatrix);
174
175} // 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:88
vsg::LookDirection::clone
ref_ptr< Object > clone(const CopyOp &copyop={}) const override
Definition ViewMatrix.h:122
vsg::RelativeViewMatrix
RelativeViewMatrix is a ViewMatrix that decorates another ViewMatrix and pre-multiplies its transform...
Definition ViewMatrix.h:135
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:37
vsg::ref_ptr
Definition ref_ptr.h:22