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

vsg 1.1.8 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
34 virtual dmat4 transform() const = 0;
35
36 virtual dmat4 inverse() const
37 {
38 return vsg::inverse(transform());
39 }
40 };
41 VSG_type_name(vsg::ViewMatrix);
42
44 class VSG_DECLSPEC LookAt : public Inherit<ViewMatrix, LookAt>
45 {
46 public:
47 LookAt() :
48 eye(0.0, 0.0, 0.0),
49 center(0.0, 1.0, 0.0),
50 up(0.0, 0.0, 1.0)
51 {
52 }
53
54 LookAt(const LookAt& lookAt, const CopyOp& copyop = {}) :
55 Inherit(lookAt, copyop),
56 eye(lookAt.eye),
57 center(lookAt.center),
58 up(lookAt.up)
59 {
60 }
61
62 LookAt(const dvec3& in_eye, const dvec3& in_center, const dvec3& in_up) :
63 eye(in_eye),
64 center(in_center),
65 up(in_up)
66 {
67 dvec3 look = normalize(center - eye);
68 dvec3 side = normalize(cross(look, up));
69 up = normalize(cross(side, look));
70 }
71
72 LookAt& operator=(const LookAt& lookAt)
73 {
74 eye = lookAt.eye;
75 center = lookAt.center;
76 up = lookAt.up;
77 return *this;
78 }
79
80 ref_ptr<Object> clone(const CopyOp& copyop = {}) const override { return LookAt::create(*this, copyop); }
81
82 void transform(const dmat4& matrix)
83 {
84 up = normalize(matrix * (eye + up) - matrix * eye);
85 center = matrix * center;
86 eye = matrix * eye;
87 }
88
89 void set(const dmat4& matrix)
90 {
91 up = normalize(matrix * (dvec3(0.0, 0.0, 0.0) + dvec3(0.0, 1.0, 0.0)) - matrix * dvec3(0.0, 0.0, 0.0));
92 center = matrix * dvec3(0.0, 0.0, -1.0);
93 eye = matrix * dvec3(0.0, 0.0, 0.0);
94 }
95
96 dmat4 transform() const override { return lookAt(eye, center, up); }
97
98 void read(Input& input) override;
99 void write(Output& output) const override;
100
101 dvec3 eye;
102 dvec3 center;
103 dvec3 up;
104 };
105 VSG_type_name(vsg::LookAt);
106
108 class RelativeViewMatrix : public Inherit<ViewMatrix, RelativeViewMatrix>
109 {
110 public:
111 RelativeViewMatrix(const dmat4& m, ref_ptr<ViewMatrix> vm) :
112 matrix(m),
113 viewMatrix(vm)
114 {
115 }
116
118 dmat4 transform() const override
119 {
120 return matrix * viewMatrix->transform();
121 }
122
123 dmat4 matrix;
124 ref_ptr<ViewMatrix> viewMatrix;
125 };
126 VSG_type_name(vsg::RelativeViewMatrix);
127
131 class VSG_DECLSPEC TrackingViewMatrix : public Inherit<ViewMatrix, TrackingViewMatrix>
132 {
133 public:
134 template<typename T>
135 explicit TrackingViewMatrix(const dmat4& initial_matrix, const T& path) :
136 matrix(initial_matrix),
137 objectPath(path.begin(), path.end()) {}
138
139 template<typename T>
140 explicit TrackingViewMatrix(const T& path) :
141 objectPath(path.begin(), path.end()) {}
142
144 dmat4 transform() const override;
145 dmat4 inverse() const override;
146
147 dmat4 matrix;
148 RefObjectPath objectPath;
149 };
150 VSG_type_name(vsg::TrackingViewMatrix);
151
152} // namespace vsg
vsg::CopyOp
Definition Object.h:42
vsg::Inherit
Definition Inherit.h:28
vsg::LookAt
LookAt is a ViewMatrix that implements the gluLookAt model for specifying the view matrix.
Definition ViewMatrix.h:45
vsg::LookAt::clone
ref_ptr< Object > clone(const CopyOp &copyop={}) const override
Definition ViewMatrix.h:80
vsg::RelativeViewMatrix
RelativeViewMatrix is a ViewMatrix that decorates another ViewMatrix and pre-multiplies its transform...
Definition ViewMatrix.h:109
vsg::RelativeViewMatrix::transform
dmat4 transform() const override
returns matrix * viewMatrix->transform()
Definition ViewMatrix.h:118
vsg::TrackingViewMatrix
Definition ViewMatrix.h:132
vsg::TrackingViewMatrix::transform
dmat4 transform() const override
returns matrix * computeTransfrom(objectPath)
vsg::ViewMatrix
ViewMatrix is a base class for specifying the Camera view matrix and its inverse.
Definition ViewMatrix.h:23
vsg::ref_ptr
Definition ref_ptr.h:22
vsg::t_mat4< double >
vsg::t_vec3< double >