From 92c27a1ef89996917818d5daaa9beff0b3c4e66b Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Wed, 28 Sep 2016 13:16:11 +0200 Subject: [PATCH] added model loading into opengl. not yet seeing output. --- game/game_window.hpp | 4 +- game/renderer_polygon_3d/polygon_model.hpp | 77 ++++++++++++++++++- .../renderer_polygon_3d.cpp | 11 ++- .../renderer_polygon_3d.hpp | 2 + 4 files changed, 87 insertions(+), 7 deletions(-) diff --git a/game/game_window.hpp b/game/game_window.hpp index f48147a..6952095 100644 --- a/game/game_window.hpp +++ b/game/game_window.hpp @@ -20,8 +20,8 @@ class GameWindow : public endofthejedi::GLWindow { private: Game* m_game; - endofthejedi::RendererPolygon2d m_renderer; - //endofthejedi::RendererPolygon3d m_renderer; + //endofthejedi::RendererPolygon2d m_renderer; + endofthejedi::RendererPolygon3d m_renderer; //endofthejedi::RendererRayTracer m_renderer; protected: diff --git a/game/renderer_polygon_3d/polygon_model.hpp b/game/renderer_polygon_3d/polygon_model.hpp index 2b1d155..9a58acd 100644 --- a/game/renderer_polygon_3d/polygon_model.hpp +++ b/game/renderer_polygon_3d/polygon_model.hpp @@ -24,6 +24,10 @@ class PolygonModel { bool import() { + m_vbo_id_position = 0; + m_vbo_id_normal = 0; + m_numVertices = 0; + // Create an instance of the Importer class Assimp::Importer importer; // And have it read the given file with some example postprocessing @@ -51,6 +55,75 @@ class PolygonModel { return true; } + bool loadIntoOpenGl() + { + glEnableVertexAttribArray(0); + + glGenBuffers(1, &m_vbo_id_position); // Generate buffer + glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_position); // Bind buffer + + glVertexAttribPointer(0, + 3, // three floats per vertex + GL_FLOAT, // Data is floating point type + GL_FALSE, // No fixed point scaling + 0, // stride: no + NULL); // No offset + + // fill buffer with the loaded mesh position data + glBufferData( + GL_ARRAY_BUFFER, // Buffer target + 3 * m_numVertices * sizeof(float), // Buffer data size + m_data_position.data(), // Buffer data pointer + GL_STATIC_DRAW); // Usage - Data never changes; + + // TODO +#if 0 + // 2 holds normal data + glEnableVertexAttribArray(2); + glGenBuffers(1, &(model->vbo_normal)); // Generate buffer + glBindBuffer(GL_ARRAY_BUFFER, model->vbo_normal); // Bind buffer + + glVertexAttribPointer(2, + 3, // three floats per normal + GL_FLOAT, // Data is floating point type + GL_FALSE, // No fixed point scaling + 0, // stride: no + NULL); // No offset + + // Fill bound buffer + glBufferData( + GL_ARRAY_BUFFER, // Buffer target + 3*model->numVertices*sizeof(float), // Buffer data size + model->normals, // Buffer data pointer + GL_STATIC_DRAW); // Usage - Data never changes; +#endif + return true; + } + + bool bind() + { + // bind position vbo + glEnableVertexAttribArray(0); + glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_position); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); + + // TODO +#if 0 + // bind normal vbo + GLuint normalLoc = glGetAttribLocation(program, "attr_normal"); + glEnableVertexAttribArray(normalLoc); + glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo_normal); + glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL); +#endif + + return true; + } + + void render() + { + glDrawArrays(GL_TRIANGLES, 0, m_numVertices); + } + private: bool copyVertices(const aiScene *scene) { @@ -104,12 +177,12 @@ class PolygonModel { std::string m_filename; bool m_loaded; - size_t m_numVertices; - // both will hold 3 * numVertices floats std::vector m_data_position; std::vector m_data_normal; + size_t m_numVertices; + GLuint m_vbo_id_position; GLuint m_vbo_id_normal; }; diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.cpp b/game/renderer_polygon_3d/renderer_polygon_3d.cpp index c2791d7..ef05f3e 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.cpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.cpp @@ -9,14 +9,19 @@ namespace endofthejedi { { std::cout<<"setup 3d" << std::endl; - PolygonModel atomicBomb("../data/mesh/small_atomic_bomb.stl"); - exit(-1); + m_atomicBomb = new PolygonModel("../data/mesh/small_atomic_bomb.stl"); + m_atomicBomb->loadIntoOpenGl(); + + m_models.push_back(m_atomicBomb); } void RendererPolygon3d::render(const game::State *state) { (void) state; - std::cout<<"render 3d" << std::endl; + m_atomicBomb->bind(); + m_atomicBomb->render(); + + //std::cout<<"render 3d" << std::endl; } } diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.hpp b/game/renderer_polygon_3d/renderer_polygon_3d.hpp index c0685f1..4d0afee 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.hpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.hpp @@ -25,5 +25,7 @@ namespace endofthejedi { private: std::vector m_models; + + PolygonModel *m_atomicBomb; }; }