]> git.tdb.fi Git - libs/gl.git/blob - source/object.h
Drop Renderable::has_pass; renderables are now expected to ignore unknown passes
[libs/gl.git] / source / object.h
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_GL_OBJECT_H_
9 #define MSP_GL_OBJECT_H_
10
11 #include <vector>
12 #include <msp/datafile/collection.h>
13 #include "objectpass.h"
14 #include "renderable.h"
15
16 namespace Msp {
17 namespace GL {
18
19 class Material;
20 class Mesh;
21 class ObjectInstance;
22 class Technique;
23 class Texture;
24
25 /**
26 Stores data for a complete 3D object.  An Object must always have a Mesh, and
27 may also have a Technique to determine its appearance.  Textures and material
28 specified by the Technique may be overridden.  Simple textured objects are also
29 possible without a Technique.
30
31 It is possible to use a single Object for rendering multiple identical or
32 similar objects.  See class ObjectInstance.
33 */
34 class Object: public Renderable
35 {
36 private:
37         std::vector<const Mesh *> meshes;
38         const Technique *technique;
39         std::vector<const Texture *> textures;
40         const Texture *main_texture;
41         const Material *material;
42
43 public:
44         class Loader: public DataFile::Loader
45         {
46         public:
47                 typedef DataFile::Collection Collection;
48
49         protected:
50                 Object &obj;
51                 Collection &coll;
52         
53         public:
54                 Loader(Object &, Collection &);
55
56                 Object &get_object() const { return obj; }
57                 Collection &get_collection() const { return coll; }
58         private:
59                 virtual void finish();
60                 void lod_mesh(unsigned, const std::string &);
61                 void material_inline();
62                 void mesh(const std::string &);
63                 void shader_texture(const std::string &);
64                 void technique(const std::string &);
65                 void texture(const std::string &);
66         };
67
68         Object();
69         ~Object();
70
71         const Technique *get_technique() const { return technique; }
72
73         /**
74         Renders the object.  A tag can be provided to render a non-default pass.
75         */
76         virtual void render(const Tag &tag=Tag()) const;
77
78         /**
79         Renders the object with an instance.  The instance's hook functions are
80         called before and after drawing the mesh.  A tag may also be given to render
81         a non-default pass.
82         */
83         virtual void render(const ObjectInstance &, const Tag &tag=Tag()) const;
84
85         /**
86         Renders multiple instances of the object in one go.  This may improve
87         performance, as the object-specific render setup only has to be done once.
88         Each instance's hook functions will be called before and after drawing the
89         mesh.
90         */
91         template<typename Iter>
92         void render(Iter begin, Iter end, const Tag &tag=Tag()) const
93         {
94                 if(!can_render(tag))
95                         return;
96
97                 const ObjectPass *pass=get_pass(tag);
98                 setup_render(pass);
99                 for(Iter i=begin; i!=end; ++i)
100                         render_instance(**i, tag);
101                 finish_render(pass);
102         }
103 private:
104         bool can_render(const Tag &) const;
105         const ObjectPass *get_pass(const Tag &) const;
106         void setup_render(const ObjectPass *) const;
107         void finish_render(const ObjectPass *) const;
108         void render_instance(const ObjectInstance &, const Tag &) const;
109 };
110
111 } // namespace GL
112 } // namespace Msp
113
114 #endif