]> git.tdb.fi Git - libs/gl.git/blobdiff - source/resources/resources.cpp
Split the Light class into subclasses by light type
[libs/gl.git] / source / resources / resources.cpp
index 62bdabf60594e796439833b110e512bcd3544b24..fa20a0bf2e38d5cc072908480ce0bf3a62008257 100644 (file)
@@ -2,28 +2,34 @@
 #include <msp/fs/utils.h>
 #include "animation.h"
 #include "armature.h"
+#include "basicmaterial.h"
 #include "camera.h"
+#include "directionallight.h"
 #include "error.h"
 #include "font.h"
 #include "keyframe.h"
-#include "light.h"
 #include "lighting.h"
-#include "material.h"
 #include "mesh.h"
 #include "module.h"
 #include "object.h"
+#include "occludedscene.h"
+#include "orderedscene.h"
+#include "pbrmaterial.h"
 #include "sequencetemplate.h"
+#include "pointlight.h"
 #include "pose.h"
 #include "program.h"
 #include "resourcemanager.h"
 #include "resources.h"
 #include "sampler.h"
-#include "scene.h"
+#include "simplescene.h"
 #include "technique.h"
 #include "texture1d.h"
 #include "texture2d.h"
 #include "texture2darray.h"
 #include "texturecube.h"
+#include "unlitmaterial.h"
+#include "zsortedscene.h"
 #include "glsl/compiler.h"
 
 using namespace std;
@@ -42,40 +48,59 @@ Resources::Resources(bool set_as_global):
 {
        add_type<Animation>().suffix(".anim").keyword("animation");
        add_type<Armature>().suffix(".arma").keyword("armature");
+       add_type<BasicMaterial>().base<Material>().suffix(".mat")
+               .creator([this](const string &n) -> BasicMaterial * { create_generic<Material>(n); return 0; })
+               .notify(&set_debug_name<Material>);
        add_type<Camera>().keyword("camera")
-               .notify(&Resources::set_debug_name<Camera>);
+               .notify(&set_debug_name<Camera>);
+       add_type<DirectionalLight>().base<Light>().suffix(".light")
+               .creator([this](const string &n) -> DirectionalLight * { create_generic<Light>(n); return 0; });
        add_type<Font>().keyword("font");
        add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
-       add_type<Light>().keyword("light");
        add_type<Lighting>().suffix(".lightn").keyword("lighting")
-               .notify(&Resources::set_debug_name<Lighting>);
-       add_type<Material>().suffix(".mat")
-               .creator(&Resources::create_material).notify(&Resources::set_debug_name<Material>);
+               .notify(&set_debug_name<Lighting>);
        add_type<Mesh>().keyword("mesh")
-               .creator(&Resources::create_mesh).notify(&Resources::set_debug_name<Mesh>);
+               .creator([this](const string &n){ return create_mesh(n); })
+               .notify(&set_debug_name<Mesh>);
        add_type<Module>().suffix(".glsl").suffix(".spv")
-               .creator(&Resources::create_module);
+               .creator([this](const string &n){ return create_module(n); });
        add_type<Object>().keyword("object");
+       add_type<OccludedScene>().base<Scene>().suffix(".scene")
+               .creator([this](const string &n) -> OccludedScene * { create_generic<Scene>(n); return 0; });
+       add_type<OrderedScene>().base<Scene>().suffix(".scene")
+               .creator([this](const string &n) -> OrderedScene * { create_generic<Scene>(n); return 0; });
+       add_type<PbrMaterial>().base<Material>().suffix(".mat")
+               .creator([this](const string &n) -> PbrMaterial * { create_generic<Material>(n); return 0; })
+               .notify(&set_debug_name<Material>);
+       add_type<PointLight>().base<Light>().suffix(".light")
+               .creator([this](const string &n) -> PointLight * { create_generic<Light>(n); return 0; });
        add_type<SequenceTemplate>().suffix(".seq").keyword("sequence");
        add_type<Pose>().keyword("pose");
        add_type<Program>().keyword("shader")
-               .creator(&Resources::create_program).notify(&Resources::set_debug_name<Program>);
+               .creator([this](const string &n){ return create_program(n); })
+               .notify(&set_debug_name<Program>);
        add_type<Sampler>().suffix(".samp").keyword("sampler")
-               .notify(&Resources::set_debug_name<Sampler>);
-       add_type<Scene>().suffix(".scene")
-               .creator(&Resources::create_scene);
+               .notify(&set_debug_name<Sampler>);
+       add_type<SimpleScene>().base<Scene>().suffix(".scene")
+               .creator([this](const string &n) -> SimpleScene * { create_generic<Scene>(n); return 0; });
        add_type<Technique>().suffix(".tech").keyword("technique")
-               .notify(&Resources::set_debug_name<Technique>);
+               .notify(&set_debug_name<Technique>);
        add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d")
-               .notify(&Resources::set_debug_name<Texture1D>);
+               .notify(&set_debug_name<Texture1D>);
        add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d")
-               .creator(&Resources::create_texture2d).notify(&Resources::set_debug_name<Texture2D>);
+               .creator([this](const string &n){ return create_texture2d(n); })
+               .notify(&set_debug_name<Texture2D>);
        add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d")
-               .notify(&Resources::set_debug_name<Texture3D>);
+               .notify(&set_debug_name<Texture3D>);
        add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube")
-               .notify(&Resources::set_debug_name<TextureCube>);
+               .notify(&set_debug_name<TextureCube>);
        add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array")
-               .notify(&Resources::set_debug_name<Texture2DArray>);
+               .notify(&set_debug_name<Texture2DArray>);
+       add_type<UnlitMaterial>().base<Material>().suffix(".mat")
+               .creator([this](const string &n) -> UnlitMaterial * { create_generic<Material>(n); return 0; })
+               .notify(&set_debug_name<Material>);
+       add_type<ZSortedScene>().base<Scene>().suffix(".scene")
+               .creator([this](const string &n) -> ZSortedScene * { create_generic<Scene>(n); return 0; });
 
        add_source(get_builtins());
 
@@ -121,14 +146,15 @@ void Resources::set_resource_manager(ResourceManager *m)
        resource_manager = m;
 }
 
-Material *Resources::create_material(const string &name)
+template<typename T>
+T *Resources::create_generic(const string &name)
 {
        if(RefPtr<IO::Seekable> io = open_raw(name))
        {
                DataFile::Parser parser(*io, name);
-               Material::GenericLoader ldr(this);
+               typename T::GenericLoader ldr(*this);
                ldr.load(parser);
-               return ldr.get_material();
+               ldr.store_object(*this, name);
        }
 
        return 0;
@@ -149,19 +175,6 @@ Mesh *Resources::create_mesh(const string &name)
        return 0;
 }
 
-Scene *Resources::create_scene(const string &name)
-{
-       if(RefPtr<IO::Seekable> io = open_raw(name))
-       {
-               DataFile::Parser parser(*io, name);
-               Scene::GenericLoader ldr(*this);
-               ldr.load(parser);
-               return ldr.get_scene();
-       }
-
-       return 0;
-}
-
 Texture2D *Resources::create_texture2d(const string &name)
 {
        string ext = FS::extpart(name);
@@ -269,7 +282,7 @@ void Resources::Loader::scene(const string &name)
 {
        Scene::GenericLoader ldr(obj);
        load_sub_with(ldr);
-       obj.add(name, ldr.get_scene());
+       obj.add(name, ldr.get_object());
 }
 
 } // namespace GL