Fixed the osg::Light so that it requires the user to explicitly define which

OpenGL light is being operated on, and also now relies upong the standard
osg::State handling of OpenGL modes to switch on the appropriate lights.

The previous static counter mechansim for the light number was causing a
redundent light to be created when the osg plugin created the first osg::Light
to use a prototype for other osg::Light's to be cloned from in the
.osg plugin execution.

The static count mechanism also prevent the lights modes being controlled
independantly from the setting of the light paramters themselves.  This
meant that a light once created was global, and couldn't be turned off
locally via the OSG's support for OpenGL mode enabling/disabling.  This
has been overcome with the new implementation, the user has complete
flexiblity of when and where to use the different lights at their
disposal.
This commit is contained in:
Robert Osfield 2001-12-24 21:34:40 +00:00
parent a6d329b812
commit 98c8447ae9
5 changed files with 43 additions and 41 deletions

View File

@ -30,7 +30,6 @@ class SG_EXPORT Light : public StateAttribute
// compare each paramter in turn against the rhs.
COMPARE_StateAttribute_Parameter(_lightnum)
COMPARE_StateAttribute_Parameter(_on)
COMPARE_StateAttribute_Parameter(_ambient)
COMPARE_StateAttribute_Parameter(_diffuse)
COMPARE_StateAttribute_Parameter(_specular)
@ -45,20 +44,16 @@ class SG_EXPORT Light : public StateAttribute
return 0; // passed all the above comparison macro's, must be equal.
}
/**
* Turn the light on.
* Calling this method doesn't directly affect OpenGL's lighting mode.
*/
inline void on() { _on = true; }
virtual void setStateSetModes(StateSet& ds,const GLModeValue value) const
{
ds.setMode(GL_LIGHT0+_lightnum,value);
}
/**
* Turn the light off.
* Calling this method doesn't directly affect OpenGL's lighting mode.
*/
inline void off() { _on = false; }
/** Apply the light's state to the OpenGL state machine. */
virtual void apply(State& state) const;
/** Set which OpenGL light to operate on.*/
void setLightNum(const int num) { _lightnum = num; }
/** Get which OpenGL light this osg::Light operates on.*/
const int getLightNum() const { return _lightnum; }
/** Set the ambient component of the light. */
inline void setAmbient( const Vec4& ambient ) { _ambient = ambient; }
@ -126,6 +121,9 @@ class SG_EXPORT Light : public StateAttribute
*/
void captureLightState();
/** Apply the light's state to the OpenGL state machine. */
virtual void apply(State& state) const;
protected :
virtual ~Light();
@ -134,7 +132,7 @@ class SG_EXPORT Light : public StateAttribute
void init();
int _lightnum; // OpenGL light number
bool _on; // on/off state
Vec4 _ambient; // r, g, b, w
Vec4 _diffuse; // r, g, b, w
Vec4 _specular; // r, g, b, w
@ -145,8 +143,6 @@ class SG_EXPORT Light : public StateAttribute
float _quadratic_attenuation; // quadratic
float _spot_exponent; // exponent
float _spot_cutoff; // spread
static int _currentLightNum; // current max. OpenGL light number
};
};

View File

@ -3,13 +3,8 @@
using namespace osg;
int Light::_currentLightNum = -1;
Light::Light( void )
{
_lightnum = ++_currentLightNum;
_on = 1;
init();
// notify(DEBUG) << "_ambient "<<_ambient<<std::endl;
@ -32,6 +27,7 @@ Light::~Light( void )
void Light::init( void )
{
_lightnum = 0;
_ambient.set(0.05f,0.05f,0.05f,1.0f);
_diffuse.set(0.8f,0.8f,0.8f,1.0f);
_specular.set(0.05f,0.05f,0.05f,1.0f);
@ -61,21 +57,14 @@ void Light::captureLightState()
void Light::apply(State&) const
{
if( _on )
{
// note state should probably be handling the glEnable...
glEnable ( (GLenum)((int)GL_LIGHT0 + _lightnum) );
glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_AMBIENT, _ambient.ptr() );
glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_DIFFUSE, _diffuse.ptr() );
glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPECULAR, _specular.ptr() );
glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_POSITION, _position.ptr() );
glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_DIRECTION, _direction.ptr() );
glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_EXPONENT, _spot_exponent );
glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_CUTOFF, _spot_cutoff );
glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_CONSTANT_ATTENUATION, _constant_attenuation );
glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_LINEAR_ATTENUATION, _linear_attenuation );
glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_QUADRATIC_ATTENUATION, _quadratic_attenuation );
}
else
glDisable( (GLenum)((int)GL_LIGHT0 + _lightnum) );
glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_AMBIENT, _ambient.ptr() );
glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_DIFFUSE, _diffuse.ptr() );
glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPECULAR, _specular.ptr() );
glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_POSITION, _position.ptr() );
glLightfv( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_DIRECTION, _direction.ptr() );
glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_EXPONENT, _spot_exponent );
glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_SPOT_CUTOFF, _spot_cutoff );
glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_CONSTANT_ATTENUATION, _constant_attenuation );
glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_LINEAR_ATTENUATION, _linear_attenuation );
glLightf ( (GLenum)((int)GL_LIGHT0 + _lightnum), GL_QUADRATIC_ATTENUATION, _quadratic_attenuation );
}

View File

@ -44,6 +44,7 @@ public:
opacity=1; specular=0; specexp=0; fname="";TextureWidth=1; TextureHeight=1;
ctx=NULL; tx=NULL; id=0; dstate=NULL;colour[0]=colour[1]=colour[2]=colour[3]=1;
bright=halfIn=halfOut=falloff=0;atyp=NONE;
_lightnum=0;
}
~dwmaterial() { }
void settexture() {
@ -135,14 +136,15 @@ public:
fname= (buff+13);
fname+= ".tga";
}
LightSource *makeLight(const Vec4 pos) {
LightSource *makeLight(const Vec4 pos)
{
Light *lt= new Light;
Vec4 cdef;
cdef[0]=cdef[1]=cdef[2]=0.0f; cdef[3]=0.0f;
lt->setLightNum(_lightnum++);
lt->setSpecular(colour*bright/2.0f);
lt->setDiffuse(colour*bright/4.0f);
lt->setAmbient(cdef);
lt->on();
if (atyp==NONE) ;
else if (atyp==INVERSE_DIST) {
lt->setLinearAttenuation(1.0f);
@ -174,6 +176,7 @@ private:
float bright,halfIn,halfOut,falloff; // light brightness
Image *ctx;
Texture *tx;
int _lightnum;
StateSet *dstate; // used to represent the dw material in OSG
};
// structure to use as data for tesselation

View File

@ -32,6 +32,16 @@ bool Light_readLocalData(Object& obj, Input& fr)
Light& light = static_cast<Light&>(obj);
if (fr[0].matchWord("light_num"))
{
int lightnum=0;
if (fr[1].getInt(lightnum))
{
light.setLightNum(lightnum);
fr += 2;
iteratorAdvanced = true;
}
}
#define ReadVec4(A,B) { \
if (fr[0].matchWord(B) && \
@ -96,6 +106,8 @@ bool Light_writeLocalData(const Object& obj,Output& fw)
{
const Light& light = static_cast<const Light&>(obj);
fw.indent() << "light_num " << light.getLightNum() << std::endl;
// Vec4's
fw.indent() << "ambient " << light.getAmbient() << std::endl;
fw.indent() << "diffuse " << light.getDiffuse() << std::endl;

View File

@ -48,6 +48,7 @@ void SceneView::setDefaults()
_lightingMode=HEADLIGHT;
_light = new osg::Light;
_light->setLightNum(0);
_light->setAmbient(Vec4(0.00f,0.0f,0.00f,1.0f));
_light->setDiffuse(Vec4(0.8f,0.8f,0.8f,1.0f));
_light->setSpecular(Vec4(1.0f,1.0f,1.0f,1.0f));
@ -75,6 +76,7 @@ void SceneView::setDefaults()
// enable lighting by default.
_globalState->setMode(GL_LIGHTING, osg::StateAttribute::ON);
_light->setStateSetModes(*_globalState,osg::StateAttribute::ON);
// enable depth testing by default.
_globalState->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);