Move Texture to simpleengine::gfx, separate texture to cpp file

This commit is contained in:
SeanOMik 2021-11-27 14:11:07 -05:00
parent d3555e112d
commit 88fe486473
7 changed files with 106 additions and 86 deletions

View File

@ -44,8 +44,8 @@ int main(int argc, char *argv[]) {
shader_prog.link();
std::shared_ptr<GLuint> base_shader_program = shader_prog.program;
simpleengine::Texture wall_texture("resources/wall.jpg");
simpleengine::Texture crate_texture("resources/container.jpg", true, true);
simpleengine::gfx::Texture wall_texture("resources/wall.jpg");
simpleengine::gfx::Texture crate_texture("resources/container.jpg", true, true);
std::vector<simpleengine::Vertex> vertices = {
{glm::vec3(-0.5f, -0.5f, 0.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f)}, // bottom left

View File

@ -19,7 +19,7 @@
#include <vector>
#include <iostream>
namespace simpleengine {
namespace simpleengine::gfx {
class Texture {
private:
unsigned char* img_data;
@ -38,38 +38,7 @@ namespace simpleengine {
* @param img_2d Whether or not the texture is 2D.
* @param mipmap Whether or not to generate mipmaps for this texture.
*/
Texture(const char* path, bool img_2d = true, bool mipmap = true) {
image_type = img_2d ? GL_TEXTURE_2D : GL_TEXTURE_3D;
glGenTextures(1, &texture_id);
bind();
glTexParameteri(image_type, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(image_type, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(image_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(image_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img_data = SOIL_load_image
(
path,
&width, &height, &channels,
SOIL_LOAD_RGBA
);
if (!img_data) {
std::cerr << "Failed to load texture from memory! (" << SOIL_last_result() << ")" << std::endl;
throw std::runtime_error("Failed to load texture from memory!");
}
glTexImage2D(image_type, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data);
if (mipmap) {
glGenerateMipmap(image_type);
}
SOIL_free_image_data(img_data);
glBindTexture(image_type, 0);
}
Texture(const char* path, bool img_2d = true, bool mipmap = true);
/**
* @brief Construct a new Texture object from the loaded file buffer.
@ -79,38 +48,7 @@ namespace simpleengine {
* @param img_2d Whether or not the texture is 2D.
* @param mipmap Whether or not to generate mipmaps for this texture.
*/
Texture(const unsigned char *const buffer, int buffer_length, bool img_2d = true, bool mipmap = true) {
image_type = img_2d ? GL_TEXTURE_2D : GL_TEXTURE_3D;
glGenTextures(1, &texture_id);
bind();
glTexParameteri(image_type, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(image_type, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(image_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(image_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img_data = SOIL_load_image_from_memory
(
buffer, buffer_length,
&width, &height, &channels,
SOIL_LOAD_RGBA
);
if (!img_data) {
std::cerr << "Failed to load texture from memory! (" << SOIL_last_result() << ")" << std::endl;
throw std::runtime_error("Failed to load texture from memory!");
}
glTexImage2D(image_type, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data);
if (mipmap) {
glGenerateMipmap(image_type);
}
SOIL_free_image_data(img_data);
glBindTexture(image_type, 0);
}
Texture(const unsigned char *const buffer, int buffer_length, bool img_2d = true, bool mipmap = true);
/**
* @brief Construct a new Texture object from the loaded file buffer.
@ -119,20 +57,9 @@ namespace simpleengine {
* @param img_2d Whether or not the texture is 2D.
* @param mipmap Whether or not to generate mipmaps for this texture.
*/
Texture(std::vector<unsigned char> buffer, bool img_2d = true, bool mipmap = true) :
Texture(buffer.data(), buffer.size(), img_2d, mipmap) {
}
Texture(std::vector<unsigned char> buffer, bool img_2d = true, bool mipmap = true);
/* ~Texture() {
if (img_data != nullptr) {
SOIL_free_image_data(img_data);
}
} */
void bind() const {
glBindTexture(image_type, texture_id);
}
void bind() const;
};
}

View File

@ -32,7 +32,7 @@ namespace simpleengine::shapes_2d {
using super = simpleengine::Renderable;
private:
gfx::Shader shader;
nonstd::optional<Texture> texture;
nonstd::optional<gfx::Texture> texture;
public:
std::vector<Vertex> vertices;
std::vector<GLuint> indicies;
@ -46,7 +46,7 @@ namespace simpleengine::shapes_2d {
virtual ~Square() = default;
void set_texture(Texture texture);
void set_texture(gfx::Texture texture);
virtual void update(const float& delta_time) override;

View File

@ -29,7 +29,7 @@ namespace simpleengine::shapes_2d {
using super = simpleengine::Renderable;
private:
gfx::Shader shader; // This only stores the shader program
nonstd::optional<Texture> texture;
nonstd::optional<gfx::Texture> texture;
public:
std::vector<Vertex> vertices;
gfx::VBO vbo;
@ -41,7 +41,7 @@ namespace simpleengine::shapes_2d {
virtual ~Triangle() = default;
void set_texture(Texture texture);
void set_texture(gfx::Texture texture);
virtual void update(const float& delta_time) override;

93
src/gfx/texture.cpp Normal file
View File

@ -0,0 +1,93 @@
#include "gfx/texture.h"
namespace simpleengine::gfx {
Texture::Texture(const char* path, bool img_2d, bool mipmap) {
image_type = img_2d ? GL_TEXTURE_2D : GL_TEXTURE_3D;
glGenTextures(1, &texture_id);
bind();
glTexParameteri(image_type, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(image_type, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(image_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(image_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img_data = SOIL_load_image
(
path,
&width, &height, &channels,
SOIL_LOAD_RGBA
);
if (!img_data) {
std::cerr << "Failed to load texture from memory! (" << SOIL_last_result() << ")" << std::endl;
throw std::runtime_error("Failed to load texture from memory!");
}
glTexImage2D(image_type, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data);
if (mipmap) {
glGenerateMipmap(image_type);
}
SOIL_free_image_data(img_data);
glBindTexture(image_type, 0);
}
/**
* @brief Construct a new Texture object from the loaded file buffer.
*
* @param buffer The bytes of the loaded file.
* @param buffer_length The length of the buffer.
* @param img_2d Whether or not the texture is 2D.
* @param mipmap Whether or not to generate mipmaps for this texture.
*/
Texture::Texture(const unsigned char *const buffer, int buffer_length, bool img_2d, bool mipmap) {
image_type = img_2d ? GL_TEXTURE_2D : GL_TEXTURE_3D;
glGenTextures(1, &texture_id);
bind();
glTexParameteri(image_type, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(image_type, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(image_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(image_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
img_data = SOIL_load_image_from_memory
(
buffer, buffer_length,
&width, &height, &channels,
SOIL_LOAD_RGBA
);
if (!img_data) {
std::cerr << "Failed to load texture from memory! (" << SOIL_last_result() << ")" << std::endl;
throw std::runtime_error("Failed to load texture from memory!");
}
glTexImage2D(image_type, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data);
if (mipmap) {
glGenerateMipmap(image_type);
}
SOIL_free_image_data(img_data);
glBindTexture(image_type, 0);
}
/**
* @brief Construct a new Texture object from the loaded file buffer.
*
* @param buffer The bytes of the loaded file.
* @param img_2d Whether or not the texture is 2D.
* @param mipmap Whether or not to generate mipmaps for this texture.
*/
Texture::Texture(std::vector<unsigned char> buffer, bool img_2d, bool mipmap) :
Texture(buffer.data(), buffer.size(), img_2d, mipmap) {
}
void Texture::bind() const {
glBindTexture(image_type, texture_id);
}
}

View File

@ -28,7 +28,7 @@ namespace simpleengine::shapes_2d {
}
void Square::set_texture(Texture texture) {
void Square::set_texture(gfx::Texture texture) {
this->texture = texture;
}

View File

@ -19,7 +19,7 @@ namespace simpleengine::shapes_2d {
}
void Triangle::set_texture(Texture texture) {
void Triangle::set_texture(gfx::Texture texture) {
this->texture = texture;
}