Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed Nov 24, 2024
1 parent 1a46fdc commit f22161d
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 31 deletions.
73 changes: 52 additions & 21 deletions include/jGL/OpenGL/Shader/glShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,83 @@
#include <jGL/OpenGL/gl.h>
#include <jGL/shader.h>

/*
Create a shader like so
Shader s(vs, fs) # for std::strings vs and fs
Uniforms are auto scraped in the form "uniform TYPE NAME;"
Get and Set like so
s.getUniform<int>("anInteger");
s.setUniform<glm::mat4>("projection",myMat4Variable);
Use, compile and release
s.compile() # compiles code, creates a program if its not one
s.use(); # glUseProgram
s.release(); # glDeleteProgram
*/

namespace jGL::GL
{

/**
* @brief An OpenGL implementation of Shader.
*
*/
struct glShader : public Shader
{

/**
* @brief Construct a glShader from a vertex and fragment source.
*
* @param v vertex shader source.
* @param f fragment shader source.
*/
glShader(const char * v, const char * f)
: Shader(v, f), program(0), compiled(false), used(false)
{}

/**
* @brief Construct an empty glShader.
*/
glShader()
: Shader(), program(0), compiled(false), used(false)
{}

/**
* @brief Construct a glShader from source given by paths.
*
* @param path path the .vs and .fs shader sources.
* @param name name of sources in path: name.vs and name.fs
*/
glShader(std::string path, std::string name)
: Shader(path, name), program(0), compiled(false), used(false)
{}

~glShader(){if(isProgram()){release();}}

/**
* @brief Create the shader program.
*
*/
void create();

/**
* @brief Destroy the shader program.
*
*/
void release();

/**
* @brief Compile the shader program.
* @remark If not a program, call glShader::create.
*/
void compile();

/**
* @brief Use the shader program (and compile if required).
* @remark If not compiled call glShader::compile.
*/
void use();

/**
* @brief Checks if the glShader is compiled.
*
* @return true it is compiled.
* @return false it is not compiled.
*/
bool isCompiled(){return compiled;}

/**
* @brief Checks if an OpenGL program is created.
*
* @return true a program has been created.
* @return false a program has not been created.
*/
bool isProgram(){return glIsProgram(program);}

private:
Expand Down
70 changes: 60 additions & 10 deletions include/jGL/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,38 @@ namespace jGL
const std::regex UNIFORM_DATA_REGEX<Sampler2D> = std::regex("uniform(\\slowp\\s|\\shighp\\s|\\smediump\\s|\\s)sampler2D (\\S+);");


/**
* @brief Generic shader program with vertex and fragment shader.
* @remark Uniforms are automatically detected.
*
*/
struct Shader
{

/*
A generic Shader object that can read vertex and fragment shaders from
file or from const char *'s
Uniforms are detected within the shader program
*/

/**
* @brief Construct a Shader from a vertex and fragment source.
*
* @param v vertex shader source.
* @param f fragment shader source.
*/
Shader(const char * v, const char * f)
: vertex(v), fragment(f)
{
parseUniforms();
}

/**
* @brief Construct an empty Shader.
*/
Shader()
: vertex(""),fragment("")
{}

/**
* @brief Construct a Shader from source given by paths.
*
* @param path path the .vs and .fs shader sources.
* @param name name of sources in path: name.vs and name.fs
*/
Shader(std::string path, std::string name);

virtual ~Shader() = default;
Expand All @@ -66,8 +76,21 @@ namespace jGL
return this->vertex == s.vertex && this->fragment == s.fragment;
}

/**
* @brief Check for common shader errors.
*
* @return true linting passes.
* @return false linting fails.
*/
bool lint();

/**
* @brief Set a Uniform to a value.
*
* @tparam T the type of uniform.
* @param name the name of the uniform.
* @param value the value of type T.
*/
template <class T>
void setUniform(std::string name, T value)
{
Expand All @@ -92,6 +115,13 @@ namespace jGL
const std::string & getVertex() const { return vertex; }
const std::string & getFragment() const { return fragment; }

/**
* @brief Get a Uniform by name.
*
* @tparam T the type of the uniform.
* @param name the name of the uniform to get.
* @return jGLUniform<T> the uniform found.
*/
template <class T>
jGLUniform<T> getUniform(std::string name)
{
Expand All @@ -113,6 +143,11 @@ namespace jGL
return NULL_UNIFORM<T>;
}

/**
* @brief Get all the parsed uniforms in the Shader.
*
* @return std::vector<std::string> the available uniform names.
*/
std::vector<std::string> getUniformNames()
{
std::vector<std::string> v;
Expand All @@ -123,9 +158,24 @@ namespace jGL
return v;
}

/**
* @brief Use the shader.
* @remark Use will auto-compile if required.
*/
virtual void use() = 0;

/**
* @brief Display the vertex shader with line numbers.
*
* @return std::string the formatted vertex shader
*/
std::string displayVertexSource() const { return formatWithLineNumbers(vertex); }

/**
* @brief Display the fragment shader with line numbers.
*
* @return std::string the formatted fragment shader
*/
std::string displayFragmentSource() const { return formatWithLineNumbers(fragment); }

protected:
Expand Down

0 comments on commit f22161d

Please sign in to comment.