texture: Use texture abstraction

This commit is contained in:
Archie Hilton 2024-05-26 20:46:09 +01:00
parent c904055632
commit 4fadf02915
2 changed files with 5 additions and 53 deletions

View File

@ -3,6 +3,7 @@ add_executable(Exponent
GLProgramLoader.cc GLProgramLoader.cc
Quad.cc Quad.cc
Mesh.cc Mesh.cc
texture.cc
) )
target_link_libraries(Exponent PUBLIC glfw glm::glm ${PNG_LIBRARY}) target_link_libraries(Exponent PUBLIC glfw glm::glm ${PNG_LIBRARY})

View File

@ -3,6 +3,7 @@
#include "glm/ext/matrix_transform.hpp" #include "glm/ext/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp" #include "glm/gtc/type_ptr.hpp"
#include "glm/trigonometric.hpp" #include "glm/trigonometric.hpp"
#include "src/texture.hh"
#include <exception> #include <exception>
#include <glm/ext.hpp> #include <glm/ext.hpp>
#include <glm/glm.hpp> #include <glm/glm.hpp>
@ -23,59 +24,9 @@ Quad::Quad(std::span<float> verts, const Uniform &uniform_data)
color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f); color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
png_uint_32 width = 0, height = 0; tex_id = TextureLoader::get_instance()
int interlace_type = 0, bit_depth = 0, color_type = 0; ->load_texture("./res/tex/util/missing.png")
->get_texture_id();
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);
} }
glm::vec2 Quad::get_position_vector() { return glm::vec2(m_pos_x, m_pos_y); } glm::vec2 Quad::get_position_vector() { return glm::vec2(m_pos_x, m_pos_y); }