From: Mikko Rasa Date: Sun, 4 Nov 2007 18:05:19 +0000 (+0000) Subject: Add class Renderable X-Git-Tag: 0.9~23 X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=4324df6e3d807d35e02e828320e436c509275520 Add class Renderable Inherit Object and ObjectInstance from Renderable Add class Scene Include GL/gl.h in renderable.h --- diff --git a/source/framebuffer.h b/source/framebuffer.h index 0dd8f5d7..8563d2f9 100644 --- a/source/framebuffer.h +++ b/source/framebuffer.h @@ -8,6 +8,7 @@ Distributed under the LGPL #ifndef MSP_GL_FRAMEBUFFER_H_ #define MSP_GL_FRAMEBUFFER_H_ +#include #include "types.h" namespace Msp { diff --git a/source/object.cpp b/source/object.cpp index e406755c..dba9774e 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -48,14 +48,24 @@ const ObjectPass &Object::get_pass(const string &pn) const return i->second; } -void Object::render(const ObjectInstance *inst) const +void Object::render() const { - render(*normal_pass, inst); + render(*normal_pass, 0); } -void Object::render(const string &pn, const ObjectInstance *inst) const +void Object::render(const ObjectInstance &inst) const { - render(get_pass(pn), inst); + render(*normal_pass, &inst); +} + +void Object::render(const string &pn) const +{ + render(get_pass(pn), 0); +} + +void Object::render(const string &pn, const ObjectInstance &inst) const +{ + render(get_pass(pn), &inst); } void Object::render(const list &insts) const diff --git a/source/object.h b/source/object.h index e38afb5c..f0940652 100644 --- a/source/object.h +++ b/source/object.h @@ -11,6 +11,7 @@ Distributed under the LGPL #include #include #include "objectpass.h" +#include "renderable.h" namespace Msp { namespace GL { @@ -29,7 +30,7 @@ are optional. See also class ObjectInstance. */ -class Object +class Object: public Renderable { private: std::vector meshes; @@ -68,15 +69,17 @@ public: Object(); ~Object(); - bool has_pass(const std::string &) const; + virtual bool has_pass(const std::string &) const; const ObjectPass &get_pass(const std::string &) const; /** Renders the object. If an ObjectInstance is provided, its hook functions are called. */ - void render(const ObjectInstance * =0) const; - void render(const std::string &, const ObjectInstance *) const; + virtual void render() const; + virtual void render(const ObjectInstance &) const; + virtual void render(const std::string &) const; + virtual void render(const std::string &, const ObjectInstance &) const; /** Renders multiple instances of the object in one go. This may be a diff --git a/source/objectinstance.cpp b/source/objectinstance.cpp index 78042bd1..10dae392 100644 --- a/source/objectinstance.cpp +++ b/source/objectinstance.cpp @@ -9,6 +9,8 @@ Distributed under the LGPL #include "objectinstance.h" #include "programdata.h" +using namespace std; + namespace Msp { namespace GL { @@ -16,14 +18,19 @@ ObjectInstance::ObjectInstance(const Object &obj): object(obj) { } +bool ObjectInstance::has_pass(const string &pn) const +{ + return object.has_pass(pn); +} + void ObjectInstance::render() const { - object.render(this); + object.render(*this); } -void ObjectInstance::render(const std::string &pn) const +void ObjectInstance::render(const string &pn) const { - object.render(pn, this); + object.render(pn, *this); } } // namespace GL diff --git a/source/objectinstance.h b/source/objectinstance.h index 151e316c..459c2da3 100644 --- a/source/objectinstance.h +++ b/source/objectinstance.h @@ -9,6 +9,7 @@ Distributed under the LGPL #define MSP_GL_OBJETCINSTANCE_H_ #include +#include "renderable.h" namespace Msp { namespace GL { @@ -23,7 +24,7 @@ Represents a single instance of an Object. An application can derive another class from this and overload the hook functions to specify location and other instance-specific parameters for the rendered objects. */ -class ObjectInstance +class ObjectInstance: public Renderable { protected: const Object &object; @@ -33,8 +34,10 @@ public: const Object &get_object() const { return object; } - void render() const; - void render(const std::string &) const; + virtual bool has_pass(const std::string &) const; + + virtual void render() const; + virtual void render(const std::string &) const; /** Hook function, called from Object just before rendering the mesh. diff --git a/source/renderable.h b/source/renderable.h new file mode 100644 index 00000000..b9b6b080 --- /dev/null +++ b/source/renderable.h @@ -0,0 +1,28 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#ifndef MSP_GL_RENDERABLE_H_ +#define MSP_GL_RENDERABLE_H_ + +#include + +namespace Msp { +namespace GL { + +class Renderable +{ +public: + virtual bool has_pass(const std::string &pn) const =0; + + virtual void render() const =0; + virtual void render(const std::string &pn) const =0; +}; + +} // namespace Msp +} // namespace GL + +#endif diff --git a/source/scene.cpp b/source/scene.cpp new file mode 100644 index 00000000..2ef2ea78 --- /dev/null +++ b/source/scene.cpp @@ -0,0 +1,39 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include "scene.h" + +using namespace std; + +namespace Msp { +namespace GL { + +void Scene::add(const Renderable &r) +{ + renderables.insert(&r); +} + +void Scene::remove(const Renderable &r) +{ + renderables.erase(&r); +} + +void Scene::render() const +{ + for(set::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) + (*i)->render(); +} + +void Scene::render(const string &pn) const +{ + for(set::const_iterator i=renderables.begin(); i!=renderables.end(); ++i) + if((*i)->has_pass(pn)) + (*i)->render(pn); +} + +} // namespace GL +} // namespace Msp diff --git a/source/scene.h b/source/scene.h new file mode 100644 index 00000000..f4e12810 --- /dev/null +++ b/source/scene.h @@ -0,0 +1,34 @@ +/* $Id$ + +This file is part of libmspgl +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#ifndef MSP_GL_SCENE_H_ +#define MSP_GL_SCENE_H_ + +#include +#include "renderable.h" + +namespace Msp { +namespace GL { + +class Scene: public Renderable +{ +private: + std::set renderables; + +public: + void add(const Renderable &); + void remove(const Renderable &); + + virtual bool has_pass(const std::string &) const { return true; } + virtual void render() const; + virtual void render(const std::string &) const; +}; + +} // namespace GL +} // namespace Msp + +#endif