Move Texture to simpleengine::gfx, separate texture to cpp file
This commit is contained in:
parent
d3555e112d
commit
88fe486473
|
@ -44,8 +44,8 @@ int main(int argc, char *argv[]) {
|
||||||
shader_prog.link();
|
shader_prog.link();
|
||||||
std::shared_ptr<GLuint> base_shader_program = shader_prog.program;
|
std::shared_ptr<GLuint> base_shader_program = shader_prog.program;
|
||||||
|
|
||||||
simpleengine::Texture wall_texture("resources/wall.jpg");
|
simpleengine::gfx::Texture wall_texture("resources/wall.jpg");
|
||||||
simpleengine::Texture crate_texture("resources/container.jpg", true, true);
|
simpleengine::gfx::Texture crate_texture("resources/container.jpg", true, true);
|
||||||
|
|
||||||
std::vector<simpleengine::Vertex> vertices = {
|
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
|
{glm::vec3(-0.5f, -0.5f, 0.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f)}, // bottom left
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
namespace simpleengine {
|
namespace simpleengine::gfx {
|
||||||
class Texture {
|
class Texture {
|
||||||
private:
|
private:
|
||||||
unsigned char* img_data;
|
unsigned char* img_data;
|
||||||
|
@ -38,38 +38,7 @@ namespace simpleengine {
|
||||||
* @param img_2d Whether or not the texture is 2D.
|
* @param img_2d Whether or not the texture is 2D.
|
||||||
* @param mipmap Whether or not to generate mipmaps for this texture.
|
* @param mipmap Whether or not to generate mipmaps for this texture.
|
||||||
*/
|
*/
|
||||||
Texture(const char* path, bool img_2d = true, bool mipmap = true) {
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Construct a new Texture object from the loaded file buffer.
|
* @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 img_2d Whether or not the texture is 2D.
|
||||||
* @param mipmap Whether or not to generate mipmaps for this texture.
|
* @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) {
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Construct a new Texture object from the loaded file buffer.
|
* @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 img_2d Whether or not the texture is 2D.
|
||||||
* @param mipmap Whether or not to generate mipmaps for this texture.
|
* @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(std::vector<unsigned char> buffer, bool img_2d = true, bool mipmap = true);
|
||||||
Texture(buffer.data(), buffer.size(), img_2d, mipmap) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ~Texture() {
|
void bind() const;
|
||||||
if (img_data != nullptr) {
|
|
||||||
SOIL_free_image_data(img_data);
|
|
||||||
}
|
|
||||||
} */
|
|
||||||
|
|
||||||
void bind() const {
|
|
||||||
glBindTexture(image_type, texture_id);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,7 @@ namespace simpleengine::shapes_2d {
|
||||||
using super = simpleengine::Renderable;
|
using super = simpleengine::Renderable;
|
||||||
private:
|
private:
|
||||||
gfx::Shader shader;
|
gfx::Shader shader;
|
||||||
nonstd::optional<Texture> texture;
|
nonstd::optional<gfx::Texture> texture;
|
||||||
public:
|
public:
|
||||||
std::vector<Vertex> vertices;
|
std::vector<Vertex> vertices;
|
||||||
std::vector<GLuint> indicies;
|
std::vector<GLuint> indicies;
|
||||||
|
@ -46,7 +46,7 @@ namespace simpleengine::shapes_2d {
|
||||||
|
|
||||||
virtual ~Square() = default;
|
virtual ~Square() = default;
|
||||||
|
|
||||||
void set_texture(Texture texture);
|
void set_texture(gfx::Texture texture);
|
||||||
|
|
||||||
virtual void update(const float& delta_time) override;
|
virtual void update(const float& delta_time) override;
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ namespace simpleengine::shapes_2d {
|
||||||
using super = simpleengine::Renderable;
|
using super = simpleengine::Renderable;
|
||||||
private:
|
private:
|
||||||
gfx::Shader shader; // This only stores the shader program
|
gfx::Shader shader; // This only stores the shader program
|
||||||
nonstd::optional<Texture> texture;
|
nonstd::optional<gfx::Texture> texture;
|
||||||
public:
|
public:
|
||||||
std::vector<Vertex> vertices;
|
std::vector<Vertex> vertices;
|
||||||
gfx::VBO vbo;
|
gfx::VBO vbo;
|
||||||
|
@ -41,7 +41,7 @@ namespace simpleengine::shapes_2d {
|
||||||
|
|
||||||
virtual ~Triangle() = default;
|
virtual ~Triangle() = default;
|
||||||
|
|
||||||
void set_texture(Texture texture);
|
void set_texture(gfx::Texture texture);
|
||||||
|
|
||||||
virtual void update(const float& delta_time) override;
|
virtual void update(const float& delta_time) override;
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -28,7 +28,7 @@ namespace simpleengine::shapes_2d {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Square::set_texture(Texture texture) {
|
void Square::set_texture(gfx::Texture texture) {
|
||||||
this->texture = texture;
|
this->texture = texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ namespace simpleengine::shapes_2d {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Triangle::set_texture(Texture texture) {
|
void Triangle::set_texture(gfx::Texture texture) {
|
||||||
this->texture = texture;
|
this->texture = texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue