瀏覽代碼

Added g_ModelMatrix, g_NormalModelMatrix, g_ViewProjectionMatrix, g_LightAmbientColor and g_LightSkylightColor to shaders
Added support for specifying array variables for shaders

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

Alexis Maiquez 3 年之前
父節點
當前提交
96b931d931

+ 5 - 0
src/WallpaperEngine/Render/Objects/CImage.cpp

@@ -263,6 +263,8 @@ CImage::CImage (CScene* scene, Core::Objects::CImage* image) :
             this->getScene ()->getCamera ()->getLookAt ();
 
     this->m_modelViewProjectionCopy = glm::ortho <float> (0.0, size.x, 0.0, size.y);
+    this->m_modelMatrix = glm::ortho <float> (0.0, size.x, 0.0, size.y);
+    this->m_viewProjectionMatrix = glm::mat4 (1.0);
 }
 
 void CImage::setup ()
@@ -363,6 +365,9 @@ void CImage::setupPasses ()
         glm::mat4* projection = (first) ? &this->m_modelViewProjectionCopy : &this->m_modelViewProjectionPass;
         first = false;
 
+        pass->setModelMatrix (&this->m_modelMatrix);
+        pass->setViewProjectionMatrix (&this->m_viewProjectionMatrix);
+
         // set viewport and target texture if needed
         if (pass->getMaterial ()->getMaterial ()->hasTarget ())
         {

+ 3 - 0
src/WallpaperEngine/Render/Objects/CImage.h

@@ -74,6 +74,9 @@ namespace WallpaperEngine::Render::Objects
         glm::mat4 m_modelViewProjectionPass;
         glm::mat4 m_modelViewProjectionCopy;
 
+        glm::mat4 m_modelMatrix;
+        glm::mat4 m_viewProjectionMatrix;
+
         CFBO* m_mainFBO;
         CFBO* m_subFBO;
         CFBO* m_currentMainFBO;

+ 33 - 1
src/WallpaperEngine/Render/Objects/Effects/CPass.cpp

@@ -201,6 +201,9 @@ void CPass::render ()
             case Matrix4:
                 glUniformMatrix4fv (entry->id, 1, GL_FALSE, glm::value_ptr (*reinterpret_cast <const glm::mat4*> (entry->value)));
                 break;
+            case Matrix3:
+                glUniformMatrix3fv (entry->id, 1, GL_FALSE, glm::value_ptr (*reinterpret_cast <const glm::mat3*> (entry->value)));
+                break;
         }
     }
     // add reference uniforms
@@ -231,6 +234,9 @@ void CPass::render ()
             case Matrix4:
                 glUniformMatrix4fv (entry->id, 1, GL_FALSE, glm::value_ptr (*reinterpret_cast <const glm::mat4*> (*entry->value)));
                 break;
+            case Matrix3:
+                glUniformMatrix3fv (entry->id, 1, GL_FALSE, glm::value_ptr (*reinterpret_cast <const glm::mat3*> (*entry->value)));
+                break;
         }
     }
 
@@ -301,11 +307,16 @@ void CPass::setModelViewProjectionMatrix (const glm::mat4* projection)
     this->m_modelViewProjectionMatrix = projection;
 }
 
-void CPass::setModelMatrix (glm::mat4 model)
+void CPass::setModelMatrix (const glm::mat4* model)
 {
     this->m_modelMatrix = model;
 }
 
+void CPass::setViewProjectionMatrix (const glm::mat4* viewProjection)
+{
+    this->m_viewProjectionMatrix = viewProjection;
+}
+
 void CPass::setTexCoord (GLuint texcoord)
 {
     this->a_TexCoord = texcoord;
@@ -579,6 +590,9 @@ void CPass::setupUniforms ()
 
     auto projection = this->getMaterial ()->getImage ()->getScene ()->getScene ()->getOrthogonalProjection ();
 
+    // lighting variables
+    this->addUniform ("g_LightAmbientColor", this->m_material->getImage ()->getScene ()->getScene ()->getAmbientColor ());
+    this->addUniform ("g_LightSkylightColor", this->m_material->getImage ()->getScene ()->getScene ()->getSkylightColor ());
     // register variables like brightness and alpha with some default value
     this->addUniform ("g_Brightness", this->m_material->getImage ()->getImage ()->getBrightness ());
     this->addUniform ("g_UserAlpha", this->m_material->getImage ()->getImage ()->getAlpha ());
@@ -590,6 +604,9 @@ void CPass::setupUniforms ()
     this->addUniform ("g_Time", &g_Time);
     // add model-view-projection matrix
     this->addUniform ("g_ModelViewProjectionMatrix", &this->m_modelViewProjectionMatrix);
+    this->addUniform ("g_ModelMatrix", &this->m_modelMatrix);
+    this->addUniform ("g_NormalModelMatrix", glm::identity <glm::mat3> ());
+    this->addUniform ("g_ViewProjectionMatrix", &this->m_viewProjectionMatrix);
     this->addUniform ("g_PointerPosition", this->m_material->getImage ()->getScene ()->getMousePosition ());
     this->addUniform ("g_PointerPositionLast", this->m_material->getImage ()->getScene ()->getMousePositionLast ());
     this->addUniform ("g_EffectTextureProjectionMatrix", glm::mat4(1.0));
@@ -899,6 +916,21 @@ void CPass::addUniform (const std::string& name, const glm::vec4** value)
     this->addUniform (name, UniformType::Vector4, value);
 }
 
+void CPass::addUniform (const std::string& name, glm::mat3 value)
+{
+    this->addUniform (name, UniformType::Matrix3, value);
+}
+
+void CPass::addUniform (const std::string& name, const glm::mat3* value)
+{
+    this->addUniform (name, UniformType::Matrix3, value, 1);
+}
+
+void CPass::addUniform (const std::string& name, const glm::mat3** value)
+{
+    this->addUniform (name, UniformType::Matrix3, value);
+}
+
 void CPass::addUniform (const std::string& name, glm::mat4 value)
 {
     this->addUniform (name, UniformType::Matrix4, value);

+ 14 - 8
src/WallpaperEngine/Render/Objects/Effects/CPass.h

@@ -32,7 +32,8 @@ namespace WallpaperEngine::Render::Objects::Effects
         void setTexCoord (GLuint texcoord);
         void setPosition (GLuint position);
         void setModelViewProjectionMatrix (const glm::mat4* projection);
-        void setModelMatrix (glm::mat4 model);
+        void setModelMatrix (const glm::mat4* model);
+        void setViewProjectionMatrix (const glm::mat4* viewProjection);
 
         const CMaterial* getMaterial () const;
         Core::Objects::Images::Materials::CPass* getPass ();
@@ -41,12 +42,13 @@ namespace WallpaperEngine::Render::Objects::Effects
         enum UniformType
         {
             Float = 0,
-            Matrix4 = 1,
-            Integer = 2,
-            Vector2 = 3,
-            Vector3 = 4,
-            Vector4 = 5,
-            Double = 6
+            Matrix3 = 1,
+            Matrix4 = 2,
+            Integer = 3,
+            Vector2 = 4,
+            Vector3 = 5,
+            Vector4 = 6,
+            Double = 7
         };
 
         class UniformEntry
@@ -102,6 +104,7 @@ namespace WallpaperEngine::Render::Objects::Effects
         void addUniform (const std::string& name, glm::vec2 value);
         void addUniform (const std::string& name, glm::vec3 value);
         void addUniform (const std::string& name, glm::vec4 value);
+        void addUniform (const std::string& name, glm::mat3 value);
         void addUniform (const std::string& name, glm::mat4 value);
         void addUniform (const std::string& name, const int* value, int count = 1);
         void addUniform (const std::string& name, const double* value, int count = 1);
@@ -109,6 +112,7 @@ namespace WallpaperEngine::Render::Objects::Effects
         void addUniform (const std::string& name, const glm::vec2* value);
         void addUniform (const std::string& name, const glm::vec3* value);
         void addUniform (const std::string& name, const glm::vec4* value);
+        void addUniform (const std::string& name, const glm::mat3* value);
         void addUniform (const std::string& name, const glm::mat4* value);
         void addUniform (const std::string& name, const int** value);
         void addUniform (const std::string& name, const double** value);
@@ -116,6 +120,7 @@ namespace WallpaperEngine::Render::Objects::Effects
         void addUniform (const std::string& name, const glm::vec2** value);
         void addUniform (const std::string& name, const glm::vec3** value);
         void addUniform (const std::string& name, const glm::vec4** value);
+        void addUniform (const std::string& name, const glm::mat3** value);
         void addUniform (const std::string& name, const glm::mat4** value);
         template <typename T> void addUniform (const std::string& name, UniformType type, T value);
         template <typename T> void addUniform (const std::string& name, UniformType type, T* value, int count = 1);
@@ -132,7 +137,8 @@ namespace WallpaperEngine::Render::Objects::Effects
         std::map<std::string, UniformEntry*> m_uniforms;
         std::map<std::string, ReferenceUniformEntry*> m_referenceUniforms;
         const glm::mat4* m_modelViewProjectionMatrix;
-        glm::mat4 m_modelMatrix;
+        const glm::mat4* m_modelMatrix;
+        const glm::mat4* m_viewProjectionMatrix;
 
         /**
          * Contains the final map of textures to be used