VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
Camera.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CAMERA_H
18 #define CAMERA_H
19 
20 #include <QMatrix4x4>
21 #include <QPointF>
22 #include <QQuickItem>
23 #include <QSGNode>
24 #include <memory>
25 
42 class Camera : public QQuickItem {
43  Q_OBJECT
44 
53  Q_PROPERTY(float fov READ getFov WRITE setFov NOTIFY fovChanged)
59  Q_PROPERTY(QPointF lookAt READ getLookAt WRITE setLookAt NOTIFY lookAtChanged)
63  Q_PROPERTY(float aspectRatio READ getAspectRatio)
64 
65 public:
70  explicit Camera(QQuickItem* parent = 0);
71  virtual ~Camera();
72 
76  float getFov() const { return mFov; }
81  void setFov(float fov);
85  QPointF getLookAt() const { return mLookAt; }
90  void setLookAt(const QPointF& lookAt);
94  float getAspectRatio() const;
95 
101  QPointF getParallaxOrigin() const { return mParallaxOrigin; }
108  void setParallaxOrigin(const QPointF& value);
114  QPointF getParallaxOffset() const { return mParallaxOffset; }
119  const QMatrix4x4& getOpenGLViewMatrix() const { return mAppliedGlViewMatrix; }
125  const QRectF& getViewportWorldBounds() const { return mViewportWorldBounds; }
132  const QRectF& getWorldCullBounds() const { return mWorldCullBounds; }
133 
138  void updateTransform();
143  void applyTransform();
144 
145 signals:
149  void fovChanged();
153  void lookAtChanged();
158  void transformApplied();
159 
160 protected:
173  virtual QSGNode* updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData* data) override;
174 
175 private:
176  QPointF toWorldSpace(const QPointF& point) const;
177  void updateOpenGLViewMatrix();
178 
179  float mFov = 1.0f;
180  QPointF mLookAt = QPointF(0.0f, 0.0f);
181  float mTransformScale = 1.0f;
182  QPointF mTransformTranslate;
183  QPointF mParallaxOrigin;
184  QPointF mParallaxOffset;
185  QRectF mViewportWorldBounds;
186  QRectF mWorldCullBounds;
187  QMatrix4x4 mGlViewMatrix;
188  QMatrix4x4 mAppliedGlViewMatrix;
189  std::unique_ptr<QSGNode> mPaintNode;
190 
191  // The distance, in world coordinates, from the edges of the camera that objects should be
192  // culled.
193  constexpr static const float VIEWPORT_CULL_PADDING = 0.5f;
194 };
195 Q_DECLARE_METATYPE(Camera*)
196 
197 #endif // CAMERA_H
float getFov() const
Returns fov.
Definition: Camera.h:76
void fovChanged()
Emitted when fov changes.
QPointF getLookAt() const
Returns lookAt.
Definition: Camera.h:85
float aspectRatio
Read only property containing the aspect ratio of the viewport.
Definition: Camera.h:63
void updateTransform()
Computes new transforms, but does not move Items visually, or update the GL transform.
QPointF getParallaxOrigin() const
Returns the current parallax origin.
Definition: Camera.h:101
void applyTransform()
Copies the computed transforms, updating Items visually and updates the GL transform.
QPointF getParallaxOffset() const
Returns the Camera's offset from the parallax origin.
Definition: Camera.h:114
const QMatrix4x4 & getOpenGLViewMatrix() const
Returns the matrix transform used to project world coordinates into normalized device coordinates...
Definition: Camera.h:119
QPointF lookAt
Point in world coordinates that the Camera is centered around.
Definition: Camera.h:59
void lookAtChanged()
Emitted when lookAt changes.
void setFov(float fov)
Sets fov.
void transformApplied()
Signals the current frame's update is complete and that the camera's transform now points at its new ...
const QRectF & getViewportWorldBounds() const
Get the viewport rectangle in world space coordinates.
Definition: Camera.h:125
const QRectF & getWorldCullBounds() const
Gets the world space rectangle encompassing the viewport, outside of which objects should be culled...
Definition: Camera.h:132
virtual QSGNode * updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data) override
Override of QQuickItem::updatePaintNode() which forces Qt's renderer to continue rendering.
void setLookAt(const QPointF &lookAt)
Sets lookAt.
QQuickItem which represents the Camera into the game world.
Definition: Camera.h:42
QObject * parent() const
float getAspectRatio() const
Returns aspectRatio.
float fov
Zoom level of the Camera, as a field of view.
Definition: Camera.h:53
void setParallaxOrigin(const QPointF &value)
Sets the current parallax origin.