mirror of
https://github.com/kuhyx/engineer-thesis-WUT.git
synced 2026-07-04 15:23:13 +02:00
feat: pass uniforms names as vector
This commit is contained in:
parent
75054ed91f
commit
fcc641ea21
17
Engine/engine/Shaders/fragmentShaderTextureUniform.fs
Normal file
17
Engine/engine/Shaders/fragmentShaderTextureUniform.fs
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
@ -111,6 +111,9 @@ namespace constants {
|
|||||||
inline const char* FRAGMENT_SHADER_TEXTURE_TASK_ONE_FILENAME{
|
inline const char* FRAGMENT_SHADER_TEXTURE_TASK_ONE_FILENAME{
|
||||||
"./Shaders/fragmentShaderTextureTaskOne.fs"};
|
"./Shaders/fragmentShaderTextureTaskOne.fs"};
|
||||||
|
|
||||||
|
inline const char* FRAGMENT_SHADER_TEXTURE_UNIFORM_FILENAME{
|
||||||
|
"./Shaders/fragmentShaderTextureUniform.fs"};
|
||||||
|
|
||||||
// we specify three vertices
|
// we specify three vertices
|
||||||
// each of them with position in 3d space
|
// each of them with position in 3d space
|
||||||
// x y z
|
// x y z
|
||||||
@ -205,7 +208,7 @@ namespace constants {
|
|||||||
|
|
||||||
inline constexpr size_t SQUARE_VERTICES_SIZE = {sizeof(SQUARE_VERTICES)};
|
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
|
// https://learnopengl.com/img/getting-started/tex_coords.png
|
||||||
inline constexpr float TEXTURE_VERTICES[]{
|
inline constexpr float TEXTURE_VERTICES[]{
|
||||||
@ -229,6 +232,17 @@ namespace constants {
|
|||||||
inline constexpr size_t TEXTURE_VERTICES_2F_SIZE = {
|
inline constexpr size_t TEXTURE_VERTICES_2F_SIZE = {
|
||||||
sizeof(TEXTURE_VERTICES_2F)};
|
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[]{
|
inline constexpr unsigned int TEXTURE_INDICES[]{
|
||||||
0, 1, 3, // first triangle
|
0, 1, 3, // first triangle
|
||||||
1, 2, 3 // second triangle
|
1, 2, 3 // second triangle
|
||||||
|
|||||||
@ -11,6 +11,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
#include "./constants.hpp"
|
#include "./constants.hpp"
|
||||||
#include "./misc.hpp"
|
#include "./misc.hpp"
|
||||||
@ -73,11 +74,18 @@ drawFigureReturn drawFigure(const int whatToDraw) {
|
|||||||
constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME,
|
constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME,
|
||||||
constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE);
|
constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE);
|
||||||
case 6:
|
case 6:
|
||||||
|
{
|
||||||
|
offsetsStruct offsets = offsetsStruct();
|
||||||
|
const std::vector<std::string> uniformInfo = {"ourColor"};
|
||||||
// set color from opengl code to uniform value in fragment shader
|
// set color from opengl code to uniform value in fragment shader
|
||||||
return draw(constants::VERTEX_SHADER_COLOR_FILENAME,
|
return draw(constants::VERTEX_SHADER_COLOR_FILENAME,
|
||||||
constants::FRAGMENT_SHADER_UNIFORMS_FILENAME,
|
constants::FRAGMENT_SHADER_UNIFORMS_FILENAME,
|
||||||
constants::TRIANGLE_VERTICES,
|
constants::TRIANGLE_VERTICES,
|
||||||
constants::TRIANGLE_VERTICES_SIZE);
|
constants::TRIANGLE_VERTICES_SIZE,
|
||||||
|
constants::TRIANGLE_INDICES,
|
||||||
|
constants::TRIANGLE_INDICES_SIZE,
|
||||||
|
false, false, {}, offsets, uniformInfo);
|
||||||
|
}
|
||||||
case 7:
|
case 7:
|
||||||
// set color from opengl code to uniform value in fragment shader
|
// set color from opengl code to uniform value in fragment shader
|
||||||
return draw(
|
return draw(
|
||||||
@ -135,7 +143,7 @@ drawFigureReturn drawFigure(const int whatToDraw) {
|
|||||||
constants::TEXTURE_INDICES_SIZE,
|
constants::TEXTURE_INDICES_SIZE,
|
||||||
true, true, textureInfoArray);
|
true, true, textureInfoArray);
|
||||||
}
|
}
|
||||||
case constants::MAX_DRAW_CALL: {
|
case 13: {
|
||||||
std::vector<textureArgument> textureInfoArray = {
|
std::vector<textureArgument> textureInfoArray = {
|
||||||
texture_constants::CONTAINER_ARGUMENT,
|
texture_constants::CONTAINER_ARGUMENT,
|
||||||
texture_constants::AWESOME_FACE_ARGUMENT
|
texture_constants::AWESOME_FACE_ARGUMENT
|
||||||
@ -148,6 +156,37 @@ drawFigureReturn drawFigure(const int whatToDraw) {
|
|||||||
constants::TEXTURE_INDICES_SIZE,
|
constants::TEXTURE_INDICES_SIZE,
|
||||||
true, true, textureInfoArray);
|
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:
|
default:
|
||||||
throw "No function for this draw call";
|
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,
|
drawFigureReturn draw(const char* vertexPath, const char* fragmentPath,
|
||||||
const float vertices[],
|
const float vertices[],
|
||||||
const size_t verticesSize,
|
const size_t verticesSize,
|
||||||
const unsigned int indices[],
|
const unsigned int indices[],
|
||||||
const size_t indicesSize,
|
const size_t indicesSize,
|
||||||
const bool colorIncluded,
|
const bool colorIncluded,
|
||||||
const bool textureIncluded,
|
const bool textureIncluded,
|
||||||
std::vector<textureArgument> textureInfo,
|
std::vector<textureArgument> textureInfo,
|
||||||
const offsetsStruct offsets) {
|
const offsetsStruct offsets,
|
||||||
|
const std::vector<std::string> uniformName) {
|
||||||
// https://stackoverflow.com/questions/33883609/opengl-linker-error-linking-with-uncompiled-shader
|
// https://stackoverflow.com/questions/33883609/opengl-linker-error-linking-with-uncompiled-shader
|
||||||
Shader ourShader(vertexPath,
|
Shader ourShader(vertexPath,
|
||||||
fragmentPath);
|
fragmentPath);
|
||||||
@ -196,8 +236,9 @@ drawFigureReturn draw(const char* vertexPath, const char* fragmentPath,
|
|||||||
copyVerticesMemory(indices, indicesSize, GL_ELEMENT_ARRAY_BUFFER);
|
copyVerticesMemory(indices, indicesSize, GL_ELEMENT_ARRAY_BUFFER);
|
||||||
|
|
||||||
configureVertexAttribute(colorIncluded, textureIncluded);
|
configureVertexAttribute(colorIncluded, textureIncluded);
|
||||||
doDrawArrays(ourShader.ID, VAO, GL_TRIANGLES, 0,
|
if (uniformName.size() > 0) {
|
||||||
verticesSize);
|
updateUniformColor(ourShader.ID, uniformName.at(0));
|
||||||
|
}
|
||||||
|
|
||||||
drawTextureArray(textureInfo, ourShader);
|
drawTextureArray(textureInfo, ourShader);
|
||||||
// unsigned int texture1 = loadAndCreateTexture(
|
// unsigned int texture1 = loadAndCreateTexture(
|
||||||
@ -229,14 +270,14 @@ drawFigureReturn draw(const char* vertexPath, const char* fragmentPath,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void updateUniformColor(const unsigned int shaderProgram,
|
void updateUniformColor(const unsigned int shaderProgram,
|
||||||
const GLchar* 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 =
|
const int vertexColorLocation =
|
||||||
glGetUniformLocation(shaderProgram, uniformName);
|
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) {
|
if (vertexColorLocation != -1) {
|
||||||
glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
|
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
|
#endif // #ifndef DRAW_CPP
|
||||||
|
|||||||
@ -28,12 +28,7 @@ struct drawFigureReturn {
|
|||||||
drawFigureReturn drawFigure(const int whatToDraw);
|
drawFigureReturn drawFigure(const int whatToDraw);
|
||||||
|
|
||||||
void updateUniformColor(const unsigned int shaderProgram,
|
void updateUniformColor(const unsigned int shaderProgram,
|
||||||
const GLchar* uniformName);
|
const std::string uniformName);
|
||||||
|
|
||||||
void doDrawArrays(const unsigned int shaderProgram,
|
|
||||||
const unsigned int vertexArrayObject,
|
|
||||||
const GLenum drawArrayMode, const int firstIndex,
|
|
||||||
const unsigned int numberOfIndicesToBeRendered);
|
|
||||||
|
|
||||||
drawFigureReturn draw(
|
drawFigureReturn draw(
|
||||||
const char* vertexPath = constants::VERTEX_SHADER_SOURCE_FILENAME,
|
const char* vertexPath = constants::VERTEX_SHADER_SOURCE_FILENAME,
|
||||||
@ -45,7 +40,8 @@ drawFigureReturn draw(
|
|||||||
const bool colorIncluded = false,
|
const bool colorIncluded = false,
|
||||||
const bool textureIncluded = false,
|
const bool textureIncluded = false,
|
||||||
std::vector<textureArgument> textureInfo = {},
|
std::vector<textureArgument> textureInfo = {},
|
||||||
const offsetsStruct offsets = offsetsStruct()
|
const offsetsStruct offsets = offsetsStruct(),
|
||||||
|
const std::vector<std::string> uniformName = {}
|
||||||
);
|
);
|
||||||
|
|
||||||
#endif // ENGINE_ENGINE_DRAW_HPP_
|
#endif // ENGINE_ENGINE_DRAW_HPP_
|
||||||
|
|||||||
Binary file not shown.
@ -26,6 +26,11 @@ namespace texture_constants {
|
|||||||
textureArgument(constants::CONTAINER_TEXTURE, true, GL_CLAMP_TO_EDGE);
|
textureArgument(constants::CONTAINER_TEXTURE, true, GL_CLAMP_TO_EDGE);
|
||||||
inline const textureArgument AWESOME_FACE_ARGUMENT =
|
inline const textureArgument AWESOME_FACE_ARGUMENT =
|
||||||
textureArgument(constants::AWESOME_FACE_TEXTURE, false, GL_REPEAT);
|
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,
|
void setTextureParametersINT(const GLenum textureTarget = GL_TEXTURE_2D,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user