|
|
@@ -1,3 +1,4 @@
|
|
|
+#include <sstream>
|
|
|
#include "CPass.h"
|
|
|
|
|
|
#include "WallpaperEngine/Render/Shaders/Variables/CShaderVariable.h"
|
|
|
@@ -58,6 +59,12 @@ void CPass::render (GLuint drawTo, GLuint input)
|
|
|
if (drawTo > 0)
|
|
|
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
+ // update variables used in the render process (like g_ModelViewProjectionMatrix)
|
|
|
+ this->m_modelViewProjectionMatrix =
|
|
|
+ this->m_material->getImage ()->getScene ()->getCamera ()->getProjection () *
|
|
|
+ this->m_material->getImage ()->getScene ()->getCamera ()->getLookAt () *
|
|
|
+ glm::mat4 (1.0f);
|
|
|
+
|
|
|
// use the shader we have registered
|
|
|
glUseProgram (this->m_programID);
|
|
|
|
|
|
@@ -143,63 +150,40 @@ void CPass::render (GLuint drawTo, GLuint input)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // add static things
|
|
|
- if (this->g_Texture0 != -1)
|
|
|
- {
|
|
|
-#ifdef DEBUG
|
|
|
- std::cout << "g_Texture0 = 0\n";
|
|
|
-#endif /* DEBUG */
|
|
|
- glUniform1i (this->g_Texture0, 0);
|
|
|
- }
|
|
|
- if (this->g_Texture1 != -1)
|
|
|
- {
|
|
|
-#ifdef DEBUG
|
|
|
- std::cout << "g_Texture1 = 1\n";
|
|
|
-#endif /* DEBUG */
|
|
|
- glUniform1i (this->g_Texture1, 1);
|
|
|
- }
|
|
|
- if (this->g_Texture2 != -1)
|
|
|
+ // add uniforms
|
|
|
{
|
|
|
-#ifdef DEBUG
|
|
|
- std::cout << "g_Texture2 = 2\n";
|
|
|
-#endif /* DEBUG */
|
|
|
- glUniform1i (this->g_Texture2, 2);
|
|
|
- }
|
|
|
- if (this->g_Time != -1)
|
|
|
- {
|
|
|
-#ifdef DEBUG
|
|
|
- std::cout << "g_Time = " << (float) ::g_Time << "\n";
|
|
|
-#endif /* DEBUG */
|
|
|
- glUniform1d (this->g_Time, (float) ::g_Time);
|
|
|
- }
|
|
|
- if (this->g_ModelViewProjectionMatrix != -1)
|
|
|
- {
|
|
|
- // calculate the new matrix
|
|
|
- glm::mat4 projection = this->m_material->getImage ()->getScene ()->getCamera ()->getProjection ();
|
|
|
- glm::mat4 view = this->m_material->getImage ()->getScene ()->getCamera ()->getLookAt ();
|
|
|
- glm::mat4 model = glm::mat4 (1.0f);
|
|
|
- glm::mat4 mvp = projection * view * model;
|
|
|
-
|
|
|
- glUniformMatrix4fv (this->g_ModelViewProjectionMatrix, 1, GL_FALSE, &mvp [0] [0]);
|
|
|
+ auto cur = this->m_uniforms.begin ();
|
|
|
+ auto end = this->m_uniforms.end ();
|
|
|
|
|
|
-#ifdef DEBUG
|
|
|
- std::cout << "g_ModelViewProjectionMatrix\n";
|
|
|
-#endif /* DEBUG */
|
|
|
- }
|
|
|
- if (this->g_Brightness != -1)
|
|
|
- {
|
|
|
-#ifdef DEBUG
|
|
|
- std::cout << "g_Brightness = 1.0\n";
|
|
|
-#endif /* DEBUG */
|
|
|
- glUniform1f (this->g_Brightness, 1.0f);
|
|
|
- }
|
|
|
- if (this->g_UserAlpha != -1)
|
|
|
- {
|
|
|
-#ifdef DEBUG
|
|
|
- std::cout << "g_UserAlpha = 1.0\n";
|
|
|
-#endif /* DEBUG */
|
|
|
- glUniform1f (this->g_UserAlpha, 1.0f);
|
|
|
+ for (; cur != end; cur ++)
|
|
|
+ {
|
|
|
+ switch ((*cur)->type)
|
|
|
+ {
|
|
|
+ case Double:
|
|
|
+ glUniform1d ((*cur)->id, *static_cast <const double*> ((*cur)->value));
|
|
|
+ break;
|
|
|
+ case Float:
|
|
|
+ glUniform1f ((*cur)->id, *static_cast <const float*> ((*cur)->value));
|
|
|
+ break;
|
|
|
+ case Integer:
|
|
|
+ glUniform1i ((*cur)->id, *static_cast <const int*> ((*cur)->value));
|
|
|
+ break;
|
|
|
+ case Vector4:
|
|
|
+ glUniform4fv ((*cur)->id, 1, glm::value_ptr (*static_cast <const glm::vec4*> ((*cur)->value)));
|
|
|
+ break;
|
|
|
+ case Vector3:
|
|
|
+ glUniform3fv ((*cur)->id, 1, glm::value_ptr (*static_cast <const glm::vec3*> ((*cur)->value)));
|
|
|
+ break;
|
|
|
+ case Vector2:
|
|
|
+ glUniform2fv ((*cur)->id, 1, glm::value_ptr (*static_cast <const glm::vec2*> ((*cur)->value)));
|
|
|
+ break;
|
|
|
+ case Matrix4:
|
|
|
+ glUniformMatrix4fv ((*cur)->id, 1, GL_FALSE, glm::value_ptr (*static_cast <const glm::mat4*> ((*cur)->value)));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
if (this->g_Texture0Rotation != -1)
|
|
|
{
|
|
|
#ifdef DEBUG
|
|
|
@@ -460,30 +444,100 @@ void CPass::setupShaders ()
|
|
|
glDeleteShader (vertexShaderID);
|
|
|
glDeleteShader (fragmentShaderID);
|
|
|
|
|
|
+ // setup uniforms
|
|
|
+ this->setupUniforms ();
|
|
|
// get information from the program, like uniforms, etc
|
|
|
// support three textures for now
|
|
|
- this->g_Texture0 = glGetUniformLocation (this->m_programID, "g_Texture0");
|
|
|
- this->g_Texture1 = glGetUniformLocation (this->m_programID, "g_Texture1");
|
|
|
- this->g_Texture2 = glGetUniformLocation (this->m_programID, "g_Texture2");
|
|
|
- this->g_Time = glGetUniformLocation (this->m_programID, "g_Time");
|
|
|
this->g_Texture0Rotation = glGetUniformLocation (this->m_programID, "g_Texture0Rotation");
|
|
|
this->g_Texture0Translation = glGetUniformLocation (this->m_programID, "g_Texture0Translation");
|
|
|
- this->g_ModelViewProjectionMatrix = glGetUniformLocation (this->m_programID, "g_ModelViewProjectionMatrix");
|
|
|
- this->g_UserAlpha = glGetUniformLocation (this->m_programID, "g_UserAlpha");
|
|
|
- this->g_Brightness = glGetUniformLocation (this->m_programID, "g_Brightness");
|
|
|
|
|
|
// bind a_TexCoord and a_Position
|
|
|
this->a_TexCoord = glGetAttribLocation (this->m_programID, "a_TexCoord");
|
|
|
this->a_Position = glGetAttribLocation (this->m_programID, "a_Position");
|
|
|
}
|
|
|
|
|
|
+void CPass::setupUniforms ()
|
|
|
+{
|
|
|
+ // register all the texture uniforms with correct values
|
|
|
+ this->addUniform ("g_Texture0", 0);
|
|
|
+ this->addUniform ("g_Texture1", 1);
|
|
|
+ this->addUniform ("g_Texture2", 2);
|
|
|
+ this->addUniform ("g_Texture3", 3);
|
|
|
+ this->addUniform ("g_Texture4", 4);
|
|
|
+ this->addUniform ("g_Texture5", 5);
|
|
|
+ this->addUniform ("g_Texture6", 6);
|
|
|
+ this->addUniform ("g_Texture7", 7);
|
|
|
+ // register all the texture sizes required
|
|
|
+ this->addUniform ("g_Texture0Resolution", this->m_material->getImage ()->getTexture ()->getResolution ());
|
|
|
+ // register the extra texture resolutions
|
|
|
+ {
|
|
|
+ auto cur = this->m_textures.begin ();
|
|
|
+ auto end = this->m_textures.end ();
|
|
|
+
|
|
|
+ for (int index = 1; cur != end; cur ++, index ++)
|
|
|
+ {
|
|
|
+ std::ostringstream namestream;
|
|
|
+
|
|
|
+ namestream << "g_Texture" << index << "Resolution";
|
|
|
+
|
|
|
+ this->addUniform (namestream.str (), (*cur)->getResolution ());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // register variables like brightness and alpha with some default value
|
|
|
+ this->addUniform ("g_Brightness", 1.0f);
|
|
|
+ this->addUniform ("g_UserAlpha", 1.0f);
|
|
|
+ // add some external variables
|
|
|
+ this->addUniform ("g_Time", &g_Time);
|
|
|
+ // add model-view-projection matrix
|
|
|
+ this->addUniform ("g_ModelViewProjectionMatrix", &this->m_modelViewProjectionMatrix);
|
|
|
+}
|
|
|
+
|
|
|
+template <typename T>
|
|
|
+void CPass::addUniform (const std::string& name, UniformType type, T value)
|
|
|
+{
|
|
|
+ GLint id = glGetUniformLocation (this->m_programID, name.c_str ());
|
|
|
+
|
|
|
+ // parameter not found, can be ignored
|
|
|
+ if (id == -1)
|
|
|
+ return;
|
|
|
+
|
|
|
+ // build a copy of the value and allocate it somewhere
|
|
|
+ T* newValue = new T (value);
|
|
|
+
|
|
|
+ // uniform found, add it to the list
|
|
|
+ this->m_uniforms.emplace_back (
|
|
|
+ new UniformEntry (id, name, type, newValue)
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+template <typename T>
|
|
|
+void CPass::addUniform (const std::string& name, UniformType type, T* value)
|
|
|
+{
|
|
|
+ // this version is used to reference to system variables so things like g_Time works fine
|
|
|
+ GLint id = glGetUniformLocation (this->m_programID, name.c_str ());
|
|
|
+
|
|
|
+ // parameter not found, can be ignored
|
|
|
+ if (id == -1)
|
|
|
+ return;
|
|
|
+
|
|
|
+ // uniform found, add it to the list
|
|
|
+ this->m_uniforms.emplace_back (
|
|
|
+ new UniformEntry (id, name, type, value)
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
void CPass::setupTextures ()
|
|
|
{
|
|
|
auto cur = this->m_pass->getTextures ().begin ();
|
|
|
auto end = this->m_pass->getTextures ().end ();
|
|
|
|
|
|
- for (; cur != end; cur ++)
|
|
|
+ for (int index = 0; cur != end; cur ++, index ++)
|
|
|
{
|
|
|
+ // ignore first texture as that'll be the input of the last pass/image
|
|
|
+ if (index == 0)
|
|
|
+ continue;
|
|
|
+
|
|
|
uint32_t textureSize = 0;
|
|
|
|
|
|
// get the first texture on the first pass (this one represents the image assigned to this object)
|
|
|
@@ -553,3 +607,75 @@ void CPass::setupShaderVariables ()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+// define some basic methods for the template
|
|
|
+/*template double Core::jsonFindDefault (nlohmann::json& data, const char *key, double defaultValue);*/
|
|
|
+void CPass::addUniform (const std::string& name, int value)
|
|
|
+{
|
|
|
+ this->addUniform (name, UniformType::Integer, value);
|
|
|
+}
|
|
|
+
|
|
|
+void CPass::addUniform (const std::string& name, const int* value)
|
|
|
+{
|
|
|
+ this->addUniform (name, UniformType::Integer, value);
|
|
|
+}
|
|
|
+
|
|
|
+void CPass::addUniform (const std::string& name, double value)
|
|
|
+{
|
|
|
+ this->addUniform (name, UniformType::Double, value);
|
|
|
+}
|
|
|
+
|
|
|
+void CPass::addUniform (const std::string& name, const double* value)
|
|
|
+{
|
|
|
+ this->addUniform (name, UniformType::Double, value);
|
|
|
+}
|
|
|
+
|
|
|
+void CPass::addUniform (const std::string& name, float value)
|
|
|
+{
|
|
|
+ this->addUniform (name, UniformType::Float, value);
|
|
|
+}
|
|
|
+
|
|
|
+void CPass::addUniform (const std::string& name, const float* value)
|
|
|
+{
|
|
|
+ this->addUniform (name, UniformType::Float, value);
|
|
|
+}
|
|
|
+
|
|
|
+void CPass::addUniform (const std::string& name, glm::vec2 value)
|
|
|
+{
|
|
|
+ this->addUniform (name, UniformType::Vector2, value);
|
|
|
+}
|
|
|
+
|
|
|
+void CPass::addUniform (const std::string& name, const glm::vec2* value)
|
|
|
+{
|
|
|
+ this->addUniform (name, UniformType::Vector2, value);
|
|
|
+}
|
|
|
+
|
|
|
+void CPass::addUniform (const std::string& name, glm::vec3 value)
|
|
|
+{
|
|
|
+ this->addUniform (name, UniformType::Vector3, value);
|
|
|
+}
|
|
|
+
|
|
|
+void CPass::addUniform (const std::string& name, const glm::vec3* value)
|
|
|
+{
|
|
|
+ this->addUniform (name, UniformType::Vector3, value);
|
|
|
+}
|
|
|
+
|
|
|
+void CPass::addUniform (const std::string& name, glm::vec4 value)
|
|
|
+{
|
|
|
+ this->addUniform (name, UniformType::Vector4, value);
|
|
|
+}
|
|
|
+
|
|
|
+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::mat4 value)
|
|
|
+{
|
|
|
+ this->addUniform (name, UniformType::Matrix4, value);
|
|
|
+}
|
|
|
+
|
|
|
+void CPass::addUniform (const std::string& name, const glm::mat4* value)
|
|
|
+{
|
|
|
+ this->addUniform (name, UniformType::Matrix4, value);
|
|
|
+}
|