diff --git a/src/include/Algo3D.hh b/src/include/Algo3D.hh index f15469e..4e5c55b 100644 --- a/src/include/Algo3D.hh +++ b/src/include/Algo3D.hh @@ -4,7 +4,7 @@ #include -using VertIndices = std::vector; +using VertIndices = std::vector; class Algo3D : public Algorithm { public: diff --git a/src/include/Scene3D.hh b/src/include/Scene3D.hh index 1ad7995..57cd59f 100644 --- a/src/include/Scene3D.hh +++ b/src/include/Scene3D.hh @@ -7,7 +7,7 @@ struct Scene3DContext { Scene3DContext(size_t w, size_t h, size_t d) - : vao(0), vbo(0), + : vao(0), vbo(0), elements(0), width(w), height(h), depth(d), n_points(width * height * depth), vertices_size(n_points * 3 * sizeof (GLfloat)), @@ -16,7 +16,10 @@ struct Scene3DContext { vertices(new GLfloat[vertices_size]), colors(new GLfloat[colors_size]), degreesRotated(0.0f), rotationEnabled(false), program(NULL) - {} + { + // selected.reserve(n_points); + } + ~Scene3DContext() { delete[] vertices; delete[] colors; @@ -33,6 +36,7 @@ struct Scene3DContext { GLuint vao; GLuint vbo; + GLuint elements; size_t width; size_t height; diff --git a/src/miners/GlfwManager.cc b/src/miners/GlfwManager.cc index aa837db..a008103 100644 --- a/src/miners/GlfwManager.cc +++ b/src/miners/GlfwManager.cc @@ -65,7 +65,7 @@ GlfwManager::init() monitor = glfwGetPrimaryMonitor(); } - window_ = glfwCreateWindow(args_->width, args_->height, "points", monitor, NULL); + window_ = glfwCreateWindow(args_->width, args_->height, "miners", monitor, NULL); if (!window_) throw std::runtime_error("!glfwCreateWindow. Can your hardware handle OpenGL 3.2?"); @@ -86,15 +86,13 @@ GlfwManager::init() } void -GlfwManager::glInit() -{ +GlfwManager::glInit() { glewExperimental = GL_TRUE; //stops glew from crashing on OSX :-/ if (glewInit() != GLEW_OK) throw std::runtime_error("!glewInit"); // GLEW throws some errors, so discard all the errors so far - //while (glGetError() != GL_NO_ERROR) {} - glProcessErrors(); + glProcessErrors(true); std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl; std::cout << "GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl; @@ -106,8 +104,7 @@ GlfwManager::glInit() } void -GlfwManager::glProcessErrors(bool quiet) -{ +GlfwManager::glProcessErrors(bool quiet) { while (true) { GLenum error = glGetError(); if (error == GL_NO_ERROR) @@ -118,8 +115,7 @@ GlfwManager::glProcessErrors(bool quiet) } void -GlfwManager::run() -{ +GlfwManager::run() { if (scene_->type() == SCENE_3D) glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_DISABLED); diff --git a/src/miners/Scene3D.cc b/src/miners/Scene3D.cc index d96f2fa..c4acd15 100644 --- a/src/miners/Scene3D.cc +++ b/src/miners/Scene3D.cc @@ -1,5 +1,6 @@ #include #include +#include #include @@ -31,6 +32,10 @@ Scene3D::load_buffers() { glVertexAttribPointer(ctx_.program->attrib("vert"), 3, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(ctx_.program->attrib("vert")); + glGenBuffers(1, &ctx_.elements); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ctx_.elements); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(decltype(ctx_.selected)::value_type) * ctx_.selected.size(), ctx_.selected.data(), GL_STATIC_DRAW); + // make and bind the VBO glGenBuffers(1, &ctx_.colors_id); glBindBuffer(GL_ARRAY_BUFFER, ctx_.colors_id); @@ -56,6 +61,7 @@ Scene3D::unload() if (ctx_.program) { delete ctx_.program; glDeleteBuffers(1, &ctx_.vbo); + glDeleteBuffers(1, &ctx_.elements); glDeleteBuffers(1, &ctx_.colors_id); glDeleteVertexArrays(1, &ctx_.vao); } @@ -69,6 +75,7 @@ Scene3D::reload() algo->apply(ctx_.vertices, ctx_.colors, ctx_.selected, ctx_.width, ctx_.height, ctx_.depth) || std::cerr << "!apply" << std::endl; std::cout << "#points: " << ctx_.selected.size() << std::endl; + ctx_.selected.shrink_to_fit(); load_buffers(); glBindVertexArray(ctx_.vao); glBindBuffer(GL_ARRAY_BUFFER, ctx_.colors_id); @@ -91,6 +98,7 @@ Scene3D::load(Algorithm* algorithm) algo->apply(ctx_.vertices, ctx_.colors, ctx_.selected, ctx_.width, ctx_.height, ctx_.depth) || std::cerr << "!apply" << std::endl; std::cout << "#points: " << ctx_.selected.size() << std::endl; + ctx_.selected.shrink_to_fit(); load_buffers(); camera_.setPosition(glm::vec3(0, 0, 4)); @@ -157,7 +165,8 @@ Scene3D::render() glBindVertexArray(ctx_.vao); // draw only the VAO's points we colored - glDrawElements(GL_POINTS, ctx_.selected.size(), GL_UNSIGNED_INT, ctx_.selected.data()); + auto mM = std::minmax_element(ctx_.selected.begin(), ctx_.selected.end()); + glDrawRangeElements(GL_POINTS, *mM.first, *mM.second, ctx_.selected.size(), GL_UNSIGNED_INT, NULL); // unbind the VAO and the program glBindVertexArray(0);