/* QGLView Copyright (C) 2020 Ivan Pelipenko peri4ko@yandex.ru This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef GLVBO_H #define GLVBO_H #include "gltypes.h" #include "chunkstream.h" class GLVBO : protected QOpenGLFunctions { friend class GLObjectBase; friend QDataStream & operator <<(QDataStream & s, const GLVBO & m); friend QDataStream & operator >>(QDataStream & s, GLVBO & m); public: GLVBO(GLenum usage = GL_DYNAMIC_DRAW); ~GLVBO(); //GLVBO & operator =(const GLVBO & o) {return *this;} void init(); void destroy(); bool rebuffer(bool clear_ = false); void draw(GLenum type, QOpenGLShaderProgram * prog, bool simplest = false); void clear(); GLuint buffer() const {return buffer_;} int verticesCount() const {return vertices_.size() / 3;} bool isInit() const {return buffer_ != 0;} bool isEmpty() const {return vertices_.size() < 3;} QVector & vertices() {changed = true; return vertices_;} QVector & normals() {changed = true; return normals_;} QVector & texcoords() {changed = true; return texcoords_;} QVector & colors() {changed = true; return colors_;} void translatePoints(const QVector3D & dp); void scalePoints(const QVector3D & dp); bool saveToFile(const QString & filename); bool loadFromFile(const QString & filename); Box3D boundingBox() const; private: void calculateBinormals(); QVector vertices_, normals_, texcoords_, colors_, tangents_, bitangents_; QVector locs; GLenum usage; GLuint buffer_, va_; int vert_count; bool changed, has_normals, has_texcoords, has_colors, has_binormals; }; inline QDataStream & operator <<(QDataStream & s, const GLVBO & m) { ChunkStream cs; //qDebug() << "place VBO" << m.vertices_.size() << m.normals_.size() << m.texcoords_.size() << m.colors_.size() << "..."; cs << cs.chunk(1, m.vertices_) << cs.chunk(2, m.normals_) << cs.chunk(3, m.texcoords_) << cs.chunk(4, m.colors_) << cs.chunk(5, m.usage); //qDebug() << "place VBO done" << cs.data().size() << "..."; s << qCompress(cs.data()); return s; } inline QDataStream & operator >>(QDataStream & s, GLVBO & m) { QByteArray ba; s >> ba; ba = qUncompress(ba); ChunkStream cs(ba); while (!cs.atEnd()) { switch (cs.read()) { case 1: m.vertices_ = cs.getData >(); break; case 2: m.normals_ = cs.getData >(); break; case 3: m.texcoords_ = cs.getData >(); break; case 4: m.colors_ = cs.getData >(); break; case 5: m.usage = cs.getData(); break; } } m.changed = true; return s; } #endif // GLVBO_H