From 37e525d6ee02e7a96e802c2f6e1bd0a55bf1d82c Mon Sep 17 00:00:00 2001 From: Teo-CD Date: Sun, 3 Nov 2019 15:50:37 +0100 Subject: [PATCH] Moved OpenGL texture generation to the Texture class --- example/main.cpp | 8 -------- src/DataHandling/Texture.cpp | 16 ++++++++++++++++ src/DataHandling/Texture.h | 3 +++ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/example/main.cpp b/example/main.cpp index 0258053..a589260 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -103,17 +103,9 @@ int main(int argc, char** argv) // Load and generate tree texture tree_texture.load_rgba_tga("resources/arbre.tga","resources/arbre_masque.tga"); - glGenTextures(1,tree_texture.opengl_id); - // TODO : Put in the Texture class - glBindTexture(GL_TEXTURE_2D,tree_texture.opengl_id[0]); - gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA8,tree_texture.width,tree_texture.height,GL_RGBA,GL_UNSIGNED_BYTE,tree_texture.image_data); // Load and generate raptor texture raptor_texture.load_rgb_tga("resources/RAPTOR.tga"); - glGenTextures(1, raptor_texture.opengl_id); - // TODO : Put in the Texture class - glBindTexture(GL_TEXTURE_2D, raptor_texture.opengl_id[0]); - gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB8, raptor_texture.width, raptor_texture.height, GL_RGB, GL_UNSIGNED_BYTE, raptor_texture.image_data); // Load raptor model raptor.load_ascii_off_file("resources/RAPTOR.off"); diff --git a/src/DataHandling/Texture.cpp b/src/DataHandling/Texture.cpp index 23bf41e..4f64c7b 100644 --- a/src/DataHandling/Texture.cpp +++ b/src/DataHandling/Texture.cpp @@ -73,6 +73,7 @@ bool Texture::load_rgb_tga(const std::string& rgb_filename) { bool return_value = load_tga(rgb_filename,image_data); invert_channels(0,2); + generate_texture(); return return_value; } @@ -118,9 +119,24 @@ bool Texture::load_rgba_tga(const std::string& rgb_filename, const std::string& color_bits = 32; invert_channels(0,2); + generate_texture(); return true; } +void Texture::generate_texture() +{ + glGenTextures(1,opengl_id); + glBindTexture(GL_TEXTURE_2D,opengl_id[0]); + if (color_bits == 24) + { + gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGB8,width,height,GL_RGB,GL_UNSIGNED_BYTE,image_data); + } + else if (color_bits == 32) + { + gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA8,width,height,GL_RGBA,GL_UNSIGNED_BYTE,image_data); + } +} + void Texture::invert_channels(uint8_t first_channel, uint8_t second_channel) { uint8_t increment = color_bits/8; diff --git a/src/DataHandling/Texture.h b/src/DataHandling/Texture.h index d0c791e..0eed6d6 100644 --- a/src/DataHandling/Texture.h +++ b/src/DataHandling/Texture.h @@ -33,6 +33,9 @@ private: /// Load and RGB TGA image file to an array bool load_tga(const std::string& filename, uint8_t*& data_array); + /// Initialize the texture for OpenGL + void generate_texture(); + void invert_channels(uint8_t first_channel,uint8_t second_channel); };