From 16d9959ace538270c49c70ee6aab5a0a3ee72e24 Mon Sep 17 00:00:00 2001 From: k Date: Sun, 12 Jan 2025 23:12:29 -0500 Subject: [PATCH] Created texture class --- headers/texture.hpp | 15 +++++++++++++++ src/main.cpp | 33 ++++++--------------------------- src/texture.cpp | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 headers/texture.hpp create mode 100644 src/texture.cpp diff --git a/headers/texture.hpp b/headers/texture.hpp new file mode 100644 index 0000000..86d7ce3 --- /dev/null +++ b/headers/texture.hpp @@ -0,0 +1,15 @@ +#ifndef TEXTURE_H +#define TEXTURE_H +#include + +class Texture { +public: + Texture(GLenum type, const char *path); + ~Texture(); + void bind(GLenum texture); + +private: + unsigned int ID; + GLenum type; +}; +#endif diff --git a/src/main.cpp b/src/main.cpp index 4f8667d..634991f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,8 +5,9 @@ #include #include #include +#include #include -#include + #define UNUSED(x) (void)(x) @@ -55,31 +56,10 @@ int main(void) { glEnableVertexAttribArray(1); /*rgb*/ glEnableVertexAttribArray(2); /*tex_cord*/ - Shader *shaderProgram; - unsigned int texture; - shaderProgram = + Shader *shaderProgram = new Shader("./shaders/MVPTexVert.glsl", "./shaders/TexFrag.glsl"); - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, - GL_LINEAR_MIPMAP_LINEAR); - - int width, height, nrChannels; - unsigned char *data; - stbi_set_flip_vertically_on_load(true); - - data = stbi_load("./texture/container.jpg", &width, &height, &nrChannels, 0); - assert(data && "Failed to load image"); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, - GL_UNSIGNED_BYTE, data); - glGenerateMipmap(GL_TEXTURE_2D); - - stbi_image_free(data); + Texture *texture = + new Texture(GL_TEXTURE_2D,"./texture/container.jpg"); while (!glfwWindowShouldClose(window)) { glm::mat4 projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, @@ -99,9 +79,8 @@ int main(void) { shaderProgram->setInt("Tex0", 0); shaderProgram->setMat4("view", view); shaderProgram->setMat4("projection", projection); + texture->bind(GL_TEXTURE0); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, texture); glBindVertexArray(VAO); glEnable(GL_DEPTH_TEST); diff --git a/src/texture.cpp b/src/texture.cpp new file mode 100644 index 0000000..df777db --- /dev/null +++ b/src/texture.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +Texture::Texture(GLenum type, const char *path) : type(type) { + glGenTextures(1, &ID); + glBindTexture(type, ID); + + glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(type, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + + int width, height, nrChannels; + unsigned char *data; + stbi_set_flip_vertically_on_load(true); + + data = stbi_load(path, &width, &height, &nrChannels, 0); + assert(data && "Failed to load image"); + + glTexImage2D(type, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, + data); + glGenerateMipmap(type); + + stbi_image_free(data); +}; + +Texture::~Texture() {} + +void Texture::bind(GLenum texture) { + glActiveTexture(texture); + glBindTexture(type, ID); +}