feat: pass uniforms names as vector

This commit is contained in:
Krzysztof Rudnicki 2023-03-19 17:50:43 +01:00
parent 75054ed91f
commit fcc641ea21
6 changed files with 97 additions and 39 deletions

View File

@ -0,0 +1,17 @@
#version 330 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
// texture samplers
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform int mixProportions; // set in OGL code
void main()
{
// linearly interpolate between both textures (80% container, 20% awesomeface)
FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), mixProportions);
}

View File

@ -111,6 +111,9 @@ namespace constants {
inline const char* FRAGMENT_SHADER_TEXTURE_TASK_ONE_FILENAME{
"./Shaders/fragmentShaderTextureTaskOne.fs"};
inline const char* FRAGMENT_SHADER_TEXTURE_UNIFORM_FILENAME{
"./Shaders/fragmentShaderTextureUniform.fs"};
// we specify three vertices
// each of them with position in 3d space
// x y z
@ -205,7 +208,7 @@ namespace constants {
inline constexpr size_t SQUARE_VERTICES_SIZE = {sizeof(SQUARE_VERTICES)};
inline constexpr int MAX_DRAW_CALL = {13};
inline constexpr int MAX_DRAW_CALL = {15};
// https://learnopengl.com/img/getting-started/tex_coords.png
inline constexpr float TEXTURE_VERTICES[]{
@ -229,6 +232,17 @@ namespace constants {
inline constexpr size_t TEXTURE_VERTICES_2F_SIZE = {
sizeof(TEXTURE_VERTICES_2F)};
inline constexpr float TEXTURE_VERTICES_TASK_THREE[]{
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.55f, 0.55f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.55f, 0.45f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.45f, 0.45f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.45f, 0.55f // top left
};
inline constexpr size_t TEXTURE_VERTICES_TASK_THREE_SIZE = {
sizeof(TEXTURE_VERTICES_TASK_THREE)};
inline constexpr unsigned int TEXTURE_INDICES[]{
0, 1, 3, // first triangle
1, 2, 3 // second triangle

View File

@ -11,6 +11,7 @@
#include <iostream>
#include <utility>
#include <vector>
#include <cstring>
#include "./constants.hpp"
#include "./misc.hpp"
@ -73,11 +74,18 @@ drawFigureReturn drawFigure(const int whatToDraw) {
constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME,
constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE);
case 6:
{
offsetsStruct offsets = offsetsStruct();
const std::vector<std::string> uniformInfo = {"ourColor"};
// set color from opengl code to uniform value in fragment shader
return draw(constants::VERTEX_SHADER_COLOR_FILENAME,
constants::FRAGMENT_SHADER_UNIFORMS_FILENAME,
constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE);
constants::FRAGMENT_SHADER_UNIFORMS_FILENAME,
constants::TRIANGLE_VERTICES,
constants::TRIANGLE_VERTICES_SIZE,
constants::TRIANGLE_INDICES,
constants::TRIANGLE_INDICES_SIZE,
false, false, {}, offsets, uniformInfo);
}
case 7:
// set color from opengl code to uniform value in fragment shader
return draw(
@ -135,7 +143,7 @@ drawFigureReturn drawFigure(const int whatToDraw) {
constants::TEXTURE_INDICES_SIZE,
true, true, textureInfoArray);
}
case constants::MAX_DRAW_CALL: {
case 13: {
std::vector<textureArgument> textureInfoArray = {
texture_constants::CONTAINER_ARGUMENT,
texture_constants::AWESOME_FACE_ARGUMENT
@ -148,6 +156,37 @@ drawFigureReturn drawFigure(const int whatToDraw) {
constants::TEXTURE_INDICES_SIZE,
true, true, textureInfoArray);
}
case 14: {
std::vector<textureArgument> textureInfoArray = {
texture_constants::TEXTURE_TASK_THREE_CONTAINER_ARGUMENT,
texture_constants::TEXTURE_TASK_THREE_FACE_ARGUMENT
};
return draw(
constants::VERTEX_SHADER_TEXTURE_FILENAME,
constants::FRAGMENT_SHADER_TEXTURE_FILENAME,
constants::TEXTURE_VERTICES_TASK_THREE,
constants::TEXTURE_VERTICES_TASK_THREE_SIZE,
constants::TEXTURE_INDICES,
constants::TEXTURE_INDICES_SIZE,
true, true, textureInfoArray);
}
case constants::MAX_DRAW_CALL: {
std::vector<textureArgument> textureInfoArray = {
texture_constants::CONTAINER_ARGUMENT,
texture_constants::AWESOME_FACE_ARGUMENT
};
offsetsStruct offsets = offsetsStruct();
const std::vector<std::string> uniformInfo = {"ourColor"};
return draw(
constants::VERTEX_SHADER_TEXTURE_FILENAME,
constants::FRAGMENT_SHADER_TEXTURE_UNIFORM_FILENAME,
constants::TEXTURE_VERTICES,
constants::TEXTURE_VERTICES_SIZE,
constants::TEXTURE_INDICES,
constants::TEXTURE_INDICES_SIZE,
true, true, textureInfoArray,
offsets);
}
default:
throw "No function for this draw call";
}
@ -173,14 +212,15 @@ void setOffsets(Shader shader, const offsetsStruct offsets) {
}
drawFigureReturn draw(const char* vertexPath, const char* fragmentPath,
const float vertices[],
const size_t verticesSize,
const unsigned int indices[],
const size_t indicesSize,
const bool colorIncluded,
const bool textureIncluded,
std::vector<textureArgument> textureInfo,
const offsetsStruct offsets) {
const float vertices[],
const size_t verticesSize,
const unsigned int indices[],
const size_t indicesSize,
const bool colorIncluded,
const bool textureIncluded,
std::vector<textureArgument> textureInfo,
const offsetsStruct offsets,
const std::vector<std::string> uniformName) {
// https://stackoverflow.com/questions/33883609/opengl-linker-error-linking-with-uncompiled-shader
Shader ourShader(vertexPath,
fragmentPath);
@ -196,8 +236,9 @@ drawFigureReturn draw(const char* vertexPath, const char* fragmentPath,
copyVerticesMemory(indices, indicesSize, GL_ELEMENT_ARRAY_BUFFER);
configureVertexAttribute(colorIncluded, textureIncluded);
doDrawArrays(ourShader.ID, VAO, GL_TRIANGLES, 0,
verticesSize);
if (uniformName.size() > 0) {
updateUniformColor(ourShader.ID, uniformName.at(0));
}
drawTextureArray(textureInfo, ourShader);
// unsigned int texture1 = loadAndCreateTexture(
@ -229,14 +270,14 @@ drawFigureReturn draw(const char* vertexPath, const char* fragmentPath,
}
void updateUniformColor(const unsigned int shaderProgram,
const GLchar* uniformName) {
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);
glGetUniformLocation(shaderProgram, uniformName.c_str());
// if glGetUniformLocation returns -1 it could not find the location
if (vertexColorLocation != -1) {
glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
@ -252,19 +293,4 @@ void updateUniformColor(const unsigned int shaderProgram,
}
}
void doDrawArrays(const unsigned int shaderProgram,
const unsigned int vertexArrayObject,
const GLenum drawArrayMode, const int firstIndex,
const unsigned int numberOfIndicesToBeRendered) {
// use shader program to render an object
glUseProgram(shaderProgram);
updateUniformColor(shaderProgram, "ourColor");
glBindVertexArray(vertexArrayObject);
// From left:
// primitive type we want to draw
// starting index of vertex array
// how many vertices we want to draw
glDrawArrays(drawArrayMode, firstIndex, numberOfIndicesToBeRendered);
}
#endif // #ifndef DRAW_CPP

View File

@ -28,12 +28,7 @@ struct drawFigureReturn {
drawFigureReturn drawFigure(const int whatToDraw);
void updateUniformColor(const unsigned int shaderProgram,
const GLchar* uniformName);
void doDrawArrays(const unsigned int shaderProgram,
const unsigned int vertexArrayObject,
const GLenum drawArrayMode, const int firstIndex,
const unsigned int numberOfIndicesToBeRendered);
const std::string uniformName);
drawFigureReturn draw(
const char* vertexPath = constants::VERTEX_SHADER_SOURCE_FILENAME,
@ -45,7 +40,8 @@ drawFigureReturn draw(
const bool colorIncluded = false,
const bool textureIncluded = false,
std::vector<textureArgument> textureInfo = {},
const offsetsStruct offsets = offsetsStruct()
const offsetsStruct offsets = offsetsStruct(),
const std::vector<std::string> uniformName = {}
);
#endif // ENGINE_ENGINE_DRAW_HPP_

Binary file not shown.

View File

@ -26,6 +26,11 @@ namespace texture_constants {
textureArgument(constants::CONTAINER_TEXTURE, true, GL_CLAMP_TO_EDGE);
inline const textureArgument AWESOME_FACE_ARGUMENT =
textureArgument(constants::AWESOME_FACE_TEXTURE, false, GL_REPEAT);
inline const textureArgument TEXTURE_TASK_THREE_CONTAINER_ARGUMENT =
textureArgument(constants::CONTAINER_TEXTURE, true, GL_NEAREST);
inline const textureArgument TEXTURE_TASK_THREE_FACE_ARGUMENT =
textureArgument(constants::AWESOME_FACE_TEXTURE, false, GL_NEAREST);
}
void setTextureParametersINT(const GLenum textureTarget = GL_TEXTURE_2D,