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

#include "glprimitives.h"


void GLPrimitivePoint::draw(QOpenGLShaderProgram * prog, bool simplest) {
	glPointSize(sz);
	glColor3f(material_.color_diffuse.redF(), material_.color_diffuse.greenF(), material_.color_diffuse.blueF());
	glBegin(GL_POINTS);
	glVertex3d(0., 0., 0.);
	glEnd();
}


void GLPrimitiveLine::draw(QOpenGLShaderProgram * prog, bool simplest) {
	glColor3f(material_.color_diffuse.redF(), material_.color_diffuse.greenF(), material_.color_diffuse.blueF());
	glBegin(GL_LINES);
	glVertex3f(p0.x(), p0.y(), p0.z());
	glVertex3f(p1.x(), p1.y(), p1.z());
	glEnd();
}


GLPrimitiveCube::GLPrimitiveCube(float width, float length, float height, QVector3D pos): GLObjectBase() {
	geom_prim = Quads;
	w         = width;
	l         = length;
	h         = height;
	moveTo(pos);
	// init();
}


void GLPrimitiveCube::init() {
	float hw = w / 2.f, hl = l / 2.f, hh = h / 2.f;
	// list = glGenLists(1);
	// glNewList(list, GL_COMPILE);
	// glColor4d(material_.color_diffuse.redF(), material_.color_diffuse.greenF(), material_.color_diffuse.blueF(),
	// material_.color_diffuse.alphaF());
	vbo.init();
	QVector<GLfloat> &d_vertices(vbo.vertices()), &d_normals(vbo.normals()), &d_uvs(vbo.texcoords());
	d_vertices.clear();
	d_normals.clear();
	d_uvs.clear();
	for (int i = 0; i < 4; ++i)
		d_normals << 0. << -1. << 0.;
	d_vertices << -hw << -hl << -hh;
	d_vertices << hw << -hl << -hh;
	d_vertices << hw << -hl << hh;
	d_vertices << -hw << -hl << hh;
	d_uvs << 0. << 0. << 1. << 0. << 1. << 1. << 0. << 1.;
	for (int i = 0; i < 4; ++i)
		d_normals << 0. << 1. << 0.;
	d_vertices << -hw << hl << -hh;
	d_vertices << -hw << hl << hh;
	d_vertices << hw << hl << hh;
	d_vertices << hw << hl << -hh;
	d_uvs << 0. << 0. << 1. << 0. << 1. << 1. << 0. << 1.;
	for (int i = 0; i < 4; ++i)
		d_normals << -1. << 0. << 0.;
	d_vertices << -hw << -hl << -hh;
	d_vertices << -hw << -hl << hh;
	d_vertices << -hw << hl << hh;
	d_vertices << -hw << hl << -hh;
	d_uvs << 0. << 0. << 1. << 0. << 1. << 1. << 0. << 1.;
	for (int i = 0; i < 4; ++i)
		d_normals << 1. << 0. << 0.;
	d_vertices << hw << -hl << -hh;
	d_vertices << hw << hl << -hh;
	d_vertices << hw << hl << hh;
	d_vertices << hw << -hl << hh;
	d_uvs << 0. << 0. << 1. << 0. << 1. << 1. << 0. << 1.;
	for (int i = 0; i < 4; ++i)
		d_normals << 0. << 0. << -1.;
	d_vertices << -hw << -hl << -hh;
	d_vertices << -hw << hl << -hh;
	d_vertices << hw << hl << -hh;
	d_vertices << hw << -hl << -hh;
	d_uvs << 0. << 0. << 1. << 0. << 1. << 1. << 0. << 1.;
	for (int i = 0; i < 4; ++i)
		d_normals << 0. << 0. << 1.;
	d_vertices << -hw << -hl << hh;
	d_vertices << hw << -hl << hh;
	d_vertices << hw << hl << hh;
	d_vertices << -hw << hl << hh;
	d_uvs << 0. << 0. << 1. << 0. << 1. << 1. << 0. << 1.;
	is_init = true;
	vbo.rebuffer();
}


GLPrimitiveEllipsoid::GLPrimitiveEllipsoid(float width, float length, float height, int seg_wl, int seg_h, QVector3D pos) {
	geom_prim = GLObjectBase::Triangles;
	w         = width;
	l         = length;
	h         = height;
	swl       = seg_wl;
	sh        = seg_h;
	moveTo(pos);
	// init();
}


void GLPrimitiveEllipsoid::putTriangle(const QVector3D & v0, const QVector3D & v1, const QVector3D & v2) {
	vbo.vertices() << v0.x() << v0.y() << v0.z() << v1.x() << v1.y() << v1.z() << v2.x() << v2.y() << v2.z();
	QVector3D n = QVector3D::normal(v1 - v0, v2 - v0);
	for (int i = 0; i < 3; ++i)
		vbo.normals() << n.x() << n.y() << n.z();
	return;
	QVector3D s(w, l, h);
	n = (v0 * s).normalized();
	vbo.normals() << n.x() << n.y() << n.z();
	n = (v1 * s).normalized();
	vbo.normals() << n.x() << n.y() << n.z();
	n = (v2 * s).normalized();
	vbo.normals() << n.x() << n.y() << n.z();
}


void GLPrimitiveEllipsoid::init() {
	QVector<QVector3D> points;
	vbo.clear();
	vbo.init();
	int ret  = 0;
	int hseg = sh + 1, wlseg = swl + 1;
	float crw, crl, a, ch, twl;
	QVector3D cp(0., 0., -h / 2.f);
	points << cp;
	for (int i = 1; i < hseg; i++) {
		ch = -cosf((float)i / hseg * float(M_PI));
		cp.setZ(ch * h / 2.f);
		twl = sqrtf(1.f - ch * ch) / 2.f;
		crw = twl * w;
		crl = twl * l;
		for (int j = 0; j < wlseg * 2; j++) {
			a = (float)j / wlseg * float(M_PI);
			cp.setY(crw * sinf(a));
			cp.setX(crl * cosf(a));
			points << cp;
			ret = points.size() - 1;
			if (i == 1)
				if (j > 0) putTriangle(points[0], points[ret], points[ret - 1]);
			if (j > 0) {
				if (i > 1) {
					putTriangle(points[ret - wlseg * 2 - 1], points[ret], points[ret - 1]);
					putTriangle(points[ret - wlseg * 2], points[ret], points[ret - wlseg * 2 - 1]);
				}
			}
		}
		if (i == 1)
			putTriangle(points[0], points[ret - wlseg * 2 + 1], points[ret]);
		else {
			putTriangle(points[ret - wlseg * 2 + 1], points[ret], points[ret - wlseg * 2]);
			putTriangle(points[ret - wlseg * 2 + 1], points[ret - wlseg * 2], points[ret - wlseg * 4 + 1]);
		}
	}
	points << QVector3D(0., 0., h / 2.f);
	ret = points.size() - 1;
	putTriangle(points[ret - 1], points[ret - wlseg * 2], points[ret]);
	for (int j = 1; j < wlseg * 2; j++)
		if (j > 0) putTriangle(points[ret - wlseg * 2 + j - 1], points[ret - wlseg * 2 + j], points[ret]);
	is_init = true;
	vbo.rebuffer();
}


void GLPrimitiveAxis::draw(QOpenGLShaderProgram * prog, bool simplest) {
	float bs   = 1.f;
	float as   = 0.1f;
	float aw   = 0.07f;
	float cr_x = 0.8f, cg_y = 0.75f, cb_z = 0.8f;
	glBegin(GL_LINES);
	glColor3f(cr_x, 0, 0);
	glVertex3f(-bs, 0, 0);
	glVertex3f(bs, 0, 0);
	glVertex3f(bs, 0, 0);
	glVertex3f(bs - as, aw, 0);
	glVertex3f(bs, 0, 0);
	glVertex3f(bs - as, -aw, 0);
	glVertex3f(bs, 0, 0);
	glVertex3f(bs - as, 0, aw);
	glVertex3f(bs, 0, 0);
	glVertex3f(bs - as, 0, -aw);
	glColor3f(0, cg_y, 0);
	glVertex3f(0, -bs, 0);
	glVertex3f(0, bs, 0);
	glVertex3f(0, bs, 0);
	glVertex3f(0, bs - as, aw);
	glVertex3f(0, bs, 0);
	glVertex3f(0, bs - as, -aw);
	glVertex3f(0, bs, 0);
	glVertex3f(aw, bs - as, 0);
	glVertex3f(0, bs, 0);
	glVertex3f(-aw, bs - as, 0);
	glColor3f(0, 0, cb_z);
	glVertex3f(0, 0, -bs);
	glVertex3f(0, 0, bs);
	glVertex3f(0, 0, bs);
	glVertex3f(aw, 0, bs - as);
	glVertex3f(0, 0, bs);
	glVertex3f(-aw, 0, bs - as);
	glVertex3f(0, 0, bs);
	glVertex3f(0, aw, bs - as);
	glVertex3f(0, 0, bs);
	glVertex3f(0, -aw, bs - as);
	glEnd();
}
