Go to the documentation of this file.
1 #pragma once
3 #include <memory>
4 #include <functional>
6 #include <QVector3D>
7 #include <QQuaternion>
8 #include <QMatrix4x4>
10 #include <resources/geometry.h>
11 #include <resources/material.h>
12 #include <resources/program.h>
14 class QOpenGLFunctions;
16 namespace terminus
17 {
19 class Camera;
20 class Program;
21 class World;
28 {
29 public:
35  virtual ~AbstractGraphicsObject();
45  virtual void update() final;
56  virtual void render(QOpenGLFunctions & gl) final;
69  virtual void bindCamera(Camera * camera);
75  virtual void unbindCamera(Camera * camera);
81  virtual void onBindCamera();
86  virtual void onUnbindCamera();
91  virtual void adjustCamera();
97  virtual void moveEvent(QVector3D movement);
103  virtual void rotateEvent(QVector2D rotation);
109  const QVector3D & minBB() const;
114  const QVector3D & maxBB() const;
120  QVector3D worldUp();
125  QVector3D worldFront();
130  QVector3D worldSide();
135  QVector3D position() const;
139  QQuaternion rotation() const;
143  QVector3D scale() const;
151  QMatrix4x4 modelMatrix() const;
157  QMatrix4x4 modelMatrixInverted() const;
159 protected:
163  virtual void localUpdate();
174  virtual void localRender(QOpenGLFunctions & gl) const;
183  virtual void localRenderSetup(QOpenGLFunctions & gl, Program & program) const;
192  virtual void localRenderCleanup(QOpenGLFunctions & gl, Program & program) const;
197  virtual bool localRenderEnabled() const;
205  virtual void doForAllChildren(std::function<void(AbstractGraphicsObject &)> callback);
211  void setPosition(const QVector3D & position);
216  void setRotation(const QQuaternion & rotation);
221  void setScale(const QVector3D & scale);
227  void setScale(float scale);
235  QVector3D worldToModel(const QVector3D & vWorld);
242  QVector3D modelToWorld(const QVector3D & vModel);
250  void dispose();
252 protected:
258  std::shared_ptr<std::unique_ptr<Program>> m_program;
259  std::shared_ptr<std::unique_ptr<Geometry>> m_geometry;
260  std::shared_ptr<std::unique_ptr<Material>> m_material;
262  QVector3D m_position;
263  QQuaternion m_rotation;
264  QVector3D m_scale;
265  mutable bool m_modelMatrixChanged;
266  mutable QMatrix4x4 m_modelMatrix;
268  mutable QMatrix4x4 m_modelMatrixInverted;
269 };
271 }
QVector3D worldFront()
Definition: abstractgraphicsobject.cpp:142
virtual void localRenderSetup(QOpenGLFunctions &gl, Program &program) const
override this method to customize the rendering process
Definition: abstractgraphicsobject.cpp:228
QMatrix4x4 modelMatrix() const
Definition: abstractgraphicsobject.cpp:165
const QVector3D & minBB() const
Definition: abstractgraphicsobject.cpp:117
QVector3D worldUp()
Definition: abstractgraphicsobject.cpp:137
void setScale(const QVector3D &scale)
sets the scale and invalidates the model matrices
Definition: abstractgraphicsobject.cpp:259
virtual void onBindCamera()
override this method to perform specific actions once a camera has been bound
Definition: abstractgraphicsobject.cpp:97
const QVector3D & maxBB() const
Definition: abstractgraphicsobject.cpp:127
virtual void localRenderCleanup(QOpenGLFunctions &gl, Program &program) const
override this method to customize the rendering process
Definition: abstractgraphicsobject.cpp:232
std::shared_ptr< std::unique_ptr< Material > > m_material
Definition: abstractgraphicsobject.h:260
virtual void doForAllChildren(std::function< void(AbstractGraphicsObject &)> callback)
override this method to make child objects known
Definition: abstractgraphicsobject.cpp:241
void dispose()
remove this object from the world and delete it as soon as it is safe to do so
Definition: abstractgraphicsobject.cpp:285
virtual void moveEvent(QVector3D movement)
override this method to react to move events received by a bound camera
Definition: abstractgraphicsobject.cpp:109
bool m_modelMatrixInvertedChanged
Definition: abstractgraphicsobject.h:267
bool m_modelMatrixChanged
Definition: abstractgraphicsobject.h:265
bool m_validState
Definition: abstractgraphicsobject.h:254
void setPosition(const QVector3D &position)
sets the position and invalidates the model matrices
Definition: abstractgraphicsobject.cpp:245
QVector3D worldSide()
Definition: abstractgraphicsobject.cpp:147
virtual void rotateEvent(QVector2D rotation)
override this method to react to rotate events received by a bound camera
Definition: abstractgraphicsobject.cpp:113
The AbstractGraphicsObject class is the root of a class hierarchy of objects that contain independen ...
Definition: abstractgraphicsobject.h:27
virtual void bindCamera(Camera *camera)
binds a camera to track this object
Definition: abstractgraphicsobject.cpp:86
virtual void render(QOpenGLFunctions &gl) final
render object hierarchy beginning at this object
Definition: abstractgraphicsobject.cpp:53
virtual bool localRenderEnabled() const
override this method to disable or enable rendering conditionally
Definition: abstractgraphicsobject.cpp:236
QVector3D m_scale
Definition: abstractgraphicsobject.h:264
QMatrix4x4 modelMatrixInverted() const
Definition: abstractgraphicsobject.cpp:178
virtual void localRender(QOpenGLFunctions &gl) const
this method contains the default render code using m_geometry, m_program and m_material ...
Definition: abstractgraphicsobject.cpp:192
std::shared_ptr< std::unique_ptr< Geometry > > m_geometry
Definition: abstractgraphicsobject.h:259
QVector3D modelToWorld(const QVector3D &vModel)
Definition: abstractgraphicsobject.cpp:279
The program class implements the basic functionality used for binding programs to openGL...
Definition: program.h:18
virtual void unbindCamera(Camera *camera)
release a camera so that it wont track this object any longer
Definition: abstractgraphicsobject.cpp:72
The World class represents a running game instance and contains all graphical (3D) elements of the sc...
Definition: world.h:46
virtual void onUnbindCamera()
override this method to perform specific actions once a camera has been released
Definition: abstractgraphicsobject.cpp:101
void setRotation(const QQuaternion &rotation)
sets the rotation and invalidates the model matrices
Definition: abstractgraphicsobject.cpp:252
virtual void update() final
update object hierarchy beginning at this object
Definition: abstractgraphicsobject.cpp:32
virtual void localUpdate()
override this method to update this objects state
Definition: abstractgraphicsobject.cpp:188
QMatrix4x4 m_modelMatrix
Definition: abstractgraphicsobject.h:266
QVector3D m_position
Definition: abstractgraphicsobject.h:262
std::shared_ptr< std::unique_ptr< Program > > m_program
Definition: abstractgraphicsobject.h:258
virtual void adjustCamera()
override this method to adjust the parameters of a bound camera according to this objects own state ...
Definition: abstractgraphicsobject.cpp:105
QMatrix4x4 m_modelMatrixInverted
Definition: abstractgraphicsobject.h:268
Definition: eventhandler.cpp:18
World & m_world
Definition: abstractgraphicsobject.h:253
The Camera class manages view and projection matrices that define a view of a 3D scene.
Definition: camera.h:26
QQuaternion rotation() const
Definition: abstractgraphicsobject.cpp:156
QVector3D worldToModel(const QVector3D &vWorld)
Definition: abstractgraphicsobject.cpp:273
AbstractGraphicsObject(World &world)
Definition: abstractgraphicsobject.cpp:16
virtual ~AbstractGraphicsObject()
Definition: abstractgraphicsobject.cpp:28
Camera * m_camera
Definition: abstractgraphicsobject.h:256
QQuaternion m_rotation
Definition: abstractgraphicsobject.h:263
QVector3D scale() const
Definition: abstractgraphicsobject.cpp:160
QVector3D position() const
Definition: abstractgraphicsobject.cpp:152