//===========================================================================
// Copyright (C) 1998, 2000-2007, 2010, 2011, 2012 SINTEF ICT, Applied
// Mathematics, Norway.
//
// This file is part of GoTools
//
// 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 version 2 of the License. 
//
// 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, see <http://www.gnu.org/licenses>
//
// Contact information: E-mail: tor.dokken@sintef.no                      
// SINTEF ICT, Department of Applied Mathematics,                         
// P.O. Box 124 Blindern,                                                 
// 0314 Oslo, Norway.                                                     
//
// Other licenses are also available for this software, notably licenses
// for:
// - Building commercial software.                                        
// - Building software whose source code you wish to keep private.        
//===========================================================================

#ifndef _GVGENERICTRIPAINTABLE_H
#define _GVGENERICTRIPAINTABLE_H


#include "GoTools/viewlib/gvPaintable.h"
#include "GoTools/tesselator/GenericTriMesh.h"
#ifdef _MSC_VER
#define NOMINMAX
#include <windows.h>
#endif
#include <GL/gl.h>


namespace
{
    inline GLenum GLfptype(float)
    {
	return GL_FLOAT;
    }
    inline GLenum GLfptype(double)
    {
	return GL_DOUBLE;
    }
}

/** Documentation ...
    etc
 */

template <typename FloatType>
class gvGenericTriPaintable : public gvPaintable
{
public:
    gvGenericTriPaintable(GenericTriMesh<FloatType>& tri,
			  const gvColor& ncolor,
			  const gvColor& scolor,
			  int id)
	: gvPaintable(ncolor, scolor, id),
	  tri_(tri)
    {}
    gvGenericTriPaintable(GenericTriMesh<FloatType>& tri,
			  const gvColor& ncolor,
			  int id)
	: gvPaintable(ncolor, id),
	  tri_(tri)
    {}
    virtual ~gvGenericTriPaintable()
    {}

    virtual void paint(gvTexture*)
    {
	GLenum fptype = GLfptype(FloatType());
	// Draw surfaces a little bit behind other things 
	glEnable (GL_POLYGON_OFFSET_FILL);
	glPolygonOffset (1.0, 1.0);

	// Deal with visibility and selection state
	if (!visible_) return;
	if (selected_) {
	    glMaterialfv(GL_FRONT_AND_BACK,
			 GL_AMBIENT_AND_DIFFUSE,
			 selected_color_.rgba);
	} else {
	    glMaterialfv(GL_FRONT_AND_BACK,
			 GL_AMBIENT_AND_DIFFUSE,
			 normal_color_.rgba);
	}
	GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 100.0);

    /// Set up the vertex arrays
	glEnableClientState(GL_VERTEX_ARRAY);
    /// Set up the normal arrays
	glEnableClientState(GL_NORMAL_ARRAY);
	glVertexPointer(3, fptype, 0, tri_.vertexArray());
	//      std::cout << tri_.vertexArray()[300] << ' '
	//  	      << tri_.vertexArray()[301] << ' '
	//  	      << tri_.vertexArray()[302] << std::endl;
	// @@@ We should check for the use of normals and textures in tri_.
	glNormalPointer(fptype, 0, tri_.normalArray());

    /// Draw the triangles
	glDrawElements(GL_TRIANGLES, tri_.numTriangles()*3,
		       GL_UNSIGNED_INT, tri_.triangleIndexArray());

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
	glDisable(GL_POLYGON_OFFSET_FILL);
    }


protected:
    GenericTriMesh<FloatType>& tri_;
};




#endif // _GVGENERICTRIPAINTABLE_H

