#include "objectpass.h"
#include "program.h"
#include "programdata.h"
+#include "technique.h"
#include "texture.h"
#include "texunit.h"
Object::Object():
meshes(1, static_cast<Mesh *>(0)),
+ technique(0),
+ main_texture(0),
material(0)
-{
- normal_pass=&passes[""];
-}
+{ }
Object::~Object()
{
- for(map<string, ObjectPass>::iterator i=passes.begin(); i!=passes.end(); ++i)
- delete i->second.shdata;
-}
-
-bool Object::has_pass(const string &pn) const
-{
- return passes.count(pn);
-}
-
-const ObjectPass &Object::get_pass(const string &pn) const
-{
- map<string, ObjectPass>::const_iterator i=passes.find(pn);
- if(i==passes.end())
- throw KeyError("Unknown pass");
- return i->second;
}
-void Object::render(const ObjectInstance *inst) const
+bool Object::has_pass(const Tag &tag) const
{
- render(*normal_pass, inst);
+ if(technique)
+ return technique->has_pass(tag);
+ else
+ return tag.id==0;
}
-void Object::render(const string &pn, const ObjectInstance *inst) const
+void Object::render(const Tag &tag) const
{
- render(get_pass(pn), inst);
+ const ObjectPass *pass=get_pass(tag);
+ setup_render(pass);
+ meshes[0]->draw();
+ finish_render(pass);
}
-void Object::render(const list<const ObjectInstance *> &insts) const
+void Object::render(const ObjectInstance &inst, const Tag &tag) const
{
- render(*normal_pass, insts);
+ const ObjectPass *pass=get_pass(tag);
+ setup_render(pass);
+ render_instance(inst, tag);
+ meshes[0]->draw();
+ finish_render(pass);
}
-void Object::render(const string &pn, const list<const ObjectInstance *> &insts) const
+const ObjectPass *Object::get_pass(const Tag &tag) const
{
- render(get_pass(pn), insts);
+ if(technique)
+ return &technique->get_pass(tag);
+ else if(tag.id==0)
+ return 0;
+ throw KeyError("Unknown pass");
}
-void Object::setup_render(const ObjectPass &pass) const
+void Object::setup_render(const ObjectPass *pass) const
{
if(!meshes[0])
throw InvalidState("Trying to render Object without mesh");
- if(pass.shprog)
+ if(pass && pass->shprog)
{
- pass.shprog->bind();
- pass.shdata->apply();
+ pass->shprog->bind();
+ pass->shdata->apply();
for(unsigned i=0; i<textures.size(); ++i)
{
TexUnit::activate(i);
textures[i]->bind();
}
}
- else if(!textures.empty())
- textures.front()->bind();
+ else if(main_texture && (!pass || pass->use_textures))
+ main_texture->bind();
if(material)
- material->apply();
-}
-
-void Object::finish_render(const ObjectPass &pass) const
-{
- if(pass.shprog)
- Program::unbind();
- for(unsigned i=textures.size(); i--;)
- {
- TexUnit::activate(i);
- Texture::unbind();
- }
+ material->bind();
}
-void Object::render(const ObjectPass &pass, const ObjectInstance *inst) const
+void Object::finish_render(const ObjectPass *pass) const
{
- setup_render(pass);
-
- unsigned lod=0;
- if(inst)
+ if(pass && pass->shprog)
{
- inst->setup_render(pass);
- lod=min(inst->get_level_of_detail(), meshes.size()-1);
+ Program::unbind();
+ for(unsigned i=textures.size(); i--;)
+ {
+ TexUnit::activate(i);
+ Texture::unbind();
+ }
}
+ else if(main_texture)
+ Texture::unbind();
- meshes[lod]->draw();
-
- if(inst)
- inst->finish_render(pass);
-
- finish_render(pass);
+ if(material)
+ Material::unbind();
}
-void Object::render(const ObjectPass &pass, const list<const ObjectInstance *> &insts) const
+void Object::render_instance(const ObjectInstance &inst, const Tag &tag) const
{
- setup_render(pass);
-
- for(list<const ObjectInstance *>::const_iterator i=insts.begin(); i!=insts.end(); ++i)
- {
- (*i)->setup_render(pass);
-
- unsigned lod=min((*i)->get_level_of_detail(), meshes.size()-1);
- meshes[lod]->draw();
-
- (*i)->finish_render(pass);
- }
-
- finish_render(pass);
+ inst.setup_render(tag);
+ unsigned lod=min(inst.get_level_of_detail(), meshes.size()-1);
+ meshes[lod]->draw();
+ inst.finish_render(tag);
}
add("material", &Object::material);
add("material_inline", &Loader::material_inline);
add("mesh", &Loader::mesh);
- add("pass", &Loader::pass);
- add("shader", &Loader::shader);
+ add("shader_texture", &Loader::shader_texture);
+ add("technique", &Loader::technique);
add("texture", &Loader::texture);
}
-Object::Loader::~Loader()
+void Object::Loader::finish()
{
- for(map<string, ObjectPass>::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
- if(i->second.shdata)
+ for(unsigned i=0; i<obj.textures.size(); ++i)
+ {
+ if(!obj.textures[i])
{
- for(unsigned j=0; j<textures.size(); ++j)
- i->second.shdata->uniform(i->second.shprog->get_uniform_location(textures[j]), static_cast<int>(j));
+ obj.textures[i]=obj.technique->get_texture(i);
+ if(!obj.textures[i])
+ throw Exception("Object does not specify all textures required by Technique");
}
+ }
}
void Object::Loader::lod_mesh(unsigned l, const string &n)
{
obj.meshes.resize(l+1, 0);
- obj.meshes[l]=&coll.get<Mesh>(n);
+ obj.meshes[l]=coll.get<Mesh>(n);
}
void Object::Loader::material_inline()
{
- throw Exception("material_inline not supported yet");
- /*RefPtr<Material> mat=new Material;
+ RefPtr<Material> mat=new Material;
load_sub(*mat);
- coll.add(format("%p%p", &obj, mat.get()), mat.get());
- obj.material=mat.release();*/
+ coll.add(format("_%p", mat.get()), mat.get());
+ obj.material=mat.release();
}
void Object::Loader::mesh(const string &n)
{
- obj.meshes[0]=&coll.get<Mesh>(n);
+ obj.meshes[0]=coll.get<Mesh>(n);
}
-void Object::Loader::pass(const string &n)
+void Object::Loader::shader_texture(const string &n)
{
- if(obj.passes.count(n))
- throw KeyError("Duplicate pass name");
- ObjectPass p;
- load_sub(p, coll);
- obj.passes[n]=p;
+ if(!obj.technique)
+ throw InvalidState("Can't specify shader textures without a Technique");
+
+ unsigned eqsign=n.find('=');
+ if(eqsign==string::npos)
+ throw InvalidParameterValue("Must specify texture slot name");
+
+ obj.textures[obj.technique->get_texture_index(n.substr(0, eqsign))]=coll.get<Texture>(n.substr(eqsign+1));
}
-void Object::Loader::shader(const string &n)
+void Object::Loader::technique(const string &n)
{
- Program *shprog=&coll.get<Program>(n);
- if(shprog) // Allow for unsupported shaders
- {
- RefPtr<ProgramData> shdata=new ProgramData;
- load_sub(*shdata, *shprog);
-
- obj.normal_pass->shprog=shprog;
- if(obj.normal_pass->shdata)
- delete obj.normal_pass->shdata;
- obj.normal_pass->shdata=shdata.release();
- }
+ obj.technique=coll.get<Technique>(n);
+ obj.textures.resize(obj.technique->get_n_textures());
+ obj.material=obj.technique->get_material();
}
void Object::Loader::texture(const string &n)
{
- unsigned eqsign=n.find('=');
- if(eqsign!=string::npos)
- {
- obj.textures.push_back(&coll.get<Texture>(n.substr(eqsign+1)));
- textures.push_back(n.substr(0, eqsign));
- }
- else
- {
- obj.textures.push_back(&coll.get<Texture>(n));
- textures.push_back(n);
- }
+ if(obj.main_texture)
+ throw Exception("Only one main texture may be specified");
+
+ Texture *tex=coll.get<Texture>(n);
+ if(obj.technique)
+ obj.textures[obj.technique->get_texture_index("texture")]=tex;
+ obj.main_texture=tex;
}
} // namespace GL