/*
    QGLView
    Ivan Pelipenko peri4ko@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef GLVBO_H
#define GLVBO_H

#include "chunkstream.h"
#include "gltypes.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<GLfloat> & vertices() {
		changed = true;
		return vertices_;
	}
	QVector<GLfloat> & normals() {
		changed = true;
		return normals_;
	}
	QVector<GLfloat> & texcoords() {
		changed = true;
		return texcoords_;
	}
	QVector<GLfloat> & 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<GLfloat> vertices_, normals_, texcoords_, colors_, tangents_, bitangents_;
	QVector<int> 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<QVector<GLfloat>>(); break;
		case 2: m.normals_ = cs.getData<QVector<GLfloat>>(); break;
		case 3: m.texcoords_ = cs.getData<QVector<GLfloat>>(); break;
		case 4: m.colors_ = cs.getData<QVector<GLfloat>>(); break;
		case 5: m.usage = cs.getData<GLenum>(); break;
		}
	}
	m.changed = true;
	return s;
}

#endif // GLVBO_H
