From c4a0218d62bdc9cbc7a098adb3afb4997d76d2c6 Mon Sep 17 00:00:00 2001 From: trotFunky Date: Mon, 4 Nov 2019 15:12:28 +0100 Subject: [PATCH] Added dislay list support for 3D models --- example/main.cpp | 1 + src/DataHandling/Model3D.cpp | 24 ++++++++++++++++++++++++ src/DataHandling/Model3D.h | 6 ++++++ 3 files changed, 31 insertions(+) diff --git a/example/main.cpp b/example/main.cpp index 2d150ce..087dfcc 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -112,6 +112,7 @@ int main(int argc, char** argv) raptor.assign_texture(raptor_texture); raptor.set_scaling(0.01,0.01,0.01); raptor.set_rotation(-90,1,0,0); + raptor.prepare_displaylist(); glutTimerFunc(50,update_angle,0); diff --git a/src/DataHandling/Model3D.cpp b/src/DataHandling/Model3D.cpp index 0ebb544..638a7d5 100644 --- a/src/DataHandling/Model3D.cpp +++ b/src/DataHandling/Model3D.cpp @@ -30,10 +30,23 @@ Model3D::~Model3D() delete(face_textures); delete(normals); } + + if (has_displaylist) + { + glDeleteLists(display_list_index,1); + } } void Model3D::draw_model() { + if (has_displaylist) + { + glCallList(display_list_index); + return; // Executing the regular draw calls is unnecessary + } + + // If there is no display list available, draw each triangles and texture them if necessary. + if (is_textured) { glEnable(GL_ALPHA_TEST); @@ -84,6 +97,17 @@ void Model3D::draw_model() } } +void Model3D::prepare_displaylist() +{ + display_list_index = glGenLists(1); + + glNewList(display_list_index,GL_COMPILE); + draw_model(); + glEndList(); + + has_displaylist = true; +} + bool Model3D::load_ascii_off_file(const std::string& file_name) { std::fstream file_stream(file_name, std::ios_base::in); diff --git a/src/DataHandling/Model3D.h b/src/DataHandling/Model3D.h index 97ef841..d160a7a 100644 --- a/src/DataHandling/Model3D.h +++ b/src/DataHandling/Model3D.h @@ -26,9 +26,13 @@ public: void set_scaling(float x_scale, float y_scale, float z_scale); void set_rotation(float angle, float x, float y, float z); + /// Draws the model, using a display list if available. void draw_model(); + /// Sets up an OpenGL display list for the model + void prepare_displaylist(); bool is_textured; + bool has_displaylist = false; private: uint32_t vertex_count; uint32_t face_count; @@ -51,6 +55,8 @@ private: Vec3f scaling{}; Vec3f rotation_axis{}; float rotation_angle{}; + + GLuint display_list_index; };