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",
"Parameteri",
"RENDERLOOP",
"VERTICE"
"VERTICE",
"vshader"
]
}

View File

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

View File

@ -6,12 +6,15 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <functional>
#include <cmath>
#include <filesystem>
#include <iostream>
#include <utility>
#include <vector>
#include <cstring>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include "./constants.hpp"
#include "./misc.hpp"
@ -176,7 +179,7 @@ drawFigureReturn drawFigure(const int whatToDraw) {
texture_constants::AWESOME_FACE_ARGUMENT
};
offsetsStruct offsets = offsetsStruct();
const std::vector<std::string> uniformInfo = {"ourColor"};
const std::vector<std::string> uniformInfo = {"mixProportions"};
return draw(
constants::VERTEX_SHADER_TEXTURE_FILENAME,
constants::FRAGMENT_SHADER_TEXTURE_UNIFORM_FILENAME,
@ -185,7 +188,7 @@ drawFigureReturn drawFigure(const int whatToDraw) {
constants::TEXTURE_INDICES,
constants::TEXTURE_INDICES_SIZE,
true, true, textureInfoArray,
offsets);
offsets, uniformInfo);
}
default:
throw "No function for this draw call";
@ -237,7 +240,14 @@ drawFigureReturn draw(const char* vertexPath, const char* fragmentPath,
configureVertexAttribute(colorIncluded, textureIncluded);
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);
@ -269,18 +279,15 @@ drawFigureReturn draw(const char* vertexPath, const char* fragmentPath,
return newReturn;
}
void updateUniformColor(const unsigned int shaderProgram,
void updateUniformColor(const Shader shaderProgram,
std::string uniformName) {
// update the uniform color
const float timeValue = glfwGetTime(); // retrieve running time
const float greenValue = sin(timeValue) / 2.0f + 0.5f;
// vary the color from 0.0 to 1.0 using sin
// query the location of our uniform
const int vertexColorLocation =
glGetUniformLocation(shaderProgram, uniformName.c_str());
shaderProgram.set4Float(uniformName, 0.0f, greenValue, 0.0f, 1.0f);
// 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
4f means that it expects 4 floats
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.
*/
}
}
#endif // #ifndef DRAW_CPP

View File

@ -27,7 +27,7 @@ struct drawFigureReturn {
drawFigureReturn drawFigure(const int whatToDraw);
void updateUniformColor(const unsigned int shaderProgram,
void updateUniformColor(const Shader shaderProgram,
const std::string uniformName);
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);
}
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) {
int success;
char infoLog[1024];

View File

@ -27,6 +27,8 @@ class Shader {
// ------------------------------------------------------------------------
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:
// utility function for checking shader compilation/linking errors.
// ------------------------------------------------------------------------