#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[0];
-}
+{ }
Object::~Object()
{
- for(map<unsigned, ObjectPass>::iterator i=passes.begin(); i!=passes.end(); ++i)
- delete i->second.shdata;
}
bool Object::has_pass(const Tag &tag) const
{
- return passes.count(tag.id);
+ if(technique)
+ return technique->has_pass(tag);
+ else
+ return tag.id==0;
}
-const ObjectPass &Object::get_pass(const Tag &tag) const
+void Object::render(const Tag &tag) const
{
- map<unsigned, ObjectPass>::const_iterator i=passes.find(tag.id);
- if(i==passes.end())
- throw KeyError("Unknown pass");
- return i->second;
+ const ObjectPass *pass=get_pass(tag);
+ setup_render(pass);
+ meshes[0]->draw();
+ finish_render(pass);
}
-void Object::render(const Tag &tag) const
+void Object::render(const ObjectInstance &inst, const Tag &tag) const
{
- render(get_pass(tag), 0);
+ const ObjectPass *pass=get_pass(tag);
+ setup_render(pass);
+ render_instance(inst, tag);
+ meshes[0]->draw();
+ finish_render(pass);
}
-void Object::render(const ObjectInstance &inst, const Tag &tag) const
+const ObjectPass *Object::get_pass(const Tag &tag) const
{
- render(get_pass(tag), &inst);
+ 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(main_texture && pass.use_textures)
+ else if(main_texture && (!pass || pass->use_textures))
main_texture->bind();
if(material)
material->bind();
}
-void Object::finish_render(const ObjectPass &pass) const
+void Object::finish_render(const ObjectPass *pass) const
{
- if(pass.shprog)
+ if(pass && pass->shprog)
{
Program::unbind();
for(unsigned i=textures.size(); i--;)
Material::unbind();
}
-void Object::render(const ObjectPass &pass, const ObjectInstance *inst) const
-{
- setup_render(pass);
-
- if(inst)
- render_instance(pass, *inst);
- else
- meshes[0]->draw();
-
- finish_render(pass);
-}
-
-void Object::render_instance(const ObjectPass &pass, const ObjectInstance &inst) const
+void Object::render_instance(const ObjectInstance &inst, const Tag &tag) const
{
- inst.setup_render(pass);
+ inst.setup_render(tag);
unsigned lod=min(inst.get_level_of_detail(), meshes.size()-1);
meshes[lod]->draw();
- inst.finish_render(pass);
+ 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);
}
void Object::Loader::finish()
{
- for(map<unsigned, 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)
{
RefPtr<Material> mat=new Material;
load_sub(*mat);
- coll.add(format("%p%p", &obj, mat.get()), mat.get());
+ coll.add(format("_%p", mat.get()), mat.get());
obj.material=mat.release();
}
obj.meshes[0]=coll.get<Mesh>(n);
}
-void Object::Loader::pass(const string &n)
+void Object::Loader::shader_texture(const string &n)
{
- unsigned id=Tag(n).id;
- if(obj.passes.count(id))
- throw KeyError("Duplicate pass name");
- ObjectPass p;
- load_sub(p, coll);
- obj.passes[id]=p;
-}
+ if(!obj.technique)
+ throw InvalidState("Can't specify shader textures without a Technique");
-void Object::Loader::shader(const string &n)
-{
- Program *shprog=coll.get<Program>(n);
- if(shprog) // Allow for unsupported shaders
- {
- RefPtr<ProgramData> shdata=new ProgramData;
- load_sub(*shdata, *shprog);
+ unsigned eqsign=n.find('=');
+ if(eqsign==string::npos)
+ throw InvalidParameterValue("Must specify texture slot name");
- obj.normal_pass->shprog=shprog;
- if(obj.normal_pass->shdata)
- delete obj.normal_pass->shdata;
- obj.normal_pass->shdata=shdata.release();
- }
+ obj.textures[obj.technique->get_texture_index(n.substr(0, eqsign))]=coll.get<Texture>(n.substr(eqsign+1));
}
-void Object::Loader::shader_texture(const string &n)
+void Object::Loader::technique(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);
- }
+ 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)
if(obj.main_texture)
throw Exception("Only one main texture may be specified");
- obj.main_texture=coll.get<Texture>(n);
- obj.textures.push_back(obj.main_texture);
- textures.push_back("texture");
+ Texture *tex=coll.get<Texture>(n);
+ if(obj.technique)
+ obj.textures[obj.technique->get_texture_index("texture")]=tex;
+ obj.main_texture=tex;
}
} // namespace GL
class Material;
class Mesh;
class ObjectInstance;
-class Program;
-class ProgramData;
+class Technique;
class Texture;
/**
-Stores data for a complete 3D object. This includes a mesh, a shader and data
-for it and a number of textures. Only the mesh is mandatory, other components
-are optional.
+Stores data for a complete 3D object. An Object must always have a Mesh, and
+may also have a Technique to determine its appearance. Textures and material
+specified by the Technique may be overridden. Simple textured objects are also
+possible without a Technique.
-See also class ObjectInstance.
+It is possible to use a single Object for rendering multiple identical or
+similar objects. See class ObjectInstance.
*/
class Object: public Renderable
{
private:
std::vector<const Mesh *> meshes;
+ const Technique *technique;
std::vector<const Texture *> textures;
const Texture *main_texture;
- std::map<unsigned, ObjectPass> passes;
const Material *material;
- ObjectPass *normal_pass;
public:
class Loader: public DataFile::Loader
protected:
Object &obj;
Collection &coll;
- private:
- std::vector<std::string> textures;
public:
Loader(Object &, Collection &);
void lod_mesh(unsigned, const std::string &);
void material_inline();
void mesh(const std::string &);
- void pass(const std::string &);
- void shader(const std::string &);
void shader_texture(const std::string &);
+ void technique(const std::string &);
void texture(const std::string &);
};
Object();
~Object();
+ const Technique *get_technique() const { return technique; }
+
virtual bool has_pass(const Tag &) const;
- const ObjectPass &get_pass(const Tag &) const;
/**
Renders the object. A tag can be provided to render a non-default pass.
*/
template<typename Iter>
void render(Iter begin, Iter end, const Tag &tag=Tag()) const
- { render(get_pass(tag), begin, end); }
-private:
- void setup_render(const ObjectPass &) const;
- void finish_render(const ObjectPass &) const;
- void render(const ObjectPass &, const ObjectInstance *) const;
-
- template<typename Iter>
- void render(const ObjectPass &pass, Iter begin, Iter end) const
{
+ const ObjectPass *pass=get_pass(tag);
setup_render(pass);
for(Iter i=begin; i!=end; ++i)
- render_instance(pass, **i);
+ render_instance(**i, tag);
finish_render(pass);
}
-
- void render_instance(const ObjectPass &, const ObjectInstance &) const;
+private:
+ const ObjectPass *get_pass(const Tag &) const;
+ void setup_render(const ObjectPass *) const;
+ void finish_render(const ObjectPass *) const;
+ void render_instance(const ObjectInstance &, const Tag &) const;
};
} // namespace GL
class Object;
class ProgramData;
-class ObjectPass;
-
/**
Represents a single instance of an Object. An application can derive another
class from this and overload the hook functions to specify location and other
/**
Hook function, called from Object just before rendering the mesh.
*/
- virtual void setup_render(const ObjectPass &) const { }
+ virtual void setup_render(const Tag &) const { }
/**
Hook function, called from Object right after rendering the mesh.
*/
- virtual void finish_render(const ObjectPass &) const { }
+ virtual void finish_render(const Tag &) const { }
virtual unsigned get_level_of_detail() const { return 0; }
};
--- /dev/null
+/* $Id$
+
+This file is part of libmspgl
+Copyright © 2007 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#include <msp/strings/formatter.h>
+#include "material.h"
+#include "program.h"
+#include "programdata.h"
+#include "tag.h"
+#include "technique.h"
+#include "texture.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+
+Technique::Technique():
+ main_texture(0),
+ normal_pass(&passes[0]),
+ material(0)
+{ }
+
+Technique::~Technique()
+{
+ for(map<unsigned, ObjectPass>::iterator i=passes.begin(); i!=passes.end(); ++i)
+ delete i->second.shdata;
+}
+
+bool Technique::has_pass(const GL::Tag &tag) const
+{
+ return passes.count(tag.id);
+}
+
+const ObjectPass &Technique::get_pass(const GL::Tag &tag) const
+{
+ map<unsigned, ObjectPass>::const_iterator i=passes.find(tag.id);
+ if(i==passes.end())
+ throw KeyError("Unknown pass");
+ return i->second;
+}
+
+unsigned Technique::get_texture_index(const std::string &n) const
+{
+ for(unsigned i=0; i<tex_names.size(); ++i)
+ if(tex_names[i]==n)
+ return i;
+
+ throw KeyError("Unknown texture slot", n);
+}
+
+const Texture *Technique::get_texture(unsigned i) const
+{
+ if(i>=textures.size())
+ throw KeyError("Texture index out of range");
+
+ return textures[i];
+}
+
+
+Technique::Loader::Loader(Technique &t, Collection &c):
+ tech(t),
+ coll(c)
+{
+ add("material", &Technique::material);
+ add("material_inline", &Loader::material_inline);
+ add("pass", &Loader::pass);
+ add("shader", &Loader::shader);
+ add("shader_texture", &Loader::shader_texture);
+ add("texture", &Loader::texture);
+ add("texture_slot", &Loader::texture_slot);
+}
+
+void Technique::Loader::finish()
+{
+ for(map<unsigned, ObjectPass>::iterator i=tech.passes.begin(); i!=tech.passes.end(); ++i)
+ 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));
+ }
+}
+
+void Technique::Loader::material_inline()
+{
+ RefPtr<Material> mat=new Material;
+ load_sub(*mat);
+ coll.add(format("_%p", mat.get()), mat.get());
+ tech.material=mat.release();
+}
+
+void Technique::Loader::pass(const string &n)
+{
+ unsigned id=Tag(n).id;
+ if(tech.passes.count(id))
+ throw KeyError("Duplicate pass name", n);
+ ObjectPass p;
+ load_sub(p, coll);
+ tech.passes[id]=p;
+}
+
+void Technique::Loader::shader(const string &n)
+{
+ Program *shprog=coll.get<Program>(n);
+ if(shprog) // Allow for unsupported shaders
+ {
+ RefPtr<ProgramData> shdata=new ProgramData;
+ load_sub(*shdata, *shprog);
+
+ tech.normal_pass->shprog=shprog;
+ if(tech.normal_pass->shdata)
+ delete tech.normal_pass->shdata;
+ tech.normal_pass->shdata=shdata.release();
+ }
+}
+
+void Technique::Loader::shader_texture(const string &n)
+{
+ unsigned eqsign=n.find('=');
+ if(eqsign!=string::npos)
+ {
+ tech.textures.push_back(coll.get<Texture>(n.substr(eqsign+1)));
+ tech.tex_names.push_back(n.substr(0, eqsign));
+ }
+ else
+ {
+ tech.textures.push_back(coll.get<Texture>(n));
+ tech.tex_names.push_back(n);
+ }
+}
+
+void Technique::Loader::texture(const string &n)
+{
+ if(tech.main_texture)
+ 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");
+}
+
+void Technique::Loader::texture_slot(const string &n)
+{
+ tech.tex_names.push_back(n);
+ tech.textures.push_back(0);
+}
+
+} // namespace GL
+} // namespace Msp
--- /dev/null
+/* $Id$
+
+This file is part of libmspgl
+Copyright © 2007 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
+#ifndef TECHNIQUE_H_
+#define TECHNIQUE_H_
+
+#include "objectpass.h"
+
+namespace Msp {
+namespace GL {
+
+class Material;
+class Tag;
+class Texture;
+
+/**
+Stores a complete multipass rendering technique for an Object. This includes
+shaders, textures and a material. A Technique can also specify empty texture
+slots which Objects must override.
+*/
+class Technique
+{
+public:
+ class Loader: public Msp::DataFile::Loader
+ {
+ public:
+ typedef DataFile::Collection Collection;
+
+ protected:
+ Technique &tech;
+ Collection &coll;
+
+ public:
+ Loader(Technique &, Collection &);
+
+ Technique &get_object() const { return tech; }
+ Collection &get_collection() const { return coll; }
+ private:
+ virtual void finish();
+ void material_inline();
+ void pass(const std::string &);
+ void shader(const std::string &);
+ void shader_texture(const std::string &);
+ void texture(const std::string &);
+ void texture_slot(const std::string &);
+ };
+
+private:
+ std::vector<std::string> tex_names;
+ std::vector<const Texture *> textures;
+ const Texture *main_texture;
+ std::map<unsigned, ObjectPass> passes;
+ ObjectPass *normal_pass;
+ const Material *material;
+
+public:
+ Technique();
+ ~Technique();
+
+ 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_texture_index(const std::string &) const;
+ const Texture *get_texture(unsigned) const;
+ const Material *get_material() const { return material; }
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif