Files
ugly/source/shader/include/ShaderProgramBuilder.hpp
2025-07-31 02:09:45 -04:00

56 lines
1.8 KiB
C++

#pragma once
#include <stdexcept>
#include <GL/glew.h>
namespace ugly{
/**
* \brief Builder class for OpenGL shader and programs.
*
* On initialization, creates a new OpenGL program. Attach and compile shader
* sources using `attachFrom*`. Link and get the program with `link()`. User is
* responsible of managing returned program's lifetime, including destroying it.
*/
class ShaderProgramBuilder {
private:
GLuint mProgram;
public:
ShaderProgramBuilder();
~ShaderProgramBuilder();
/**
* \brief Deletes program and starts from scratch.
*/
void reset();
/**
* \brief Creates, compiles, and attaches a shader.
* \param type Specifies the type of shader to be created. Must be one of
* \c GL_VERTEX_SHADER, \c GL_GEOMETRY_SHADER or
* \c GL_FRAGMENT_SHADER .
* \param string Shader source code.
* \param length Bytes to read from \p string, \c 0 reads until NUL byte.
* \returns *this
*/
ShaderProgramBuilder &attachFromMemory(GLenum type, const GLchar *string, GLint length = 0);
/**
* \brief Creates, compiles, and attaches a shader. Reads source from file.
* \param type Specifies the type of shader to be created. Must be one of
* \c GL_VERTEX_SHADER, \c GL_GEOMETRY_SHADER or
* \c GL_FRAGMENT_SHADER .
* \param filename Shader source code file.
* \returns \c *this
*/
ShaderProgramBuilder &attachFromFile(GLenum type, const char *filename);
/**
* \brief Links OpenGL program after attaching sources.
*
* After this function is called the builder object is reset.
* \return OpenGL object name. User is reponsible for managing it.
*/
[[nodiscard]] GLuint link();
};
}