From 6485de44cd2a8ae9101dfd815355d11111cc9c19 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 1 Aug 2023 15:02:25 +0200 Subject: [PATCH] improved texture class --- src/Engine/Engine.vcxproj | 1 + src/Engine/Engine.vcxproj.filters | 3 +++ src/Engine/main.cpp | 5 ----- src/Engine/textures/Texture2D.h | 30 +++++++++++++++++++++++++++--- src/Engine/util/property.h | 14 ++++++++++++++ 5 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 src/Engine/util/property.h diff --git a/src/Engine/Engine.vcxproj b/src/Engine/Engine.vcxproj index 72a3a42..c62a1fd 100644 --- a/src/Engine/Engine.vcxproj +++ b/src/Engine/Engine.vcxproj @@ -158,6 +158,7 @@ + diff --git a/src/Engine/Engine.vcxproj.filters b/src/Engine/Engine.vcxproj.filters index 6b0f6d2..aa346ca 100644 --- a/src/Engine/Engine.vcxproj.filters +++ b/src/Engine/Engine.vcxproj.filters @@ -53,6 +53,9 @@ Header Files + + Header Files + diff --git a/src/Engine/main.cpp b/src/Engine/main.cpp index 3150759..0623457 100644 --- a/src/Engine/main.cpp +++ b/src/Engine/main.cpp @@ -171,11 +171,6 @@ int main() Texture2D containerImage("images/container.jpg"); Texture2D faceImage("images/awesomeface.png", 0, GL_RGBA); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // use nearest neighbour when zooming out - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // use bilinear when zooming in - mat4 modelMatrix = mat4(1.0f); modelMatrix = glm::rotate(modelMatrix, glm::radians(-55.0f), vec3(1.0f, 0, 0)); mat4 viewMatrix = mat4(1.0f); diff --git a/src/Engine/textures/Texture2D.h b/src/Engine/textures/Texture2D.h index 20f7601..b344fbf 100644 --- a/src/Engine/textures/Texture2D.h +++ b/src/Engine/textures/Texture2D.h @@ -3,15 +3,35 @@ #include #include "../util/stb_image.h" +#include "../util/property.h" #include "../exceptions/IOException.h" // Maybe make a base "Texture" class and make a Texture2D and Texture3D child class. look into what would make more sense class Texture2D { public: + /// + /// Defines how the texture should be wrapped if the uv-coordinates are out of bounds. + /// Default: GL_REPEAT + /// + Property TextureWrapMode; + /// + /// Defines how a texel should be interpolated when zooming out of the texture. + /// Default: GL_LINEAR + /// + Property MinifyingInterpolation; + /// + /// Defines how a texel should be interpolated when zooming in to the texture. + /// Default: GL_LINEAR + /// + Property MagnifyingInterpolation; + + // maybe add a parameter for configuring mipmapping - Texture2D(std::string pathToTexture, int desiredColourChannels = 0, GLenum fileFormat = GL_RGB) + Texture2D(std::string pathToTexture, int desiredColourChannels = 0, GLenum fileFormat = GL_RGB) + : TextureWrapMode{GL_REPEAT}, MinifyingInterpolation{GL_LINEAR}, MagnifyingInterpolation{GL_LINEAR} { + unsigned char* image = stbi_load(pathToTexture.c_str(), &width, &height, &nrChannels, desiredColourChannels); if (!image) { @@ -30,12 +50,16 @@ public: } /// - /// Activate and bind this texture to use it + /// Activate and bind this texture to use it. Also sets properties like texture wrapping and interpolation /// /// The texture unit of this texture. For example GL_TEXTURE0 (default) void BindTexture(GLenum textureUnit = GL_TEXTURE0) { - // TODO: add the interpolation configuration. Make as properties which have default settings kinda like Unity + // MAybe move these to a seperate, virtual method? And also maybe make this method virtual to allow a child class "Texture3D" + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, TextureWrapMode()); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, MinifyingInterpolation()); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, MagnifyingInterpolation()); + glActiveTexture(textureUnit); // before binding texture, activate correct textre Unit (some drivers might show nothing if this is omitted) glBindTexture(GL_TEXTURE_2D, textureId); } diff --git a/src/Engine/util/property.h b/src/Engine/util/property.h new file mode 100644 index 0000000..30f864c --- /dev/null +++ b/src/Engine/util/property.h @@ -0,0 +1,14 @@ +#pragma once + +template +class Property +{ +public: + Property(const T &data): data{data}{} + + T operator()() { return data; } + void operator()(const T &d) { data = d; } +private: + T data; + void operator=(const Property& p); // so this property can not be overwritten, since this might lead to a memory leak. Maybe this operator can be used to assign only the data (C#-like)? +};