improved texture class

This commit is contained in:
Daniel 2023-08-01 15:02:25 +02:00
parent a89e820bd8
commit 6485de44cd
5 changed files with 45 additions and 8 deletions

View File

@ -158,6 +158,7 @@
<ClInclude Include="Shaders\Enums.h" />
<ClInclude Include="Shaders\Shader.h" />
<ClInclude Include="textures\Texture2D.h" />
<ClInclude Include="util\property.h" />
<ClInclude Include="util\stb_image.h" />
</ItemGroup>
<ItemGroup>

View File

@ -53,6 +53,9 @@
<ClInclude Include="textures\Texture2D.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="util\property.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CopyFileToFolders Include="Shaders\default\default.frag" />

View File

@ -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);

View File

@ -3,15 +3,35 @@
#include <string>
#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:
/// <summary>
/// Defines how the texture should be wrapped if the uv-coordinates are out of bounds.
/// Default: GL_REPEAT
/// </summary>
Property<GLenum> TextureWrapMode;
/// <summary>
/// Defines how a texel should be interpolated when zooming out of the texture.
/// Default: GL_LINEAR
/// </summary>
Property<GLenum> MinifyingInterpolation;
/// <summary>
/// Defines how a texel should be interpolated when zooming in to the texture.
/// Default: GL_LINEAR
/// </summary>
Property<GLenum> 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:
}
/// <summary>
/// Activate and bind this texture to use it
/// Activate and bind this texture to use it. Also sets properties like texture wrapping and interpolation
/// </summary>
/// <param name="texture">The texture unit of this texture. For example GL_TEXTURE0 (default)</param>
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);
}

View File

@ -0,0 +1,14 @@
#pragma once
template<typename T>
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<T>& 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)?
};