refactor: remove unused abstraction
This commit is contained in:
parent
e357a4eb9f
commit
8de408fd10
@ -6,7 +6,7 @@
|
||||
|
||||
#include "debug/Logger.hpp"
|
||||
#include "io/io.hpp"
|
||||
#include "graphics/core/GLTexture.hpp"
|
||||
#include "graphics/core/Texture.hpp"
|
||||
#include "graphics/core/ImageData.hpp"
|
||||
|
||||
static debug::Logger logger("png-coder");
|
||||
@ -205,7 +205,7 @@ std::unique_ptr<ImageData> png::load_image(const ubyte* bytes, size_t size) {
|
||||
|
||||
std::unique_ptr<Texture> png::load_texture(const ubyte* bytes, size_t size) {
|
||||
auto image = load_image(bytes, size);
|
||||
auto texture = GLTexture::from(image.get());
|
||||
auto texture = Texture::from(image.get());
|
||||
texture->setNearestFilter();
|
||||
return texture;
|
||||
}
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
#include <GL/glew.h>
|
||||
|
||||
Cubemap::Cubemap(uint width, uint height, ImageFormat imageFormat)
|
||||
: GLTexture(0, width, height)
|
||||
: Texture(0, width, height)
|
||||
{
|
||||
glGenTextures(1, &id);
|
||||
glBindTexture(GL_TEXTURE_CUBE_MAP, id);
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "GLTexture.hpp"
|
||||
#include "Texture.hpp"
|
||||
|
||||
/// @brief Cubemap texture
|
||||
class Cubemap : public GLTexture {
|
||||
class Cubemap : public Texture {
|
||||
public:
|
||||
Cubemap(uint width, uint height, ImageFormat format);
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#include "Framebuffer.hpp"
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include "GLTexture.hpp"
|
||||
#include "Texture.hpp"
|
||||
|
||||
Framebuffer::Framebuffer(uint fbo, uint depth, std::unique_ptr<Texture> texture)
|
||||
: fbo(fbo), depth(depth), texture(std::move(texture))
|
||||
@ -25,7 +25,7 @@ static std::unique_ptr<Texture> create_texture(int width, int height, int format
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
|
||||
return std::make_unique<GLTexture>(tex, width, height);
|
||||
return std::make_unique<Texture>(tex, width, height);
|
||||
}
|
||||
|
||||
Framebuffer::Framebuffer(uint width, uint height, bool alpha)
|
||||
|
||||
@ -1,101 +0,0 @@
|
||||
#include "GLTexture.hpp"
|
||||
#include "gl_util.hpp"
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include <stdexcept>
|
||||
#include <memory>
|
||||
|
||||
uint Texture::MAX_RESOLUTION = 1024; // Window.initialize overrides it
|
||||
|
||||
GLTexture::GLTexture(uint id, uint width, uint height)
|
||||
: Texture(width, height), id(id) {
|
||||
}
|
||||
|
||||
GLTexture::GLTexture(const ubyte* data, uint width, uint height, ImageFormat imageFormat)
|
||||
: Texture(width, height) {
|
||||
glGenTextures(1, &id);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
GLenum format = gl::to_glenum(imageFormat);
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D, 0, format, width, height, 0,
|
||||
format, GL_UNSIGNED_BYTE, static_cast<const GLvoid*>(data)
|
||||
);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
GLTexture::~GLTexture() {
|
||||
glDeleteTextures(1, &id);
|
||||
}
|
||||
|
||||
void GLTexture::bind() const {
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
}
|
||||
|
||||
void GLTexture::unbind() const {
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void GLTexture::reload(const ImageData& image) {
|
||||
width = image.getWidth();
|
||||
height = image.getHeight();
|
||||
reload(image.getData());
|
||||
}
|
||||
|
||||
void GLTexture::reload(const ubyte* data) {
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, static_cast<const GLvoid*>(data));
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
std::unique_ptr<ImageData> GLTexture::readData() {
|
||||
auto data = std::make_unique<ubyte[]>(width * height * 4);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data.get());
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
return std::make_unique<ImageData>(
|
||||
ImageFormat::rgba8888, width, height, std::move(data)
|
||||
);
|
||||
}
|
||||
|
||||
void GLTexture::setNearestFilter() {
|
||||
bind();
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void GLTexture::setMipMapping(bool flag, bool pixelated) {
|
||||
bind();
|
||||
if (flag) {
|
||||
glTexParameteri(
|
||||
GL_TEXTURE_2D,
|
||||
GL_TEXTURE_MIN_FILTER,
|
||||
pixelated ? GL_NEAREST : GL_LINEAR_MIPMAP_NEAREST
|
||||
);
|
||||
} else {
|
||||
glTexParameteri(
|
||||
GL_TEXTURE_2D,
|
||||
GL_TEXTURE_MIN_FILTER,
|
||||
pixelated ? GL_NEAREST : GL_LINEAR
|
||||
);
|
||||
}
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
std::unique_ptr<GLTexture> GLTexture::from(const ImageData* image) {
|
||||
uint width = image->getWidth();
|
||||
uint height = image->getHeight();
|
||||
void* data = image->getData();
|
||||
return std::make_unique<GLTexture>(static_cast<ubyte*>(data), width, height, image->getFormat());
|
||||
}
|
||||
|
||||
uint GLTexture::getId() const {
|
||||
return id;
|
||||
}
|
||||
@ -1,31 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "Texture.hpp"
|
||||
|
||||
class GLTexture : public Texture {
|
||||
protected:
|
||||
uint id;
|
||||
public:
|
||||
GLTexture(uint id, uint width, uint height);
|
||||
GLTexture(const ubyte* data, uint width, uint height, ImageFormat format);
|
||||
virtual ~GLTexture();
|
||||
|
||||
virtual void bind() const override;
|
||||
virtual void unbind() const override;
|
||||
virtual void reload(const ubyte* data);
|
||||
|
||||
void setNearestFilter();
|
||||
|
||||
virtual void reload(const ImageData& image) override;
|
||||
|
||||
virtual void setMipMapping(bool flag, bool pixelated) override;
|
||||
|
||||
virtual std::unique_ptr<ImageData> readData() override;
|
||||
virtual uint getId() const override;
|
||||
|
||||
virtual UVRegion getUVRegion() const override {
|
||||
return UVRegion(0.0f, 0.0f, 1.0f, 1.0f);
|
||||
}
|
||||
|
||||
static std::unique_ptr<GLTexture> from(const ImageData* image);
|
||||
};
|
||||
@ -1,6 +1,103 @@
|
||||
#include "Texture.hpp"
|
||||
#include "GLTexture.hpp"
|
||||
#include "gl_util.hpp"
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include <stdexcept>
|
||||
#include <memory>
|
||||
|
||||
uint Texture::MAX_RESOLUTION = 1024; // Window.initialize overrides it
|
||||
|
||||
Texture::Texture(uint id, uint width, uint height)
|
||||
: id(id), width(width), height(height) {
|
||||
}
|
||||
|
||||
Texture::Texture(const ubyte* data, uint width, uint height, ImageFormat imageFormat)
|
||||
: width(width), height(height) {
|
||||
glGenTextures(1, &id);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
GLenum format = gl::to_glenum(imageFormat);
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D, 0, format, width, height, 0,
|
||||
format, GL_UNSIGNED_BYTE, static_cast<const GLvoid*>(data)
|
||||
);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
Texture::~Texture() {
|
||||
glDeleteTextures(1, &id);
|
||||
}
|
||||
|
||||
void Texture::bind() const {
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
}
|
||||
|
||||
void Texture::unbind() const {
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void Texture::reload(const ImageData& image) {
|
||||
width = image.getWidth();
|
||||
height = image.getHeight();
|
||||
reload(image.getData());
|
||||
}
|
||||
|
||||
void Texture::reload(const ubyte* data) {
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, static_cast<const GLvoid*>(data));
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
std::unique_ptr<ImageData> Texture::readData() {
|
||||
auto data = std::make_unique<ubyte[]>(width * height * 4);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data.get());
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
return std::make_unique<ImageData>(
|
||||
ImageFormat::rgba8888, width, height, std::move(data)
|
||||
);
|
||||
}
|
||||
|
||||
void Texture::setNearestFilter() {
|
||||
bind();
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void Texture::setMipMapping(bool flag, bool pixelated) {
|
||||
bind();
|
||||
if (flag) {
|
||||
glTexParameteri(
|
||||
GL_TEXTURE_2D,
|
||||
GL_TEXTURE_MIN_FILTER,
|
||||
pixelated ? GL_NEAREST : GL_LINEAR_MIPMAP_NEAREST
|
||||
);
|
||||
} else {
|
||||
glTexParameteri(
|
||||
GL_TEXTURE_2D,
|
||||
GL_TEXTURE_MIN_FILTER,
|
||||
pixelated ? GL_NEAREST : GL_LINEAR
|
||||
);
|
||||
}
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
std::unique_ptr<Texture> Texture::from(const ImageData* image) {
|
||||
return GLTexture::from(image);
|
||||
uint width = image->getWidth();
|
||||
uint height = image->getHeight();
|
||||
void* data = image->getData();
|
||||
return std::make_unique<Texture>(
|
||||
static_cast<ubyte*>(data), width, height, image->getFormat()
|
||||
);
|
||||
}
|
||||
|
||||
uint Texture::getId() const {
|
||||
return id;
|
||||
}
|
||||
|
||||
@ -8,33 +8,39 @@
|
||||
|
||||
class Texture {
|
||||
protected:
|
||||
uint id;
|
||||
uint width;
|
||||
uint height;
|
||||
|
||||
Texture(uint width, uint height) : width(width), height(height) {}
|
||||
public:
|
||||
static uint MAX_RESOLUTION;
|
||||
Texture(uint id, uint width, uint height);
|
||||
Texture(const ubyte* data, uint width, uint height, ImageFormat format);
|
||||
virtual ~Texture();
|
||||
|
||||
virtual ~Texture() {}
|
||||
virtual void bind() const;
|
||||
virtual void unbind() const;
|
||||
void reload(const ubyte* data);
|
||||
|
||||
virtual void bind() const = 0;
|
||||
virtual void unbind() const = 0;
|
||||
void setNearestFilter();
|
||||
|
||||
virtual void reload(const ImageData& image) = 0;
|
||||
void reload(const ImageData& image);
|
||||
|
||||
virtual std::unique_ptr<ImageData> readData() = 0;
|
||||
void setMipMapping(bool flag, bool pixelated);
|
||||
|
||||
virtual uint getWidth() const {
|
||||
std::unique_ptr<ImageData> readData();
|
||||
uint getId() const;
|
||||
|
||||
UVRegion getUVRegion() const {
|
||||
return UVRegion(0.0f, 0.0f, 1.0f, 1.0f);
|
||||
}
|
||||
|
||||
uint getWidth() const {
|
||||
return width;
|
||||
}
|
||||
virtual uint getHeight() const {
|
||||
|
||||
uint getHeight() const {
|
||||
return height;
|
||||
}
|
||||
virtual UVRegion getUVRegion() const = 0;
|
||||
|
||||
virtual uint getId() const = 0;
|
||||
|
||||
virtual void setMipMapping(bool flag, bool pixelated) = 0;
|
||||
|
||||
static std::unique_ptr<Texture> from(const ImageData* image);
|
||||
static uint MAX_RESOLUTION;
|
||||
};
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#include "TextureAnimation.hpp"
|
||||
#include "GLTexture.hpp"
|
||||
#include "Texture.hpp"
|
||||
#include "Framebuffer.hpp"
|
||||
|
||||
#include <GL/glew.h>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user