2007-05-27 12:01:53 -04:00
|
|
|
// $Id$
|
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2004 Steve Baker <sjbaker1@airmail.net>
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
2008-06-20 05:34:35 -04:00
|
|
|
// as published by the Free Software Foundation; either version 3
|
2007-05-27 12:01:53 -04:00
|
|
|
// 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 "material.hpp"
|
2009-01-22 17:27:13 -05:00
|
|
|
|
|
|
|
#include "file_manager.hpp"
|
2009-01-05 21:39:30 -05:00
|
|
|
#include "stk_config.hpp"
|
2009-01-22 17:27:13 -05:00
|
|
|
#include "utils/string_utils.hpp"
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
#define UCLAMP 1
|
|
|
|
#define VCLAMP 2
|
|
|
|
|
|
|
|
int clearSpheremap ( ssgEntity * )
|
|
|
|
{
|
|
|
|
glDisable ( GL_TEXTURE_GEN_S ) ;
|
|
|
|
glDisable ( GL_TEXTURE_GEN_T ) ;
|
|
|
|
return true ;
|
2008-02-23 03:21:30 -05:00
|
|
|
} // clearSpheremap
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
int setSpheremap ( ssgEntity * )
|
|
|
|
{
|
|
|
|
glTexGeni ( GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP ) ;
|
|
|
|
glTexGeni ( GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP ) ;
|
|
|
|
glEnable ( GL_TEXTURE_GEN_S ) ;
|
|
|
|
glEnable ( GL_TEXTURE_GEN_T ) ;
|
|
|
|
return true ;
|
2008-02-23 03:21:30 -05:00
|
|
|
} // setSpheremap
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
bool Material::parseBool ( char **p )
|
|
|
|
{
|
|
|
|
/* Skip leading spaces */
|
|
|
|
|
|
|
|
while ( **p <= ' ' && **p != '\0' ) (*p)++ ;
|
|
|
|
|
|
|
|
const bool RES = ( ( **p == 'Y' ) || ( **p == 'y' ) ) ;
|
|
|
|
|
|
|
|
while ( **p > ' ' && **p != '\0' ) (*p)++ ;
|
|
|
|
|
|
|
|
return RES ;
|
2008-02-23 03:21:30 -05:00
|
|
|
} // parseBool
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
int Material::parseInt ( char **p )
|
|
|
|
{
|
|
|
|
/* Skip leading spaces */
|
|
|
|
|
|
|
|
while ( **p <= ' ' && **p != '\0' ) (*p)++ ;
|
|
|
|
|
|
|
|
return strtol ( *p, p, 0 ) ;
|
2008-02-23 03:21:30 -05:00
|
|
|
} // parseInt
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
float Material::parseFloat ( char **p )
|
|
|
|
{
|
|
|
|
/* Skip leading spaces */
|
|
|
|
|
|
|
|
while ( **p <= ' ' && **p != '\0' ) (*p)++ ;
|
|
|
|
|
|
|
|
return (float)strtod ( *p, p ) ;
|
2008-02-23 03:21:30 -05:00
|
|
|
} // parseFloat
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-02-23 03:21:30 -05:00
|
|
|
Material::Material(int index)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2008-02-23 03:21:30 -05:00
|
|
|
m_texname = "";
|
2007-05-27 12:01:53 -04:00
|
|
|
m_predraw = m_postdraw = NULL ;
|
|
|
|
|
2008-02-28 22:26:07 -05:00
|
|
|
init (index);
|
|
|
|
install();
|
2008-02-23 03:21:30 -05:00
|
|
|
} // Material
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-02-28 22:26:07 -05:00
|
|
|
Material::Material(const std::string& fname, char *description,
|
|
|
|
int index, bool is_full_path)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2008-02-23 03:21:30 -05:00
|
|
|
m_texname = fname;
|
2007-05-27 12:01:53 -04:00
|
|
|
m_predraw = m_postdraw = NULL ;
|
|
|
|
|
2008-02-23 03:21:30 -05:00
|
|
|
init (index);
|
2007-05-27 12:01:53 -04:00
|
|
|
|
2007-08-27 09:11:06 -04:00
|
|
|
if(strlen(description)>0)
|
|
|
|
{
|
2008-02-28 22:26:07 -05:00
|
|
|
m_clamp_tex = parseBool (&description) ? UCLAMP : 0 ;
|
|
|
|
m_clamp_tex += parseBool (&description) ? VCLAMP : 0 ;
|
2007-08-27 09:11:06 -04:00
|
|
|
|
2009-01-05 17:55:25 -05:00
|
|
|
m_transparency = parseBool (&description);
|
|
|
|
m_alpha_ref = parseFloat(&description);
|
|
|
|
m_lighting = parseBool (&description);
|
|
|
|
m_sphere_map = parseBool (&description);
|
|
|
|
m_friction = parseFloat(&description);
|
|
|
|
m_ignore = parseBool (&description);
|
|
|
|
m_zipper = parseBool (&description);
|
|
|
|
m_resetter = parseBool (&description);
|
|
|
|
m_collideable = parseBool (&description);
|
|
|
|
m_max_speed_fraction = parseFloat(&description);
|
|
|
|
m_slowdown = parseFloat(&description);
|
|
|
|
// Set the optional parameters.
|
2009-01-05 21:39:30 -05:00
|
|
|
if(m_max_speed_fraction <= 0.0f)
|
|
|
|
m_max_speed_fraction = 1.0f;
|
|
|
|
if(m_slowdown <= 0.0f)
|
|
|
|
m_slowdown = stk_config->m_slowdown_factor;
|
2007-08-27 09:11:06 -04:00
|
|
|
}
|
2008-02-28 22:26:07 -05:00
|
|
|
install(is_full_path);
|
2008-02-23 03:21:30 -05:00
|
|
|
} // Material
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
Material::~Material()
|
|
|
|
{
|
|
|
|
ssgDeRefDelete(m_state);
|
2008-02-23 03:21:30 -05:00
|
|
|
} // ~Material
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-02-23 03:21:30 -05:00
|
|
|
void Material::init(int index)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2009-01-05 19:36:56 -05:00
|
|
|
m_index = index ;
|
|
|
|
m_clamp_tex = 0 ;
|
|
|
|
m_transparency = false ;
|
|
|
|
m_alpha_ref = 0.1f ;
|
|
|
|
m_lighting = true ;
|
|
|
|
m_sphere_map = false ;
|
|
|
|
m_friction = 1.0f ;
|
|
|
|
m_ignore = false ;
|
|
|
|
m_zipper = false ;
|
|
|
|
m_resetter = false ;
|
|
|
|
m_collideable = true ;
|
|
|
|
m_max_speed_fraction = 1.0f;
|
2009-01-05 21:39:30 -05:00
|
|
|
m_slowdown = stk_config->m_slowdown_factor;
|
2007-05-27 12:01:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-02-23 03:21:30 -05:00
|
|
|
void Material::applyToLeaf(ssgLeaf *l)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
|
|
|
if ( m_predraw ) l -> setCallback ( SSG_CALLBACK_PREDRAW , m_predraw ) ;
|
|
|
|
if ( m_postdraw ) l -> setCallback ( SSG_CALLBACK_POSTDRAW, m_postdraw ) ;
|
2008-02-23 03:21:30 -05:00
|
|
|
} // applyToLeaf
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-02-28 22:26:07 -05:00
|
|
|
void Material::install(bool is_full_path)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
|
|
|
if ( isSphereMap () )
|
|
|
|
{
|
|
|
|
m_predraw = setSpheremap ;
|
|
|
|
m_postdraw = clearSpheremap ;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_state = new ssgSimpleState ;
|
|
|
|
|
|
|
|
m_state -> ref () ;
|
|
|
|
m_state -> setExternalPropertyIndex ( m_index ) ;
|
2008-02-23 03:21:30 -05:00
|
|
|
if ( m_texname.size()>0 )
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2008-02-28 22:26:07 -05:00
|
|
|
std::string fn=is_full_path ? m_texname
|
2008-02-29 22:18:53 -05:00
|
|
|
: file_manager->getTextureFile(m_texname);
|
2008-02-28 22:26:07 -05:00
|
|
|
if(fn=="")
|
|
|
|
{
|
|
|
|
fprintf(stderr, "WARNING: texture '%s' not found.\n",
|
|
|
|
m_texname.c_str());
|
|
|
|
}
|
2008-02-23 22:32:14 -05:00
|
|
|
m_state -> setTexture ( fn.c_str(), !(m_clamp_tex & UCLAMP),
|
2007-05-27 12:01:53 -04:00
|
|
|
!(m_clamp_tex & VCLAMP) );
|
|
|
|
m_state -> enable ( GL_TEXTURE_2D ) ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_state -> disable ( GL_TEXTURE_2D ) ;
|
|
|
|
|
|
|
|
if ( m_lighting )
|
|
|
|
m_state -> enable ( GL_LIGHTING ) ;
|
|
|
|
else
|
|
|
|
m_state -> disable ( GL_LIGHTING ) ;
|
|
|
|
|
|
|
|
m_state -> setShadeModel ( GL_SMOOTH ) ;
|
|
|
|
m_state -> enable ( GL_COLOR_MATERIAL ) ;
|
|
|
|
m_state -> enable ( GL_CULL_FACE ) ;
|
|
|
|
m_state -> setColourMaterial ( GL_AMBIENT_AND_DIFFUSE ) ;
|
|
|
|
m_state -> setMaterial ( GL_EMISSION, 0, 0, 0, 1 ) ;
|
|
|
|
m_state -> setMaterial ( GL_SPECULAR, 0, 0, 0, 1 ) ;
|
|
|
|
m_state -> setShininess ( 0 ) ;
|
|
|
|
|
|
|
|
if ( m_transparency )
|
|
|
|
{
|
|
|
|
m_state -> setTranslucent () ;
|
|
|
|
m_state -> enable ( GL_ALPHA_TEST ) ;
|
|
|
|
m_state -> setAlphaClamp ( m_alpha_ref ) ;
|
|
|
|
m_state -> enable ( GL_BLEND ) ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_state -> setOpaque () ;
|
|
|
|
m_state -> disable ( GL_BLEND ) ;
|
|
|
|
}
|
2008-03-17 00:43:01 -04:00
|
|
|
|
|
|
|
// now set the name to the basename, so that all tests work as expected
|
|
|
|
m_texname = StringUtils::basename(m_texname);
|
2007-05-27 12:01:53 -04:00
|
|
|
}
|
|
|
|
|