]> git.tdb.fi Git - libs/gl.git/blob - source/object.h
Inherit Loaders from the ObjectLoader classes
[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 "objectpass.h"
13 #include "renderable.h"
14
15 namespace Msp {
16 namespace GL {
17
18 class Material;
19 class Mesh;
20 class ObjectInstance;
21 class Technique;
22 class Texture;
23
24 /**
25 Stores data for a complete 3D object.  An Object must always have a Mesh, and
26 may also have a Technique to determine its appearance.  Textures and material
27 specified by the Technique may be overridden.  Simple textured objects are also
28 possible without a Technique.
29
30 It is possible to use a single Object for rendering multiple identical or
31 similar objects.  See class ObjectInstance.
32 */
33 class Object: public Renderable
34 {
35 private:
36         std::vector<const Mesh *> meshes;
37         const Technique *technique;
38         std::vector<const Texture *> textures;
39         const Texture *main_texture;
40         const Material *material;
41
42 public:
43         class Loader: public DataFile::CollectionObjectLoader<Object>
44         {
45         public:
46                 Loader(Object &, Collection &);
47
48         private:
49                 virtual void finish();
50                 void lod_mesh(unsigned, const std::string &);
51                 void material_inline();
52                 void mesh(const std::string &);
53                 void shader_texture(const std::string &);
54                 void technique(const std::string &);
55                 void texture(const std::string &);
56         };
57
58         Object();
59         ~Object();
60
61         const Technique *get_technique() const { return technique; }
62
63         /**
64         Renders the object.  A tag can be provided to render a non-default pass.
65         */
66         virtual void render(const Tag &tag=Tag()) const;
67
68         /**
69         Renders the object with an instance.  The instance's hook functions are
70         called before and after drawing the mesh.  A tag may also be given to render
71         a non-default pass.
72         */
73         virtual void render(const ObjectInstance &, const Tag &tag=Tag()) const;
74
75         /**
76         Renders multiple instances of the object in one go.  This may improve
77         performance, as the object-specific render setup only has to be done once.
78         Each instance's hook functions will be called before and after drawing the
79         mesh.
80         */
81         template<typename Iter>
82         void render(Iter begin, Iter end, const Tag &tag=Tag()) const
83         {
84                 if(!can_render(tag))
85                         return;
86
87                 const ObjectPass *pass=get_pass(tag);
88                 setup_render(pass);
89                 for(Iter i=begin; i!=end; ++i)
90                         render_instance(**i, tag);
91                 finish_render(pass);
92         }
93 private:
94         bool can_render(const Tag &) const;
95         const ObjectPass *get_pass(const Tag &) const;
96         void setup_render(const ObjectPass *) const;
97         void finish_render(const ObjectPass *) const;
98         void render_instance(const ObjectInstance &, const Tag &) const;
99 };
100
101 } // namespace GL
102 } // namespace Msp
103
104 #endif