/*
 * PVVMUD a 3D MUD
 * Copyright (C) 1998-1999  Programvareverkstedet (pvv@pvv.org)
 *
 * 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 <stdlib.h>
#include <stdio.h>
#include <GL/gl.h>
#include <iostream.h>

#include "pvvmud.H"
#include "glmaterial.H"


CGLMaterial::CGLMaterial():CMaterial(){
}

CGLMaterial::CGLMaterial(int materialId):CMaterial(materialId){
}

int CGLMaterial::exec(CTextureCache * texCache){
  CTexture * tex;
  if (getTextureId() != -1){
    if ((tex = texCache->get(getTextureId()))== NULL){
      if ((tex = texCache->get(0)) == NULL){
	cdebug << "Failed to find texture!\n";
	glDisable(GL_TEXTURE_2D);
      } else {
	cdebug << "Using default texture for id " << getTextureId() << "\n";
        glEnable(GL_TEXTURE_2D);
        tex->exec();
      }
    } else {
      glEnable(GL_TEXTURE_2D);
      tex->exec();
    }
  } else {
    glDisable(GL_TEXTURE_2D);
  }

  glMaterialfv(GL_FRONT, GL_AMBIENT, m_ambient );
  glMaterialfv(GL_FRONT, GL_DIFFUSE, m_diffuse );
  glMaterialfv(GL_FRONT, GL_SPECULAR, m_specular );
  glMaterialfv(GL_FRONT, GL_SHININESS, m_shininess );
  glMaterialfv(GL_FRONT, GL_EMISSION, m_emission );

  return TRUE;
}