sequence.set_debug_name(debug_name);
#endif
+ map<string, Renderable *> final_renderables = renderables;
+ vector<const SequenceTemplate::Renderable *> sequences;
+ for(const SequenceTemplate::Renderable &r: tmpl.get_renderables())
+ {
+ Renderable *&ptr = final_renderables[r.slot_name];
+ if(!ptr)
+ {
+ if(r.effect_template)
+ {
+ Effect *effect = r.effect_template->create(final_renderables);
+#ifdef DEBUG
+ if(!debug_name.empty())
+ effect->set_debug_name(format("%s/%s", debug_name, r.slot_name));
+#endif
+ ptr = effect;
+ }
+ else if(r.sequence_template)
+ {
+ Sequence *seq = new Sequence;
+#ifdef DEBUG
+ if(!debug_name.empty())
+ seq->set_debug_name(format("%s/%s", debug_name, r.slot_name));
+#endif
+ ptr = seq;
+ sequences.push_back(&r);
+ }
+ else
+ throw invalid_operation("SequenceBuilder::build");
+
+ sequence.add_owned(ptr);
+ }
+ }
+
+ for(const SequenceTemplate::Renderable *s: sequences)
+ {
+ SequenceBuilder bld(*s->sequence_template);
+ for(const auto &kvp: s->sequence_renderables)
+ if(Renderable *r = get_item(final_renderables, kvp.second))
+ bld.set_renderable(kvp.first, *r);
+ bld.build(*static_cast<Sequence *>(get_item(final_renderables, s->slot_name)));
+ }
+
sequence.set_clear_enabled(tmpl.is_clear_enabled());
if(tmpl.is_clear_enabled())
{
for(const SequenceTemplate::Step &s: tmpl.get_steps())
{
- Renderable *renderable = get_item(renderables, s.renderable_name);
- if(!renderable)
- throw missing_renderable(s.renderable_name);
-
- Sequence::Step &step = sequence.add_step(s.tag, *renderable);
+ // Existence is already ensured above
+ Renderable &renderable = *get_item(final_renderables, s.renderable_name);
+ Sequence::Step &step = sequence.add_step(s.tag, renderable);
step.set_depth_test(s.depth_test);
step.set_stencil_test(s.stencil_test);
step.set_lighting(s.lighting);
#include "blend.h"
#include "bloom.h"
#include "colorcurve.h"
+#include "environmentmap.h"
#include "lighting.h"
#include "object.h"
#include "resources.h"
#include "scene.h"
#include "sequencetemplate.h"
+#include "shadowmap.h"
+#include "sky.h"
using namespace std;
SequenceTemplate::~SequenceTemplate()
{
+ for(const Renderable &r: renderables)
+ delete r.effect_template;
for(const PostProcessor &p: postprocessors)
delete p.postprocessor_template;
}
return registry;
}
+template<>
+SequenceTemplate::TemplateRegistry<Effect> &SequenceTemplate::get_registry<Effect>()
+{
+ static TemplateRegistry<Effect> registry;
+ static bool initialized = false;
+ if(!initialized)
+ {
+ registry.register_type<EnvironmentMap::Template>("environment_map");
+ registry.register_type<ShadowMap::Template>("shadow_map");
+ registry.register_type<Sky::Template>("sky");
+ initialized = true;
+ }
+ return registry;
+}
+
SequenceTemplate::PostProcessor::PostProcessor(GL::PostProcessor::Template *ppt):
postprocessor_template(ppt)
add("hdr", &SequenceTemplate::hdr);
add("alpha", &SequenceTemplate::alpha);
add("clear", &Loader::clear);
+ add("effect", &Loader::effect);
add("multisample", &Loader::multisample);
add("multisample", &Loader::multisample_range);
add("postprocessor", &Loader::postprocessor);
add("postprocessor", &Loader::postprocessor_with_slot);
add("renderable", &Loader::renderable);
add("renderable", &Loader::renderable_with_default);
+ add("sequence", &Loader::sequence);
add("step", &Loader::step);
}
obj.clear_enabled = true;
}
+void SequenceTemplate::Loader::effect(const string &slot)
+{
+ TemplateLoader<Effect> ldr(get_collection());
+ load_sub_with(ldr);
+ Renderable rend;
+ rend.slot_name = slot;
+ rend.effect_template = ldr.get_object();
+ obj.renderables.push_back(rend);
+}
+
void SequenceTemplate::Loader::multisample(unsigned samples)
{
obj.required_multisample = samples;
obj.renderables.push_back(rend);
}
+void SequenceTemplate::Loader::sequence(const string &slot, const string &name)
+{
+ Renderable rend;
+ rend.sequence_template = &get_collection().get<SequenceTemplate>(name);
+ rend.slot_name = slot;
+ SequenceLoader ldr(rend);
+ load_sub_with(ldr);
+ obj.renderables.push_back(rend);
+}
+
void SequenceTemplate::Loader::step(const string &tag, const string &rend)
{
Step stp;
}
+DataFile::Loader::ActionMap SequenceTemplate::SequenceLoader::shared_actions;
+
+SequenceTemplate::SequenceLoader::SequenceLoader(Renderable &r):
+ ObjectLoader<Renderable>(r)
+{
+ set_actions(shared_actions);
+}
+
+void SequenceTemplate::SequenceLoader::init_actions()
+{
+ add("renderable", &SequenceLoader::renderable);
+}
+
+void SequenceTemplate::SequenceLoader::renderable(const string &slot, const string &name)
+{
+ obj.sequence_renderables[slot] = name;
+}
+
+
DataFile::Loader::ActionMap SequenceTemplate::Step::Loader::shared_actions;
SequenceTemplate::Step::Loader::Loader(Step &p, Collection &c):
#include <msp/datafile/dynamicobjectloader.h>
#include <msp/datafile/objectloader.h>
#include "depthtest.h"
+#include "effect.h"
#include "postprocessor.h"
#include "resources.h"
#include "stenciltest.h"
virtual void init_actions();
void clear();
+ void effect(const std::string &);
void multisample(unsigned);
void multisample_range(unsigned, unsigned);
void postprocessor();
void postprocessor_with_slot(const std::string &);
void renderable(const std::string &);
void renderable_with_default(const std::string &, const std::string &);
+ void sequence(const std::string &, const std::string &);
void step(const std::string &, const std::string &);
};
struct Renderable
{
+ Effect::Template *effect_template = 0;
GL::Renderable *renderable = 0;
+ SequenceTemplate *sequence_template = 0;
+ std::map<std::string, std::string> sequence_renderables;
std::string slot_name;
};
void stencil(int);
};
+ class SequenceLoader: public DataFile::ObjectLoader<Renderable>
+ {
+ private:
+ static ActionMap shared_actions;
+
+ public:
+ SequenceLoader(Renderable &);
+ private:
+ virtual void init_actions();
+
+ void renderable(const std::string &, const std::string &);
+ };
+
bool hdr = false;
bool alpha = false;
unsigned required_multisample = 0;
float get_clear_depth() const { return clear_depth; }
int get_clear_stencil() const { return clear_stencil; }
+ template<typename T>
+ static void register_effect(const std::string &);
+
template<typename T>
static void register_postprocessor(const std::string &);
static TemplateRegistry<T> &get_registry();
};
+template<typename T>
+void SequenceTemplate::register_effect(const std::string &kw)
+{
+ get_registry<Effect>().register_type<typename T::Template>(kw);
+}
+
template<typename T>
void SequenceTemplate::register_postprocessor(const std::string &kw)
{
enabled_methods.erase(tag);
}
+
+Effect::Template::Loader::Loader(Template &t, Collection &c):
+ CollectionObjectLoader<Template>(t, &c)
+{ }
+
+void Effect::Template::Loader::init_actions()
+{
+ add("content", &Template::content_name);
+}
+
} // namespace GL
} // namespace Msp
#define MSP_GL_EFFECT_H_
#include <set>
+#include <msp/datafile/objectloader.h>
#include "renderable.h"
namespace Msp {
*/
class Effect: public Renderable
{
+public:
+ struct Template
+ {
+ class Loader: public DataFile::CollectionObjectLoader<Template>
+ {
+ public:
+ Loader(Template &, Collection &);
+ protected:
+ virtual void init_actions();
+ };
+
+ std::string content_name;
+
+ virtual ~Template() = default;
+
+ virtual Effect *create(const std::map<std::string, Renderable *> &) const = 0;
+ };
+
protected:
Renderable &renderable;
std::set<Tag> enabled_methods;
#include <cmath>
#include <msp/strings/format.h>
#include "environmentmap.h"
+#include "error.h"
#include "mesh.h"
#include "renderer.h"
#include "resources.h"
#endif
}
+
+EnvironmentMap *EnvironmentMap::Template::create(const map<string, Renderable *> &renderables) const
+{
+ Renderable *content = get_item(renderables, content_name);
+ Renderable *environment = get_item(renderables, environment_name);
+ if(!content || !environment)
+ throw invalid_operation("EnvironmentMap::Template::create");
+
+ RefPtr<EnvironmentMap> env_map = new EnvironmentMap(size, format, roughness_levels, *content, *environment);
+ if(use_fixed_position)
+ env_map->set_fixed_position(fixed_position);
+ env_map->set_depth_clip(near_clip, far_clip);
+
+ return env_map.release();
+}
+
+
+DataFile::Loader::ActionMap EnvironmentMap::Template::Loader::shared_actions;
+
+EnvironmentMap::Template::Loader::Loader(Template &t, Collection &c):
+ DerivedObjectLoader<Template, Effect::Template::Loader>(t, c)
+{
+ set_actions(shared_actions);
+}
+
+void EnvironmentMap::Template::Loader::init_actions()
+{
+ Effect::Template::Loader::init_actions();
+ add("depth_clip", &Template::near_clip, &Template::far_clip);
+ add("environment", &Template::environment_name);
+ add("fixed_position", &Loader::fixed_position);
+ add("format", &Template::format);
+ add("roughness_levels", &Template::roughness_levels);
+ add("size", &Template::size);
+}
+
+void EnvironmentMap::Template::Loader::fixed_position(float x, float y, float z)
+{
+ obj.fixed_position = Vector3(x, y, z);
+ obj.use_fixed_position = true;
+}
+
} // namespace GL
} // namespace Msp
*/
class EnvironmentMap: public Effect
{
-private:
+public:
+ struct Template: Effect::Template
+ {
+ class Loader: public DataFile::DerivedObjectLoader<Template, Effect::Template::Loader>
+ {
+ private:
+ static ActionMap shared_actions;
+
+ public:
+ Loader(Template &, Collection &);
+ private:
+ virtual void init_actions();
+
+ void fixed_position(float, float, float);
+ };
+
+ unsigned size = 512;
+ PixelFormat format = RGB16F;
+ std::string environment_name;
+ unsigned roughness_levels = 5;
+ Vector3 fixed_position;
+ bool use_fixed_position = false;
+ float near_clip = 0.1f;
+ float far_clip = 100.0f;
+
+ virtual EnvironmentMap *create(const std::map<std::string, Renderable *> &) const;
+ };
+
+protected:
struct Face
{
Framebuffer fbo;
#endif
}
+
+ShadowMap *ShadowMap::Template::create(const map<string, Renderable *> &renderables) const
+{
+ Renderable *content = get_item(renderables, content_name);
+ if(!content || !lighting)
+ throw invalid_operation("ShadowMap::Template::create");
+
+ RefPtr<ShadowMap> shadow_map = new ShadowMap(width, height, *content, *lighting);
+ shadow_map->set_target(target, radius);
+ shadow_map->set_depth_bias(depth_bias);
+ shadow_map->set_darkness(darkness);
+
+ for(const ShadowedLight &l: lights)
+ {
+ Renderable *shadow_caster = get_item(renderables, l.shadow_caster_name);
+ if(!l.light || !shadow_caster)
+ throw invalid_operation("ShadowMap::Template::create");
+ if(const DirectionalLight *dir_light = dynamic_cast<const DirectionalLight *>(l.light))
+ shadow_map->add_light(*dir_light, l.size, *shadow_caster);
+ else if(const PointLight *point_light = dynamic_cast<const PointLight *>(l.light))
+ shadow_map->add_light(*point_light, l.size, *shadow_caster);
+ else
+ throw invalid_operation("ShadowMap::Template::create");
+ }
+
+ return shadow_map.release();
+}
+
+
+DataFile::Loader::ActionMap ShadowMap::Template::Loader::shared_actions;
+
+ShadowMap::Template::Loader::Loader(Template &t, Collection &c):
+ DerivedObjectLoader<Template, Effect::Template::Loader>(t, c)
+{
+ set_actions(shared_actions);
+}
+
+void ShadowMap::Template::Loader::init_actions()
+{
+ Effect::Template::Loader::init_actions();
+ add("darkness", &Template::darkness);
+ add("depth_bias", &Template::depth_bias);
+ add("light", &Loader::light);
+ add("lighting", &Template::lighting);
+ add("radius", &Template::radius);
+ add("size", &Loader::size_square);
+ add("size", &Template::width, &Template::height);
+ add("target", &Loader::target);
+}
+
+void ShadowMap::Template::Loader::light(const string &name)
+{
+ ShadowedLight light;
+ light.light = &get_collection().get<Light>(name);
+ load_sub(light);
+ obj.lights.push_back(light);
+}
+
+void ShadowMap::Template::Loader::size_square(unsigned s)
+{
+ obj.width = s;
+ obj.height = s;
+}
+
+void ShadowMap::Template::Loader::target(float x, float y, float z)
+{
+ obj.target = Vector3(x, y, z);
+}
+
+
+DataFile::Loader::ActionMap ShadowMap::Template::ShadowedLight::Loader::shared_actions;
+
+ShadowMap::Template::ShadowedLight::Loader::Loader(ShadowedLight &l):
+ ObjectLoader<ShadowedLight>(l)
+{
+ set_actions(shared_actions);
+}
+
+void ShadowMap::Template::ShadowedLight::Loader::init_actions()
+{
+ add("size", &ShadowedLight::size);
+ add("shadow_caster", &ShadowedLight::shadow_caster_name);
+}
+
} // namespace GL
} // namespace Msp
*/
class ShadowMap: public Effect
{
+public:
+ struct Template: Effect::Template
+ {
+ class Loader: public DataFile::DerivedObjectLoader<Template, Effect::Template::Loader>
+ {
+ private:
+ static ActionMap shared_actions;
+
+ public:
+ Loader(Template &, Collection &);
+ private:
+ virtual void init_actions();
+
+ void light(const std::string &);
+ void size_square(unsigned);
+ void target(float, float, float);
+ };
+
+ struct ShadowedLight
+ {
+ class Loader: public DataFile::ObjectLoader<ShadowedLight>
+ {
+ private:
+ static ActionMap shared_actions;
+
+ public:
+ Loader(ShadowedLight &);
+ private:
+ virtual void init_actions();
+ };
+
+ const Light *light = 0;
+ unsigned size;
+ std::string shadow_caster_name;
+ };
+
+ unsigned width = 2048;
+ unsigned height = 2048;
+ const Lighting *lighting = 0;
+ std::vector<ShadowedLight> lights;
+ Vector3 target;
+ float radius = 1.0f;
+ float depth_bias = 4.0f;
+ float darkness = 1.0f;
+
+ virtual ShadowMap *create(const std::map<std::string, Renderable *> &) const;
+ };
+
private:
enum ShadowType
{
#include <msp/geometry/hypersphere.h>
#include <msp/geometry/ray.h>
#include "directionallight.h"
+#include "error.h"
#include "mesh.h"
#include "renderer.h"
#include "resources.h"
return planet;
}
+
+Sky *Sky::Template::create(const map<string, Renderable *> &renderables) const
+{
+ Renderable *content = get_item(renderables, content_name);
+ if(!content || !sun)
+ throw invalid_operation("Sky::Template::create");
+ return new Sky(*content, *sun);
+}
+
+
+DataFile::Loader::ActionMap Sky::Template::Loader::shared_actions;
+
+Sky::Template::Loader::Loader(Template &t, Collection &c):
+ DerivedObjectLoader<Template, Effect::Template::Loader>(t, c)
+{
+ set_actions(shared_actions);
+}
+
+void Sky::Template::Loader::init_actions()
+{
+ Effect::Template::Loader::init_actions();
+ add("sun", &Template::sun);
+}
+
} // namespace GL
} // namespace Msp
static Planet earth();
};
+ struct Template: Effect::Template
+ {
+ class Loader: public DataFile::DerivedObjectLoader<Template, Effect::Template::Loader>
+ {
+ private:
+ static ActionMap shared_actions;
+
+ public:
+ Loader(Template &, Collection &);
+ private:
+ virtual void init_actions();
+ };
+
+ DirectionalLight *sun = 0;
+
+ virtual Sky *create(const std::map<std::string, Renderable *> &) const;
+ };
+
private:
Planet planet;
DirectionalLight &sun;