Skip to content

Commit

Permalink
osx-drawElements: temp fix for that inexplicable GL error 1282 on Mac…
Browse files Browse the repository at this point in the history
… OS (#26)

* osx-drawElements: temp fix for that inexplicable GL error 1282 on Mac OS

* osx-drawElements: found the real fix!

* remove buffer on quit

* osx-drawElements: this appears to be much more reliable somehow

* osx-drawElements: this is perfect!

* reclaim indices` memory

* rename window to project`s name

* hide false errors

* osx-drawElements: this works on Linux, what about osx?
  • Loading branch information
fenollp authored Apr 11, 2017
1 parent 919752f commit c801c9a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/include/Algo3D.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <Algorithm.hh>

using VertIndices = std::vector<unsigned int>;
using VertIndices = std::vector<GLuint>;

class Algo3D : public Algorithm {
public:
Expand Down
8 changes: 6 additions & 2 deletions src/include/Scene3D.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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;
Expand All @@ -33,6 +36,7 @@ struct Scene3DContext {

GLuint vao;
GLuint vbo;
GLuint elements;

size_t width;
size_t height;
Expand Down
14 changes: 5 additions & 9 deletions src/miners/GlfwManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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?");

Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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);

Expand Down
11 changes: 10 additions & 1 deletion src/miners/Scene3D.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include <stdexcept>
#include <algorithm>

#include <platform.hpp>

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c801c9a

Please sign in to comment.