From e98ca5b03a1314255ac353e2f1c57b536e6d7dc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 20 Feb 2012 13:19:09 +0100 Subject: [PATCH] Moved drawing functions from Scene to Camera. Also removed non-trivial orthographic projection and clear color defaults, addded GL_STENCIL_BUFFER_BIT to glClear(). --- src/Camera.cpp | 30 +++++++++++++++++++++++++++--- src/Camera.h | 36 +++++++++++++++++++++++++++++++++--- src/Object.cpp | 1 + src/Scene.cpp | 27 --------------------------- src/Scene.h | 22 +--------------------- 5 files changed, 62 insertions(+), 54 deletions(-) diff --git a/src/Camera.cpp b/src/Camera.cpp index d3df1d543..72c97bd31 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -16,11 +16,11 @@ #include "Camera.h" #include "Scene.h" +using namespace std; + namespace Magnum { -Camera::Camera(Object* parent): Object(parent), _aspectRatioPolicy(Extend) { - setOrthographic(2, 1, 1000); -} +Camera::Camera(Object* parent): Object(parent), _aspectRatioPolicy(Extend) {} void Camera::setOrthographic(GLfloat size, GLfloat near, GLfloat far) { _near = near; @@ -100,4 +100,28 @@ void Camera::fixAspectRatio() { } else _projectionMatrix = rawProjectionMatrix; } +void Camera::setClearColor(const Magnum::Vector4& color) { + glClearColor(color.r(), color.g(), color.b(), color.a()); + _clearColor = color; +} + +void Camera::draw() { + /** @todo Clear only set features */ + glClear(GL_COLOR_BUFFER_BIT|GL_STENCIL_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + + /* Recursively draw child objects */ + drawChildren(scene(), cameraMatrix()); +} + +void Camera::drawChildren(Object* object, const Matrix4& transformationMatrix) { + for(set::const_iterator it = object->children().begin(); it != object->children().end(); ++it) { + /* Transformation matrix for the object */ + Matrix4 matrix = transformationMatrix*(*it)->transformation(); + + /* Draw the object and its children */ + (*it)->draw(matrix, this); + drawChildren(*it, matrix); + } +} + } diff --git a/src/Camera.h b/src/Camera.h index a1bfb664b..5a9713777 100644 --- a/src/Camera.h +++ b/src/Camera.h @@ -46,7 +46,9 @@ class MAGNUM_EXPORT Camera: public Object { * @brief Constructor * @param parent Parent object * - * Calls setOrthographic(2, 1, 1000). + * Sets orthographic projection to the default OpenGL cube (range + * @f[ [-1; 1] @f] in all directions) and clear color to black. + * @see setOrthographic(), setClearColor() */ Camera(Object* parent = nullptr); @@ -62,8 +64,8 @@ class MAGNUM_EXPORT Camera: public Object { * @param near Near clipping plane * @param far Far clipping plane * - * The volume of given size will be scaled down to range (-1, 1) on all - * directions. + * The volume of given size will be scaled down to range + * @f[ [-1; 1] @f] on all directions. */ void setOrthographic(GLfloat size, GLfloat near, GLfloat far); @@ -116,15 +118,43 @@ class MAGNUM_EXPORT Camera: public Object { setViewport({width, height}); } + /** @brief Clear color */ + inline Vector4 clearColor() const { return _clearColor; } + + /** @brief Set clear color */ + void setClearColor(const Vector4& color); + + /** @copydoc setClearColor(const Vector4&) */ + inline void setClearColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a) { + setClearColor(Vector4(r, g, b, a)); + } + + /** + * @brief Draw the scene + * + * Clears the color, depth and stencil buffer and draws the scene + * using drawChildren(). + */ + virtual void draw(); + /** * Recalculates camera matrix. */ void setClean(); + protected: + /** + * @brief Draw object children + * + * Recursively draws all children of the object. + */ + void drawChildren(Object* object, const Matrix4& transformationMatrix); + private: Matrix4 rawProjectionMatrix; Matrix4 _projectionMatrix; Matrix4 _cameraMatrix; + Vector4 _clearColor; GLfloat _near, _far; Math::Vector2 _viewport; diff --git a/src/Object.cpp b/src/Object.cpp index d525489f1..a61097538 100644 --- a/src/Object.cpp +++ b/src/Object.cpp @@ -15,6 +15,7 @@ #include "Object.h" #include "Scene.h" +#include "Camera.h" using namespace std; diff --git a/src/Scene.cpp b/src/Scene.cpp index 9b47a6d28..67b4eea78 100644 --- a/src/Scene.cpp +++ b/src/Scene.cpp @@ -15,13 +15,10 @@ #include "Scene.h" -using namespace std; - namespace Magnum { Scene::Scene(): Object(nullptr), _features(0) { _parent = this; - setClearColor(0.1f, 0.1f, 0.1f, 1.0f); /* Bind default VAO */ glGenVertexArrays(1, &vao); @@ -40,28 +37,4 @@ void Scene::setFeature(Scene::Feature feature, bool enabled) { enabled ? glEnable(_feature) : glDisable(_feature); } -void Scene::setClearColor(const Magnum::Vector4& color) { - glClearColor(color.r(), color.g(), color.b(), color.a()); - _clearColor = color; -} - -void Scene::draw(Camera* camera) { - /** @todo Clear only set features */ - glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); - - /* Recursively draw child objects */ - drawChildren(this, camera->cameraMatrix(), camera); -} - -void Scene::drawChildren(Object* object, const Matrix4& transformationMatrix, Camera* camera) { - for(set::const_iterator it = object->children().begin(); it != object->children().end(); ++it) { - /* Transformation matrix for the object */ - Matrix4 matrix = transformationMatrix*(*it)->transformation(); - - /* Draw the object and its children */ - (*it)->draw(matrix, camera); - drawChildren(*it, matrix, camera); - } -} - } diff --git a/src/Scene.h b/src/Scene.h index 281a98be5..24b575fd4 100644 --- a/src/Scene.h +++ b/src/Scene.h @@ -19,7 +19,7 @@ * @brief Class Magnum::Scene */ -#include "Camera.h" +#include "Object.h" namespace Magnum { @@ -59,37 +59,17 @@ class MAGNUM_EXPORT Scene: public Object { */ inline ~Scene() { glDeleteVertexArrays(1, &vao); } - /** @brief Clear color */ - inline Vector4 clearColor() const { return _clearColor; } - /** @brief Which features are set */ inline unsigned int features() const { return _features; } /** @brief Set feature */ void setFeature(Feature feature, bool enabled); - /** @brief Set clear color */ - void setClearColor(const Vector4& color); - - /** @brief Set clear color */ - inline void setClearColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a) { - setClearColor(Vector4(r, g, b, a)); - } - - /** - * @brief Draw whole scene - * - * Recursively draws all child objects with given camera. - */ - virtual void draw(Camera* camera); - private: unsigned int _features; - Vector4 _clearColor; GLuint vao; inline virtual void draw(const Magnum::Matrix4& transformationMatrix, Camera* camera) {} - void drawChildren(Object* object, const Matrix4& transformationMatrix, Camera* camera); }; }