mirror of
https://github.com/kuhyx/engineer-thesis-WUT.git
synced 2026-07-04 14:43:10 +02:00
141 lines
4.4 KiB
C++
141 lines
4.4 KiB
C++
// Copyright [2023] Krzysztof Rudnicki
|
|
#ifndef HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_SHADER_CPP
|
|
#define HOME_KUCHY_ENGINEER_THESIS_WUT_BREAKOUT_SHADER_CPP
|
|
#include "../breakout/shader.hpp"
|
|
|
|
auto Shader::Use() -> Shader & {
|
|
glUseProgram(this->ID);
|
|
return *this;
|
|
}
|
|
|
|
void Shader::Compile(const char *vertexSource, const char *fragmentSource,
|
|
const char *geometrySource) {
|
|
unsigned int sVertex;
|
|
unsigned int sFragment;
|
|
unsigned int gShader;
|
|
// vertex Shader
|
|
sVertex = glCreateShader(GL_VERTEX_SHADER);
|
|
glShaderSource(sVertex, 1, &vertexSource, nullptr);
|
|
glCompileShader(sVertex);
|
|
checkCompileErrors(sVertex, "VERTEX");
|
|
// fragment Shader
|
|
sFragment = glCreateShader(GL_FRAGMENT_SHADER);
|
|
glShaderSource(sFragment, 1, &fragmentSource, nullptr);
|
|
glCompileShader(sFragment);
|
|
checkCompileErrors(sFragment, "FRAGMENT");
|
|
// if geometry shader source code is given, also compile geometry shader
|
|
if (geometrySource != nullptr) {
|
|
gShader = glCreateShader(GL_GEOMETRY_SHADER);
|
|
glShaderSource(gShader, 1, &geometrySource, nullptr);
|
|
glCompileShader(gShader);
|
|
checkCompileErrors(gShader, "GEOMETRY");
|
|
}
|
|
// shader program
|
|
this->ID = glCreateProgram();
|
|
glAttachShader(this->ID, sVertex);
|
|
glAttachShader(this->ID, sFragment);
|
|
if (geometrySource != nullptr) {
|
|
glAttachShader(this->ID, gShader);
|
|
}
|
|
glLinkProgram(this->ID);
|
|
checkCompileErrors(this->ID, "PROGRAM");
|
|
// delete the shaders as they're linked into our
|
|
// program now and no longer necessary
|
|
glDeleteShader(sVertex);
|
|
glDeleteShader(sFragment);
|
|
if (geometrySource != nullptr) {
|
|
glDeleteShader(gShader);
|
|
}
|
|
}
|
|
|
|
void Shader::SetFloat(const char *name, float value, bool useShader) {
|
|
if (useShader) {
|
|
this->Use();
|
|
}
|
|
glUniform1f(glGetUniformLocation(this->ID, name), value);
|
|
}
|
|
void Shader::SetInteger(const char *name, int value, bool useShader) {
|
|
if (useShader) {
|
|
this->Use();
|
|
}
|
|
glUniform1i(glGetUniformLocation(this->ID, name), value);
|
|
}
|
|
void Shader::SetVector2f(const char *name, float x, float y, bool useShader) {
|
|
if (useShader) {
|
|
this->Use();
|
|
}
|
|
glUniform2f(glGetUniformLocation(this->ID, name), x, y);
|
|
}
|
|
void Shader::SetVector2f(const char *name, const glm::vec2 &value,
|
|
bool useShader) {
|
|
if (useShader) {
|
|
this->Use();
|
|
}
|
|
glUniform2f(glGetUniformLocation(this->ID, name), value.x, value.y);
|
|
}
|
|
void Shader::SetVector3f(const char *name, float x, float y, float z,
|
|
bool useShader) {
|
|
if (useShader) {
|
|
this->Use();
|
|
}
|
|
glUniform3f(glGetUniformLocation(this->ID, name), x, y, z);
|
|
}
|
|
void Shader::SetVector3f(const char *name, const glm::vec3 &value,
|
|
bool useShader) {
|
|
if (useShader) {
|
|
this->Use();
|
|
}
|
|
glUniform3f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z);
|
|
}
|
|
void Shader::SetVector4f(const char *name, float x, float y, float z, float w,
|
|
bool useShader) {
|
|
if (useShader) {
|
|
this->Use();
|
|
}
|
|
glUniform4f(glGetUniformLocation(this->ID, name), x, y, z, w);
|
|
}
|
|
void Shader::SetVector4f(const char *name, const glm::vec4 &value,
|
|
bool useShader) {
|
|
if (useShader) {
|
|
this->Use();
|
|
}
|
|
glUniform4f(glGetUniformLocation(this->ID, name), value.x, value.y, value.z,
|
|
value.w);
|
|
}
|
|
void Shader::SetMatrix4(const char *name, const glm::mat4 &matrix,
|
|
bool useShader) {
|
|
if (useShader) {
|
|
this->Use();
|
|
}
|
|
glUniformMatrix4fv(glGetUniformLocation(this->ID, name), 1, 0u,
|
|
glm::value_ptr(matrix));
|
|
}
|
|
|
|
void Shader::checkCompileErrors(unsigned int object, const std::string &type) {
|
|
int success = 0;
|
|
char infoLog[1024];
|
|
if (type != "PROGRAM") {
|
|
glGetShaderiv(object, GL_COMPILE_STATUS, &success);
|
|
if (success == 0) {
|
|
glGetShaderInfoLog(object, 1024, nullptr, infoLog);
|
|
std::cout
|
|
<< "| ERROR::SHADER: Compile-time error: Type: " << type << "\n"
|
|
<< infoLog
|
|
<< "\n -- --------------------------------------------------- -- "
|
|
<< std::endl;
|
|
}
|
|
} else {
|
|
glGetProgramiv(object, GL_LINK_STATUS, &success);
|
|
if (success == 0) {
|
|
glGetProgramInfoLog(object, 1024, nullptr, infoLog);
|
|
std::cout
|
|
<< "| ERROR::Shader: Link-time error: Type: " << type << "\n"
|
|
<< infoLog
|
|
<< "\n -- --------------------------------------------------- -- "
|
|
<< std::endl;
|
|
}
|
|
}
|
|
}
|
|
|
|
#endif
|