]> git.tdb.fi Git - libs/gl.git/commitdiff
Use a struct to store texture slots in Technique
authorMikko Rasa <tdb@tdb.fi>
Fri, 4 Dec 2009 17:18:21 +0000 (17:18 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 4 Dec 2009 17:18:21 +0000 (17:18 +0000)
source/technique.cpp
source/technique.h

index 74cd6c311be0699e10760922452c702507458e58..53c7a9ae76cacdc3ba68b05bd54db5000de452e2 100644 (file)
@@ -45,8 +45,8 @@ const ObjectPass &Technique::get_pass(const GL::Tag &tag) const
 
 unsigned Technique::get_texture_index(const std::string &n) const
 {
-       for(unsigned i=0; i<tex_names.size(); ++i)
-               if(tex_names[i]==n)
+       for(unsigned i=0; i<textures.size(); ++i)
+               if(textures[i].name==n)
                        return i;
 
        throw KeyError("Unknown texture slot", n);
@@ -57,7 +57,7 @@ const Texture *Technique::get_texture(unsigned i) const
        if(i>=textures.size())
                throw KeyError("Texture index out of range");
 
-       return textures[i];
+       return textures[i].texture;
 }
 
 
@@ -80,7 +80,10 @@ void Technique::Loader::finish()
                if(i->second.shdata)
                {
                        for(unsigned j=0; j<tech.textures.size(); ++j)
-                               i->second.shdata->uniform(i->second.shprog->get_uniform_location(tech.tex_names[j]), static_cast<int>(j));
+                       {
+                               unsigned loc=i->second.shprog->get_uniform_location(tech.textures[j].name);
+                               i->second.shdata->uniform(loc, static_cast<int>(j));
+                       }
                }
 }
 
@@ -120,16 +123,22 @@ void Technique::Loader::shader(const string &n)
 void Technique::Loader::shader_texture(const string &n)
 {
        string::size_type eqsign=n.find('=');
+       TextureSlot tex;
        if(eqsign!=string::npos)
        {
-               tech.textures.push_back(coll.get<Texture>(n.substr(eqsign+1)));
-               tech.tex_names.push_back(n.substr(0, eqsign));
+               tex.name=n.substr(0, eqsign);
+               tex.texture=coll.get<Texture>(n.substr(eqsign+1));
        }
        else
        {
-               tech.textures.push_back(coll.get<Texture>(n));
-               tech.tex_names.push_back(n);
+               string::size_type dot=n.rfind('.');
+               tex.name=n.substr(0, dot);
+               tex.texture = coll.get<Texture>(n);
        }
+       for(string::iterator i=tex.name.begin(); i!=tex.name.end(); ++i)
+               if(!isalnum(*i))
+                       *i='_';
+       tech.textures.push_back(tex);
 }
 
 void Technique::Loader::texture(const string &n)
@@ -138,14 +147,17 @@ void Technique::Loader::texture(const string &n)
                throw Exception("Only one main texture may be specified");
 
        tech.main_texture=coll.get<Texture>(n);
-       tech.textures.push_back(tech.main_texture);
-       tech.tex_names.push_back("texture");
+       TextureSlot tex;
+       tex.name="texture";
+       tex.texture=tech.main_texture;
+       tech.textures.push_back(tex);
 }
 
 void Technique::Loader::texture_slot(const string &n)
 {
-       tech.tex_names.push_back(n);
-       tech.textures.push_back(0);
+       TextureSlot tex;
+       tex.name=n;
+       tech.textures.push_back(tex);
 }
 
 } // namespace GL
index 711db79dce2af54f4adb45e411caf3b82acd2b50..ef7e9e12e451f942fba1431b175d8bb4bfc0693b 100644 (file)
@@ -50,8 +50,15 @@ public:
        };
 
 private:
-       std::vector<std::string> tex_names;
-       std::vector<const Texture *> textures;
+       struct TextureSlot
+       {
+               std::string name;
+               const Texture *texture;
+
+               TextureSlot(): texture(0) { }
+       };
+
+       std::vector<TextureSlot> textures;
        const Texture *main_texture;
        std::map<unsigned, ObjectPass> passes;
        ObjectPass *normal_pass;
@@ -63,7 +70,7 @@ public:
 
        bool has_pass(const GL::Tag &) const;
        const ObjectPass &get_pass(const GL::Tag &) const;
-       unsigned get_n_textures() const { return tex_names.size(); }
+       unsigned get_n_textures() const { return textures.size(); }
        unsigned get_texture_index(const std::string &) const;
        const Texture *get_texture(unsigned) const;
        const Texture *get_main_texture() const { return main_texture; }