Jelajahi Sumber

+ Comments to FileResolver
~ Limit projects to scenes only
~ Updated shader definitions to compile all the WallpaperEngine shaders
+ Documented shader compiler

Signed-off-by: Alexis Maiquez <almamu@almamu.com>

Alexis Maiquez 7 tahun lalu
induk
melakukan
1c9db8c159

+ 2 - 1
CMakeLists.txt

@@ -4,6 +4,7 @@ project(wallengine)
 set(CMAKE_CXX_STANDARD 11)
 set(CMAKE_CXX_FLAGS "-fpermissive")
 set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules")
+set(OpenGL_GL_PREFERENCE "LEGACY")
 
 find_package(X11 REQUIRED)
 find_package(OpenGL REQUIRED)
@@ -13,6 +14,6 @@ find_package(Irrlicht REQUIRED)
 
 include_directories(${X11_INCLUDE_DIR} ${IRRLICHT_INCLUDE_DIR} .)
 
-add_executable(wallengine main.cpp common.h wallpaperengine/shaders/compiler.h wallpaperengine/shaders/compiler.cpp wallpaperengine/project.cpp wallpaperengine/project.h wallpaperengine/scene.cpp wallpaperengine/scene.h wallpaperengine/object.cpp wallpaperengine/object.h wallpaperengine/camera.cpp wallpaperengine/camera.h wallpaperengine/core.cpp wallpaperengine/core.h wallpaperengine/image.cpp wallpaperengine/image.h wallpaperengine/object3d.cpp wallpaperengine/object3d.h wallpaperengine/effect.cpp wallpaperengine/effect.h wallpaperengine/fs/fileResolver.cpp wallpaperengine/fs/fileResolver.h wallpaperengine/irrlicht.cpp wallpaperengine/irrlicht.h wallpaperengine/config.cpp wallpaperengine/config.h wallpaperengine/video/renderer.cpp wallpaperengine/video/renderer.h wallpaperengine/video/node.cpp wallpaperengine/video/node.h wallpaperengine/video/material.cpp wallpaperengine/video/material.h wallpaperengine/texture.cpp wallpaperengine/texture.h)
+add_executable(wallengine main.cpp wallpaperengine/shaders/compiler.h wallpaperengine/shaders/compiler.cpp wallpaperengine/project.cpp wallpaperengine/project.h wallpaperengine/scene.cpp wallpaperengine/scene.h wallpaperengine/object.cpp wallpaperengine/object.h wallpaperengine/camera.cpp wallpaperengine/camera.h wallpaperengine/core.cpp wallpaperengine/core.h wallpaperengine/image.cpp wallpaperengine/image.h wallpaperengine/object3d.cpp wallpaperengine/object3d.h wallpaperengine/effect.cpp wallpaperengine/effect.h wallpaperengine/fs/fileResolver.cpp wallpaperengine/fs/fileResolver.h wallpaperengine/irrlicht.cpp wallpaperengine/irrlicht.h wallpaperengine/config.cpp wallpaperengine/config.h wallpaperengine/video/renderer.cpp wallpaperengine/video/renderer.h wallpaperengine/video/node.cpp wallpaperengine/video/node.h wallpaperengine/video/material.cpp wallpaperengine/video/material.h wallpaperengine/texture.cpp wallpaperengine/texture.h)
 
 target_link_libraries(wallengine ${X11_LIBRARIES} ${X11_Xxf86vm_LIB} ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${ZLIB_LIBRARIES} ${IRRLICHT_LIBRARY})

+ 14 - 0
docs/TEXTURE_FORMAT.md

@@ -0,0 +1,14 @@
+# Texture format
+Wallpaper engine uses a custom texture format converted by resourcecompiler.exe
+
+## Header
+
+| Type indicator |
+|---|
+| 8 bytes  |
+
+#### Format
+
+|   |
+|---|
+|   |

+ 60 - 0
wallpaperengine/fs/fileResolver.h

@@ -1,3 +1,6 @@
+/**
+ * @author Alexis Maiquez Murcia <almamu@almamu.com>
+ */
 #ifndef WALLENGINE_RESOLVER_H
 #define WALLENGINE_RESOLVER_H
 
@@ -9,20 +12,70 @@
 namespace wp
 {
     using json = nlohmann::json;
+
     namespace fs
     {
+        /**
+         * Custom file resolver to limit our searches to specific folders
+         */
         class fileResolver
         {
         public:
+            /**
+             * Basic constructor, uses current path as base folder
+             */
             fileResolver ();
+            /**
+             * Creates a file resolver using a list as environment folders
+             *
+             * @param environment List of directories to use for finding files
+             */
             fileResolver (std::vector<irr::io::path> environment);
 
+            /**
+             * Adds a new folder to the environment list
+             *
+             * @param path
+             */
             void appendEnvironment (irr::io::path path);
+            /**
+             * Removes the given folder from the environment list
+             *
+             * @param path
+             */
             void removeEnvironment (irr::io::path path);
+            /**
+             * Updates the current working directory
+             *
+             * @param newpath
+             */
             void changeWorkingDirectory (irr::io::path newpath);
+            /**
+             * @return The current working directory
+             */
             irr::io::path getWorkingDirectory ();
+            /**
+             * @return A clone of this fileResolver
+             */
             fileResolver clone ();
+            /**
+             * Resolves the given filename or relative path
+             * on the current working directory or any of the registered
+             * environment folders
+             *
+             * @param name The filename/relative path to resolve
+             *
+             * @return The full path to the file
+             */
             irr::io::path resolve (irr::io::path name);
+            /**
+             * Resolves the given filename or relative path
+             * on the current working directory only
+             *
+             * @param name The filename/relative path to resolve
+             *
+             * @return  The full path to the file
+             */
             irr::io::path resolveOnWorkingDirectory (irr::io::path name);
             irr::io::path resolve (json name);
             irr::io::path resolveOnWorkingDirectory (json name);
@@ -33,9 +86,16 @@ namespace wp
             void prependEnvironment (irr::io::path path);
 
         private:
+            /**
+             * List of environment paths in which the resolver will look for
+             * files in when trying to resolve them
+             */
             std::vector<irr::io::path> m_environment;
         };
 
+        /**
+         * Static resolver used trough most of the application
+         */
         extern fileResolver resolver;
     };
 }

+ 14 - 0
wallpaperengine/image.cpp

@@ -87,6 +87,20 @@ namespace wp
         }
 
         // TODO: CHECK EFFECT PASSES HERE SO WE CAN TAKE IN ACCOUNT THE EXTRA TEXTURES FOR SHADERS, ETC
+        json::const_iterator effects = json_data.find ("effects");
+
+        if (effects != json_data.end ())
+        {
+            // TODO: SUPPORT MULTIPLE EFFECTS
+            json::const_iterator effectsItem = (*effects).begin ();
+
+            if (effectsItem != (*effects).end ())
+            {
+                // TODO: SUPPORT MULTIPLE PASSES FOR EFFECTS
+                json::const_iterator passes = (*effectsItem).find ("passes");
+                json::const_iterator file = (*effectsItem).find ("file");
+            }
+        }
 
         if (origin_it != json_data.end ())
         {

+ 5 - 0
wallpaperengine/project.cpp

@@ -38,6 +38,11 @@ namespace wp
         {
             this->m_title = name_it.value ();
         }
+
+        if (this->m_type != "scene")
+        {
+            throw "Only scene wallpapers supported for now";
+        }
     }
 
     scene* project::getScene ()

+ 16 - 2
wallpaperengine/shaders/compiler.cpp

@@ -24,10 +24,24 @@ namespace wp
             // begin with an space so it gets ignored properly on parse
             if (recursive == false)
             {
+                // compatibility layer for OpenGL shaders
                 this->m_content =   "#version 120\n"
-                                    "#define texSample2D texture2D\n"
+                                    "#define highp\n"
+                                    "#define mediump\n"
+                                    "#define lowp\n"
+                                    "#define mul(x, y) (y * x)\n"
                                     "#define frac fract\n"
-                                    "vec4 mul(vec4 x, mat4 y) { return x * y; }\n";
+                                    "#define CAST2(x) (vec2(x))\n"
+                                    "#define CAST3(x) (vec3(x))\n"
+                                    "#define CAST4(x) (vec4(x))\n"
+                                    "#define CAST3X3(x) (mat3(x))\n"
+                                    "#define saturate(x) (clamp(x, 0.0, 1.0))\n"
+                                    "#define texSample2D texture2D\n"
+                                    "#define texSample2DLod texture2DLod\n"
+                                    "#define atan2 atan\n"
+                                    "#define ddx dFdx\n"
+                                    "#define ddy(x) dFdy(-(x))\n"
+                                    "#define GLSL 1\n";
             }
             else
             {

+ 130 - 0
wallpaperengine/shaders/compiler.h

@@ -18,6 +18,11 @@ namespace wp
         class compiler
         {
         public:
+            /**
+             * Basic struct used to define all the shader variables
+             * the compiler will replace in pre-processing time
+             * to make sure the shaders compile under OpenGL
+             */
             struct VariableReplacement
             {
                 const char* original;
@@ -30,39 +35,164 @@ namespace wp
                 int size;
             };
 
+            /**
+             * Types of shaders
+             */
             enum Type
             {
                 Type_Vertex = 0,
                 Type_Pixel = 1,
             };
 
+            /**
+             * List of variables to replace when pre-process is performed
+             */
             static std::map<std::string, std::string> sVariableReplacement;
+            /**
+             * Types of variables the pre-processor understands
+             */
             static std::vector<std::string> sTypes;
 
+            /**
+             * Compiler constructor, loads the given shader file and prepares
+             * the pre-processing and compilation of the shader, adding
+             * required definitions if needed
+             *
+             * @param file The file to load
+             * @param type The type of shader
+             * @param recursive Whether the compiler should add base definitions or not
+             */
             compiler (irr::io::path file, Type type, bool recursive = false);
+            /**
+             * Performs the actual pre-compilation/pre-processing over the shader files
+             * This step is kinda big, replaces variables names on sVariableReplacement,
+             * ensures #include directives are correctly handled
+             * and takes care of attribute comments for the wallpaper engine specifics
+             *
+             * @return The shader contents ready to be used by OpenGL
+             */
             std::string precompile();
 
         private:
+            /**
+             * Checks if there is "str" in the current position without advancing the
+             * iterator in use
+             *
+             * @param str The string to check for
+             * @param it  The position to start checking at
+             *
+             * @return
+             */
             bool peekString (std::string str, std::string::const_iterator& it);
+            /**
+             * Checks for a semicolon as current character, advancing the iterator
+             * after finding it, otherwise returns an error
+             *
+             * @param it The position where to expect the semicolon
+             *
+             * @return
+             */
             bool expectSemicolon (std::string::const_iterator& it);
+            /**
+             * Ignores contiguous space characters in the string advancing the iterator
+             * until the first non-space character
+             *
+             * @param it The iterator to increase
+             */
             void ignoreSpaces (std::string::const_iterator& it);
+            /**
+             * Ignores all characters until next line-fee (\n) advancing the interator
+             *
+             * @param it The iterator to increase
+             */
             void ignoreUpToNextLineFeed (std::string::const_iterator& it);
+            /**
+             * Ignores all characters until a block comment end is found, advancing the iterator
+             *
+             * @param it The iterator to increase
+             */
             void ignoreUpToBlockCommentEnd (std::string::const_iterator& it);
+            /**
+             * Parses the current position as a variable type, extracts it and compares it
+             * to the registered types in the pre-processor, returning it's name if valid
+             * increasing the iterator at the same time
+             *
+             * @param it The position to extract it from
+             *
+             * @return The type name
+             */
             std::string extractType (std::string::const_iterator& it);
+            /**
+             * Parses the current position as a variable name, extractig it's name and
+             * increasing the iterator as the name is extracted
+             *
+             * @param it The position to start extracting the variable name from
+             *
+             * @return The variable name
+             */
             std::string extractName (std::string::const_iterator& it);
+            /**
+             * Parses the current position as a quoted value, extracting it's value
+             * and increasing the iterator at the same time
+             *
+             * @param it The position to start extracting the value from
+             *
+             * @return The value
+             */
             std::string extractQuotedValue (std::string::const_iterator& it);
+            /**
+             * Tries to find the given shader file and compile it
+             *
+             * @param filename The shader's filename
+             *
+             * @return The compiled contents
+             */
             std::string lookupShaderFile (std::string filename);
+            /**
+             * Searches for the given symbol in the replace table
+             *
+             * @param symbol The symbol to look for
+             *
+             * @return The symbol it should be replaced with
+             */
             std::string lookupReplaceSymbol (std::string symbol);
 
+            /**
+             * @return Whether the character in the current position is a character or not
+             */
             bool isChar (std::string::const_iterator& it);
+            /**
+             * @return Whether the character in the current position is a number or not
+             */
             bool isNumeric (std::string::const_iterator& it);
 
+            /**
+             * The shader file this instance is loading
+             */
             irr::io::path m_file;
+            /**
+             * The original file content
+             */
             std::string m_content;
+            /**
+             * The final, compiled content ready to be used by OpenGL
+             */
             std::string m_compiledContent;
+            /**
+             * Whether there was any kind of error in the compilation or not
+             */
             bool m_error;
+            /**
+             * Extra information about the error (if any)
+             */
             std::string m_errorInfo;
+            /**
+             * The type of shader
+             */
             Type m_type;
+            /**
+             * The file resolver to be used by the compiler to load the files
+             */
             wp::fs::fileResolver m_resolver;
         };
     }