#ifndef GLPOINTS_H #define GLPOINTS_H #include #include "GL.h" #include "GLHelper.h" #include class GLPoints : protected QOpenGLFunctions { private: QOpenGLBuffer arrayBuf; QOpenGLBuffer indexBuf; std::vector vertices; std::vector indices; int mode = GL_POINTS; bool initOnce = true; public: /** ctor */ GLPoints() : arrayBuf(QOpenGLBuffer::VertexBuffer), indexBuf(QOpenGLBuffer::IndexBuffer) { alloc(); } /** dtor */ ~GLPoints() { destroy(); } /** add a new face to this element */ void addPoint(const QVector3D& pt, const QColor& color) { indices.push_back(vertices.size()); QVector3D c(color.redF(), color.greenF(), color.blueF()); vertices.push_back(VertColor(pt, c)); } void alloc() { if (!indexBuf.isCreated()) {indexBuf.create();} if (!arrayBuf.isCreated()) {arrayBuf.create();} } void destroy() { if (indexBuf.isCreated()) {indexBuf.destroy();} if (arrayBuf.isCreated()) {arrayBuf.destroy();} } /** build the underlying buffers */ void build() { // Transfer vertex data to VBO 0 arrayBuf.bind(); arrayBuf.allocate(vertices.data(), vertices.size() * sizeof(vertices[0])); // Transfer index data to VBO 1 indexBuf.bind(); indexBuf.allocate(indices.data(), indices.size() * sizeof(indices[0])); } void initGL() { initializeOpenGLFunctions(); } void rebuild() { build(); } void clear() { indices.clear(); vertices.clear(); } void setMode(const int mode) { this->mode = mode; } /** render the element */ void render(QOpenGLShaderProgram *program) { if (indices.empty()) {return;} // Tell OpenGL which VBOs to use arrayBuf.bind(); indexBuf.bind(); // vertices int vertLoc = program->attributeLocation("a_position"); program->enableAttributeArray(vertLoc); program->setAttributeBuffer(vertLoc, GL_FLOAT, vertices[0].getVertOffset(), 3, sizeof(vertices[0])); // colors int colorLoc = program->attributeLocation("a_color"); program->enableAttributeArray(colorLoc); program->setAttributeBuffer(colorLoc, GL_FLOAT, vertices[0].getColorOffset(), 3, sizeof(vertices[0])); // Draw cube geometry using indices from VBO 1 glDrawElements(mode, indices.size(), GL_UNSIGNED_INT, 0); } }; #endif // GLPOINTS_H