From 85b81398720ce3cc0520ff7cd439cefde855c4bc Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Wed, 28 Sep 2016 13:36:22 +0200 Subject: [PATCH] got minimal vertex shader working. --- game/renderer_polygon_3d/polygon_model.hpp | 60 +++++++++++++++---- .../renderer_polygon_3d.cpp | 42 +++++++++++-- .../renderer_polygon_3d.hpp | 11 ++-- 3 files changed, 93 insertions(+), 20 deletions(-) diff --git a/game/renderer_polygon_3d/polygon_model.hpp b/game/renderer_polygon_3d/polygon_model.hpp index 9a58acd..79b7f1f 100644 --- a/game/renderer_polygon_3d/polygon_model.hpp +++ b/game/renderer_polygon_3d/polygon_model.hpp @@ -14,19 +14,27 @@ class PolygonModel { public: PolygonModel(const std::string &filename) : m_filename(filename) { - if (!import()) { - m_loaded = false; + clear(); + } - } else { - m_loaded = true; - } + void clear() + { + // TODO: delete buffers if there's data + + m_loaded_from_file = false; + m_loaded_to_opengl = false; + + m_data_position.clear(); + m_data_normal.clear(); + + m_vbo_id_position = 0; + m_vbo_id_normal = 0; + m_numVertices = 0; } bool import() { - m_vbo_id_position = 0; - m_vbo_id_normal = 0; - m_numVertices = 0; + clear(); // Create an instance of the Importer class Assimp::Importer importer; @@ -51,12 +59,21 @@ class PolygonModel { // Now we can access the file's contents. copyVertices(scene); + m_loaded_from_file = true; + // We're done. Everything will be cleaned up by the importer destructor return true; } - bool loadIntoOpenGl() + bool uploadToOpenGl() { + if (!m_loaded_from_file) { + std::cerr<<"[polygonmodel] warning: try to upload model data " + << "to OpenGL but no data is loaded!" << std::endl; + + return false; + } + glEnableVertexAttribArray(0); glGenBuffers(1, &m_vbo_id_position); // Generate buffer @@ -97,11 +114,21 @@ class PolygonModel { model->normals, // Buffer data pointer GL_STATIC_DRAW); // Usage - Data never changes; #endif + m_loaded_to_opengl = true; + return true; } bool bind() { + // TODO: check whether this vbo is bound! + if (!m_loaded_to_opengl) { + std::cout<<"[polygonmodel] warning: try to bind model vbo " + << "which was not uploaded to OpenGL!" << std::endl; + + return false; + } + // bind position vbo glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_position); @@ -116,12 +143,20 @@ class PolygonModel { glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL); #endif + m_binding_active = true; + return true; } - void render() + bool render() { + if (!m_binding_active || !m_loaded_to_opengl) { + std::cout<<"[polygonmodel] warning: try to render model without bind()" << std::endl; + return false; + } + glDrawArrays(GL_TRIANGLES, 0, m_numVertices); + return true; } private: @@ -171,11 +206,12 @@ class PolygonModel { } const std::string &filename() const { return m_filename; } - bool ready() const { return m_loaded; } private: std::string m_filename; - bool m_loaded; + bool m_loaded_from_file; + bool m_loaded_to_opengl; + bool m_binding_active; // both will hold 3 * numVertices floats std::vector m_data_position; diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.cpp b/game/renderer_polygon_3d/renderer_polygon_3d.cpp index ef05f3e..d9e8760 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.cpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.cpp @@ -9,18 +9,52 @@ namespace endofthejedi { { std::cout<<"setup 3d" << std::endl; - m_atomicBomb = new PolygonModel("../data/mesh/small_atomic_bomb.stl"); - m_atomicBomb->loadIntoOpenGl(); + //m_atomicBomb = new PolygonModel("../data/mesh/small_atomic_bomb.stl"); + m_atomicBomb = new PolygonModel("../data/mesh/quad_screen_coords.stl"); + m_atomicBomb->import(); + m_atomicBomb->uploadToOpenGl(); m_models.push_back(m_atomicBomb); + + std::string vss = + "varying vec3 vertex;\n" + "void main()\n" + "{\n" + " gl_Position = gl_Vertex;\n" + " vertex = gl_Vertex.xyz;\n" + "}\n" + ; + + std::string fss = + "varying vec3 vertex;\n" + "void main()\n" + "{\n" + //" gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n" + " gl_FragColor = vec4(0.5+0.5*vertex.xyz, 1.0);\n" + "}\n" + ; + + m_shader.init(); + m_shader.load(vss.c_str(), GL_VERTEX_SHADER); + m_shader.load(fss.c_str(), GL_FRAGMENT_SHADER); } void RendererPolygon3d::render(const game::State *state) { + m_shader.bind(); + (void) state; - m_atomicBomb->bind(); - m_atomicBomb->render(); + //m_atomicBomb->bind(); + //m_atomicBomb->render(); + + glColor3f(1.0, 0.0, 0.0); + glBegin(GL_QUADS); + glVertex2f(-1.0f, -1.0f); + glVertex2f(1.0f, -1.0f); + glVertex2f(1.0f, 1.0f); + glVertex2f(-1.0f, 1.0f); + glEnd(); //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 4d0afee..7750b4d 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.hpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.hpp @@ -17,15 +17,18 @@ class PolygonModel; namespace endofthejedi { class RendererPolygon3d : public Renderer { - private: - protected: public: void setup(); void render(const game::State *state) override; private: - std::vector m_models; + // all models are also here (for easy reloading etc.) + std::vector m_models; - PolygonModel *m_atomicBomb; + // and with a specific variable name here + PolygonModel *m_atomicBomb; + + // for rendering everything + Shader m_shader; }; }