OpenSceneGraph/src/osg/Material.cpp

437 lines
13 KiB
C++
Raw Normal View History

2006-07-18 23:21:48 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library 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
* OpenSceneGraph Public License for more details.
*/
#include <osg/Material>
#include <osg/BoundsChecking>
2001-01-11 00:32:10 +08:00
using namespace osg;
Material::Material()
2001-01-11 00:32:10 +08:00
{
_colorMode = OFF;
_ambientFrontAndBack = true;
_ambientFront.set(0.2f, 0.2f, 0.2f, 1.0f);
_ambientBack.set(0.2f, 0.2f, 0.2f, 1.0f);
_diffuseFrontAndBack = true;
_diffuseFront.set(0.8f, 0.8f, 0.8f, 1.0f);
_diffuseBack.set(0.8f, 0.8f, 0.8f, 1.0f);
_specularFrontAndBack = true;
_specularFront.set(0.0f, 0.0f, 0.0f, 1.0f);
_specularBack.set(0.0f, 0.0f, 0.0f, 1.0f);
_emissionFrontAndBack = true;
_emissionFront.set(0.0f, 0.0f, 0.0f, 1.0f);
_emissionBack.set(0.0f, 0.0f, 0.0f, 1.0f);
_shininessFrontAndBack = true;
_shininessFront = 0.0f;
_shininessBack = 0.0f;
}
Material::~Material()
2001-01-11 00:32:10 +08:00
{
}
2004-02-20 19:51:14 +08:00
Material& Material:: operator = (const Material& rhs)
{
if (&rhs==this) return *this;
_colorMode= rhs._colorMode;
_ambientFrontAndBack= rhs._ambientFrontAndBack;
_ambientFront= rhs._ambientFront;
_ambientBack= rhs._ambientBack;
_diffuseFrontAndBack= rhs._diffuseFrontAndBack;
_diffuseFront= rhs._diffuseFront;
_diffuseBack= rhs._diffuseBack;
_specularFrontAndBack= rhs._specularFrontAndBack;
_specularFront= rhs._specularFront;
_specularBack= rhs._specularBack;
_emissionFrontAndBack= rhs._emissionFrontAndBack;
_emissionFront= rhs._emissionFront;
_emissionBack= rhs._emissionBack;
_shininessFrontAndBack= rhs._shininessFrontAndBack;
_shininessFront= rhs._shininessFront;
_shininessBack= rhs._shininessBack;
return *this;
}
2001-01-11 00:32:10 +08:00
void Material::setAmbient(Face face, const Vec4& ambient )
2001-01-11 00:32:10 +08:00
{
switch(face)
{
case(FRONT):
_ambientFrontAndBack = false;
_ambientFront = ambient;
2003-06-24 23:40:09 +08:00
//clampArray4BetweenRange(_ambientFront,0.0f,1.0f,"osg::Material::setAmbient(..)");
break;
case(BACK):
_ambientFrontAndBack = false;
_ambientBack = ambient;
2003-06-24 23:40:09 +08:00
//clampArray4BetweenRange(_ambientBack,0.0f,1.0f,"Material::setAmbient(..)");
break;
case(FRONT_AND_BACK):
_ambientFrontAndBack = true;
_ambientFront = ambient;
2003-06-24 23:40:09 +08:00
//clampArray4BetweenRange(_ambientFront,0.0f,1.0f,"Material::setAmbient(..)");
_ambientBack = _ambientFront;
break;
default:
notify(NOTICE)<<"Notice: invalid Face passed to Material::setAmbient()."<<std::endl;
2001-01-11 00:32:10 +08:00
}
}
const Vec4& Material::getAmbient(Face face) const
2001-01-11 00:32:10 +08:00
{
switch(face)
{
case(FRONT):
return _ambientFront;
case(BACK):
return _ambientBack;
case(FRONT_AND_BACK):
if (!_ambientFrontAndBack)
{
notify(NOTICE)<<"Notice: Material::getAmbient(FRONT_AND_BACK) called on material "<< std::endl;
notify(NOTICE)<<" with separate FRONT and BACK ambient colors."<< std::endl;
}
return _ambientFront;
}
notify(NOTICE)<<"Notice: invalid Face passed to Material::getAmbient()."<< std::endl;
2001-01-11 00:32:10 +08:00
return _ambientFront;
}
void Material::setDiffuse(Face face, const Vec4& diffuse )
2001-01-11 00:32:10 +08:00
{
switch(face)
{
case(FRONT):
_diffuseFrontAndBack = false;
_diffuseFront = diffuse;
2003-06-24 23:40:09 +08:00
//clampArray4BetweenRange(_diffuseFront,0.0f,1.0f,"Material::setDiffuse(..)");
break;
case(BACK):
_diffuseFrontAndBack = false;
_diffuseBack = diffuse;
2003-06-24 23:40:09 +08:00
//clampArray4BetweenRange(_diffuseBack,0.0f,1.0f,"Material::setDiffuse(..)");
break;
case(FRONT_AND_BACK):
_diffuseFrontAndBack = true;
_diffuseFront = diffuse;
2003-06-24 23:40:09 +08:00
//clampArray4BetweenRange(_diffuseFront,0.0f,1.0f,"Material::setDiffuse(..)");
_diffuseBack = _diffuseFront;
break;
default:
notify(NOTICE)<<"Notice: invalid Face passed to Material::setDiffuse()."<< std::endl;
break;
2001-01-11 00:32:10 +08:00
}
}
const Vec4& Material::getDiffuse(Face face) const
2001-01-11 00:32:10 +08:00
{
switch(face)
{
case(FRONT):
return _diffuseFront;
case(BACK):
return _diffuseBack;
case(FRONT_AND_BACK):
if (!_diffuseFrontAndBack)
{
notify(NOTICE)<<"Notice: Material::getDiffuse(FRONT_AND_BACK) called on material "<< std::endl;
notify(NOTICE)<<" with separate FRONT and BACK diffuse colors."<< std::endl;
}
return _diffuseFront;
}
notify(NOTICE)<<"Notice: invalid Face passed to Material::getDiffuse()."<< std::endl;
return _diffuseFront;
2001-01-11 00:32:10 +08:00
}
void Material::setSpecular(Face face, const Vec4& specular )
2001-01-11 00:32:10 +08:00
{
switch(face)
{
case(FRONT):
_specularFrontAndBack = false;
_specularFront = specular;
2003-06-24 23:40:09 +08:00
//clampArray4BetweenRange(_specularFront,0.0f,1.0f,"Material::setSpecular(..)");
break;
case(BACK):
_specularFrontAndBack = false;
_specularBack = specular;
2003-06-24 23:40:09 +08:00
//clampArray4BetweenRange(_specularBack,0.0f,1.0f,"Material::setSpecular(..)");
break;
case(FRONT_AND_BACK):
_specularFrontAndBack = true;
_specularFront = specular;
2003-06-24 23:40:09 +08:00
//clampArray4BetweenRange(_specularFront,0.0f,1.0f,"Material::setSpecular(..)");
_specularBack = _specularFront;
break;
default:
notify(NOTICE)<<"Notice: invalid Face passed to Material::setSpecular()."<< std::endl;
break;
2001-01-11 00:32:10 +08:00
}
}
const Vec4& Material::getSpecular(Face face) const
2001-01-11 00:32:10 +08:00
{
switch(face)
{
case(FRONT):
return _specularFront;
case(BACK):
return _specularBack;
case(FRONT_AND_BACK):
if (!_specularFrontAndBack)
{
notify(NOTICE)<<"Notice: Material::getSpecular(FRONT_AND_BACK) called on material "<< std::endl;
notify(NOTICE)<<" with separate FRONT and BACK specular colors."<< std::endl;
}
return _specularFront;
}
notify(NOTICE)<<"Notice: invalid Face passed to Material::getSpecular()."<< std::endl;
return _specularFront;
2001-01-11 00:32:10 +08:00
}
void Material::setEmission(Face face, const Vec4& emission )
2001-01-11 00:32:10 +08:00
{
switch(face)
2001-01-11 00:32:10 +08:00
{
case(FRONT):
_emissionFrontAndBack = false;
_emissionFront = emission;
2003-06-24 23:40:09 +08:00
//clampArray4BetweenRange(_emissionFront,0.0f,1.0f,"Material::setEmission(..)");
break;
case(BACK):
_emissionFrontAndBack = false;
_emissionBack = emission;
2003-06-24 23:40:09 +08:00
//clampArray4BetweenRange(_emissionBack,0.0f,1.0f,"Material::setEmission(..)");
break;
case(FRONT_AND_BACK):
_emissionFrontAndBack = true;
_emissionFront = emission;
2003-06-24 23:40:09 +08:00
//clampArray4BetweenRange(_emissionFront,0.0f,1.0f,"Material::setEmission(..)");
_emissionBack = _emissionFront;
break;
default:
notify(NOTICE)<<"Notice: invalid Face passed to Material::setEmission()."<< std::endl;
break;
2001-01-11 00:32:10 +08:00
}
}
const Vec4& Material::getEmission(Face face) const
2001-01-11 00:32:10 +08:00
{
switch(face)
2001-01-11 00:32:10 +08:00
{
case(FRONT):
return _emissionFront;
case(BACK):
return _emissionBack;
case(FRONT_AND_BACK):
if (!_emissionFrontAndBack)
{
notify(NOTICE)<<"Notice: Material::getEmission(FRONT_AND_BACK) called on material "<< std::endl;
notify(NOTICE)<<" with separate FRONT and BACK emission colors."<< std::endl;
}
return _emissionFront;
}
notify(NOTICE)<<"Notice: invalid Face passed to Material::getEmission()."<< std::endl;
return _emissionFront;
}
2001-01-11 00:32:10 +08:00
void Material::setShininess(Face face, float shininess )
{
clampBetweenRange(shininess,0.0f,128.0f,"Material::setShininess()");
switch(face)
2001-01-11 00:32:10 +08:00
{
case(FRONT):
_shininessFrontAndBack = false;
_shininessFront = shininess;
break;
case(BACK):
_shininessFrontAndBack = false;
_shininessBack = shininess;
break;
case(FRONT_AND_BACK):
_shininessFrontAndBack = true;
_shininessFront = shininess;
_shininessBack = shininess;
break;
default:
notify(NOTICE)<<"Notice: invalid Face passed to Material::setShininess()."<< std::endl;
break;
2001-01-11 00:32:10 +08:00
}
}
2001-01-11 00:32:10 +08:00
float Material::getShininess(Face face) const
{
switch(face)
2001-01-11 00:32:10 +08:00
{
case(FRONT):
return _shininessFront;
case(BACK):
return _shininessBack;
case(FRONT_AND_BACK):
if (!_shininessFrontAndBack)
{
notify(NOTICE)<<"Notice: Material::getShininess(FRONT_AND_BACK) called on material "<< std::endl;
notify(NOTICE)<<" with separate FRONT and BACK shininess colors."<< std::endl;
}
return _shininessFront;
}
notify(NOTICE)<<"Notice: invalid Face passed to Material::getShininess()."<< std::endl;
return _shininessFront;
}
2001-01-11 00:32:10 +08:00
void Material::setTransparency(Face face,float transparency)
{
2003-06-24 23:40:09 +08:00
//clampBetweenRange(transparency,0.0f,1.0f,"Material::setTransparency()");
2001-01-11 00:32:10 +08:00
if (face==FRONT || face==FRONT_AND_BACK)
{
2001-01-11 00:32:10 +08:00
_ambientFront[3] = 1.0f-transparency;
_diffuseFront[3] = 1.0f-transparency;
_specularFront[3] = 1.0f-transparency;
_emissionFront[3] = 1.0f-transparency;
}
2001-01-11 00:32:10 +08:00
if (face==BACK || face==FRONT_AND_BACK)
{
2001-01-11 00:32:10 +08:00
_ambientBack[3] = 1.0f-transparency;
_diffuseBack[3] = 1.0f-transparency;
_specularBack[3] = 1.0f-transparency;
_emissionBack[3] = 1.0f-transparency;
}
}
2001-01-11 00:32:10 +08:00
void Material::setAlpha(Face face,float alpha)
2001-01-11 00:32:10 +08:00
{
clampBetweenRange(alpha,0.0f,1.0f,"Material::setAlpha()");
2001-01-11 00:32:10 +08:00
if (face==FRONT || face==FRONT_AND_BACK)
{
_ambientFront[3] = alpha;
_diffuseFront[3] = alpha;
_specularFront[3] = alpha;
_emissionFront[3] = alpha;
2001-01-11 00:32:10 +08:00
}
if (face==BACK || face==FRONT_AND_BACK)
2001-01-11 00:32:10 +08:00
{
_ambientBack[3] = alpha;
_diffuseBack[3] = alpha;
_specularBack[3] = alpha;
_emissionBack[3] = alpha;
2001-01-11 00:32:10 +08:00
}
}
void Material::apply(State&) const
2001-01-11 00:32:10 +08:00
{
if (_colorMode==OFF)
{
glDisable(GL_COLOR_MATERIAL);
glColor4fv(_diffuseFront.ptr());
2001-01-11 00:32:10 +08:00
}
else
{
glEnable(GL_COLOR_MATERIAL);
2001-01-11 00:32:10 +08:00
glColorMaterial(GL_FRONT_AND_BACK,(GLenum)_colorMode);
switch(_colorMode)
{
case(AMBIENT): glColor4fv(_ambientFront.ptr()); break;
case(DIFFUSE): glColor4fv(_diffuseFront.ptr()); break;
case(SPECULAR): glColor4fv(_specularFront.ptr()); break;
case(EMISSION): glColor4fv(_emissionFront.ptr()); break;
case(AMBIENT_AND_DIFFUSE): glColor4fv(_diffuseFront.ptr()); break;
case(OFF): break;
2001-01-11 00:32:10 +08:00
}
}
if (_colorMode!=AMBIENT && _colorMode!=AMBIENT_AND_DIFFUSE)
{
if (_ambientFrontAndBack)
{
glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, _ambientFront.ptr() );
}
else
{
glMaterialfv( GL_FRONT, GL_AMBIENT, _ambientFront.ptr() );
glMaterialfv( GL_BACK, GL_AMBIENT, _ambientBack.ptr() );
}
}
2001-01-11 00:32:10 +08:00
if (_colorMode!=DIFFUSE && _colorMode!=AMBIENT_AND_DIFFUSE)
{
if (_diffuseFrontAndBack)
{
glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, _diffuseFront.ptr() );
}
else
{
glMaterialfv( GL_FRONT, GL_DIFFUSE, _diffuseFront.ptr() );
glMaterialfv( GL_BACK, GL_DIFFUSE, _diffuseBack.ptr() );
}
}
2001-01-11 00:32:10 +08:00
if (_colorMode!=SPECULAR)
{
if (_specularFrontAndBack)
{
glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, _specularFront.ptr() );
}
else
{
glMaterialfv( GL_FRONT, GL_SPECULAR, _specularFront.ptr() );
glMaterialfv( GL_BACK, GL_SPECULAR, _specularBack.ptr() );
}
}
2001-01-11 00:32:10 +08:00
if (_colorMode!=EMISSION)
{
if (_emissionFrontAndBack)
{
glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, _emissionFront.ptr() );
}
else
{
glMaterialfv( GL_FRONT, GL_EMISSION, _emissionFront.ptr() );
glMaterialfv( GL_BACK, GL_EMISSION, _emissionBack.ptr() );
}
}
2001-01-11 00:32:10 +08:00
if (_shininessFrontAndBack)
{
glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, _shininessFront );
2001-01-11 00:32:10 +08:00
}
else
{
glMaterialf( GL_FRONT, GL_SHININESS, _shininessFront );
glMaterialf( GL_BACK, GL_SHININESS, _shininessBack );
2001-01-11 00:32:10 +08:00
}
}