/*
    QGLEngineShaders
    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/>.
*/

#include "glshaders_types.h"


QGLEngineShaders::QGLMap::QGLMap() {
	offset      = 0.;
	amount      = 1.;
	scale       = QVector2D(1., 1.);
	array_index = map_index = 0;
}


QGLEngineShaders::QGLMaterial::QGLMaterial() {
	color_diffuse           = QVector4D(1., 1., 1., 0.);
	color_emission          = QVector4D(0., 0., 0., 0.);
	transparency            = 0.;
	reflectivity            = 0.;
	iof                     = 0.;
	dispersion              = 0.;
	map[mtNormal].map_index = emrBlue;
	map[mtRelief].map_index = emrBlack;
	map[mtRoughness].amount = 0.75;
	map[mtMetalness].amount = 0.25;
}


void QGLEngineShaders::prepareDrawGeom(QOpenGLExtraFunctions * f) {
	// qDebug() << "prepareDrawGeom";

	f->glEnableVertexAttribArray(pos_loc);
	f->glEnableVertexAttribArray(normal_loc);
	f->glEnableVertexAttribArray(tangent_loc);
	f->glEnableVertexAttribArray(bitangent_loc);
	f->glEnableVertexAttribArray(tex_loc);

	int size = sizeof(Vertex);
	f->glVertexAttribPointer(pos_loc, 3, GL_FLOAT, GL_FALSE, size, (const void *)pos_offset);
	f->glVertexAttribPointer(normal_loc, 3, GL_FLOAT, GL_FALSE, size, (const void *)normal_offset);
	f->glVertexAttribPointer(tangent_loc, 3, GL_FLOAT, GL_FALSE, size, (const void *)tangent_offset);
	f->glVertexAttribPointer(bitangent_loc, 3, GL_FLOAT, GL_FALSE, size, (const void *)bitangent_offset);
	f->glVertexAttribPointer(tex_loc, 2, GL_FLOAT, GL_FALSE, size, (const void *)tex_offset);
}


void QGLEngineShaders::prepareDrawObj(QOpenGLExtraFunctions * f) {
	// qDebug() << "prepareDrawObj";

	f->glEnableVertexAttribArray(integers_loc);
	f->glEnableVertexAttribArray(color_loc);
	for (int i = 0; i < 4; ++i)
		f->glEnableVertexAttribArray(modelmatrix_loc + i);
	for (int i = 0; i < 2; ++i)
		f->glEnableVertexAttribArray(texturematrix_loc + i);

	GLsizei size = sizeof(Object);
	f->glVertexAttribIPointer(integers_loc, 4, GL_UNSIGNED_INT, size, (const void *)integers_offset);
	f->glVertexAttribPointer(color_loc, 4, GL_FLOAT, GL_FALSE, size, (const void *)color_offset);
	for (int i = 0; i < 4; ++i) {
		f->glVertexAttribPointer(modelmatrix_loc + i,
		                         4,
		                         GL_FLOAT,
		                         GL_FALSE,
		                         size,
		                         (const void *)(modelmatrix_offset + sizeof(QVector4D) * i));
	}
	for (int i = 0; i < 2; ++i) {
		f->glVertexAttribPointer(texturematrix_loc + i,
								 3,
								 GL_FLOAT,
								 GL_FALSE,
								 size,
								 (const void *)(texturematrix_offset + sizeof(QVector3D) * i));
	}

	f->glVertexAttribDivisor(integers_loc, 1);
	f->glVertexAttribDivisor(color_loc, 1);
	for (int i = 0; i < 4; ++i)
		f->glVertexAttribDivisor(modelmatrix_loc + i, 1);
	for (int i = 0; i < 2; ++i)
		f->glVertexAttribDivisor(texturematrix_loc + i, 1);
}


void QGLEngineShaders::prepareDrawSel(QOpenGLExtraFunctions * f) {
	// qDebug() << "prepareDrawObj";

	f->glEnableVertexAttribArray(is_selected_loc);
	GLsizei size = 1;
	f->glVertexAttribIPointer(is_selected_loc, 1, GL_UNSIGNED_BYTE, size, (const void *)is_selected_offset);
	f->glVertexAttribDivisor(is_selected_loc, 1);
}
