From 7c16ca791347a88a9770eac74e8130ab81eb6a3e Mon Sep 17 00:00:00 2001 From: Lauchmelder Date: Sun, 26 Dec 2021 06:12:08 +0100 Subject: [PATCH] Added more texture types --- include/lol/Image.hpp | 2 +- include/lol/Shader.hpp | 16 ++++++++++ include/lol/Texture.hpp | 22 ++++++++++++-- include/lol/util/Enums.hpp | 27 +++++++++++++++++ src/Shader.cpp | 18 ++++++++++++ src/Texture.cpp | 60 +++++++++++++++++++++++++++++--------- 6 files changed, 129 insertions(+), 16 deletions(-) diff --git a/include/lol/Image.hpp b/include/lol/Image.hpp index 7f26ee3..f09d8c0 100644 --- a/include/lol/Image.hpp +++ b/include/lol/Image.hpp @@ -12,7 +12,7 @@ namespace lol { public: Image(); - Image(unsigned int width, unsigned int height, PixelFormat pixelFormat = PixelFormat::RGBA, PixelType pixelType = PixelType::UByte); + Image(unsigned int width, unsigned int height, PixelFormat pixelFormat = PixelFormat::RGB, PixelType pixelType = PixelType::UByte); Image(const std::string& filepath); ~Image(); diff --git a/include/lol/Shader.hpp b/include/lol/Shader.hpp index 16a5e02..31ea750 100644 --- a/include/lol/Shader.hpp +++ b/include/lol/Shader.hpp @@ -39,6 +39,14 @@ namespace lol void Bind(); void Unbind(); + /** + * Set a int uniform + * + * @param name Name of the uniform + * @param value Value of the uniform + */ + void SetUniform(const std::string& name, int value); + /** * Set a float uniform * @@ -55,6 +63,14 @@ namespace lol */ void SetUniform(const std::string& name, const glm::mat4& value); + /** + * Set a 2 component vector uniform + * + * @param name Name of the uniform + * @param value Value of the uniform + */ + void SetUniform(const std::string& name, const glm::vec2& value); + /** * Set a 4 component vector uniform * diff --git a/include/lol/Texture.hpp b/include/lol/Texture.hpp index a1f12bc..5806ece 100644 --- a/include/lol/Texture.hpp +++ b/include/lol/Texture.hpp @@ -11,18 +11,36 @@ namespace lol class Texture : public NonCopyable { public: - Texture(const Image& image, TextureFormat texFormat = TextureFormat::RGB); + Texture(TargetTexture target); ~Texture(); void Bind(); void Unbind(); + void SetWrap(TextureWrap s, TextureWrap t, TextureWrap r = TextureWrap::Repeat); + void SetBorderColor(float r, float g, float b, float a); + // TODO: Remove again later inline unsigned int GetID() const { return id; } - private: + protected: + TargetTexture target; unsigned int id; }; typedef ObjectManager TextureManager; + + + class Texture2D : public Texture + { + public: + Texture2D(const Image& image, TextureFormat texFormat = TextureFormat::RGB); + }; + + class Texture1D : public Texture + { + public: + Texture1D(unsigned int width, const void* data, PixelFormat pixFormat = PixelFormat::RGB, PixelType pixType = PixelType::UByte, TextureFormat texFormat = TextureFormat::RGB); + Texture1D(const Image& image, TextureFormat texFormat = TextureFormat::RGB); + }; } \ No newline at end of file diff --git a/include/lol/util/Enums.hpp b/include/lol/util/Enums.hpp index 49a80f2..9f3a645 100644 --- a/include/lol/util/Enums.hpp +++ b/include/lol/util/Enums.hpp @@ -179,6 +179,24 @@ namespace lol ReadWrite = GL_READ_WRITE }; + /** + * @brief OpenGL target texture + */ + enum class TargetTexture : GLenum + { + Texture1D = GL_TEXTURE_1D, + Texture2D = GL_TEXTURE_2D, + Texture3D = GL_TEXTURE_3D, + Array1D = GL_TEXTURE_1D_ARRAY, + Array2D = GL_TEXTURE_2D_ARRAY, + Rectangle = GL_TEXTURE_RECTANGLE, + Cubemap = GL_TEXTURE_CUBE_MAP, + CubemapArray = GL_TEXTURE_CUBE_MAP_ARRAY, + Buffer = GL_TEXTURE_BUFFER, + Multisample = GL_TEXTURE_2D_MULTISAMPLE, + MultisampleArra = GL_TEXTURE_2D_MULTISAMPLE_ARRAY + }; + /** * @brief OpenGL internal texture formats */ @@ -309,4 +327,13 @@ namespace lol return 0; } } + + enum class TextureWrap : GLenum + { + ClampToEdge = GL_CLAMP_TO_EDGE, + ClampToBorder = GL_CLAMP_TO_BORDER, + MirroredRepeat = GL_MIRRORED_REPEAT, + Repeat = GL_REPEAT, + MirrorClampToEdge = GL_MIRROR_CLAMP_TO_EDGE + }; } \ No newline at end of file diff --git a/src/Shader.cpp b/src/Shader.cpp index df51bb8..09d27e2 100644 --- a/src/Shader.cpp +++ b/src/Shader.cpp @@ -86,6 +86,15 @@ namespace lol glUseProgram(0); } + void Shader::SetUniform(const std::string& name, int value) + { + GLint location = glGetUniformLocation(id, name.c_str()); + if (location == -1) + return; + + glUniform1i(location, value); + } + void Shader::SetUniform(const std::string& name, float value) { GLint location = glGetUniformLocation(id, name.c_str()); @@ -104,6 +113,15 @@ namespace lol glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(value)); } + void Shader::SetUniform(const std::string& name, const glm::vec2& value) + { + GLint location = glGetUniformLocation(id, name.c_str()); + if (location == -1) + return; + + glUniform2fv(location, 1, glm::value_ptr(value)); + } + void Shader::SetUniform(const std::string& name, const glm::vec4& value) { GLint location = glGetUniformLocation(id, name.c_str()); diff --git a/src/Texture.cpp b/src/Texture.cpp index f1e3947..6b19345 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -6,20 +6,14 @@ namespace lol { - Texture::Texture(const Image& image, TextureFormat texFormat) : - id(0) + Texture::Texture(TargetTexture target) : + id(0), target(target) { glGenTextures(1, &id); - glBindTexture(GL_TEXTURE_2D, id); + glBindTexture(NATIVE(target), id); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - glm::uvec2 imageSize = image.GetDimensions(); - glTexImage2D(GL_TEXTURE_2D, 0, NATIVE(texFormat), imageSize.x, imageSize.y, 0, NATIVE(image.GetPixelFormat()), NATIVE(image.GetPixelType()), image.GetPixels()); - glGenerateMipmap(GL_TEXTURE_2D); + glTexParameteri(NATIVE(target), GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(NATIVE(target), GL_TEXTURE_MAG_FILTER, GL_LINEAR); } Texture::~Texture() @@ -27,13 +21,53 @@ namespace lol glDeleteTextures(1, &id); } + void Texture::SetWrap(TextureWrap s, TextureWrap t, TextureWrap r) + { + glBindTexture(NATIVE(target), id); + glTexParameteri(NATIVE(target), GL_TEXTURE_WRAP_S, NATIVE(s)); + glTexParameteri(NATIVE(target), GL_TEXTURE_WRAP_T, NATIVE(t)); + glTexParameteri(NATIVE(target), GL_TEXTURE_WRAP_R, NATIVE(r)); + } + + void Texture::SetBorderColor(float r, float g, float b, float a) + { + glBindTexture(NATIVE(target), id); + + glm::vec4 color(r, g, b, a); + glTexParameterfv(NATIVE(target), GL_TEXTURE_BORDER_COLOR, &color[0]); + } + void Texture::Bind() { - glBindTexture(GL_TEXTURE_2D, id); + glBindTexture(NATIVE(target), id); } void Texture::Unbind() { - glBindTexture(GL_TEXTURE_2D, 0); + glBindTexture(NATIVE(target), 0); + } + + + Texture2D::Texture2D(const Image& image, TextureFormat texFormat) : + Texture(TargetTexture::Texture2D) + { + glm::uvec2 imageSize = image.GetDimensions(); + glTexImage2D(NATIVE(target), 0, NATIVE(texFormat), imageSize.x, imageSize.y, 0, NATIVE(image.GetPixelFormat()), NATIVE(image.GetPixelType()), image.GetPixels()); + glGenerateMipmap(NATIVE(target)); + } + + + Texture1D::Texture1D(unsigned int width, const void* data, PixelFormat pixFormat, PixelType pixType, TextureFormat texFormat) : + Texture(TargetTexture::Texture1D) + { + glTexImage1D(NATIVE(target), 0, NATIVE(texFormat), width, 0, NATIVE(pixFormat), NATIVE(pixType), data); + glGenerateMipmap(NATIVE(target)); + } + + Texture1D::Texture1D(const Image& image, TextureFormat texFormat) : + Texture(TargetTexture::Texture1D) + { + glTexImage1D(NATIVE(target), 0, NATIVE(texFormat), image.GetDimensions().x, 0, NATIVE(image.GetPixelFormat()), NATIVE(image.GetPixelType()), image.GetPixels()); + glGenerateMipmap(NATIVE(target)); } } \ No newline at end of file