Bladeren bron

+ Added parsing of images info and their materials

Signed-off-by: Alexis Maiquez <almamu@almamu.com>
Alexis Maiquez 7 jaren geleden
bovenliggende
commit
2ba3498517

+ 22 - 1
CMakeLists.txt

@@ -1,7 +1,7 @@
 cmake_minimum_required(VERSION 3.0)
 project(wallengine)
 
-set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_CXX_STANDARD 17)
 set(CMAKE_CXX_FLAGS "-fpermissive")
 set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules")
 set(OpenGL_GL_PREFERENCE "LEGACY")
@@ -58,6 +58,27 @@ add_executable(
         wallpaperengine/irr/CFileList.cpp
         wallpaperengine/sound.cpp
         wallpaperengine/sound.h
+
+        wallpaperengine/core/project.cpp
+        wallpaperengine/core/project.h
+        wallpaperengine/core/scene.cpp
+        wallpaperengine/core/scene.h
+        wallpaperengine/core/object.cpp
+        wallpaperengine/core/object.h
+
+        wallpaperengine/core/scenes/camera.cpp
+        wallpaperengine/core/scenes/camera.h
+        wallpaperengine/core/scenes/projection.cpp
+        wallpaperengine/core/scenes/projection.h
+
+        wallpaperengine/core/objects/image.cpp
+        wallpaperengine/core/objects/image.h
+
+        wallpaperengine/core/objects/images/material.cpp
+        wallpaperengine/core/objects/images/material.h
+
+        wallpaperengine/core/objects/images/materials/passes.cpp
+        wallpaperengine/core/objects/images/materials/passes.h
 )
 
 target_link_libraries(wallengine ${X11_LIBRARIES} ${X11_Xxf86vm_LIB} ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${ZLIB_LIBRARIES} ${IRRLICHT_LIBRARY} ${LZ4_LIBRARY} ${SDL_LIBRARY} ${SDL_MIXER_LIBRARIES})

+ 82 - 0
wallpaperengine/core/object.cpp

@@ -0,0 +1,82 @@
+#include "object.h"
+#include "objects/image.h"
+
+#include "../core.h"
+
+using namespace wp::core;
+
+
+object::object (
+        bool visible,
+        irr::u32 id,
+        std::string name,
+        const irr::core::vector3df& origin,
+        const irr::core::vector3df& scale,
+        const irr::core::vector3df& angles) :
+    m_visible (visible),
+    m_id (id),
+    m_name (name),
+    m_origin (origin),
+    m_scale (scale),
+    m_angles (angles)
+{
+}
+
+object* object::fromJSON (json data)
+{
+    json::const_iterator id_it = data.find ("id");
+    json::const_iterator visible_it = data.find ("visible");
+    json::const_iterator origin_it = data.find ("origin");
+    json::const_iterator scale_it = data.find ("scale");
+    json::const_iterator size_it = data.find ("size");
+    json::const_iterator angles_it = data.find ("angles");
+    json::const_iterator name_it = data.find ("name");
+
+    bool visible = true;
+
+    if (id_it == data.end ())
+    {
+        throw std::runtime_error ("Objects must have id");
+    }
+
+    if (origin_it == data.end ())
+    {
+        throw std::runtime_error ("Objects must have origin point");
+    }
+
+    if (scale_it == data.end ())
+    {
+        throw std::runtime_error ("Objects must have scale");
+    }
+
+    if (angles_it == data.end ())
+    {
+        throw std::runtime_error ("Objects must have angles");
+    }
+
+    if (name_it == data.end ())
+    {
+        throw std::runtime_error ("Objects must have name");
+    }
+
+    // visibility is optional
+    if (visible_it != data.end ())
+    {
+        visible = *visible_it;
+    }
+
+    json::const_iterator image_it = data.find ("image");
+
+    if (image_it != data.end () && (*image_it).is_null () == false)
+    {
+        return objects::image::fromJSON (
+            data,
+            visible,
+            *id_it,
+            *name_it,
+            wp::core::ato3vf (*origin_it),
+            wp::core::ato3vf (*scale_it),
+            wp::core::ato3vf (*angles_it)
+        );
+    }
+}

+ 31 - 0
wallpaperengine/core/object.h

@@ -0,0 +1,31 @@
+#pragma once
+
+#include <nlohmann/json.hpp>
+#include <irrlicht/irrlicht.h>
+
+namespace wp::core
+{
+    using json = nlohmann::json;
+
+    class object
+    {
+    public:
+        static object* fromJSON (json data);
+    protected:
+        object (
+            bool visible,
+            irr::u32 id,
+            std::string name,
+            const irr::core::vector3df& origin,
+            const irr::core::vector3df& scale,
+            const irr::core::vector3df& angles
+        );
+    private:
+        bool m_visible;
+        irr::u32 m_id;
+        std::string m_name;
+        irr::core::vector3df m_origin;
+        irr::core::vector3df m_scale;
+        irr::core::vector3df m_angles;
+    };
+};

+ 60 - 0
wallpaperengine/core/objects/image.cpp

@@ -0,0 +1,60 @@
+#include "image.h"
+#include "images/material.h"
+
+#include "../../core.h"
+#include "../../fs/utils.h"
+
+using namespace wp::core::objects;
+
+image::image (
+        images::material* material,
+        bool visible,
+        irr::u32 id,
+        std::string name,
+        const irr::core::vector3df& origin,
+        const irr::core::vector3df& scale,
+        const irr::core::vector3df& angles,
+        const irr::core::vector2df& size) :
+    object (visible, id, std::move(name), origin, scale, angles),
+    m_size (size)
+{
+
+}
+
+wp::core::object* image::fromJSON (
+    json data,
+    bool visible,
+    irr::u32 id,
+    std::string name,
+    const irr::core::vector3df& origin,
+    const irr::core::vector3df& scale,
+    const irr::core::vector3df& angles)
+{
+    json::const_iterator image_it = data.find ("image");
+    json::const_iterator size_it = data.find ("size");
+
+    if (size_it == data.end ())
+    {
+        throw std::runtime_error ("Images must have size");
+    }
+
+    json content = json::parse (wp::fs::utils::loadFullFile ((*image_it).get <std::string> ().c_str ()));
+
+    json::const_iterator material_it = content.find ("material");
+
+    if (material_it == content.end ())
+    {
+        throw std::runtime_error ("Image must have a material");
+    }
+
+    return new image (
+        images::material::fromFile ((*material_it).get <std::string> ().c_str ()),
+        visible,
+        id,
+        name,
+        origin,
+        scale,
+        angles,
+        wp::core::ato2vf (*size_it)
+    );
+}

+ 42 - 0
wallpaperengine/core/objects/image.h

@@ -0,0 +1,42 @@
+#pragma once
+
+#include <nlohmann/json.hpp>
+#include <irrlicht/irrlicht.h>
+
+#include "../object.h"
+#include "images/material.h"
+
+namespace wp::core::objects
+{
+    using json = nlohmann::json;
+
+    class image : object
+    {
+    protected:
+        friend class object;
+
+        static object* fromJSON (
+            json data,
+            bool visible,
+            irr::u32 id,
+            std::string name,
+            const irr::core::vector3df& origin,
+            const irr::core::vector3df& scale,
+            const irr::core::vector3df& angles
+        );
+
+        image (
+            images::material* material,
+            bool visible,
+            irr::u32 id,
+            std::string name,
+            const irr::core::vector3df& origin,
+            const irr::core::vector3df& scale,
+            const irr::core::vector3df& angles,
+            const irr::core::vector2df& size
+        );
+
+    private:
+        irr::core::vector2df m_size;
+    };
+};

+ 48 - 0
wallpaperengine/core/objects/images/material.cpp

@@ -0,0 +1,48 @@
+#include "material.h"
+
+#include <irrlicht/irrlicht.h>
+#include <nlohmann/json.hpp>
+
+#include "../../../fs/utils.h"
+
+using namespace wp::core::objects::images;
+
+material::material ()
+{
+}
+
+material* material::fromFile (irr::io::path filename)
+{
+    json content = json::parse (wp::fs::utils::loadFullFile (filename));
+
+    json::const_iterator passes_it = content.find ("passes");
+
+    if (passes_it == content.end ())
+    {
+        throw std::runtime_error ("Material must have at least one pass");
+    }
+
+    material* material = new class material ();
+
+    json::const_iterator cur = (*passes_it).begin ();
+    json::const_iterator end = (*passes_it).end ();
+
+    for (; cur != end; cur ++)
+    {
+        material->insertPass (
+            materials::passes::fromJSON (*cur)
+        );
+    }
+
+    return material;
+}
+
+void material::insertPass (materials::passes* mass)
+{
+    this->m_passes.push_back (mass);
+}
+
+std::vector <materials::passes*>* material::getPasses ()
+{
+    return &this->m_passes;
+}

+ 25 - 0
wallpaperengine/core/objects/images/material.h

@@ -0,0 +1,25 @@
+#pragma once
+
+#include <irrlicht/irrlicht.h>
+#include <nlohmann/json.hpp>
+
+#include "materials/passes.h"
+
+namespace wp::core::objects::images
+{
+    using json = nlohmann::json;
+
+    class material
+    {
+    public:
+        static material* fromFile (irr::io::path filename);
+
+        void insertPass (materials::passes* mass);
+
+        std::vector <materials::passes*>* getPasses ();
+    protected:
+        material ();
+    private:
+        std::vector <materials::passes*> m_passes;
+    };
+};

+ 93 - 0
wallpaperengine/core/objects/images/materials/passes.cpp

@@ -0,0 +1,93 @@
+//
+// Created by almamu on 13/08/19.
+//
+
+#include <nlohmann/json.hpp>
+#include <utility>
+
+#include "passes.h"
+
+using namespace wp::core::objects::images::materials;
+
+passes::passes (std::string blending, std::string cullmode, std::string depthtest, std::string depthwrite, std::string shader) :
+    m_blending (std::move(blending)),
+    m_cullmode (std::move(cullmode)),
+    m_depthtest (std::move(depthtest)),
+    m_depthwrite (std::move(depthwrite)),
+    m_shader (std::move(shader))
+{
+}
+
+std::vector<std::string>* passes::getTextures ()
+{
+    return &this->m_textures;
+}
+
+passes* passes::fromJSON (json data)
+{
+    json::const_iterator blending_it = data.find ("blending");
+    json::const_iterator cullmode_it = data.find ("cullmode");
+    json::const_iterator depthtest_it = data.find ("depthtest");
+    json::const_iterator depthwrite_it = data.find ("depthwrite");
+    json::const_iterator shader_it = data.find ("shader");
+    json::const_iterator textures_it = data.find ("textures");
+
+    if (blending_it == data.end ())
+    {
+        throw std::runtime_error ("Material pass must have blending specified");
+    }
+
+    if (cullmode_it == data.end ())
+    {
+        throw std::runtime_error ("Material pass must have cullmode specified");
+    }
+
+    if (depthtest_it == data.end ())
+    {
+        throw std::runtime_error ("Material pass must have depthtest specified");
+    }
+
+    if (depthwrite_it == data.end ())
+    {
+        throw std::runtime_error ("Material pass must have depthwrite specified");
+    }
+
+    if (shader_it == data.end ())
+    {
+        throw std::runtime_error ("Material pass must have shader specified");
+    }
+
+    if (textures_it == data.end ())
+    {
+        throw std::runtime_error ("Material pass must have textures specified");
+    }
+
+    if ((*textures_it).is_array () == false)
+    {
+        throw std::runtime_error ("Textures for material must be a list");
+    }
+
+    passes* pass = new passes (
+        *blending_it,
+        *cullmode_it,
+        *depthtest_it,
+        *depthwrite_it,
+        *shader_it
+    );
+
+    json::const_iterator cur = (*textures_it).begin ();
+    json::const_iterator end = (*textures_it).end ();
+
+    for (;cur != end; cur ++)
+    {
+        pass->insertTexture (*cur);
+    }
+
+    return pass;
+}
+
+
+void passes::insertTexture (std::string texture)
+{
+    this->m_textures.push_back (texture);
+}

+ 27 - 0
wallpaperengine/core/objects/images/materials/passes.h

@@ -0,0 +1,27 @@
+#pragma once
+
+#include <nlohmann/json.hpp>
+
+namespace wp::core::objects::images::materials
+{
+    using json = nlohmann::json;
+
+    class passes
+    {
+    public:
+        static passes* fromJSON (json data);
+
+        std::vector<std::string>* getTextures ();
+    protected:
+        passes (std::string blending, std::string cullmode, std::string depthtest, std::string depthwrite, std::string shader);
+
+        void insertTexture (std::string texture);
+    private:
+        std::string m_blending;
+        std::string m_cullmode;
+        std::string m_depthtest;
+        std::string m_depthwrite;
+        std::string m_shader;
+        std::vector<std::string> m_textures;
+    };
+}

+ 25 - 1
wallpaperengine/core/scene.cpp

@@ -52,6 +52,7 @@ scene* scene::fromFile (const irr::io::path& filename)
 
     json::const_iterator camera_it = content.find ("camera");
     json::const_iterator general_it = content.find ("general");
+    json::const_iterator objects_it = content.find ("objects");
 
     if (camera_it == content.end ())
     {
@@ -63,6 +64,11 @@ scene* scene::fromFile (const irr::io::path& filename)
         throw std::runtime_error ("Scenes must have a general section");
     }
 
+    if (objects_it == content.end ())
+    {
+        throw std::runtime_error ("Scenes must have a list of objects to display");
+    }
+
     json::const_iterator ambientcolor_it = (*general_it).find ("ambientcolor");
     json::const_iterator bloom_it = (*general_it).find ("bloom");
     json::const_iterator bloomstrength_it = (*general_it).find ("bloomstrength");
@@ -166,7 +172,7 @@ scene* scene::fromFile (const irr::io::path& filename)
         throw std::runtime_error ("General section must have skylight color");
     }
 
-    return new scene (
+    scene* scene = new class scene (
         scenes::camera::fromJSON (*camera_it),
         wp::core::atoSColorf (*ambientcolor_it),
         *bloom_it,
@@ -186,6 +192,24 @@ scene* scene::fromFile (const irr::io::path& filename)
         scenes::projection::fromJSON (*orthogonalprojection_it),
         wp::core::atoSColorf (*skylightcolor_it)
     );
+
+    json::const_iterator cur = (*objects_it).begin ();
+    json::const_iterator end = (*objects_it).end ();
+
+    for (; cur != end; cur ++)
+    {
+        scene->insertObject (
+            object::fromJSON (*cur)
+        );
+    }
+
+    return scene;
+}
+
+
+void scene::insertObject (object* object)
+{
+    this->m_objects.push_back (object);
 }
 
 project* scene::getProject ()

+ 5 - 0
wallpaperengine/core/scene.h

@@ -4,6 +4,7 @@
 #include <nlohmann/json.hpp>
 
 #include "project.h"
+#include "object.h"
 
 #include "scenes/camera.h"
 #include "scenes/projection.h"
@@ -13,6 +14,7 @@ namespace wp::core
     using json = nlohmann::json;
 
     class project;
+    class object;
 
     class scene
     {
@@ -67,6 +69,7 @@ namespace wp::core
             irr::video::SColorf skylightColor
         );
 
+        void insertObject (object* object);
     private:
         project* m_project;
         scenes::camera* m_camera;
@@ -89,5 +92,7 @@ namespace wp::core
         irr::video::SColorf m_clearColor;
         scenes::projection* m_orthogonalProjection;
         irr::video::SColorf m_skylightColor;
+
+        std::vector<object*> m_objects;
     };
 };