engineer-thesis-WUT/Engine/engine/shaders.cpp

104 lines
3.3 KiB
C++
Raw Normal View History

2023-03-12 16:24:48 +01:00
// "Copyright [2023] <Krzysztof Rudnicki>"
2022-09-05 20:17:25 +02:00
#ifndef SHADERS_CPP
#define SHADERS_CPP
#include <glad/glad.h>
#include <GLFW/glfw3.h>
2023-03-12 17:32:42 +01:00
#include "./shaders.hpp"
#include "./constants.hpp"
#include "./misc.hpp"
2022-09-05 20:17:25 +02:00
2023-03-12 17:32:42 +01:00
unsigned int linkShaderObjectsShaderProgram(
const unsigned int vertexShaders,
const unsigned int fragmentShader) {
2022-09-07 18:54:59 +02:00
// link shader objects into shader program
2022-09-05 20:17:25 +02:00
// will store shader program id
2022-09-07 18:54:59 +02:00
// creates program
2022-09-10 13:07:13 +02:00
const unsigned int shaderProgram = glCreateProgram();
2022-09-05 20:17:25 +02:00
// attachShaders
glAttachShader(shaderProgram, vertexShaders);
glAttachShader(shaderProgram, fragmentShader);
// link shaders
glLinkProgram(shaderProgram);
2022-09-07 18:54:59 +02:00
if (!shaderSuccessful(shaderProgram, false))
return 0;
2022-09-05 20:17:25 +02:00
// activate program
// after that every shader and rendering call will use this program object
glUseProgram(shaderProgram);
2022-09-07 18:54:59 +02:00
2023-03-12 17:32:42 +01:00
// delete shaders
// (they are linked into shaderProgram and we do not need them anymore)
2022-09-05 20:17:25 +02:00
glDeleteShader(vertexShaders);
glDeleteShader(fragmentShader);
2022-09-07 18:54:59 +02:00
if (shaderProgram == 0)
print("Shader Program Linking Failed");
2022-09-05 20:17:25 +02:00
return shaderProgram;
}
2023-03-12 17:32:42 +01:00
unsigned int compileShader(
const GLenum shaderType,
const char *shaderSource) {
2022-09-06 21:52:06 +02:00
// we create vertex shader and assign its id to shader variable
2022-09-07 18:54:59 +02:00
const unsigned int shaderID = glCreateShader(shaderType);
2022-09-06 21:52:06 +02:00
// attach shader source code to shader object
2023-03-12 17:32:42 +01:00
// from left: shader object to compile,
// how many strings as source code, actual source code
// (we leave the 4th as nullptr)
2022-12-11 18:49:16 +01:00
glShaderSource(shaderID, 1, &shaderSource, nullptr);
2022-09-06 21:52:06 +02:00
// compile shader
glCompileShader(shaderID);
2022-09-07 18:54:59 +02:00
if (!shaderSuccessful(shaderID, true))
return 0;
2022-09-06 21:52:06 +02:00
return shaderID;
}
2023-03-12 17:32:42 +01:00
std::pair<unsigned int, unsigned int> compileShaders(
const char* vertexShaderSource,
const char* fragmentShaderSource) {
const unsigned int vertexShader =
compileShader(GL_VERTEX_SHADER, vertexShaderSource);
if (vertexShader == 0) {
2022-09-05 20:17:25 +02:00
print("Vertex Shader Compilation Failed");
return std::make_pair(0, 0);
}
2023-03-12 17:32:42 +01:00
const unsigned int fragmentShader =
compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
if (fragmentShader == 0) {
2022-09-05 20:17:25 +02:00
print("Fragment Shader Compilation Failed");
return std::make_pair(0, 0);
}
return std::make_pair(vertexShader, fragmentShader);
}
2023-03-12 17:32:42 +01:00
void shaderFailedMessage(const unsigned int shader, const bool compilation) {
2022-09-05 20:17:25 +02:00
char infoLog[512];
2022-09-06 21:52:06 +02:00
const size_t sizeOfInfoLog = sizeof(infoLog) / sizeof(infoLog[0]);
2022-12-11 18:49:16 +01:00
compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, nullptr, infoLog)
: glGetProgramInfoLog(shader, sizeOfInfoLog, nullptr, infoLog);
2022-09-07 18:54:59 +02:00
std::cout << "ERROR vertex shader compilation failed \n"
<< infoLog << std::endl;
2022-09-05 20:17:25 +02:00
}
2023-03-12 17:32:42 +01:00
int shaderSuccessful(const unsigned int shader, const bool compilation) {
2022-09-05 20:17:25 +02:00
// check if compilation was successful
// int because glGetShaderiv requires int
2022-09-06 21:52:06 +02:00
int success;
2022-09-05 20:17:25 +02:00
// here we store info about compilation
2022-09-07 18:54:59 +02:00
// check if compilation was successful
compilation ? glGetShaderiv(shader, GL_COMPILE_STATUS, &success)
: glGetProgramiv(shader, GL_LINK_STATUS, &success);
2022-09-05 20:17:25 +02:00
// if not display compilation log
2023-03-12 17:32:42 +01:00
if (!success) {
2022-09-06 21:52:06 +02:00
shaderFailedMessage(shader, compilation);
2022-09-05 20:17:25 +02:00
}
2022-09-06 21:52:06 +02:00
return success;
2022-09-05 20:17:25 +02:00
}
2023-03-12 17:32:42 +01:00
#endif