]> git.tdb.fi Git - libs/gl.git/blob - source/technique.cpp
Fix compilation on 64-bit platforms
[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<tex_names.size(); ++i)
49                 if(tex_names[i]==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];
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                                 i->second.shdata->uniform(i->second.shprog->get_uniform_location(tech.tex_names[j]), static_cast<int>(j));
84                 }
85 }
86
87 void Technique::Loader::material_inline()
88 {
89         RefPtr<Material> mat=new Material;
90         load_sub(*mat);
91         coll.add(format("_%p", mat.get()), mat.get());
92         tech.material=mat.release();
93 }
94
95 void Technique::Loader::pass(const string &n)
96 {
97         unsigned id=Tag(n).id;
98         if(tech.passes.count(id))
99                 throw KeyError("Duplicate pass name", n);
100         ObjectPass p;
101         load_sub(p, coll);
102         tech.passes[id]=p;
103 }
104
105 void Technique::Loader::shader(const string &n)
106 {
107         Program *shprog=coll.get<Program>(n);
108         if(shprog)  // Allow for unsupported shaders
109         {
110                 RefPtr<ProgramData> shdata=new ProgramData;
111                 load_sub(*shdata, *shprog);
112
113                 tech.normal_pass->shprog=shprog;
114                 if(tech.normal_pass->shdata)
115                         delete tech.normal_pass->shdata;
116                 tech.normal_pass->shdata=shdata.release();
117         }
118 }
119
120 void Technique::Loader::shader_texture(const string &n)
121 {
122         string::size_type eqsign=n.find('=');
123         if(eqsign!=string::npos)
124         {
125                 tech.textures.push_back(coll.get<Texture>(n.substr(eqsign+1)));
126                 tech.tex_names.push_back(n.substr(0, eqsign));
127         }
128         else
129         {
130                 tech.textures.push_back(coll.get<Texture>(n));
131                 tech.tex_names.push_back(n);
132         }
133 }
134
135 void Technique::Loader::texture(const string &n)
136 {
137         if(tech.main_texture)
138                 throw Exception("Only one main texture may be specified");
139
140         tech.main_texture=coll.get<Texture>(n);
141         tech.textures.push_back(tech.main_texture);
142         tech.tex_names.push_back("texture");
143 }
144
145 void Technique::Loader::texture_slot(const string &n)
146 {
147         tech.tex_names.push_back(n);
148         tech.textures.push_back(0);
149 }
150
151 } // namespace GL
152 } // namespace Msp