diff --git a/include/simpleengine/gfx/shader.h b/include/simpleengine/gfx/shader.h index 48752da..2af38cb 100644 --- a/include/simpleengine/gfx/shader.h +++ b/include/simpleengine/gfx/shader.h @@ -91,13 +91,20 @@ namespace simpleengine::gfx { */ static void unuse(); + /** + * @brief Attach the shader to a program. No need to call this if its already attached once. + * + * @param program The program to attach the shader to. + */ + void attach(std::shared_ptr program); + /** * @brief Get a Uniform Float from the shader using a `location`. * * @param location The location of the uniform float. * @return GLfloat The value of the uniform float from the shader. */ - GLfloat getUniformFloat(GLint location) const; + GLfloat get_uniform_float(GLint location) const; /** * @brief Get a Uniform Float from the shader finding the location of the uniform using `uniform_name`. @@ -105,7 +112,7 @@ namespace simpleengine::gfx { * @param uniform_name The name of the uniform inside of the shader. * @return GLfloat The value of the uniform float from the shader. */ - GLfloat getUniformFloat(const char* uniform_name) const; + GLfloat get_uniform_float(const char* uniform_name) const; /** * @brief Get a Uniform integer from the shader using a `location`. @@ -113,7 +120,7 @@ namespace simpleengine::gfx { * @param location The location of the uniform integer. * @return GLint The value of the uniform integer from the shader. */ - GLint getUniformInt(GLint location) const; + GLint get_uniform_int(GLint location) const; /** * @brief Get a Uniform integer from the shader finding the location of the uniform using `uniform_name`. @@ -121,7 +128,7 @@ namespace simpleengine::gfx { * @param uniform_name The name of the uniform inside of the shader. * @return GLint The value of the uniform integer from the shader. */ - GLint getUniformInt(const char* uniform_name) const; + GLint get_uniform_int(const char* uniform_name) const; /** * @brief Get a Uniform unsigned integer from the shader using a `location`. @@ -129,7 +136,7 @@ namespace simpleengine::gfx { * @param location The location of the uniform unsigned integer. * @return GLuint The value of the uniform unsigned integer from the shader. */ - GLuint getUniformUInt(GLint location) const; + GLuint get_uniform_uint(GLint location) const; /** * @brief Get a Uniform unsigned integer from the shader finding the location of the uniform using `uniform_name`. @@ -137,7 +144,7 @@ namespace simpleengine::gfx { * @param uniform_name The name of the uniform inside of the shader. * @return GLuint The value of the uniform unsigned integer from the shader. */ - GLuint getUniformUInt(const char* uniform_name) const; + GLuint get_uniform_uint(const char* uniform_name) const; /** * @brief Get a Uniform double from the shader using a `location`. @@ -145,7 +152,7 @@ namespace simpleengine::gfx { * @param location The location of the uniform double. * @return GLdouble The value of the uniform double from the shader. */ - GLdouble getUniformDouble(GLint location) const; + GLdouble get_uniform_double(GLint location) const; /** * @brief Get a Uniform double from the shader finding the location of the uniform using `uniform_name`. @@ -153,42 +160,42 @@ namespace simpleengine::gfx { * @param uniform_name The name of the uniform inside of the shader. * @return GLdouble The value of the uniform double from the shader. */ - GLdouble getUniformDouble(const char* uniform_name) const; + GLdouble get_uniform_double(const char* uniform_name) const; - void setUniformFloat(GLint location, GLfloat fl, bool bind_shader = true); - void setUniformFloat(const char* uniform_name, GLfloat fl, bool bind_shader = true); + void set_uniform_float(GLint location, GLfloat fl, bool bind_shader = true); + void set_uniform_float(const char* uniform_name, GLfloat fl, bool bind_shader = true); - void setUniformFloatVec2(GLint location, glm::vec2 vec, bool bind_shader = true); - void setUniformFloatVec2(const char* uniform_name, glm::vec2 vec, bool bind_shader = true); + void set_uniform_float_vec2(GLint location, glm::vec2 vec, bool bind_shader = true); + void set_uniform_float_vec2(const char* uniform_name, glm::vec2 vec, bool bind_shader = true); - void setUniformFloatVec3(GLint location, glm::vec3 vec, bool bind_shader = true); - void setUniformFloatVec3(const char* uniform_name, glm::vec3 vec, bool bind_shader = true); + void set_uniform_float_vec3(GLint location, glm::vec3 vec, bool bind_shader = true); + void set_uniform_float_vec3(const char* uniform_name, glm::vec3 vec, bool bind_shader = true); - void setUniformFloatVec4(GLint location, glm::vec4 vec, bool bind_shader = true); - void setUniformFloatVec4(const char* uniform_name, glm::vec4 vec, bool bind_shader = true); + void set_uniform_float_vec4(GLint location, glm::vec4 vec, bool bind_shader = true); + void set_uniform_float_vec4(const char* uniform_name, glm::vec4 vec, bool bind_shader = true); - void setUniformInt(GLint location, GLint i, bool bind_shader = true); - void setUniformInt(const char* uniform_name, GLint i, bool bind_shader = true); + void set_uniform_int(GLint location, GLint i, bool bind_shader = true); + void set_uniform_int(const char* uniform_name, GLint i, bool bind_shader = true); - void setUniformIntVec2(GLint location, glm::ivec2 vec, bool bind_shader = true); - void setUniformIntVec2(const char* uniform_name, glm::ivec2 vec, bool bind_shader = true); + void set_uniform_int_vec2(GLint location, glm::ivec2 vec, bool bind_shader = true); + void set_uniform_int_vec2(const char* uniform_name, glm::ivec2 vec, bool bind_shader = true); - void setUniformIntVec3(GLint location, glm::ivec3 vec, bool bind_shader = true); - void setUniformIntVec3(const char* uniform_name, glm::ivec3 vec, bool bind_shader = true); + void set_uniform_int_vec3(GLint location, glm::ivec3 vec, bool bind_shader = true); + void set_uniform_int_vec3(const char* uniform_name, glm::ivec3 vec, bool bind_shader = true); - void setUniformIntVec4(GLint location, glm::ivec4 vec, bool bind_shader = true); - void setUniformIntVec4(const char* uniform_name, glm::ivec4 vec, bool bind_shader = true); + void set_uniform_int_vec4(GLint location, glm::ivec4 vec, bool bind_shader = true); + void set_uniform_int_vec4(const char* uniform_name, glm::ivec4 vec, bool bind_shader = true); - void setUniformUInt(GLint location, GLuint ui, bool bind_shader = true); - void setUniformUInt(const char* uniform_name, GLuint ui, bool bind_shader = true); + void set_uniform_uint(GLint location, GLuint ui, bool bind_shader = true); + void set_uniform_uint(const char* uniform_name, GLuint ui, bool bind_shader = true); - void setUniformUIntVec2(GLint location, glm::uvec2 vec, bool bind_shader = true); - void setUniformUIntVec2(const char* uniform_name, glm::uvec2 vec, bool bind_shader = true); + void set_uniform_uint_vec2(GLint location, glm::uvec2 vec, bool bind_shader = true); + void set_uniform_uint_vec2(const char* uniform_name, glm::uvec2 vec, bool bind_shader = true); - void setUniformUIntVec3(GLint location, glm::uvec3 vec, bool bind_shader = true); - void setUniformUIntVec3(const char* uniform_name, glm::uvec3 vec, bool bind_shader = true); + void set_uniform_uint_vec3(GLint location, glm::uvec3 vec, bool bind_shader = true); + void set_uniform_uint_vec3(const char* uniform_name, glm::uvec3 vec, bool bind_shader = true); - void setUniformUIntVec4(GLint location, glm::uvec4 vec, bool bind_shader = true); - void setUniformUIntVec4(const char* uniform_name, glm::uvec4 vec, bool bind_shader = true); + void set_uniform_uint_vec4(GLint location, glm::uvec4 vec, bool bind_shader = true); + void set_uniform_uint_vec4(const char* uniform_name, glm::uvec4 vec, bool bind_shader = true); }; } \ No newline at end of file diff --git a/src/gfx/shader.cpp b/src/gfx/shader.cpp index ec84711..0e25ff9 100644 --- a/src/gfx/shader.cpp +++ b/src/gfx/shader.cpp @@ -1,6 +1,7 @@ #include "gfx/shader.h" #include +#include #include namespace simpleengine::gfx { @@ -41,7 +42,7 @@ namespace simpleengine::gfx { throw ShaderException("Failed to compile shader!"); } - glAttachShader(*shd.program, shd.shader); + shd.attach(shd.program); return shd; } @@ -105,7 +106,11 @@ namespace simpleengine::gfx { glUseProgram(0); } - GLfloat Shader::getUniformFloat(GLint location) const { + void Shader::attach(std::shared_ptr program) { + glAttachShader(*program, shader); + } + + GLfloat Shader::get_uniform_float(GLint location) const { use(); GLfloat fl; @@ -114,12 +119,12 @@ namespace simpleengine::gfx { return fl; } - GLfloat Shader::getUniformFloat(const char* uniform_name) const { + GLfloat Shader::get_uniform_float(const char* uniform_name) const { int location = glGetUniformLocation(*program, uniform_name); - return getUniformFloat(location); + return get_uniform_float(location); } - GLint Shader::getUniformInt(GLint location) const { + GLint Shader::get_uniform_int(GLint location) const { use(); GLint _int; @@ -128,12 +133,12 @@ namespace simpleengine::gfx { return _int; } - GLint Shader::getUniformInt(const char* uniform_name) const { + GLint Shader::get_uniform_int(const char* uniform_name) const { int location = glGetUniformLocation(*program, uniform_name); - return getUniformInt(location); + return get_uniform_int(location); } - GLuint Shader::getUniformUInt(GLint location) const { + GLuint Shader::get_uniform_uint(GLint location) const { use(); GLuint _uint; @@ -142,12 +147,12 @@ namespace simpleengine::gfx { return _uint; } - GLuint Shader::getUniformUInt(const char* uniform_name) const { + GLuint Shader::get_uniform_uint(const char* uniform_name) const { int location = glGetUniformLocation(*program, uniform_name); - return getUniformUInt(location); + return get_uniform_uint(location); } - GLdouble Shader::getUniformDouble(GLint location) const { + GLdouble Shader::get_uniform_double(GLint location) const { use(); GLdouble dbl; @@ -156,188 +161,188 @@ namespace simpleengine::gfx { return dbl; } - GLdouble Shader::getUniformDouble(const char* uniform_name) const { + GLdouble Shader::get_uniform_double(const char* uniform_name) const { int location = glGetUniformLocation(*program, uniform_name); - return getUniformDouble(location); + return get_uniform_double(location); } - void Shader::setUniformFloat(GLint location, GLfloat fl, bool bind_shader) { + void Shader::set_uniform_float(GLint location, GLfloat fl, bool bind_shader) { if (bind_shader) { use(); } - glUniform1f(location, fl); + glUniform1fv(location, 1, &fl); if (bind_shader) { unuse(); } } - void Shader::setUniformFloat(const char* uniform_name, GLfloat fl, bool bind_shader) { + void Shader::set_uniform_float(const char* uniform_name, GLfloat fl, bool bind_shader) { int location = glGetUniformLocation(*program, uniform_name); - setUniformFloat(location, fl, bind_shader); + set_uniform_float(location, fl, bind_shader); } - void Shader::setUniformFloatVec2(GLint location, glm::vec2 vec, bool bind_shader) { + void Shader::set_uniform_float_vec2(GLint location, glm::vec2 vec, bool bind_shader) { if (bind_shader) { use(); } - glUniform2f(location, vec.x, vec.y); + glUniform2fv(location, 1, glm::value_ptr(vec)); if (bind_shader) { unuse(); } } - void Shader::setUniformFloatVec2(const char* uniform_name, glm::vec2 vec, bool bind_shader) { + void Shader::set_uniform_float_vec2(const char* uniform_name, glm::vec2 vec, bool bind_shader) { int location = glGetUniformLocation(*program, uniform_name); - setUniformFloatVec2(location, vec, bind_shader); + set_uniform_float_vec2(location, vec, bind_shader); } - void Shader::setUniformFloatVec3(GLint location, glm::vec3 vec, bool bind_shader) { + void Shader::set_uniform_float_vec3(GLint location, glm::vec3 vec, bool bind_shader) { if (bind_shader) { use(); } - glUniform3f(location, vec.x, vec.y, vec.z); + glUniform3fv(location, 1, glm::value_ptr(vec)); if (bind_shader) { unuse(); } } - void Shader::setUniformFloatVec3(const char* uniform_name, glm::vec3 vec, bool bind_shader) { + void Shader::set_uniform_float_vec3(const char* uniform_name, glm::vec3 vec, bool bind_shader) { int location = glGetUniformLocation(*program, uniform_name); - setUniformFloatVec3(location, vec, bind_shader); + set_uniform_float_vec3(location, vec, bind_shader); } - void Shader::setUniformFloatVec4(GLint location, glm::vec4 vec, bool bind_shader) { + void Shader::set_uniform_float_vec4(GLint location, glm::vec4 vec, bool bind_shader) { if (bind_shader) { use(); } - glUniform4f(location, vec.x, vec.y, vec.z, vec.w); + glUniform4fv(location, 1, glm::value_ptr(vec)); if (bind_shader) { unuse(); } } - void Shader::setUniformFloatVec4(const char* uniform_name, glm::vec4 vec, bool bind_shader) { + void Shader::set_uniform_float_vec4(const char* uniform_name, glm::vec4 vec, bool bind_shader) { int location = glGetUniformLocation(*program, uniform_name); - setUniformFloatVec4(location, vec, bind_shader); + set_uniform_float_vec4(location, vec, bind_shader); } - void Shader::setUniformInt(GLint location, GLint i, bool bind_shader) { + void Shader::set_uniform_int(GLint location, GLint i, bool bind_shader) { if (bind_shader) { use(); } - glUniform1i(location, i); + glUniform1iv(location, 1, &i); if (bind_shader) { unuse(); } } - void Shader::setUniformInt(const char* uniform_name, GLint i, bool bind_shader) { + void Shader::set_uniform_int(const char* uniform_name, GLint i, bool bind_shader) { int location = glGetUniformLocation(*program, uniform_name); - setUniformInt(location, i, bind_shader); + set_uniform_int(location, i, bind_shader); } - void Shader::setUniformIntVec2(GLint location, glm::ivec2 vec, bool bind_shader) { + void Shader::set_uniform_int_vec2(GLint location, glm::ivec2 vec, bool bind_shader) { if (bind_shader) { use(); } - glUniform2i(location, vec.x, vec.y); + glUniform2iv(location, 1, glm::value_ptr(vec)); if (bind_shader) { unuse(); } } - void Shader::setUniformIntVec2(const char* uniform_name, glm::ivec2 vec, bool bind_shader) { + void Shader::set_uniform_int_vec2(const char* uniform_name, glm::ivec2 vec, bool bind_shader) { int location = glGetUniformLocation(*program, uniform_name); - setUniformIntVec2(location, vec, bind_shader); + set_uniform_int_vec2(location, vec, bind_shader); } - void Shader::setUniformIntVec3(GLint location, glm::ivec3 vec, bool bind_shader) { + void Shader::set_uniform_int_vec3(GLint location, glm::ivec3 vec, bool bind_shader) { if (bind_shader) { use(); } - glUniform3i(location, vec.x, vec.y, vec.z); + glUniform3iv(location, 1, glm::value_ptr(vec)); if (bind_shader) { unuse(); } } - void Shader::setUniformIntVec3(const char* uniform_name, glm::ivec3 vec, bool bind_shader) { + void Shader::set_uniform_int_vec3(const char* uniform_name, glm::ivec3 vec, bool bind_shader) { int location = glGetUniformLocation(*program, uniform_name); - setUniformIntVec3(location, vec, bind_shader); + set_uniform_int_vec3(location, vec, bind_shader); } - void Shader::setUniformIntVec4(GLint location, glm::ivec4 vec, bool bind_shader) { + void Shader::set_uniform_int_vec4(GLint location, glm::ivec4 vec, bool bind_shader) { if (bind_shader) { use(); } - glUniform4i(location, vec.x, vec.y, vec.z, vec.w); + glUniform4iv(location, 1, glm::value_ptr(vec)); if (bind_shader) { unuse(); } } - void Shader::setUniformIntVec4(const char* uniform_name, glm::ivec4 vec, bool bind_shader) { + void Shader::set_uniform_int_vec4(const char* uniform_name, glm::ivec4 vec, bool bind_shader) { int location = glGetUniformLocation(*program, uniform_name); - setUniformIntVec4(location, vec, bind_shader); + set_uniform_int_vec4(location, vec, bind_shader); } - void Shader::setUniformUInt(GLint location, GLuint ui, bool bind_shader) { + void Shader::set_uniform_uint(GLint location, GLuint ui, bool bind_shader) { if (bind_shader) { use(); } - glUniform1ui(location, ui); + glUniform1uiv(location, 1, &ui); if (bind_shader) { unuse(); } } - void Shader::setUniformUInt(const char* uniform_name, GLuint ui, bool bind_shader) { + void Shader::set_uniform_uint(const char* uniform_name, GLuint ui, bool bind_shader) { int location = glGetUniformLocation(*program, uniform_name); - setUniformUInt(location, ui, bind_shader); + set_uniform_uint(location, ui, bind_shader); } - void Shader::setUniformUIntVec2(GLint location, glm::uvec2 vec, bool bind_shader) { + void Shader::set_uniform_uint_vec2(GLint location, glm::uvec2 vec, bool bind_shader) { if (bind_shader) { use(); } - glUniform2ui(location, vec.x, vec.y); + glUniform2uiv(location, 1,glm::value_ptr(vec)); if (bind_shader) { unuse(); } } - void Shader::setUniformUIntVec2(const char* uniform_name, glm::uvec2 vec, bool bind_shader) { + void Shader::set_uniform_uint_vec2(const char* uniform_name, glm::uvec2 vec, bool bind_shader) { int location = glGetUniformLocation(*program, uniform_name); - setUniformUIntVec2(location, vec, bind_shader); + set_uniform_uint_vec2(location, vec, bind_shader); } - void Shader::setUniformUIntVec3(GLint location, glm::uvec3 vec, bool bind_shader) { + void Shader::set_uniform_uint_vec3(GLint location, glm::uvec3 vec, bool bind_shader) { if (bind_shader) { use(); } - glUniform3ui(location, vec.x, vec.y, vec.z); + glUniform3uiv(location, 1, glm::value_ptr(vec)); if (bind_shader) { unuse(); } } - void Shader::setUniformUIntVec3(const char* uniform_name, glm::uvec3 vec, bool bind_shader) { + void Shader::set_uniform_uint_vec3(const char* uniform_name, glm::uvec3 vec, bool bind_shader) { int location = glGetUniformLocation(*program, uniform_name); - setUniformUIntVec3(location, vec, bind_shader); + set_uniform_uint_vec3(location, vec, bind_shader); } - void Shader::setUniformUIntVec4(GLint location, glm::uvec4 vec, bool bind_shader) { + void Shader::set_uniform_uint_vec4(GLint location, glm::uvec4 vec, bool bind_shader) { if (bind_shader) { use(); } - glUniform4ui(location, vec.x, vec.y, vec.z, vec.w); + glUniform4uiv(location, 1, glm::value_ptr(vec)); if (bind_shader) { unuse(); } } - void Shader::setUniformUIntVec4(const char* uniform_name, glm::uvec4 vec, bool bind_shader) { + void Shader::set_uniform_uint_vec4(const char* uniform_name, glm::uvec4 vec, bool bind_shader) { int location = glGetUniformLocation(*program, uniform_name); - setUniformUIntVec4(location, vec, bind_shader); + set_uniform_uint_vec4(location, vec, bind_shader); } } \ No newline at end of file diff --git a/src/shapes/2d/triangle.cpp b/src/shapes/2d/triangle.cpp index 6c0e94e..a791a82 100644 --- a/src/shapes/2d/triangle.cpp +++ b/src/shapes/2d/triangle.cpp @@ -10,6 +10,9 @@ namespace simpleengine::shapes_2d { vao.enable_attrib(vbo, 1, 3, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, color)); vao.enable_attrib(vbo, 2, 2, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, tex_coord)); + // Tell the shader that there is no texture. + shader.set_uniform_int("texture_is_set", false); + glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } @@ -21,6 +24,9 @@ namespace simpleengine::shapes_2d { void Triangle::set_texture(gfx::Texture texture) { this->texture = texture; + + // Tell the shader that there is a texture set. + shader.set_uniform_int("texture_is_set", true); } void Triangle::update(const float& delta_time) { @@ -30,12 +36,8 @@ namespace simpleengine::shapes_2d { void Triangle::render(std::shared_ptr target) { shader.use(); - // If theres a texture set, tell the fragment shader that and bind to the texture for drawing. if (texture.has_value()) { - shader.setUniformInt("texture_is_set", true, false); texture.value().bind(); - } else { - shader.setUniformInt("texture_is_set", false, false); } vao.bind();