From 4ea7de39c6cd6ff09241be735e3a1bc086d41423 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 4 Jan 2002 17:35:54 +0000 Subject: [PATCH] Added osg::ShadeModel state attribute which encapsulates glShadeModel. --- VisualStudio/osg/osg.dsp | 8 ++++ VisualStudio/osgPlugins/osg/dot_osg.dsp | 4 ++ include/osg/ShadeModel | 58 +++++++++++++++++++++++ include/osg/StateAttribute | 3 +- src/osg/Makefile | 1 + src/osg/ShadeModel.cpp | 19 ++++++++ src/osgGLUT/Viewer.cpp | 18 +++++-- src/osgPlugins/osg/Makefile | 1 + src/osgPlugins/osg/ShadeModel.cpp | 62 +++++++++++++++++++++++++ 9 files changed, 169 insertions(+), 5 deletions(-) create mode 100644 include/osg/ShadeModel create mode 100644 src/osg/ShadeModel.cpp create mode 100644 src/osgPlugins/osg/ShadeModel.cpp diff --git a/VisualStudio/osg/osg.dsp b/VisualStudio/osg/osg.dsp index bb580e0f2..c478a6eff 100755 --- a/VisualStudio/osg/osg.dsp +++ b/VisualStudio/osg/osg.dsp @@ -249,6 +249,10 @@ SOURCE=..\..\src\osg\Quat.cpp # End Source File # Begin Source File +SOURCE=..\..\src\osg\ShadeModel.cpp +# End Source File +# Begin Source File + SOURCE=..\..\src\osg\State.cpp # End Source File # Begin Source File @@ -505,6 +509,10 @@ SOURCE=..\..\Include\Osg\Referenced # End Source File # Begin Source File +SOURCE=..\..\Include\Osg\ShadeModel +# End Source File +# Begin Source File + SOURCE=..\..\Include\Osg\State # End Source File # Begin Source File diff --git a/VisualStudio/osgPlugins/osg/dot_osg.dsp b/VisualStudio/osgPlugins/osg/dot_osg.dsp index 2d0ee3d4e..179095c49 100755 --- a/VisualStudio/osgPlugins/osg/dot_osg.dsp +++ b/VisualStudio/osgPlugins/osg/dot_osg.dsp @@ -206,6 +206,10 @@ SOURCE=..\..\..\src\osgPlugins\osg\ReaderWriterOSG.cpp # End Source File # Begin Source File +SOURCE=..\..\..\src\osgPlugins\osg\ShadeModel.cpp +# End Source File +# Begin Source File + SOURCE=..\..\..\src\osgPlugins\osg\StateSet.cpp # End Source File # Begin Source File diff --git a/include/osg/ShadeModel b/include/osg/ShadeModel new file mode 100644 index 000000000..b684db274 --- /dev/null +++ b/include/osg/ShadeModel @@ -0,0 +1,58 @@ +//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_SHADEMODEL +#define OSG_SHADEMODEL 1 + +#include +#include +#include + +namespace osg { + +/** Class which encapsulates glShadeModel(..). +*/ +class SG_EXPORT ShadeModel : public StateAttribute +{ + public : + + ShadeModel(); + + META_StateAttribute(ShadeModel, SHADEMODEL); + + /** 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(ShadeModel,sa) + + // compare each paramter in turn against the rhs. + COMPARE_StateAttribute_Parameter(_mode) + + return 0; // passed all the above comparison macro's, must be equal. + } + + enum Mode { + FLAT = GL_FLAT, + SMOOTH = GL_SMOOTH + }; + + inline void setMode(const Mode mode) { _mode = mode; } + + inline const Mode getMode() const { return _mode; } + + virtual void apply(State& state) const; + + protected: + + virtual ~ShadeModel(); + + Mode _mode; + +}; + +}; + +#endif diff --git a/include/osg/StateAttribute b/include/osg/StateAttribute index ca54e8f4c..88af486dc 100644 --- a/include/osg/StateAttribute +++ b/include/osg/StateAttribute @@ -123,7 +123,8 @@ class SG_EXPORT StateAttribute : public Object POINT =LIGHT_7+1, LINEWIDTH =POINT+1, - POLYGONMODE =LINEWIDTH+1, + SHADEMODEL =LINEWIDTH+1, + POLYGONMODE =SHADEMODEL+1, POLYGONOFFSET =POLYGONMODE+1, TEXENV =POLYGONOFFSET+1, TEXGEN =TEXENV+1, diff --git a/src/osg/Makefile b/src/osg/Makefile index b3b5bc24b..501098aab 100644 --- a/src/osg/Makefile +++ b/src/osg/Makefile @@ -42,6 +42,7 @@ C++FILES = \ PolygonMode.cpp\ PolygonOffset.cpp\ Quat.cpp\ + ShadeModel.cpp\ State.cpp\ StateSet.cpp\ Stencil.cpp \ diff --git a/src/osg/ShadeModel.cpp b/src/osg/ShadeModel.cpp new file mode 100644 index 000000000..dae351a16 --- /dev/null +++ b/src/osg/ShadeModel.cpp @@ -0,0 +1,19 @@ +#include +#include + +using namespace osg; + +ShadeModel::ShadeModel() +{ + _mode = SMOOTH; +} + + +ShadeModel::~ShadeModel() +{ +} + +void ShadeModel::apply(State&) const +{ + glShadeModel((GLenum)_mode); +} diff --git a/src/osgGLUT/Viewer.cpp b/src/osgGLUT/Viewer.cpp index 6b06668d9..928e949eb 100644 --- a/src/osgGLUT/Viewer.cpp +++ b/src/osgGLUT/Viewer.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -852,14 +853,23 @@ void Viewer::keyboard(unsigned char key, int x, int y) break; case 's' : + { flat_shade = 1 - flat_shade ; + osg::StateSet* stateset = sceneView->getGlobalStateSet(); + osg::ShadeModel* shademodel = dynamic_cast(stateset->getAttribute(osg::StateAttribute::SHADEMODEL)); + if (!shademodel) + { + shademodel = new osg::ShadeModel; + stateset->setAttribute(shademodel,osg::StateAttribute::OVERRIDE); + } + if( flat_shade ) - glShadeModel( GL_FLAT ); + shademodel->setMode( osg::ShadeModel::FLAT ); else - glShadeModel( GL_SMOOTH ); - + shademodel->setMode( osg::ShadeModel::SMOOTH ); + break; - + } case 'S' : { osg::notify(osg::NOTICE) << "Smoothing scene..."<< std::endl; diff --git a/src/osgPlugins/osg/Makefile b/src/osgPlugins/osg/Makefile index c8cd1046b..be19f447a 100644 --- a/src/osgPlugins/osg/Makefile +++ b/src/osgPlugins/osg/Makefile @@ -30,6 +30,7 @@ C++FILES = \ PolygonMode.cpp\ PolygonOffset.cpp\ ReaderWriterOSG.cpp\ + ShadeModel.cpp\ StateSet.cpp\ Stencil.cpp\ Switch.cpp\ diff --git a/src/osgPlugins/osg/ShadeModel.cpp b/src/osgPlugins/osg/ShadeModel.cpp new file mode 100644 index 000000000..9b02b4da2 --- /dev/null +++ b/src/osgPlugins/osg/ShadeModel.cpp @@ -0,0 +1,62 @@ +#include "osg/ShadeModel" + +#include "osgDB/Registry" +#include "osgDB/Input" +#include "osgDB/Output" + +using namespace osg; +using namespace osgDB; + +// forward declare functions to use later. +bool ShadeModel_readLocalData(Object& obj, Input& fr); +bool ShadeModel_writeLocalData(const Object& obj, Output& fw); + +// register the read and write functions with the osgDB::Registry. +RegisterDotOsgWrapperProxy g_ShadeModelFuncProxy +( + new osg::ShadeModel, + "ShadeModel", + "Object StateAttribute ShadeModel", + &ShadeModel_readLocalData, + &ShadeModel_writeLocalData +); + + +bool ShadeModel_readLocalData(Object& obj,Input& fr) +{ + bool iteratorAdvanced = false; + + ShadeModel& shademodel = static_cast(obj); + + if (fr[0].matchWord("mode")) + { + if (fr[1].matchWord("FLAT")) + { + shademodel.setMode(ShadeModel::FLAT); + fr+=2; + iteratorAdvanced = true; + } + else if (fr[1].matchWord("SMOOTH")) + { + shademodel.setMode(ShadeModel::SMOOTH); + fr+=2; + iteratorAdvanced = true; + } + } + + return iteratorAdvanced; +} + + +bool ShadeModel_writeLocalData(const Object& obj, Output& fw) +{ + + const ShadeModel& shademodel = static_cast(obj); + + switch(shademodel.getMode()) + { + case(ShadeModel::FLAT): fw.indent() << "mode FLAT" <