/*
 * Copyright (C) 2004 Clemens Fuchslocher
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 */

#include <GL/gl.h>
#include <GL/glu.h>

#include "GlCompileFlatVisitor.h"


namespace nsSceneGraph {

GlCompileFlatVisitor::~GlCompileFlatVisitor(void)
{
	DEBUG_MESSAGE("GlCompileFlatVisitor::~GlCompileFlatVisitor()");
}


void GlCompileFlatVisitor::visit(VertexArray *array)
{
	DEBUG_MESSAGE("GlCompileFlatVisitor::visit(VertexArray *array)");

	if (array->isNodeCompiled())
	{
		return;
	}

	array->setCompiledStatus(true);
	array->setCompiledDisplayList(glGenLists(1));
	glNewList(array->getCompiledDisplayList(), GL_COMPILE);

	// Calculate one normal for each face.
	std::vector<Vector3F> normalPerTriangle;
	this->calculateOneNormalForEachFace(array, &normalPerTriangle);

	unsigned int index[array->vertexIndex.size() * 3];
	float vertex[array->vertexIndex.size() * 3 * 3];
	float normals[array->vertexIndex.size() * 3 * 3];

	enum
	{
		X = 0,
		Y = 1,
		Z = 2,
	};

	enum
	{
		A = 0,
		B = 1,
		C = 2
	};

	int end = array->vertexIndex.size();
	for (int n = 0, nn = 0; n < end; n++, nn += 3)
	{
		index[n * 3 + A] = n * 3 + A;
		index[n * 3 + B] = n * 3 + B;
		index[n * 3 + C] = n * 3 + C;

		vertex[(nn + 0) * 3 + X] = array->vertexArray[array->vertexIndex[n].a].x;
		vertex[(nn + 0) * 3 + Y] = array->vertexArray[array->vertexIndex[n].a].y;
		vertex[(nn + 0) * 3 + Z] = array->vertexArray[array->vertexIndex[n].a].z;

		vertex[(nn + 1) * 3 + X] = array->vertexArray[array->vertexIndex[n].b].x;
		vertex[(nn + 1) * 3 + Y] = array->vertexArray[array->vertexIndex[n].b].y;
		vertex[(nn + 1) * 3 + Z] = array->vertexArray[array->vertexIndex[n].b].z;

		vertex[(nn + 2) * 3 + X] = array->vertexArray[array->vertexIndex[n].c].x;
		vertex[(nn + 2) * 3 + Y] = array->vertexArray[array->vertexIndex[n].c].y;
		vertex[(nn + 2) * 3 + Z] = array->vertexArray[array->vertexIndex[n].c].z;

		normals[(nn + 0) * 3 + X] = normalPerTriangle[n].x;
		normals[(nn + 0) * 3 + Y] = normalPerTriangle[n].y;
		normals[(nn + 0) * 3 + Z] = normalPerTriangle[n].z;

		normals[(nn + 1) * 3 + X] = normalPerTriangle[n].x;
		normals[(nn + 1) * 3 + Y] = normalPerTriangle[n].y;
		normals[(nn + 1) * 3 + Z] = normalPerTriangle[n].z;

		normals[(nn + 2) * 3 + X] = normalPerTriangle[n].x;
		normals[(nn + 2) * 3 + Y] = normalPerTriangle[n].y;
		normals[(nn + 2) * 3 + Z] = normalPerTriangle[n].z;
	}

	glNormalPointer(GL_FLOAT, 0, normals);
	glVertexPointer(3, GL_FLOAT, 0, vertex);
	glDrawElements(GL_TRIANGLES, array->vertexIndex.size() * 3, GL_UNSIGNED_INT, index);

	glEndList();

	GL_ERROR_CHECK
}


}

