Created texture class

This commit is contained in:
k 2025-01-12 23:12:29 -05:00
parent e0a695c7b1
commit 16d9959ace
3 changed files with 54 additions and 27 deletions

15
headers/texture.hpp Normal file
View File

@ -0,0 +1,15 @@
#ifndef TEXTURE_H
#define TEXTURE_H
#include <glad/glad.h>
class Texture {
public:
Texture(GLenum type, const char *path);
~Texture();
void bind(GLenum texture);
private:
unsigned int ID;
GLenum type;
};
#endif

View File

@ -5,8 +5,9 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <shader.hpp> #include <shader.hpp>
#include <texture.hpp>
#include <shapes.h> #include <shapes.h>
#include <stb_image.h>
#define UNUSED(x) (void)(x) #define UNUSED(x) (void)(x)
@ -55,31 +56,10 @@ int main(void) {
glEnableVertexAttribArray(1); /*rgb*/ glEnableVertexAttribArray(1); /*rgb*/
glEnableVertexAttribArray(2); /*tex_cord*/ glEnableVertexAttribArray(2); /*tex_cord*/
Shader *shaderProgram; Shader *shaderProgram =
unsigned int texture;
shaderProgram =
new Shader("./shaders/MVPTexVert.glsl", "./shaders/TexFrag.glsl"); new Shader("./shaders/MVPTexVert.glsl", "./shaders/TexFrag.glsl");
glGenTextures(1, &texture); Texture *texture =
glBindTexture(GL_TEXTURE_2D, texture); new Texture(GL_TEXTURE_2D,"./texture/container.jpg");
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);
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
glm::mat4 projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 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->setInt("Tex0", 0);
shaderProgram->setMat4("view", view); shaderProgram->setMat4("view", view);
shaderProgram->setMat4("projection", projection); shaderProgram->setMat4("projection", projection);
texture->bind(GL_TEXTURE0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glBindVertexArray(VAO); glBindVertexArray(VAO);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);

33
src/texture.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <cassert>
#include <stb_image.h>
#include <texture.hpp>
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);
}