From e21af2adcee85f6a1876dab8edfc6484c6f9b595 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 25 Feb 2002 22:46:38 +0000 Subject: [PATCH] Added support for glLightModel functionality via osg::LightModel. --- VisualStudio/osg/osg.dsp | 8 ++ VisualStudio/osgPlugins/osg/dot_osg.dsp | 4 + include/osg/LightModel | 87 ++++++++++++++++++ include/osg/StateAttribute | 1 + include/osgGLUT/Viewer | 1 - src/osg/LightModel.cpp | 28 ++++++ src/osg/Makefile | 2 + src/osgGLUT/Viewer.cpp | 15 ++-- src/osgPlugins/osg/LightModel.cpp | 115 ++++++++++++++++++++++++ src/osgPlugins/osg/Makefile | 1 + 10 files changed, 256 insertions(+), 6 deletions(-) create mode 100644 include/osg/LightModel create mode 100644 src/osg/LightModel.cpp create mode 100644 src/osgPlugins/osg/LightModel.cpp diff --git a/VisualStudio/osg/osg.dsp b/VisualStudio/osg/osg.dsp index 0e9e69054..aeeef1472 100755 --- a/VisualStudio/osg/osg.dsp +++ b/VisualStudio/osg/osg.dsp @@ -193,6 +193,10 @@ SOURCE=..\..\src\osg\Light.cpp # End Source File # Begin Source File +SOURCE=..\..\src\osg\LightModel.cpp +# End Source File +# Begin Source File + SOURCE=..\..\src\osg\LightSource.cpp # End Source File # Begin Source File @@ -441,6 +445,10 @@ SOURCE=..\..\Include\Osg\Light # End Source File # Begin Source File +SOURCE=..\..\Include\Osg\LightModel +# End Source File +# Begin Source File + SOURCE=..\..\Include\Osg\LightSource # End Source File # Begin Source File diff --git a/VisualStudio/osgPlugins/osg/dot_osg.dsp b/VisualStudio/osgPlugins/osg/dot_osg.dsp index 76e27ee9e..3cd086ede 100755 --- a/VisualStudio/osgPlugins/osg/dot_osg.dsp +++ b/VisualStudio/osgPlugins/osg/dot_osg.dsp @@ -162,6 +162,10 @@ SOURCE=..\..\..\src\osgPlugins\osg\Light.cpp # End Source File # Begin Source File +SOURCE=..\..\..\src\osgPlugins\osg\LightModel.cpp +# End Source File +# Begin Source File + SOURCE=..\..\..\src\osgPlugins\osg\LightSource.cpp # End Source File # Begin Source File diff --git a/include/osg/LightModel b/include/osg/LightModel new file mode 100644 index 000000000..e5dcfb26e --- /dev/null +++ b/include/osg/LightModel @@ -0,0 +1,87 @@ +//C++ header - Open Scene Graph - Copyright (C) 1998-2001 Robert Osfield +//Distributed under the terms of the GNU Library General Public License (LGPL) +//as published by the Free Software Foundation. + +#ifndef OSG_LIGHTMODEL +#define OSG_LIGHTMODEL 1 + +#include +#include +#include + +namespace osg { + +class SG_EXPORT LightModel : public StateAttribute +{ + public : + + LightModel(); + + /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ + LightModel(const LightModel& lw,const CopyOp& copyop=CopyOp::SHALLOW_COPY): + StateAttribute(lw,copyop), + _ambient(lw._ambient), + _colorControl(lw._colorControl), + _localViewer(lw._localViewer), + _twoSided(lw._twoSided) {} + + + META_StateAttribute(LightModel, LIGHTMODEL); + + /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/ + virtual int compare(const StateAttribute& sa) const + { + // check the types are equal and then create the rhs variable + // used by the COMPARE_StateAttribute_Paramter macro's below. + COMPARE_StateAttribute_Types(LightModel,sa) + + // compare each paramter in turn against the rhs. + COMPARE_StateAttribute_Parameter(_ambient) + COMPARE_StateAttribute_Parameter(_colorControl) + COMPARE_StateAttribute_Parameter(_localViewer) + COMPARE_StateAttribute_Parameter(_twoSided) + + return 0; // passed all the above comparison macro's, must be equal. + } + + + void setAmbientIntensity(const osg::Vec4& ambient) { _ambient = ambient; } + const osg::Vec4& getAmbientIntensity() const { return _ambient; } + + + enum ColorControl + { + SEPERATE_SPECULAR_COLOR, + SINGLE_COLOR + }; + + void setColorControl(const ColorControl cc) { _colorControl = cc; } + inline const ColorControl getColorControl() const { return _colorControl; } + + + void setLocalViewer(const bool localViewer) { _localViewer=localViewer; } + inline const bool getLocalViewer() const { return _localViewer; } + + + void setTwoSided(const bool twoSided) { _twoSided = twoSided; } + inline const bool getTwoSided() const { return _twoSided; } + + + + virtual void apply(State& state) const; + + + protected : + + virtual ~LightModel(); + + osg::Vec4 _ambient; + ColorControl _colorControl; + bool _localViewer; + bool _twoSided; + +}; + +} + +#endif diff --git a/include/osg/StateAttribute b/include/osg/StateAttribute index 97b8fb982..274b62730 100644 --- a/include/osg/StateAttribute +++ b/include/osg/StateAttribute @@ -131,6 +131,7 @@ class SG_EXPORT StateAttribute : public Object TEXENV, TEXGEN, TEXMAT, + LIGHTMODEL, TRANSPARENCY, STENCIL, COLORMASK, diff --git a/include/osgGLUT/Viewer b/include/osgGLUT/Viewer index 1732c38cf..2cc10b9b2 100644 --- a/include/osgGLUT/Viewer +++ b/include/osgGLUT/Viewer @@ -129,7 +129,6 @@ class OSGGLUT_EXPORT Viewer : public Window, public osgUtil::GUIActionAdapter int backface; int lighting; int flat_shade; - int _two_sided_lighting; float frRate; // gwm Jul 2001 added convolved ('averaged') frame rate int _printStats; // gwm Jul 2001 change from bool struct { // gwm Jul 2001, added for display of statistics diff --git a/src/osg/LightModel.cpp b/src/osg/LightModel.cpp new file mode 100644 index 000000000..2085be779 --- /dev/null +++ b/src/osg/LightModel.cpp @@ -0,0 +1,28 @@ +#include +#include + +using namespace osg; + + +LightModel::LightModel(): + StateAttribute(), + _ambient(0.2f,0.2f,0.2f,1.0f), + _colorControl(LightModel::SINGLE_COLOR), + _localViewer(false), + _twoSided(false) +{ +} + + +LightModel::~LightModel() +{ +} + +void LightModel::apply(State&) const +{ + glLightModelfv(GL_LIGHT_MODEL_AMBIENT,_ambient.ptr()); + glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,_colorControl); + glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,_localViewer); + glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,_twoSided); +} + diff --git a/src/osg/Makefile b/src/osg/Makefile index a1ec5a2ac..14423ca59 100644 --- a/src/osg/Makefile +++ b/src/osg/Makefile @@ -28,6 +28,7 @@ C++FILES = \ Impostor.cpp\ ImpostorSprite.cpp\ Light.cpp\ + LightModel.cpp\ LightSource.cpp\ LineSegment.cpp\ LineStipple.cpp\ @@ -98,6 +99,7 @@ TARGET_INCLUDE_FILES = \ osg/ImpostorSprite\ osg/LOD\ osg/Light\ + osg/LightModel\ osg/LightSource\ osg/LineSegment\ osg/LineStipple\ diff --git a/src/osgGLUT/Viewer.cpp b/src/osgGLUT/Viewer.cpp index c7341c914..66e286492 100644 --- a/src/osgGLUT/Viewer.cpp +++ b/src/osgGLUT/Viewer.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -101,8 +102,6 @@ Viewer::Viewer() _viewFrustumCullingActive = true; _smallFeatureCullingActive = true; - _two_sided_lighting=0; - _useDisplayLists = true; _saveFileName = "saved_model.osg"; @@ -359,8 +358,6 @@ float Viewer::draw(unsigned int viewport) { osg::Timer_t beforeDraw = _timer.tick(); - glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,_two_sided_lighting); - // do draw traversal. getViewportSceneView(viewport)->draw(); @@ -943,7 +940,15 @@ void Viewer::keyboard(unsigned char key, int x, int y) break; case 'T' : - _two_sided_lighting = 1 - _two_sided_lighting; + { + osg::LightModel* lightmodel = dynamic_cast(sceneView->getGlobalStateSet()->getAttribute(osg::StateAttribute::LIGHTMODEL)); + if (lightmodel) + { + lightmodel = new osg::LightModel; + sceneView->getGlobalStateSet()->setAttribute(lightmodel); + } + lightmodel->setTwoSided(!lightmodel->getTwoSided()); + } break; case 'w' : diff --git a/src/osgPlugins/osg/LightModel.cpp b/src/osgPlugins/osg/LightModel.cpp new file mode 100644 index 000000000..173c544bc --- /dev/null +++ b/src/osgPlugins/osg/LightModel.cpp @@ -0,0 +1,115 @@ +#include + +#include +#include +#include + +using namespace osg; +using namespace osgDB; + +// forward declare functions to use later. +bool LightModel_readLocalData(Object& obj, Input& fr); +bool LightModel_writeLocalData(const Object& obj, Output& fw); + + +// register the read and write functions with the osgDB::Registry. +RegisterDotOsgWrapperProxy g_LightModelProxy +( + new osg::LightModel, + "LightModel", + "Object StateAttribute LightModel", + &LightModel_readLocalData, + &LightModel_writeLocalData +); + + +bool LightModel_readLocalData(Object& obj, Input& fr) +{ + bool iteratorAdvanced = false; + + LightModel& lightmodel = static_cast(obj); + + osg::Vec4 ambient; + if (fr[0].matchWord("ambientIntensity") && + fr[1].getFloat(ambient[0]) && + fr[2].getFloat(ambient[1]) && + fr[3].getFloat(ambient[2]) && + fr[4].getFloat(ambient[3])) + { + lightmodel.setAmbientIntensity(ambient); + fr+=5; + iteratorAdvanced = true; + } + + if (fr[0].matchWord("colorControl")) + { + if (fr[1].matchWord("SEPERATE_SPECULAR_COLOR")) + { + lightmodel.setColorControl(osg::LightModel::SEPERATE_SPECULAR_COLOR); + } + else if (fr[1].matchWord("SINGLE_COLOR")) + { + lightmodel.setColorControl(osg::LightModel::SINGLE_COLOR); + } + } + + int result; + if (fr[0].matchWord("localViewer") && fr[1].getInt(result)) + { + if (fr[1].matchWord("TRUE")) + { + lightmodel.setLocalViewer(true); + fr+=2; + iteratorAdvanced = true; + } + else if (fr[1].matchWord("FALSE")) + { + lightmodel.setLocalViewer(false); + fr+=2; + iteratorAdvanced = true; + } + } + + if (fr[0].matchWord("twoSided")) + { + if (fr[1].matchWord("TRUE")) + { + lightmodel.setTwoSided(true); + fr+=2; + iteratorAdvanced = true; + } + else if (fr[1].matchWord("FALSE")) + { + lightmodel.setTwoSided(false); + fr+=2; + iteratorAdvanced = true; + } + } + + return iteratorAdvanced; +} + +bool LightModel_writeLocalData(const Object& obj,Output& fw) +{ + const LightModel& lightmodel = static_cast(obj); + + fw.indent() << "ambientIntensity " << lightmodel.getAmbientIntensity() << std::endl; + + if (lightmodel.getColorControl()==osg::LightModel::SEPERATE_SPECULAR_COLOR) + fw.indent() << "colorControl SEPERATE_SPECULAR_COLOR" << std::endl; + else + fw.indent() << "colorControl SINGLE_COLOR" << std::endl; + + if (lightmodel.getLocalViewer()) + fw.indent() << "localViewer TRUE"<< std::endl; + else + fw.indent() << "localViewer FALSE"<< std::endl; + + if (lightmodel.getTwoSided()) + fw.indent() << "twoSided TRUE"<< std::endl; + else + fw.indent() << "twoSided FALSE"<< std::endl; + + return true; +} + diff --git a/src/osgPlugins/osg/Makefile b/src/osgPlugins/osg/Makefile index 43162d62d..b7ca3c438 100644 --- a/src/osgPlugins/osg/Makefile +++ b/src/osgPlugins/osg/Makefile @@ -19,6 +19,7 @@ C++FILES = \ Image.cpp\ Impostor.cpp\ Light.cpp\ + LightModel.cpp\ LightSource.cpp\ LineStipple.cpp\ LineWidth.cpp\