]> git.tdb.fi Git - libs/gl.git/blob - source/object.cpp
Add Uniform* classes to store uniform data of Programs
[libs/gl.git] / source / object.cpp
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 #include "except.h"
9 #include "material.h"
10 #include "mesh.h"
11 #include "object.h"
12 #include "objectinstance.h"
13 #include "program.h"
14 #include "programdata.h"
15 #include "texture.h"
16 #include "texunit.h"
17
18 using namespace std;
19
20 namespace Msp {
21 namespace GL {
22
23 Object::Object():
24         mesh(0),
25         shprog(0),
26         shdata(0),
27         material(0)
28 { }
29
30 Object::~Object()
31 {
32         delete shdata;
33 }
34
35 void Object::render(const ObjectInstance *inst) const
36 {
37         setup_render();
38
39         if(inst)
40                 inst->setup_render();
41
42         mesh->draw();
43
44         if(inst)
45                 inst->finish_render();
46
47         finish_render();
48 }
49
50 void Object::render(const list<const ObjectInstance *> &insts) const
51 {
52         setup_render();
53
54         for(list<const ObjectInstance *>::const_iterator i=insts.begin(); i!=insts.end(); ++i)
55         {
56                 (*i)->setup_render();
57
58                 mesh->draw();
59
60                 (*i)->finish_render();
61         }
62
63         finish_render();
64 }
65
66 void Object::setup_render() const
67 {
68         if(!mesh)
69                 throw InvalidState("Trying to render Object without mesh");
70
71         if(shprog)
72         {
73                 shprog->bind();
74                 shdata->apply();
75                 for(unsigned i=0; i<textures.size(); ++i)
76                 {
77                         TexUnit::activate(i);
78                         textures[i]->bind();
79                 }
80         }
81         else if(!textures.empty())
82                 textures.front()->bind();
83
84         if(material)
85                 material->apply();
86 }
87
88 void Object::finish_render() const
89 {
90         if(shprog)
91                 Program::unbind();
92         for(unsigned i=0; i<textures.size(); ++i)
93         {
94                 TexUnit::activate(i);
95                 Texture::unbind();
96         }
97 }
98
99
100 Object::Loader::Loader(Object &o, Collection &c):
101         obj(o),
102         coll(c)
103 {
104         add("material", &Object::material);
105         add("mesh",     &Object::mesh);
106         add("shader",   &Loader::shader);
107         add("texture",  &Loader::texture);
108 }
109
110 Object::Loader::~Loader()
111 {
112         if(obj.shdata)
113         {
114                 for(unsigned i=0; i<textures.size(); ++i)
115                         obj.shdata->uniform(obj.shprog->get_uniform_location(textures[i]), static_cast<int>(i));
116         }
117 }
118
119 void Object::Loader::shader(const string &n)
120 {
121         obj.shprog=&coll.get<Program>(n);
122         if(!obj.shdata)
123                 obj.shdata=new ProgramData;
124 }
125
126 void Object::Loader::texture(const string &n)
127 {
128         unsigned eqsign=n.find('=');
129         if(eqsign!=string::npos)
130         {
131                 obj.textures.push_back(&coll.get<Texture>(n.substr(eqsign+1)));
132                 textures.push_back(n.substr(0, eqsign));
133         }
134         else
135         {
136                 obj.textures.push_back(&coll.get<Texture>(n));
137                 textures.push_back(n);
138         }
139 }
140
141 } // namespace GL
142 } // namespace Msp