From d9c50ee7c4bcfb7e734ef9d6f134ba9210e0e6db Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 10 May 2005 20:20:20 +0000 Subject: [PATCH] Put in place the class to implement GLSL support in .ive --- VisualStudio/osgPlugins/ive/ive.dsp | 24 +++++++++++ src/osgPlugins/ive/DataInputStream.cpp | 30 +++++++++++++ src/osgPlugins/ive/DataInputStream.h | 8 +++- src/osgPlugins/ive/DataOutputStream.cpp | 36 +++++++++++++++- src/osgPlugins/ive/DataOutputStream.h | 6 ++- src/osgPlugins/ive/GNUmakefile | 5 ++- src/osgPlugins/ive/IveVersion.h | 3 +- src/osgPlugins/ive/Program.cpp | 57 +++++++++++++++++++++++++ src/osgPlugins/ive/Program.h | 15 +++++++ src/osgPlugins/ive/ReadWrite.h | 5 ++- src/osgPlugins/ive/Shader.cpp | 57 +++++++++++++++++++++++++ src/osgPlugins/ive/Shader.h | 15 +++++++ src/osgPlugins/ive/StateSet.cpp | 25 ++++++++++- src/osgPlugins/ive/Uniform.cpp | 57 +++++++++++++++++++++++++ src/osgPlugins/ive/Uniform.h | 15 +++++++ 15 files changed, 349 insertions(+), 9 deletions(-) create mode 100644 src/osgPlugins/ive/Program.cpp create mode 100644 src/osgPlugins/ive/Program.h create mode 100644 src/osgPlugins/ive/Shader.cpp create mode 100644 src/osgPlugins/ive/Shader.h create mode 100644 src/osgPlugins/ive/Uniform.cpp create mode 100644 src/osgPlugins/ive/Uniform.h diff --git a/VisualStudio/osgPlugins/ive/ive.dsp b/VisualStudio/osgPlugins/ive/ive.dsp index 435985eb7..1a4d4cdca 100755 --- a/VisualStudio/osgPlugins/ive/ive.dsp +++ b/VisualStudio/osgPlugins/ive/ive.dsp @@ -372,6 +372,18 @@ SOURCE=..\..\..\src\osgPlugins\ive\Text.cpp # End Source File # Begin Source File +SOURCE=..\..\..\src\osgPlugins\ive\Program.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\ive\Shader.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\ive\Uniform.cpp +# End Source File +# Begin Source File + SOURCE=..\..\..\src\osgPlugins\ive\Texture.cpp # End Source File # Begin Source File @@ -688,6 +700,18 @@ SOURCE=..\..\..\src\osgPlugins\ive\Text.h # End Source File # Begin Source File +SOURCE=..\..\..\src\osgPlugins\ive\Program.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\ive\Shader.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\ive\Uniform.h +# End Source File +# Begin Source File + SOURCE=..\..\..\src\osgPlugins\ive\Texture.h # End Source File # Begin Source File diff --git a/src/osgPlugins/ive/DataInputStream.cpp b/src/osgPlugins/ive/DataInputStream.cpp index e4e476e53..49546d463 100644 --- a/src/osgPlugins/ive/DataInputStream.cpp +++ b/src/osgPlugins/ive/DataInputStream.cpp @@ -40,6 +40,7 @@ #include "LightModel.h" #include "ProxyNode.h" #include "FrontFace.h" +#include "Program.h" #include "Group.h" #include "MatrixTransform.h" @@ -58,6 +59,7 @@ #include "OccluderNode.h" #include "Impostor.h" #include "CoordinateSystemNode.h" +#include "Uniform.h" #include "LightPointNode.h" #include "MultiSwitch.h" @@ -775,6 +777,10 @@ osg::StateAttribute* DataInputStream::readStateAttribute() attribute = new osg::FrontFace(); ((ive::FrontFace*)(attribute))->read(this); } + else if(attributeID == IVEPROGRAM){ + attribute = new osg::Program(); + ((ive::Program*)(attribute))->read(this); + } else{ throw Exception("Unknown StateAttribute in StateSet::read()"); } @@ -788,6 +794,30 @@ osg::StateAttribute* DataInputStream::readStateAttribute() return attribute; } +osg::Uniform* DataInputStream::readUniform() +{ + // Read uniforms unique ID. + int id = readInt(); + // See if uniform is already in the list. + UniformMap::iterator itr= _uniformMap.find(id); + if (itr!=_uniformMap.end()) return itr->second.get(); + + // Uniform is not in list. + // Create a new uniform, + osg::Uniform* uniform = new osg::Uniform(); + + // read its properties from stream + ((ive::Uniform*)(uniform))->read(this); + + // and add it to the uniform map, + _uniformMap[id] = uniform; + + + if (_verboseOutput) std::cout<<"read/writeUniform() ["< #include #include +#include #include #include @@ -79,6 +80,7 @@ public: osg::Image* readImage(std::string s); osg::StateSet* readStateSet(); osg::StateAttribute* readStateAttribute(); + osg::Uniform* readUniform(); osg::Drawable* readDrawable(); osg::Shape* readShape(); osg::Node* readNode(); @@ -90,13 +92,14 @@ public: typedef std::map > ImageMap; typedef std::map > StateSetMap; typedef std::map > StateAttributeMap; + typedef std::map > UniformMap; typedef std::map > DrawableMap; typedef std::map > ShapeMap; typedef std::map > NodeMap; - bool _verboseOutput; + bool _verboseOutput; std::istream* _istream; - int _byteswap; + int _byteswap; private: int _version; @@ -105,6 +108,7 @@ private: ImageMap _imageMap; StateSetMap _statesetMap; StateAttributeMap _stateAttributeMap; + UniformMap _uniformMap; DrawableMap _drawableMap; ShapeMap _shapeMap; NodeMap _nodeMap; diff --git a/src/osgPlugins/ive/DataOutputStream.cpp b/src/osgPlugins/ive/DataOutputStream.cpp index c035aaaf0..7bcf8dc1d 100644 --- a/src/osgPlugins/ive/DataOutputStream.cpp +++ b/src/osgPlugins/ive/DataOutputStream.cpp @@ -41,6 +41,8 @@ #include "LightModel.h" #include "ProxyNode.h" #include "FrontFace.h" +#include "Program.h" +#include "Uniform.h" #include "Group.h" #include "MatrixTransform.h" @@ -467,7 +469,6 @@ void DataOutputStream::writeStateSet(const osg::StateSet* stateset) if (_verboseOutput) std::cout<<"read/writeStateSet() ["<(attribute)){ ((ive::FrontFace*)(attribute))->write(this); } + // This is a FrontFace + else if(dynamic_cast(attribute)){ + ((ive::Program*)(attribute))->write(this); + } else{ std::string className = attribute->className(); @@ -586,6 +591,35 @@ void DataOutputStream::writeStateAttribute(const osg::StateAttribute* attribute) } } +void DataOutputStream::writeUniform(const osg::Uniform* uniform) +{ + UniformMap::iterator itr = _uniformMap.find(uniform); + if (itr!=_uniformMap.end()) + { + // Id already exists so just write ID. + writeInt(itr->second); + + if (_verboseOutput) std::cout<<"read/writeStateSet() ["<second<<"]"<write(this); + + if (_verboseOutput) std::cout<<"read/writeUniform() ["< #include #include +#include #include #include "IveVersion.h" @@ -71,6 +72,7 @@ public: void writeStateSet(const osg::StateSet* stateset); void writeStateAttribute(const osg::StateAttribute* sa); + void writeUniform(const osg::Uniform* uniform); void writeDrawable(const osg::Drawable* sa); void writeShape(const osg::Shape* sa); void writeNode(const osg::Node* sa); @@ -99,12 +101,14 @@ private: // Container to map stateset uniques to their respective stateset. typedef std::map StateSetMap; typedef std::map StateAttributeMap; + typedef std::map UniformMap; typedef std::map DrawableMap; - typedef std::map ShapeMap; + typedef std::map ShapeMap; typedef std::map NodeMap; StateSetMap _stateSetMap; StateAttributeMap _stateAttributeMap; + UniformMap _uniformMap; DrawableMap _drawableMap; ShapeMap _shapeMap; NodeMap _nodeMap; diff --git a/src/osgPlugins/ive/GNUmakefile b/src/osgPlugins/ive/GNUmakefile index 5bb07e6e8..41c468206 100644 --- a/src/osgPlugins/ive/GNUmakefile +++ b/src/osgPlugins/ive/GNUmakefile @@ -57,10 +57,12 @@ CXXFILES =\ PolygonOffset.cpp\ PositionAttitudeTransform.cpp\ PrimitiveSet.cpp\ + Program.cpp\ ProxyNode.cpp\ ReaderWriterIVE.cpp\ Sequence.cpp\ ShadeModel.cpp\ + Shader.cpp\ Shape.cpp\ ShapeDrawable.cpp\ StateSet.cpp\ @@ -70,13 +72,14 @@ CXXFILES =\ TexGen.cpp\ TexGenNode.cpp\ TexMat.cpp\ + Text.cpp\ Texture1D.cpp\ Texture2D.cpp\ Texture3D.cpp\ Texture.cpp\ - Text.cpp\ TextureCubeMap.cpp\ Transform.cpp\ + Uniform.cpp\ VertexProgram.cpp\ VisibilityGroup.cpp\ diff --git a/src/osgPlugins/ive/IveVersion.h b/src/osgPlugins/ive/IveVersion.h index e15c03070..61a6fa323 100644 --- a/src/osgPlugins/ive/IveVersion.h +++ b/src/osgPlugins/ive/IveVersion.h @@ -18,8 +18,9 @@ #define VERSION_0008 8 #define VERSION_0009 9 +#define VERSION_0010 10 -#define VERSION VERSION_0009 +#define VERSION VERSION_0010 /* The BYTE_SEX tag is used to check the endian diff --git a/src/osgPlugins/ive/Program.cpp b/src/osgPlugins/ive/Program.cpp new file mode 100644 index 000000000..0321daaef --- /dev/null +++ b/src/osgPlugins/ive/Program.cpp @@ -0,0 +1,57 @@ +/********************************************************************** + * + * FILE: Program.cpp + * + * DESCRIPTION: Read/Write osg::Program in binary format to disk. + * + * CREATED BY: Auto generated by iveGenerated + * and later modified by Rune Schmidt Jensen. + * + * HISTORY: Created 20.3.2003 + * + * Copyright 2003 VR-C + **********************************************************************/ + +#include "Exception.h" +#include "Program.h" +#include "Object.h" + +using namespace ive; + +void Program::write(DataOutputStream* out){ + // Write Program's identification. + out->writeInt(IVEPROGRAM); + // If the osg class is inherited by any other class we should also write this to file. + osg::Object* obj = dynamic_cast(this); + if(obj) + { + ((ive::Object*)(obj))->write(out); + } + else + throw Exception("Program::write(): Could not cast this osg::Program to an osg::Object."); + +} + +void Program::read(DataInputStream* in) +{ + // Read Program's identification. + int id = in->peekInt(); + if(id == IVEPROGRAM) + { + // Read Program's identification. + id = in->readInt(); + // If the osg class is inherited by any other class we should also read this from file. + osg::Object* obj = dynamic_cast(this); + if(obj) + { + ((ive::Object*)(obj))->read(in); + } + else + throw Exception("Program::read(): Could not cast this osg::Program to an osg::Object."); + + } + else + { + throw Exception("Program::read(): Expected Program identification."); + } +} diff --git a/src/osgPlugins/ive/Program.h b/src/osgPlugins/ive/Program.h new file mode 100644 index 000000000..856c2d40a --- /dev/null +++ b/src/osgPlugins/ive/Program.h @@ -0,0 +1,15 @@ +#ifndef IVE_PROGRAM +#define IVE_PROGRAM 1 + +#include +#include "ReadWrite.h" + +namespace ive{ +class Program : public osg::Program, public ReadWrite { +public: + void write(DataOutputStream* out); + void read(DataInputStream* in); +}; +} + +#endif diff --git a/src/osgPlugins/ive/ReadWrite.h b/src/osgPlugins/ive/ReadWrite.h index 961eeaad2..80d4daca6 100644 --- a/src/osgPlugins/ive/ReadWrite.h +++ b/src/osgPlugins/ive/ReadWrite.h @@ -63,7 +63,10 @@ namespace ive { #define IVEVERTEXPROGRAM 0x0000012F #define IVELIGHTMODEL 0x00001121 #define IVECLIPPLANE 0x00001122 -#define IVEFRONTFACE 0x00001123 +#define IVEFRONTFACE 0x00001123 +#define IVEPROGRAM 0x00001124 +#define IVESHADER 0x00001125 +#define IVEUNIFORM 0x00001126 // Drawables #define IVEDRAWABLE 0x00001000 diff --git a/src/osgPlugins/ive/Shader.cpp b/src/osgPlugins/ive/Shader.cpp new file mode 100644 index 000000000..79df6478a --- /dev/null +++ b/src/osgPlugins/ive/Shader.cpp @@ -0,0 +1,57 @@ +/********************************************************************** + * + * FILE: Shader.cpp + * + * DESCRIPTION: Read/Write osg::Shader in binary format to disk. + * + * CREATED BY: Auto generated by iveGenerated + * and later modified by Rune Schmidt Jensen. + * + * HISTORY: Created 20.3.2003 + * + * Copyright 2003 VR-C + **********************************************************************/ + +#include "Exception.h" +#include "Shader.h" +#include "Object.h" + +using namespace ive; + +void Shader::write(DataOutputStream* out){ + // Write Shader's identification. + out->writeInt(IVESHADER); + // If the osg class is inherited by any other class we should also write this to file. + osg::Object* obj = dynamic_cast(this); + if(obj) + { + ((ive::Object*)(obj))->write(out); + } + else + throw Exception("Shader::write(): Could not cast this osg::Shader to an osg::Object."); + +} + +void Shader::read(DataInputStream* in) +{ + // Read Shader's identification. + int id = in->peekInt(); + if(id == IVESHADER) + { + // Read Shader's identification. + id = in->readInt(); + // If the osg class is inherited by any other class we should also read this from file. + osg::Object* obj = dynamic_cast(this); + if(obj) + { + ((ive::Object*)(obj))->read(in); + } + else + throw Exception("Shader::read(): Could not cast this osg::Shader to an osg::Object."); + + } + else + { + throw Exception("Shader::read(): Expected Shader identification."); + } +} diff --git a/src/osgPlugins/ive/Shader.h b/src/osgPlugins/ive/Shader.h new file mode 100644 index 000000000..1d25349d7 --- /dev/null +++ b/src/osgPlugins/ive/Shader.h @@ -0,0 +1,15 @@ +#ifndef IVE_SHADER +#define IVE_SHADER 1 + +#include +#include "ReadWrite.h" + +namespace ive{ +class Shader : public osg::Shader, public ReadWrite { +public: + void write(DataOutputStream* out); + void read(DataInputStream* in); +}; +} + +#endif diff --git a/src/osgPlugins/ive/StateSet.cpp b/src/osgPlugins/ive/StateSet.cpp index 483bb5334..ac8a15b1a 100644 --- a/src/osgPlugins/ive/StateSet.cpp +++ b/src/osgPlugins/ive/StateSet.cpp @@ -87,6 +87,17 @@ void StateSet::write(DataOutputStream* out){ out->writeInt(aitr->second.second); } } + + if ( out->getVersion() >= VERSION_0010 ) + { + // Write stateset attributes, this could for instance be alphafunctions, materials, etc. + StateSet::UniformList ul = getUniformList(); + out->writeInt(ul.size()); + for(StateSet::UniformList::iterator uitr=ul.begin(); uitr!=ul.end(); ++uitr){ + out->writeUniform(uitr->second.first.get()); + out->writeInt(uitr->second.second); + } + } } void StateSet::read(DataInputStream* in){ @@ -126,7 +137,7 @@ void StateSet::read(DataInputStream* in){ // Read stateset modes. int size = in->readInt(); - int i; + int i; for(i=0;ireadInt(); int value = in->readInt(); @@ -142,7 +153,7 @@ void StateSet::read(DataInputStream* in){ // Read texture stateset mode. int nUnits = in->readInt(); - int unit; + int unit; for(unit=0;unitreadInt(); for(i=0;ireadInt()); } } + + if ( in->getVersion() >= VERSION_0010 ) + { + // Read uniforms + size = in->readInt(); + for(i=0;ireadUniform(); + addUniform(uniform, (osg::StateAttribute::OverrideValue)in->readInt()); + } + } } else{ throw Exception("StateSet::read(): Expected StateSet identification"); diff --git a/src/osgPlugins/ive/Uniform.cpp b/src/osgPlugins/ive/Uniform.cpp new file mode 100644 index 000000000..23e17aa46 --- /dev/null +++ b/src/osgPlugins/ive/Uniform.cpp @@ -0,0 +1,57 @@ +/********************************************************************** + * + * FILE: Uniform.cpp + * + * DESCRIPTION: Read/Write osg::Uniform in binary format to disk. + * + * CREATED BY: Auto generated by iveGenerated + * and later modified by Rune Schmidt Jensen. + * + * HISTORY: Created 20.3.2003 + * + * Copyright 2003 VR-C + **********************************************************************/ + +#include "Exception.h" +#include "Uniform.h" +#include "Object.h" + +using namespace ive; + +void Uniform::write(DataOutputStream* out){ + // Write Uniform's identification. + out->writeInt(IVEUNIFORM); + // If the osg class is inherited by any other class we should also write this to file. + osg::Object* obj = dynamic_cast(this); + if(obj) + { + ((ive::Object*)(obj))->write(out); + } + else + throw Exception("Uniform::write(): Could not cast this osg::Uniform to an osg::Object."); + +} + +void Uniform::read(DataInputStream* in) +{ + // Read Uniform's identification. + int id = in->peekInt(); + if(id == IVEUNIFORM) + { + // Read Uniform's identification. + id = in->readInt(); + // If the osg class is inherited by any other class we should also read this from file. + osg::Object* obj = dynamic_cast(this); + if(obj) + { + ((ive::Object*)(obj))->read(in); + } + else + throw Exception("Uniform::read(): Could not cast this osg::Uniform to an osg::Object."); + + } + else + { + throw Exception("Uniform::read(): Expected Uniform identification."); + } +} diff --git a/src/osgPlugins/ive/Uniform.h b/src/osgPlugins/ive/Uniform.h new file mode 100644 index 000000000..16b6bf853 --- /dev/null +++ b/src/osgPlugins/ive/Uniform.h @@ -0,0 +1,15 @@ +#ifndef IVE_UNIFORM +#define IVE_UNIFORM 1 + +#include +#include "ReadWrite.h" + +namespace ive{ +class Uniform : public osg::Uniform, public ReadWrite { +public: + void write(DataOutputStream* out); + void read(DataInputStream* in); +}; +} + +#endif