feat: set texture opacity randomly

This commit is contained in:
Krzysztof Rudnicki 2023-03-29 14:46:58 +02:00
parent fcc641ea21
commit 6a92a83f5e
7 changed files with 25 additions and 12 deletions

View File

@ -104,6 +104,7 @@
"Parameterfv", "Parameterfv",
"Parameteri", "Parameteri",
"RENDERLOOP", "RENDERLOOP",
"VERTICE" "VERTICE",
"vshader"
] ]
} }

View File

@ -7,7 +7,7 @@ in vec2 TexCoord;
// texture samplers // texture samplers
uniform sampler2D texture1; uniform sampler2D texture1;
uniform sampler2D texture2; uniform sampler2D texture2;
uniform int mixProportions; // set in OGL code uniform float mixProportions; // set in OGL code
void main() void main()
{ {

View File

@ -6,12 +6,15 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <functional>
#include <cmath> #include <cmath>
#include <filesystem> #include <filesystem>
#include <iostream> #include <iostream>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <cstring> #include <cstring>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include "./constants.hpp" #include "./constants.hpp"
#include "./misc.hpp" #include "./misc.hpp"
@ -176,7 +179,7 @@ drawFigureReturn drawFigure(const int whatToDraw) {
texture_constants::AWESOME_FACE_ARGUMENT texture_constants::AWESOME_FACE_ARGUMENT
}; };
offsetsStruct offsets = offsetsStruct(); offsetsStruct offsets = offsetsStruct();
const std::vector<std::string> uniformInfo = {"ourColor"}; const std::vector<std::string> uniformInfo = {"mixProportions"};
return draw( return draw(
constants::VERTEX_SHADER_TEXTURE_FILENAME, constants::VERTEX_SHADER_TEXTURE_FILENAME,
constants::FRAGMENT_SHADER_TEXTURE_UNIFORM_FILENAME, constants::FRAGMENT_SHADER_TEXTURE_UNIFORM_FILENAME,
@ -185,7 +188,7 @@ drawFigureReturn drawFigure(const int whatToDraw) {
constants::TEXTURE_INDICES, constants::TEXTURE_INDICES,
constants::TEXTURE_INDICES_SIZE, constants::TEXTURE_INDICES_SIZE,
true, true, textureInfoArray, true, true, textureInfoArray,
offsets); offsets, uniformInfo);
} }
default: default:
throw "No function for this draw call"; throw "No function for this draw call";
@ -237,7 +240,14 @@ drawFigureReturn draw(const char* vertexPath, const char* fragmentPath,
configureVertexAttribute(colorIncluded, textureIncluded); configureVertexAttribute(colorIncluded, textureIncluded);
if (uniformName.size() > 0) { if (uniformName.size() > 0) {
updateUniformColor(ourShader.ID, uniformName.at(0)); if(uniformName.at(0) == "ourColor") {
updateUniformColor(ourShader, uniformName.at(0));
}
if(uniformName.at(0) == "mixProportions") {
srand (time(NULL));
const float randomNumber = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
ourShader.setFloat(uniformName.at(0), randomNumber);
}
} }
drawTextureArray(textureInfo, ourShader); drawTextureArray(textureInfo, ourShader);
@ -269,18 +279,15 @@ drawFigureReturn draw(const char* vertexPath, const char* fragmentPath,
return newReturn; return newReturn;
} }
void updateUniformColor(const unsigned int shaderProgram, void updateUniformColor(const Shader shaderProgram,
std::string uniformName) { std::string uniformName) {
// update the uniform color // update the uniform color
const float timeValue = glfwGetTime(); // retrieve running time const float timeValue = glfwGetTime(); // retrieve running time
const float greenValue = sin(timeValue) / 2.0f + 0.5f; const float greenValue = sin(timeValue) / 2.0f + 0.5f;
// vary the color from 0.0 to 1.0 using sin // vary the color from 0.0 to 1.0 using sin
// query the location of our uniform // query the location of our uniform
const int vertexColorLocation = shaderProgram.set4Float(uniformName, 0.0f, greenValue, 0.0f, 1.0f);
glGetUniformLocation(shaderProgram, uniformName.c_str());
// if glGetUniformLocation returns -1 it could not find the location // if glGetUniformLocation returns -1 it could not find the location
if (vertexColorLocation != -1) {
glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
/* we set the uniform value using glUniform4f /* we set the uniform value using glUniform4f
4f means that it expects 4 floats 4f means that it expects 4 floats
few of possible postfixes: few of possible postfixes:
@ -291,6 +298,5 @@ void updateUniformColor(const unsigned int shaderProgram,
fv: the function expects a float vector/array as its value. fv: the function expects a float vector/array as its value.
*/ */
} }
}
#endif // #ifndef DRAW_CPP #endif // #ifndef DRAW_CPP

View File

@ -27,7 +27,7 @@ struct drawFigureReturn {
drawFigureReturn drawFigure(const int whatToDraw); drawFigureReturn drawFigure(const int whatToDraw);
void updateUniformColor(const unsigned int shaderProgram, void updateUniformColor(const Shader shaderProgram,
const std::string uniformName); const std::string uniformName);
drawFigureReturn draw( drawFigureReturn draw(

Binary file not shown.

View File

@ -78,6 +78,10 @@ void Shader::setFloat(const std::string &name, float value) const {
glUniform1f(glGetUniformLocation(ID, name.c_str()), value); glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
} }
void Shader::set4Float(const std::string &name, const float floatOne, const float floatTwo, const float floatThree, const float floatFour) const {
glUniform4f(glGetUniformLocation(ID, name.c_str()), floatOne, floatTwo, floatThree, floatFour);
}
void Shader::checkCompileErrors(unsigned int shader, std::string type) { void Shader::checkCompileErrors(unsigned int shader, std::string type) {
int success; int success;
char infoLog[1024]; char infoLog[1024];

View File

@ -27,6 +27,8 @@ class Shader {
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void setFloat(const std::string &name, float value) const; void setFloat(const std::string &name, float value) const;
void set4Float(const std::string &name, const float floatOne, const float floatTwo, const float floatThree, const float floatFour) const;
private: private:
// utility function for checking shader compilation/linking errors. // utility function for checking shader compilation/linking errors.
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------