From 4fadf02915e8788b31b0d3cb02b9e5f9fa28a1be Mon Sep 17 00:00:00 2001 From: Archie Hilton Date: Sun, 26 May 2024 20:46:09 +0100 Subject: [PATCH] texture: Use texture abstraction --- src/CMakeLists.txt | 1 + src/Quad.cc | 57 ++++------------------------------------------ 2 files changed, 5 insertions(+), 53 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 30a6531..afbf5a3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,6 +3,7 @@ add_executable(Exponent GLProgramLoader.cc Quad.cc Mesh.cc + texture.cc ) target_link_libraries(Exponent PUBLIC glfw glm::glm ${PNG_LIBRARY}) diff --git a/src/Quad.cc b/src/Quad.cc index c979db4..96375a0 100644 --- a/src/Quad.cc +++ b/src/Quad.cc @@ -3,6 +3,7 @@ #include "glm/ext/matrix_transform.hpp" #include "glm/gtc/type_ptr.hpp" #include "glm/trigonometric.hpp" +#include "src/texture.hh" #include #include #include @@ -23,59 +24,9 @@ Quad::Quad(std::span verts, const Uniform &uniform_data) color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f); - png_uint_32 width = 0, height = 0; - int interlace_type = 0, bit_depth = 0, color_type = 0; - - FILE *fp = fopen("./res/tex/util/missing.png", "rb"); - png_const_bytep header[8] = {0}; - - if (fp == NULL) { - spdlog::error("Can't find texture file!"); - throw std::exception(); - } - - fread(header, 1, 8, fp); - - png_structp png_ptr = - png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - - png_infop info_ptr = png_create_info_struct(png_ptr); - - setjmp(png_jmpbuf(png_ptr)); - - png_init_io(png_ptr, fp); - png_set_sig_bytes(png_ptr, 8); - - png_read_png(png_ptr, info_ptr, - PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | - PNG_TRANSFORM_EXPAND, - NULL); - - png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, - &interlace_type, NULL, NULL); - - setjmp(png_jmpbuf(png_ptr)); - - png_size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr); - size_t data_length = rowbytes * height; - - png_byte *data = (png_byte *)malloc(data_length); - - png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr); - - for (int x = 0; x < height; x++) { - memcpy(data + (rowbytes * (height - x - 1)), row_pointers[x], rowbytes); - } - - png_destroy_read_struct(&png_ptr, &info_ptr, NULL); - fclose(fp); - - glGenTextures(1, &tex_id); - - glBindTexture(GL_TEXTURE_2D, tex_id); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, - GL_UNSIGNED_BYTE, data); - glGenerateMipmap(GL_TEXTURE_2D); + tex_id = TextureLoader::get_instance() + ->load_texture("./res/tex/util/missing.png") + ->get_texture_id(); } glm::vec2 Quad::get_position_vector() { return glm::vec2(m_pos_x, m_pos_y); }