/*
 * 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 "pvvmud.H"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "gltexture.H"

#ifdef HAVE_GL_GLUT_H
#include <GL/glut.h>
#endif /* HAVE_GL_GLUT_H */

static GLboolean HaveTexObj = GL_FALSE;

#if defined(GL_VERSION_1_1)
#  define TEXTURE_OBJECT 1
#elif defined(GL_EXT_texture_object)
#  define TEXTURE_OBJECT 1
#  define glBindTexture(A,B)     glBindTextureEXT(A,B)
#  define glGenTextures(A,B)     glGenTexturesEXT(A,B)
#  define glDeleteTextures(A,B)  glDeleteTexturesEXT(A,B)
#endif

void GLTexture_Init(){
   /* check that renderer has the GL_EXT_texture_object extension
    * or supports OpenGL 1.1
    */
#ifdef TEXTURE_OBJECT
  char *exten = (char *) glGetString( GL_EXTENSIONS );
  char *version = (char *) glGetString( GL_VERSION );
  if (   strstr( exten, "GL_EXT_texture_object" )
	 || strncmp( version, "1.1", 3 )==0 ) {
    HaveTexObj = GL_TRUE;
  }
#endif

}


/******************************************************************************
 * 
 * Construct texture with given id 
 *
 *****************************************************************************/
CGLTexture::CGLTexture(int textureId):CTexture(textureId){
}

/******************************************************************************
 * 
 * Destruct texture  
 *
 *****************************************************************************/

CGLTexture::~CGLTexture(){
  
#ifdef TEXTURE_OBJECT
  if (loaded())
    glDeleteTextures( 1, &texObj );
#endif

}


/******************************************************************************
 * 
 * Set texturemap to use with texture 
 *
 *****************************************************************************/

void CGLTexture::setTextureMap(CTextureMap * textureMap){

  /* generate texture object IDs */
  if (HaveTexObj) {
#ifdef TEXTURE_OBJECT
    glGenTextures( 1, &texObj );
#endif
   }
   else {
      texObj = glGenLists(1);
   }
   /* setup texture object */
   if (HaveTexObj) {
#ifdef TEXTURE_OBJECT
      glBindTexture( GL_TEXTURE_2D, texObj );
#endif
   }
   else {
      glNewList( texObj, GL_COMPILE );
   }

   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
   glTexImage2D( GL_TEXTURE_2D, 0, 4, textureMap->getWidth(),
                 textureMap->getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE,
                 textureMap->getData() );
   if (!HaveTexObj) {
      glEndList();
   }
   /* end of texture object */

  CTexture::setTextureMap(textureMap);

}


/******************************************************************************
 * 
 * Exec texture
 *
 *****************************************************************************/

int CGLTexture::exec(){
  
  if (!loaded()) return FALSE;

  if (HaveTexObj) {
#ifdef TEXTURE_OBJECT
      glBindTexture( GL_TEXTURE_2D, texObj );
#endif
   }
   else {
      glCallList( texObj );
   }

  return TRUE;
}