Further work on GLSL support in .ive
This commit is contained in:
@@ -60,6 +60,7 @@
|
||||
#include "Impostor.h"
|
||||
#include "CoordinateSystemNode.h"
|
||||
#include "Uniform.h"
|
||||
#include "Shader.h"
|
||||
|
||||
#include "LightPointNode.h"
|
||||
#include "MultiSwitch.h"
|
||||
@@ -818,6 +819,31 @@ osg::Uniform* DataInputStream::readUniform()
|
||||
return uniform;
|
||||
}
|
||||
|
||||
|
||||
osg::Shader* DataInputStream::readShader()
|
||||
{
|
||||
// Read shaders unique ID.
|
||||
int id = readInt();
|
||||
// See if shader is already in the list.
|
||||
ShaderMap::iterator itr= _shaderMap.find(id);
|
||||
if (itr!=_shaderMap.end()) return itr->second.get();
|
||||
|
||||
// Shader is not in list.
|
||||
// Create a new shader,
|
||||
osg::Shader* shader = new osg::Shader();
|
||||
|
||||
// read its properties from stream
|
||||
((ive::Shader*)(shader))->read(this);
|
||||
|
||||
// and add it to the shader map,
|
||||
_shaderMap[id] = shader;
|
||||
|
||||
|
||||
if (_verboseOutput) std::cout<<"read/writeShader() ["<<id<<"]"<<std::endl;
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
osg::Drawable* DataInputStream::readDrawable()
|
||||
{
|
||||
// Read stateattributes unique ID.
|
||||
|
||||
@@ -81,6 +81,7 @@ public:
|
||||
osg::StateSet* readStateSet();
|
||||
osg::StateAttribute* readStateAttribute();
|
||||
osg::Uniform* readUniform();
|
||||
osg::Shader* readShader();
|
||||
osg::Drawable* readDrawable();
|
||||
osg::Shape* readShape();
|
||||
osg::Node* readNode();
|
||||
@@ -93,6 +94,7 @@ public:
|
||||
typedef std::map<int,osg::ref_ptr<osg::StateSet> > StateSetMap;
|
||||
typedef std::map<int,osg::ref_ptr<osg::StateAttribute> > StateAttributeMap;
|
||||
typedef std::map<int,osg::ref_ptr<osg::Uniform> > UniformMap;
|
||||
typedef std::map<int,osg::ref_ptr<osg::Shader> > ShaderMap;
|
||||
typedef std::map<int,osg::ref_ptr<osg::Drawable> > DrawableMap;
|
||||
typedef std::map<int,osg::ref_ptr<osg::Shape> > ShapeMap;
|
||||
typedef std::map<int,osg::ref_ptr<osg::Node> > NodeMap;
|
||||
@@ -109,13 +111,14 @@ private:
|
||||
StateSetMap _statesetMap;
|
||||
StateAttributeMap _stateAttributeMap;
|
||||
UniformMap _uniformMap;
|
||||
ShaderMap _shaderMap;
|
||||
DrawableMap _drawableMap;
|
||||
ShapeMap _shapeMap;
|
||||
NodeMap _nodeMap;
|
||||
|
||||
bool _loadExternalReferenceFiles;
|
||||
|
||||
osg::ref_ptr<const osgDB::ReaderWriter::Options> _options;
|
||||
osg::ref_ptr<const osgDB::ReaderWriter::Options> _options;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@
|
||||
#include "FrontFace.h"
|
||||
#include "Program.h"
|
||||
#include "Uniform.h"
|
||||
#include "Shader.h"
|
||||
|
||||
#include "Group.h"
|
||||
#include "MatrixTransform.h"
|
||||
@@ -599,7 +600,7 @@ void DataOutputStream::writeUniform(const osg::Uniform* uniform)
|
||||
// Id already exists so just write ID.
|
||||
writeInt(itr->second);
|
||||
|
||||
if (_verboseOutput) std::cout<<"read/writeStateSet() ["<<itr->second<<"]"<<std::endl;
|
||||
if (_verboseOutput) std::cout<<"read/writeUniform() ["<<itr->second<<"]"<<std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -620,6 +621,35 @@ void DataOutputStream::writeUniform(const osg::Uniform* uniform)
|
||||
}
|
||||
}
|
||||
|
||||
void DataOutputStream::writeShader(const osg::Shader* shader)
|
||||
{
|
||||
ShaderMap::iterator itr = _shaderMap.find(shader);
|
||||
if (itr!=_shaderMap.end())
|
||||
{
|
||||
// Id already exists so just write ID.
|
||||
writeInt(itr->second);
|
||||
|
||||
if (_verboseOutput) std::cout<<"read/writeShader() ["<<itr->second<<"]"<<std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
// id doesn't exist so create a new ID and
|
||||
// register the shader.
|
||||
|
||||
int id = _shaderMap.size();
|
||||
_shaderMap[shader] = id;
|
||||
|
||||
// write the id.
|
||||
writeInt(id);
|
||||
|
||||
// write the stateset.
|
||||
((ive::Shader*)(shader))->write(this);
|
||||
|
||||
if (_verboseOutput) std::cout<<"read/writeShader() ["<<id<<"]"<<std::endl;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void DataOutputStream::writeDrawable(const osg::Drawable* drawable)
|
||||
{
|
||||
DrawableMap::iterator itr = _drawableMap.find(drawable);
|
||||
|
||||
@@ -73,6 +73,7 @@ public:
|
||||
void writeStateSet(const osg::StateSet* stateset);
|
||||
void writeStateAttribute(const osg::StateAttribute* sa);
|
||||
void writeUniform(const osg::Uniform* uniform);
|
||||
void writeShader(const osg::Shader* shader);
|
||||
void writeDrawable(const osg::Drawable* sa);
|
||||
void writeShape(const osg::Shape* sa);
|
||||
void writeNode(const osg::Node* sa);
|
||||
@@ -102,6 +103,7 @@ private:
|
||||
typedef std::map<const osg::StateSet*,int> StateSetMap;
|
||||
typedef std::map<const osg::StateAttribute*,int> StateAttributeMap;
|
||||
typedef std::map<const osg::Uniform*,int> UniformMap;
|
||||
typedef std::map<const osg::Shader*,int> ShaderMap;
|
||||
typedef std::map<const osg::Drawable*,int> DrawableMap;
|
||||
typedef std::map<const osg::Shape*,int> ShapeMap;
|
||||
typedef std::map<const osg::Node*,int> NodeMap;
|
||||
@@ -109,6 +111,7 @@ private:
|
||||
StateSetMap _stateSetMap;
|
||||
StateAttributeMap _stateAttributeMap;
|
||||
UniformMap _uniformMap;
|
||||
ShaderMap _shaderMap;
|
||||
DrawableMap _drawableMap;
|
||||
ShapeMap _shapeMap;
|
||||
NodeMap _nodeMap;
|
||||
|
||||
@@ -30,6 +30,22 @@ void Program::write(DataOutputStream* out){
|
||||
else
|
||||
throw Exception("Program::write(): Could not cast this osg::Program to an osg::Object.");
|
||||
|
||||
const AttribBindingList& abl = getAttribBindingList();
|
||||
out->writeUInt(abl.size());
|
||||
for(AttribBindingList::const_iterator itr = abl.begin();
|
||||
itr != abl.end();
|
||||
++itr)
|
||||
{
|
||||
out->writeString(itr->first);
|
||||
out->writeUInt(itr->second);
|
||||
}
|
||||
|
||||
// Write
|
||||
out->writeUInt(getNumShaders());
|
||||
for(unsigned int si=0; si<getNumShaders(); ++si)
|
||||
{
|
||||
out->writeShader(getShader(si));
|
||||
}
|
||||
}
|
||||
|
||||
void Program::read(DataInputStream* in)
|
||||
@@ -54,4 +70,22 @@ void Program::read(DataInputStream* in)
|
||||
{
|
||||
throw Exception("Program::read(): Expected Program identification.");
|
||||
}
|
||||
|
||||
// reading in shaders.
|
||||
unsigned int size = in->readUInt();
|
||||
for(unsigned int ai=0; ai<size; ++ai)
|
||||
{
|
||||
std::string name = in->readString();
|
||||
unsigned int index = in->readUInt();
|
||||
addBindAttribLocation(name, index);
|
||||
}
|
||||
|
||||
// reading in shaders.
|
||||
size = in->readUInt();
|
||||
for(unsigned int si=0; si<size; ++si)
|
||||
{
|
||||
osg::Shader* shader = in->readShader();
|
||||
addShader(shader);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,6 +30,10 @@ void Shader::write(DataOutputStream* out){
|
||||
else
|
||||
throw Exception("Shader::write(): Could not cast this osg::Shader to an osg::Object.");
|
||||
|
||||
out->writeString(getName());
|
||||
out->writeInt(getType());
|
||||
out->writeString(getShaderSource());
|
||||
|
||||
}
|
||||
|
||||
void Shader::read(DataInputStream* in)
|
||||
@@ -54,4 +58,9 @@ void Shader::read(DataInputStream* in)
|
||||
{
|
||||
throw Exception("Shader::read(): Expected Shader identification.");
|
||||
}
|
||||
|
||||
setName(in->readString());
|
||||
setType(static_cast<Type>(in->readInt()));
|
||||
setShaderSource(in->readString());
|
||||
|
||||
}
|
||||
|
||||
@@ -90,10 +90,11 @@ void StateSet::write(DataOutputStream* out){
|
||||
|
||||
if ( out->getVersion() >= VERSION_0010 )
|
||||
{
|
||||
// Write stateset attributes, this could for instance be alphafunctions, materials, etc.
|
||||
// Write stateset uniforms
|
||||
StateSet::UniformList ul = getUniformList();
|
||||
out->writeInt(ul.size());
|
||||
for(StateSet::UniformList::iterator uitr=ul.begin(); uitr!=ul.end(); ++uitr){
|
||||
for(StateSet::UniformList::iterator uitr=ul.begin(); uitr!=ul.end(); ++uitr)
|
||||
{
|
||||
out->writeUniform(uitr->second.first.get());
|
||||
out->writeInt(uitr->second.second);
|
||||
}
|
||||
@@ -165,9 +166,11 @@ void StateSet::read(DataInputStream* in){
|
||||
|
||||
// Read texture attributes.
|
||||
nUnits = in->readInt();
|
||||
for(unit=0;unit<nUnits;unit++){
|
||||
for(unit=0;unit<nUnits;unit++)
|
||||
{
|
||||
size = in->readInt();
|
||||
for(i=0;i<size;i++){
|
||||
for(i=0;i<size;i++)
|
||||
{
|
||||
osg::StateAttribute* attribute = in->readStateAttribute();
|
||||
setTextureAttribute(unit, attribute, (osg::StateAttribute::OverrideValue)in->readInt());
|
||||
}
|
||||
@@ -177,7 +180,8 @@ void StateSet::read(DataInputStream* in){
|
||||
{
|
||||
// Read uniforms
|
||||
size = in->readInt();
|
||||
for(i=0;i<size;i++){
|
||||
for(i=0;i<size;i++)
|
||||
{
|
||||
osg::Uniform* uniform = in->readUniform();
|
||||
addUniform(uniform, (osg::StateAttribute::OverrideValue)in->readInt());
|
||||
}
|
||||
|
||||
@@ -30,6 +30,8 @@ void Uniform::write(DataOutputStream* out){
|
||||
else
|
||||
throw Exception("Uniform::write(): Could not cast this osg::Uniform to an osg::Object.");
|
||||
|
||||
out->writeInt(getType());
|
||||
out->writeString(getName());
|
||||
}
|
||||
|
||||
void Uniform::read(DataInputStream* in)
|
||||
@@ -54,4 +56,7 @@ void Uniform::read(DataInputStream* in)
|
||||
{
|
||||
throw Exception("Uniform::read(): Expected Uniform identification.");
|
||||
}
|
||||
|
||||
setType(static_cast<Type>(in->readInt()));
|
||||
setName(in->readString());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user