]> git.tdb.fi Git - libs/gl.git/blob - source/technique.cpp
Use a struct to store texture slots in Technique
[libs/gl.git] / source / technique.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 <msp/strings/formatter.h>
9 #include "material.h"
10 #include "program.h"
11 #include "programdata.h"
12 #include "tag.h"
13 #include "technique.h"
14 #include "texture.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GL {
20
21 Technique::Technique():
22         main_texture(0),
23         normal_pass(&passes[0]),
24         material(0)
25 { }
26
27 Technique::~Technique()
28 {
29         for(map<unsigned, ObjectPass>::iterator i=passes.begin(); i!=passes.end(); ++i)
30                 delete i->second.shdata;
31 }
32
33 bool Technique::has_pass(const GL::Tag &tag) const
34 {
35         return passes.count(tag.id);
36 }
37
38 const ObjectPass &Technique::get_pass(const GL::Tag &tag) const
39 {
40         map<unsigned, ObjectPass>::const_iterator i=passes.find(tag.id);
41         if(i==passes.end())
42                 throw KeyError("Unknown pass");
43         return i->second;
44 }
45
46 unsigned Technique::get_texture_index(const std::string &n) const
47 {
48         for(unsigned i=0; i<textures.size(); ++i)
49                 if(textures[i].name==n)
50                         return i;
51
52         throw KeyError("Unknown texture slot", n);
53 }
54
55 const Texture *Technique::get_texture(unsigned i) const
56 {
57         if(i>=textures.size())
58                 throw KeyError("Texture index out of range");
59
60         return textures[i].texture;
61 }
62
63
64 Technique::Loader::Loader(Technique &t, Collection &c):
65         tech(t),
66         coll(c)
67 {
68         add("material",        &Technique::material);
69         add("material_inline", &Loader::material_inline);
70         add("pass",            &Loader::pass);
71         add("shader",          &Loader::shader);
72         add("shader_texture",  &Loader::shader_texture);
73         add("texture",         &Loader::texture);
74         add("texture_slot",    &Loader::texture_slot);
75 }
76
77 void Technique::Loader::finish()
78 {
79         for(map<unsigned, ObjectPass>::iterator i=tech.passes.begin(); i!=tech.passes.end(); ++i)
80                 if(i->second.shdata)
81                 {
82                         for(unsigned j=0; j<tech.textures.size(); ++j)
83                         {
84                                 unsigned loc=i->second.shprog->get_uniform_location(tech.textures[j].name);
85                                 i->second.shdata->uniform(loc, static_cast<int>(j));
86                         }
87                 }
88 }
89
90 void Technique::Loader::material_inline()
91 {
92         RefPtr<Material> mat=new Material;
93         load_sub(*mat);
94         coll.add(format("_%p", mat.get()), mat.get());
95         tech.material=mat.release();
96 }
97
98 void Technique::Loader::pass(const string &n)
99 {
100         unsigned id=Tag(n).id;
101         if(tech.passes.count(id))
102                 throw KeyError("Duplicate pass name", n);
103         ObjectPass p;
104         load_sub(p, coll);
105         tech.passes[id]=p;
106 }
107
108 void Technique::Loader::shader(const string &n)
109 {
110         Program *shprog=coll.get<Program>(n);
111         if(shprog)  // Allow for unsupported shaders
112         {
113                 RefPtr<ProgramData> shdata=new ProgramData;
114                 load_sub(*shdata, *shprog);
115
116                 tech.normal_pass->shprog=shprog;
117                 if(tech.normal_pass->shdata)
118                         delete tech.normal_pass->shdata;
119                 tech.normal_pass->shdata=shdata.release();
120         }
121 }
122
123 void Technique::Loader::shader_texture(const string &n)
124 {
125         string::size_type eqsign=n.find('=');
126         TextureSlot tex;
127         if(eqsign!=string::npos)
128         {
129                 tex.name=n.substr(0, eqsign);
130                 tex.texture=coll.get<Texture>(n.substr(eqsign+1));
131         }
132         else
133         {
134                 string::size_type dot=n.rfind('.');
135                 tex.name=n.substr(0, dot);
136                 tex.texture = coll.get<Texture>(n);
137         }
138         for(string::iterator i=tex.name.begin(); i!=tex.name.end(); ++i)
139                 if(!isalnum(*i))
140                         *i='_';
141         tech.textures.push_back(tex);
142 }
143
144 void Technique::Loader::texture(const string &n)
145 {
146         if(tech.main_texture)
147                 throw Exception("Only one main texture may be specified");
148
149         tech.main_texture=coll.get<Texture>(n);
150         TextureSlot tex;
151         tex.name="texture";
152         tex.texture=tech.main_texture;
153         tech.textures.push_back(tex);
154 }
155
156 void Technique::Loader::texture_slot(const string &n)
157 {
158         TextureSlot tex;
159         tex.name=n;
160         tech.textures.push_back(tex);
161 }
162
163 } // namespace GL
164 } // namespace Msp