feat(shader)!: rework shader class

- rename IDs
- replace constructor without doing anything
- add compile function (it was the older constructor method)
This commit is contained in:
2025-12-08 15:43:41 +01:00
parent 0f15a015d4
commit 0d5caf1d19
2 changed files with 83 additions and 30 deletions

View File

@@ -6,19 +6,29 @@
class Shader
{
private:
GLuint vertexShader;
GLuint fragmentShader;
GLuint shaderProgram;
GLuint vertexShaderID; // vertex shader id
GLuint fragmentShaderID; // fragment shader id
void addVertShader(const char* vertexShaderSource);
void addFragShader(const char* fragmentShaderSource);
// final compiled shader program id used by the gpu
GLuint shaderProgramID;
// compile vertex shader source code
void addVertShader(const char* vertexSource);
// compile fragment shader source code
void addFragShader(const char* fragmentSource);
// link both compile vertex and fragment shader
void compileInProgram();
public:
Shader(const char* vertexShaderSource, const char* fragmentShaderSource);
// Does nothing apart initializing all IDs to 0
Shader();
// compile and link vertex and fragment into the shader struct
void compile(const char* vertexSource, const char* fragmentSource);
// free shader program
~Shader();
GLuint getProgram() const;
GLuint getShaderProgramID() const;
// activate shader program into gpu pipeline
void use() const;
};

View File

@@ -1,43 +1,86 @@
#include "shader.hpp"
Shader::Shader(const char* vertexShaderSource, const char* fragmentShaderSource)
#include <iostream>
Shader::Shader() : shaderProgramID(0), vertexShaderID(0), fragmentShaderID(0) {}
void Shader::compile(const char* vertexSource, const char* fragmentSource)
{
vertexShader = glCreateShader(GL_VERTEX_SHADER);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
shaderProgram = glCreateProgram();
if (this->shaderProgramID != 0) glDeleteProgram(this->shaderProgramID);
addVertShader(vertexShaderSource);
addFragShader(fragmentShaderSource);
this->vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
this->fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
this->shaderProgramID = glCreateProgram();
compileInProgram();
this->addVertShader(vertexSource);
this->addFragShader(fragmentSource);
this->compileInProgram();
}
void Shader::addVertShader(const char* vertexShaderSource)
void Shader::addVertShader(const char* vertexSource)
{
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShader);
glShaderSource(this->vertexShaderID, 1, &vertexSource, nullptr);
glCompileShader(this->vertexShaderID);
GLint success;
glGetShaderiv(this->vertexShaderID, GL_COMPILE_STATUS, &success);
if (!success)
{
GLchar infoLog[1024];
glGetShaderInfoLog(this->vertexShaderID, 1024, nullptr, infoLog);
std::cerr << "ERROR(SHADER): VERTEX COMPILATION FAILED (ID "
<< this->vertexShaderID << ")\n"
<< infoLog << std::endl;
}
}
void Shader::addFragShader(const char* fragmentShaderSource)
void Shader::addFragShader(const char* fragmentSource)
{
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShader);
glShaderSource(this->fragmentShaderID, 1, &fragmentSource, nullptr);
glCompileShader(this->fragmentShaderID);
GLint success;
glGetShaderiv(this->fragmentShaderID, GL_COMPILE_STATUS, &success);
if (!success)
{
GLchar infoLog[1024];
glGetShaderInfoLog(this->fragmentShaderID, 1024, nullptr, infoLog);
std::cerr << "ERROR(SHADER): FRAGMENT COMPILATION FAILED (ID "
<< this->fragmentShaderID << ")\n"
<< infoLog << std::endl;
}
}
void Shader::compileInProgram()
{
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glAttachShader(this->shaderProgramID, this->vertexShaderID);
glAttachShader(this->shaderProgramID, this->fragmentShaderID);
glLinkProgram(this->shaderProgramID);
glDetachShader(shaderProgram, vertexShader);
glDetachShader(shaderProgram, fragmentShader);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
GLint success;
glGetProgramiv(this->shaderProgramID, GL_LINK_STATUS, &success);
if (!success)
{
GLchar infoLog[1024];
glGetProgramInfoLog(this->shaderProgramID, 1024, nullptr, infoLog);
std::cerr << "ERROR(SHADER): LINKING FAILED (Program ID "
<< this->shaderProgramID << ")\n"
<< infoLog << std::endl;
}
Shader::~Shader() { glDeleteProgram(shaderProgram); }
glDetachShader(this->shaderProgramID, this->vertexShaderID);
glDetachShader(this->shaderProgramID, this->fragmentShaderID);
void Shader::use() const { glUseProgram(shaderProgram); }
glDeleteShader(this->vertexShaderID);
glDeleteShader(this->fragmentShaderID);
}
GLuint Shader::getProgram() const { return shaderProgram; }
Shader::~Shader() { glDeleteProgram(this->shaderProgramID); }
void Shader::use() const { glUseProgram(this->shaderProgramID); }
GLuint Shader::getShaderProgramID() const { return this->shaderProgramID; }