Browse Source

Moved drawing functions from Scene to Camera.

Also removed non-trivial orthographic projection and clear color
defaults, addded GL_STENCIL_BUFFER_BIT to glClear().
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
e98ca5b03a
  1. 30
      src/Camera.cpp
  2. 36
      src/Camera.h
  3. 1
      src/Object.cpp
  4. 27
      src/Scene.cpp
  5. 22
      src/Scene.h

30
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<Object*>::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);
}
}
}

36
src/Camera.h

@ -46,7 +46,9 @@ class MAGNUM_EXPORT Camera: public Object {
* @brief Constructor
* @param parent Parent object
*
* Calls <tt>setOrthographic(2, 1, 1000)</tt>.
* 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<unsigned int> _viewport;

1
src/Object.cpp

@ -15,6 +15,7 @@
#include "Object.h"
#include "Scene.h"
#include "Camera.h"
using namespace std;

27
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<Object*>::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);
}
}
}

22
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);
};
}

Loading…
Cancel
Save