]> git.tdb.fi Git - libs/gl.git/blob - source/object.h
Add class Technique to share passes between Objects
[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         virtual bool has_pass(const Tag &) const;
74
75         /**
76         Renders the object.  A tag can be provided to render a non-default pass.
77         */
78         virtual void render(const Tag &tag=Tag()) const;
79
80         /**
81         Renders the object with an instance.  The instance's hook functions are
82         called before and after drawing the mesh.  A tag may also be given to render
83         a non-default pass.
84         */
85         virtual void render(const ObjectInstance &, const Tag &tag=Tag()) const;
86
87         /**
88         Renders multiple instances of the object in one go.  This may improve
89         performance, as the object-specific render setup only has to be done once.
90         Each instance's hook functions will be called before and after drawing the
91         mesh.
92         */
93         template<typename Iter>
94         void render(Iter begin, Iter end, const Tag &tag=Tag()) const
95         {
96                 const ObjectPass *pass=get_pass(tag);
97                 setup_render(pass);
98                 for(Iter i=begin; i!=end; ++i)
99                         render_instance(**i, tag);
100                 finish_render(pass);
101         }
102 private:
103         const ObjectPass *get_pass(const Tag &) const;
104         void setup_render(const ObjectPass *) const;
105         void finish_render(const ObjectPass *) const;
106         void render_instance(const ObjectInstance &, const Tag &) const;
107 };
108
109 } // namespace GL
110 } // namespace Msp
111
112 #endif