]> git.tdb.fi Git - libs/gl.git/commitdiff
Make RenderPass use a Texturing for textures
authorMikko Rasa <tdb@tdb.fi>
Sun, 26 Dec 2010 16:35:14 +0000 (16:35 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sun, 26 Dec 2010 16:35:14 +0000 (16:35 +0000)
source/renderpass.cpp
source/renderpass.h

index b9a2cb83a05a09cbaab1ec3f6ea674a1fec707ac..5b5f72984b3bbfdbfe293d594ba68b3c17b4bd43 100644 (file)
@@ -12,8 +12,10 @@ Distributed under the LGPL
 #include "renderpass.h"
 #include "program.h"
 #include "programdata.h"
+#include "texenv.h"
 #include "texture.h"
 #include "texture2d.h"
+#include "texturing.h"
 
 using namespace std;
 
@@ -23,7 +25,8 @@ namespace GL {
 RenderPass::RenderPass():
        shprog(0),
        shdata(0),
-       material(0)
+       material(0),
+       texturing(0)
 { }
 
 RenderPass::RenderPass(const RenderPass &other):
@@ -31,12 +34,13 @@ RenderPass::RenderPass(const RenderPass &other):
        shprog(other.shprog),
        shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
        material(other.material),
-       textures(other.textures)
+       texturing(other.texturing ? new Texturing(*other.texturing) : 0)
 { }
 
 RenderPass::~RenderPass()
 {
        delete shdata;
+       delete texturing;
 }
 
 void RenderPass::set_material(const Material *mat)
@@ -47,17 +51,10 @@ void RenderPass::set_material(const Material *mat)
 
 void RenderPass::set_texture(unsigned index, const Texture *tex)
 {
-       for(vector<TextureSlot>::iterator i=textures.begin(); i!=textures.end(); ++i)
-               if(i->index==index)
-               {
-                       i->texture = tex;
-                       i->texture.keep();
-                       return;
-               }
+       if(!texturing)
+               texturing = new Texturing;
 
-       textures.push_back(TextureSlot(index));
-       textures.back().texture = tex;
-       textures.back().texture.keep();
+       texturing->attach(index, *tex);
 }
 
 void RenderPass::bind() const
@@ -72,25 +69,17 @@ void RenderPass::bind() const
                shdata->apply();
        }
        else if(old && old->shprog)
-               GL::Program::unbind();
+               Program::unbind();
 
        if(material)
                material->bind();
        else if(old && old->material)
-               GL::Material::unbind();
+               Material::unbind();
 
-       unsigned used_tex_units = 0;
-       for(vector<TextureSlot>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
-       {
-               i->texture->bind_to(i->index);
-               used_tex_units |= 1<<i->index;
-       }
-       if(old)
-       {
-               for(vector<TextureSlot>::const_iterator i=old->textures.begin(); i!=old->textures.end(); ++i)
-                       if(!used_tex_units&(1<<i->index))
-                               Texture::unbind_from(i->index);
-       }
+       if(texturing)
+               texturing->bind();
+       else if(old && old->texturing)
+               Texturing::unbind();
 }
 
 void RenderPass::unbind()
@@ -100,22 +89,16 @@ void RenderPass::unbind()
                return;
 
        if(old->shprog)
-               GL::Program::unbind();
+               Program::unbind();
 
        if(old->material)
-               GL::Material::unbind();
+               Material::unbind();
 
-       for(unsigned i=old->textures.size(); i--; )
-               GL::Texture::unbind_from(i);
+       if(old->texturing)
+               Texturing::unbind();
 }
 
 
-RenderPass::TextureSlot::TextureSlot(unsigned i):
-       index(i),
-       texture(0)
-{ }
-
-
 RenderPass::Loader::Loader(RenderPass &p):
        DataFile::CollectionObjectLoader<RenderPass>(p, 0)
 {
@@ -161,9 +144,10 @@ void RenderPass::Loader::material(const string &name)
 
 void RenderPass::Loader::texunit(unsigned i)
 {
-       TextureSlot slot(i);
-       load_sub(slot);
-       obj.textures.push_back(slot);
+       if(!obj.texturing)
+               obj.texturing = new Texturing;
+       TextureLoader ldr(*obj.texturing, i, coll);
+       load_sub_with(ldr);
 }
 
 void RenderPass::Loader::uniforms()
@@ -176,35 +160,44 @@ void RenderPass::Loader::uniforms()
 }
 
 
-RenderPass::TextureSlot::Loader::Loader(TextureSlot &s):
-       DataFile::CollectionObjectLoader<TextureSlot>(s, 0)
+RenderPass::TextureLoader::TextureLoader(Texturing &t, unsigned i, Collection *c):
+       DataFile::CollectionObjectLoader<Texturing>(t, c),
+       index(i)
 {
-       init();
+       add("texture",   &TextureLoader::texture);
+       add("texture2d", &TextureLoader::texture2d);
 }
 
-RenderPass::TextureSlot::Loader::Loader(TextureSlot &s, Collection &c):
-       DataFile::CollectionObjectLoader<TextureSlot>(s, &c)
+void RenderPass::TextureLoader::finish()
 {
-       init();
+       if(tex)
+       {
+               if(env)
+                       obj.attach(index, *tex, *env);
+               else
+                       obj.attach(index, *tex);
+               tex.release();
+               env.release();
+       }
 }
 
-void RenderPass::TextureSlot::Loader::init()
+void RenderPass::TextureLoader::texenv()
 {
-       add("texture",   &Loader::texture);
-       add("texture2d", &Loader::texture2d);
+       throw Exception("TexEnvs can't be loaded yet");
+       /*env = new TexEnv;
+       load_sub(*env);*/
 }
 
-void RenderPass::TextureSlot::Loader::texture(const string &name)
+void RenderPass::TextureLoader::texture(const string &name)
 {
-       obj.texture = get_collection().get<Texture>(name);
-       obj.texture.keep();
+       tex = get_collection().get<Texture>(name);
+       tex.keep();
 }
 
-void RenderPass::TextureSlot::Loader::texture2d()
+void RenderPass::TextureLoader::texture2d()
 {
-       RefPtr<Texture2D> tex = new Texture2D;
-       load_sub(*tex);
-       obj.texture = tex;
+       tex = new Texture2D;
+       load_sub(static_cast<Texture2D &>(*tex));
 }
 
 } // namespace GL
index 10960b3f37cb1b082d0cf26d20404514297982b8..acbe29491cf9f493477663f8f51954ef63de2605 100644 (file)
@@ -18,11 +18,15 @@ namespace GL {
 class Material;
 class Program;
 class ProgramData;
+class TexEnv;
 class Texture;
+class Texturing;
 
 /**
 Encapsulates the data that determines the appearance of a rendered surface.
-This includes shader and data for it, material and textures.
+This includes shader and data for it, material and texturing.
+
+XXX Does not delete inline texture from datafiles properly
 */
 class RenderPass: public Bindable<RenderPass>
 {
@@ -43,30 +47,27 @@ public:
        };
 
 private:
-       struct TextureSlot
+       struct TextureLoader: public DataFile::CollectionObjectLoader<Texturing>
        {
-               class Loader: public DataFile::CollectionObjectLoader<TextureSlot>
-               {
-               public:
-                       Loader(TextureSlot &);
-                       Loader(TextureSlot &, Collection &);
-
-               private:
-                       void init();
-                       void texture(const std::string &);
-                       void texture2d();
-               };
-
+       private:
                unsigned index;
-               RefPtr<const Texture> texture;
+               RefPtr<Texture> tex;
+               RefPtr<TexEnv> env;
+
+       public:
+               TextureLoader(Texturing &, unsigned, Collection *);
+       private:
+               virtual void finish();
 
-               TextureSlot(unsigned);
+               void texenv();
+               void texture(const std::string &);
+               void texture2d();
        };
 
        Program *shprog;
        ProgramData *shdata;
        RefPtr<const Material> material;
-       std::vector<TextureSlot> textures;
+       Texturing *texturing;
 
        RenderPass &operator=(const RenderPass &);
 public: