/*
    QGL VertexObject
    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/>.
*/

#define GL_GLEXT_PROTOTYPES
#include "glvertexobject.h"

#include <QOpenGLExtraFunctions>

using namespace QGLEngineShaders;


VertexObject::VertexObject(): buffer_obj(GL_ARRAY_BUFFER, GL_STREAM_DRAW), buffer_sel(GL_ARRAY_BUFFER, GL_STREAM_DRAW) {
	vao_            = 0;
	buffers_binded  = false;
	objects_changed = selected_changed = true;
}


VertexObject::~VertexObject() {}


void VertexObject::init(QOpenGLExtraFunctions * f) {
	if (!isInit()) {
		buffer_obj.init(f);
		buffer_sel.init(f);
		f->glGenVertexArrays(1, &vao_);
	}
}


void VertexObject::destroy(QOpenGLExtraFunctions * f) {
	if (vao_ != 0) {
		buffer_obj.destroy(f);
		buffer_sel.destroy(f);
		f->glDeleteVertexArrays(1, &vao_);
	}
	vao_ = 0;
}


void VertexObject::reinit() {
	vao_ = 0;
	buffer_obj.reinit();
	buffer_sel.reinit();
	buffers_binded  = false;
	objects_changed = selected_changed = true;
}


void VertexObject::bind(QOpenGLExtraFunctions * f) {
	// qDebug() << "bind" << target_ << buffer_;
	f->glBindVertexArray(vao_);
}


void VertexObject::release(QOpenGLExtraFunctions * f) {
	f->glBindVertexArray(0);
}


void VertexObject::bindBuffers(QOpenGLExtraFunctions * f, Buffer & geom, Buffer & elem, bool force) {
	if (!force && buffers_binded) return;
	buffers_binded = true;

	init(f);
	bind(f);

	geom.bind(f);
	prepareDrawGeom(f);

	elem.bind(f);

	buffer_obj.bind(f);
	prepareDrawObj(f);

	buffer_sel.bind(f);
	prepareDrawSel(f);

	release(f);
}


void VertexObject::loadObject(QOpenGLExtraFunctions * f, const Object & object) {
	loadBuffer(f, buffer_obj, &object, sizeof(Object));
}


void VertexObject::loadObjects(QOpenGLExtraFunctions * f, const QVector<Object> & objects) {
	loadBuffer(f, buffer_obj, objects.constData(), objects.size() * sizeof(Object));
}


void VertexObject::loadSelections(QOpenGLExtraFunctions * f, const QVector<uchar> & sels) {
	loadBuffer(f, buffer_sel, sels.constData(), sels.size());
}


void VertexObject::draw(QOpenGLExtraFunctions * f, GLenum geom_type, int vert_cout, int obj_count) {
	if (obj_count <= 0) return;
	bind(f);
	f->glDrawElementsInstanced(geom_type, vert_cout, GL_UNSIGNED_INT, 0, obj_count);
	release(f);
}


void VertexObject::loadBuffer(QOpenGLExtraFunctions * f, Buffer & buf, const void * data, int size) {
	buf.init(f);
	if (!data) return;
	buf.bind(f);
	buf.resize(f, size);
	buf.load(f, data, size);
}
