vsg  1.1.0
VulkanSceneGraph library
ViewMatrix.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 Robert Osfield
6 
7 Permission 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 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE 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 
18 namespace vsg
19 {
20 
22  class VSG_DECLSPEC ViewMatrix : public Inherit<Object, ViewMatrix>
23  {
24  public:
25  virtual dmat4 transform() const = 0;
26 
27  virtual dmat4 inverse() const
28  {
29  return vsg::inverse(transform());
30  }
31  };
32  VSG_type_name(vsg::ViewMatrix);
33 
35  class VSG_DECLSPEC LookAt : public Inherit<ViewMatrix, LookAt>
36  {
37  public:
38  LookAt() :
39  eye(0.0, 0.0, 0.0),
40  center(0.0, 1.0, 0.0),
41  up(0.0, 0.0, 1.0)
42  {
43  }
44 
45  LookAt(const LookAt& lookAt) :
46  Inherit(lookAt),
47  eye(lookAt.eye),
48  center(lookAt.center),
49  up(lookAt.up)
50  {
51  }
52 
53  LookAt(const dvec3& in_eye, const dvec3& in_center, const dvec3& in_up) :
54  eye(in_eye),
55  center(in_center),
56  up(in_up)
57  {
58  dvec3 look = normalize(center - eye);
59  dvec3 side = normalize(cross(look, up));
60  up = normalize(cross(side, look));
61  }
62 
63  LookAt& operator=(const LookAt& lookAt)
64  {
65  eye = lookAt.eye;
66  center = lookAt.center;
67  up = lookAt.up;
68  return *this;
69  }
70 
71  void transform(const dmat4& matrix)
72  {
73  up = normalize(matrix * (eye + up) - matrix * eye);
74  center = matrix * center;
75  eye = matrix * eye;
76  }
77 
78  void set(const dmat4& matrix)
79  {
80  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));
81  center = matrix * dvec3(0.0, 0.0, -1.0);
82  eye = matrix * dvec3(0.0, 0.0, 0.0);
83  }
84 
85  dmat4 transform() const override { return lookAt(eye, center, up); }
86 
87  void read(Input& input) override;
88  void write(Output& output) const override;
89 
90  dvec3 eye;
91  dvec3 center;
92  dvec3 up;
93  };
94  VSG_type_name(vsg::LookAt);
95 
97  class RelativeViewMatrix : public Inherit<ViewMatrix, RelativeViewMatrix>
98  {
99  public:
101  matrix(m),
102  viewMatrix(vm)
103  {
104  }
105 
107  dmat4 transform() const override
108  {
109  return matrix * viewMatrix->transform();
110  }
111 
112  dmat4 matrix;
113  ref_ptr<ViewMatrix> viewMatrix;
114  };
115  VSG_type_name(vsg::RelativeViewMatrix);
116 
120  class VSG_DECLSPEC TrackingViewMatrix : public Inherit<ViewMatrix, TrackingViewMatrix>
121  {
122  public:
123  template<typename T>
124  explicit TrackingViewMatrix(const dmat4& initial_matrix, const T& path) :
125  matrix(initial_matrix),
126  objectPath(path.begin(), path.end()) {}
127 
128  template<typename T>
129  explicit TrackingViewMatrix(const T& path) :
130  objectPath(path.begin(), path.end()) {}
131 
133  dmat4 transform() const override;
134  dmat4 inverse() const override;
135 
136  dmat4 matrix;
137  RefObjectPath objectPath;
138  };
139  VSG_type_name(vsg::TrackingViewMatrix);
140 
141 } // namespace vsg
Definition: Inherit.h:28
Definition: Input.h:44
LookAt is a ViewMatrix that implements the gluLookAt model for specifying the view matrix.
Definition: ViewMatrix.h:36
Definition: Output.h:41
RelativeViewMatrix is a ViewMatrix that decorates another ViewMatrix and pre-multiplies its transform...
Definition: ViewMatrix.h:98
dmat4 transform() const override
returns matrix * viewMatrix->transform()
Definition: ViewMatrix.h:107
Definition: ViewMatrix.h:121
dmat4 transform() const override
returns matrix * computeTransfrom(objectPath)
ViewMatrix is a base class for specifying the Camera view matrix and its inverse.
Definition: ViewMatrix.h:23
Definition: ref_ptr.h:22