]> git.tdb.fi Git - libs/gl.git/blobdiff - source/object.h
Fix a stale pointer issue with Animation
[libs/gl.git] / source / object.h
index f0940652992692bfce462258cf0d4a72e58b48ec..c8e372601077c1b1e9c43dd82a221b53b1a904a7 100644 (file)
@@ -1,17 +1,11 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #ifndef MSP_GL_OBJECT_H_
 #define MSP_GL_OBJECT_H_
 
 #include <vector>
-#include <msp/datafile/collection.h>
-#include "objectpass.h"
+#include "bindable.h"
 #include "renderable.h"
+#include "renderpass.h"
+#include "resourceobserver.h"
 
 namespace Msp {
 namespace GL {
@@ -19,80 +13,115 @@ namespace GL {
 class Material;
 class Mesh;
 class ObjectInstance;
-class Program;
-class ProgramData;
+class Technique;
 class Texture;
 
 /**
-Stores data for a complete 3D object.  This includes a mesh, a shader and data
-for it and a number of textures.  Only the mesh is mandatory, other components
-are optional.
+Combines a Mesh with a Technique to give it an appearance.  The Technique will
+define which render passes the Object supports.
 
-See also class ObjectInstance.
+In many cases, it's desirable to include multiple copies of an Object in a
+Scene, with different model matrices.  ObjectInstances can be used to alter the
+rendering of an object on a per-instance basis.
+
+Objects can have multiple levels of detail.  The most detailed level has index
+0, with increasing indices having less detail.  When rendering an instance, the
+instance's get_level_of_detail method is called to determine which LoD to use.
 */
-class Object: public Renderable
+class Object: public Renderable, private ResourceObserver
 {
 private:
-       std::vector<Mesh *> meshes;
-       std::vector<Texture *> textures;
-       std::map<std::string, ObjectPass> passes;
-       Material *material;
-       ObjectPass *normal_pass;
+       struct LevelOfDetail;
 
-public:
-       class Loader: public DataFile::Loader
+       class LodLoader: public DataFile::CollectionObjectLoader<Object>
        {
+       private:
+               unsigned index;
+               LevelOfDetail &lod;
+
        public:
-               typedef DataFile::Collection Collection;
+               LodLoader(Object &, unsigned, Collection *);
 
-       protected:
-               Object &obj;
-               Collection &coll;
        private:
-               std::vector<std::string> textures;
-       
+               void mesh(const std::string &);
+               void mesh_inline();
+               void technique(const std::string &);
+               void technique_inline();
+       };
+
+public:
+       class Loader: public LodLoader
+       {
        public:
+               Loader(Object &);
                Loader(Object &, Collection &);
-               ~Loader();
-
-               Object &get_object() const { return obj; }
-               Collection &get_collection() const { return coll; }
        private:
-               void lod_mesh(unsigned, const std::string &);
-               void material_inline();
-               void mesh(const std::string &);
-               void pass(const std::string &);
-               void shader(const std::string &);
-               void texture(const std::string &);
+               void init();
+               virtual void finish();
+
+               void bounding_sphere_hint(float, float, float, float);
+               void level_of_detail(unsigned);
+       };
+
+private:
+       struct LevelOfDetail
+       {
+               RefPtr<const Mesh> mesh;
+               RefPtr<const Technique> technique;
        };
 
+       std::vector<LevelOfDetail> lods;
+       Geometry::BoundingSphere<float, 3> bounding_sphere;
+       bool lod0_watched;
+
+public:
        Object();
+       Object(const Mesh *, const Technique *);
        ~Object();
 
-       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.
-       */
-       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
-       performance improvement, as the object-specific render setup only has to be
-       done once.
-       */
-       void render(const std::list<const ObjectInstance *> &) const;
-       void render(const std::string &, const std::list<const ObjectInstance *> &) const;
 private:
-       void setup_render(const ObjectPass &) const;
-       void finish_render(const ObjectPass &) const;
-       void render(const ObjectPass &, const ObjectInstance *) const;
-       void render(const ObjectPass &, const std::list<const ObjectInstance *> &) const;
+       LevelOfDetail &get_lod(unsigned, const char *);
+
+public:
+       /** Sets the mesh for the highest level of detail (index 0). */
+       void set_mesh(const Mesh *m) { set_mesh(0, m); }
+
+       /** Sets the mesh for a given level of detail.  Previous LoDs must have been
+       defined. */
+       void set_mesh(unsigned, const Mesh *);
+
+private:
+       void update_bounding_sphere();
+public:
+       const Mesh *get_mesh(unsigned = 0) const;
+
+       /** Sets the technique for the highest level of detail (index 0). */
+       void set_technique(const Technique *t) { set_technique(0, t); }
+
+       /** Sets the technique for a given level of detail.  Previous LoDs must have
+       been defined. */
+       void set_technique(unsigned, const Technique *);
+
+       const Technique *get_technique(unsigned = 0) const;
+       unsigned get_n_lods() const { return lods.size(); }
+
+       virtual const Geometry::BoundingSphere<float, 3> *get_bounding_sphere() const { return &bounding_sphere; }
+
+       virtual void render(Renderer &, const Tag & = Tag()) const;
+
+       /** Renders an instance of the object.  The instance's hook functions are
+       called before and after drawing the mesh. */
+       virtual void render(Renderer &, const ObjectInstance &, const Tag & = Tag()) const;
+
+protected:
+       virtual void setup_render(Renderer &, const Tag &) const { }
+       virtual void finish_render(Renderer &, const Tag &) const { }
+
+private:
+       const RenderPass *get_pass(const Tag &, unsigned) const;
+
+       virtual void resource_loaded(Resource &);
+       virtual void resource_removed(Resource &);
 };
 
 } // namespace GL