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

97 lines
3.1 KiB
C++
Raw Normal View History

2022-09-05 20:17:25 +02:00
#ifndef SHADERS_CPP
#define SHADERS_CPP
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "shaders.hpp"
#include "constants.hpp"
#include "misc.hpp"
2022-09-10 13:07:13 +02:00
unsigned int linkShaderObjectsShaderProgram(const unsigned int vertexShaders, const unsigned int fragmentShader)
2022-09-05 20:17:25 +02:00
{
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
2022-09-05 20:17:25 +02:00
// delete shaders (they are linked into shaderProgram and we do not need them anymore)
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;
}
2022-09-07 18:54:59 +02: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
// from left: shader object to compile, how many strings as source code, actual source code (we leave the 4th as NULL)
glShaderSource(shaderID, 1, &shaderSource, NULL);
// 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;
}
2022-09-05 20:17:25 +02:00
std::pair<unsigned int, unsigned int> compileShaders()
{
2022-09-07 18:54:59 +02:00
const unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, constants::vertexShaderSource);
if (vertexShader == 0)
2022-09-05 20:17:25 +02:00
{
print("Vertex Shader Compilation Failed");
return std::make_pair(0, 0);
}
2022-09-07 18:54:59 +02:00
const unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, constants::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);
}
2022-09-07 18:54:59 +02: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-09-07 18:54:59 +02:00
compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, NULL, infoLog)
: glGetProgramInfoLog(shader, sizeOfInfoLog, NULL, infoLog);
std::cout << "ERROR vertex shader compilation failed \n"
<< infoLog << std::endl;
2022-09-05 20:17:25 +02:00
}
2022-09-07 18:54:59 +02: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
2022-09-07 18:54:59 +02:00
if (!success)
2022-09-05 20:17:25 +02:00
{
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
}
#endif