/*
 * Copyright (C) 2003-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 <string.h>

#include "SceneGraphVisitor.h"
#include "Material.h"


namespace nsSceneGraph {

Material::Material(void) : GraphNode(MATERIAL, "MATERIAL")
{
	DEBUG_MESSAGE("Material::Material()")

	this->diffuseColor[0] = this->diffuseColor[1] = this->diffuseColor[2] = this->diffuseColor[3] = -1;
	this->emissiveColor[0] = this->emissiveColor[1] = this->emissiveColor[2] = this->emissiveColor[3] = -1;
	this->specularColor[0] = this->specularColor[1] = this->specularColor[2] = this->specularColor[3] = -1;
	this->ambientColor[0] = this->ambientColor[1] = this->ambientColor[2] = this->ambientColor[3] = -1;
	this->transparency = -1;
	this->shininess = -1;
}


Material::Material(double diffuseColor[4], double emissiveColor[4],
	double specularColor[4], double ambientColor[4],
	double transparency, double shininess, const char *nodeName)  : GraphNode(MATERIAL, "MATERIAL")
{
	DEBUG_MESSAGE("Material::Material()")

	memcpy(this->diffuseColor, diffuseColor, sizeof(this->diffuseColor));
	memcpy(this->emissiveColor, emissiveColor, sizeof(this->emissiveColor));
	memcpy(this->specularColor, specularColor, sizeof(this->specularColor));
	memcpy(this->ambientColor, ambientColor, sizeof(this->ambientColor));
	this->transparency = transparency;
	this->shininess = shininess;
	this->setNodeName(nodeName);
}


Material::~Material(void)
{
	DEBUG_MESSAGE("Material::~Material()")

	if (this->isNodeCompiled())
	{
		glDeleteLists(this->getCompiledDisplayList(), 1);
	}
}


int Material::action(SceneGraphVisitor *visitor)
{
	DEBUG_MESSAGE("Material::accept()")

	visitor->visit(this);

	GraphNode::action(visitor);

	return SG_OK;
}


GraphNode *Material::duplicate(void)
{
	return new Material(this->diffuseColor, this->emissiveColor, this->specularColor, this->ambientColor, this->transparency, this->shininess, this->getNodeName());
}


void Material::setDiffuseColor(double r, double g, double b, double opacity)
{
	this->diffuseColor[0] = r;
	this->diffuseColor[1] = g;
	this->diffuseColor[2] = b;
	this->diffuseColor[3] = opacity;
}


void Material::setEmissiveColor(double r, double g, double b, double opacity)
{
	this->emissiveColor[0] = r;
	this->emissiveColor[1] = g;
	this->emissiveColor[2] = b;
	this->emissiveColor[3] = opacity;
}


void Material::setSpecularColor(double r, double g, double b, double opacity)
{
	this->specularColor[0] = r;
	this->specularColor[1] = g;
	this->specularColor[2] = b;
	this->specularColor[3] = opacity;
}


void Material::setAmbientColor(double r, double g, double b, double opacity)
{
	this->ambientColor[0] = r;
	this->ambientColor[1] = g;
	this->ambientColor[2] = b;
	this->ambientColor[3] = opacity;
}


void Material::setTransparency(double transparency)
{
	this->diffuseColor[3] = transparency;
	this->ambientColor[3] = transparency;
	this->specularColor[3] = transparency;
	this->emissiveColor[3] = transparency;
}


void Material::setShininess(double shininess)
{
	this->shininess = shininess;
}


}

