Added more texture types

This commit is contained in:
Lauchmelder 2021-12-26 06:12:08 +01:00
parent e60328cf04
commit 7c16ca7913
6 changed files with 129 additions and 16 deletions

View file

@ -12,7 +12,7 @@ namespace lol
{ {
public: public:
Image(); 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(const std::string& filepath);
~Image(); ~Image();

View file

@ -39,6 +39,14 @@ namespace lol
void Bind(); void Bind();
void Unbind(); 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 * Set a float uniform
* *
@ -55,6 +63,14 @@ namespace lol
*/ */
void SetUniform(const std::string& name, const glm::mat4& value); 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 * Set a 4 component vector uniform
* *

View file

@ -11,18 +11,36 @@ namespace lol
class Texture : public NonCopyable class Texture : public NonCopyable
{ {
public: public:
Texture(const Image& image, TextureFormat texFormat = TextureFormat::RGB); Texture(TargetTexture target);
~Texture(); ~Texture();
void Bind(); void Bind();
void Unbind(); 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 // TODO: Remove again later
inline unsigned int GetID() const { return id; } inline unsigned int GetID() const { return id; }
private: protected:
TargetTexture target;
unsigned int id; unsigned int id;
}; };
typedef ObjectManager<Texture> TextureManager; typedef ObjectManager<Texture> 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);
};
} }

View file

@ -179,6 +179,24 @@ namespace lol
ReadWrite = GL_READ_WRITE 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 * @brief OpenGL internal texture formats
*/ */
@ -309,4 +327,13 @@ namespace lol
return 0; 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
};
} }

View file

@ -86,6 +86,15 @@ namespace lol
glUseProgram(0); 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) void Shader::SetUniform(const std::string& name, float value)
{ {
GLint location = glGetUniformLocation(id, name.c_str()); GLint location = glGetUniformLocation(id, name.c_str());
@ -104,6 +113,15 @@ namespace lol
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(value)); 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) void Shader::SetUniform(const std::string& name, const glm::vec4& value)
{ {
GLint location = glGetUniformLocation(id, name.c_str()); GLint location = glGetUniformLocation(id, name.c_str());

View file

@ -6,20 +6,14 @@
namespace lol namespace lol
{ {
Texture::Texture(const Image& image, TextureFormat texFormat) : Texture::Texture(TargetTexture target) :
id(0) id(0), target(target)
{ {
glGenTextures(1, &id); glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(NATIVE(target), id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(NATIVE(target), GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(NATIVE(target), GL_TEXTURE_MAG_FILTER, GL_LINEAR);
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);
} }
Texture::~Texture() Texture::~Texture()
@ -27,13 +21,53 @@ namespace lol
glDeleteTextures(1, &id); 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() void Texture::Bind()
{ {
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(NATIVE(target), id);
} }
void Texture::Unbind() 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));
} }
} }