root/OpenSceneGraph/trunk/src/osgPlugins/ive/StateSet.cpp @ 13041

Revision 13041, 6.7 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/**********************************************************************
2 *
3 *    FILE:            StateSet.cpp
4 *
5 *    DESCRIPTION:    Read/Write osg::StateSet in binary format to disk.
6 *
7 *    CREATED BY:        Auto generated by iveGenerated
8 *                    and later modified by Rune Schmidt Jensen.
9 *
10 *    HISTORY:        Created 17.3.2003
11 *
12 *    Copyright 2003 VR-C
13 **********************************************************************/
14
15#include "Exception.h"
16#include "StateSet.h"
17#include "Object.h"
18
19#include <osg/StateAttribute>
20
21using namespace ive;
22
23void StateSet::write(DataOutputStream* out){
24    // Write StateSet's identification.
25    out->writeInt(IVESTATESET);
26    // If the osg class is inherited by any other class we should also write this to file.
27    osg::Object*  obj = dynamic_cast<osg::Object*>(this);
28    if(obj){
29         ((ive::Object*)(obj))->write(out);
30    }
31    else
32        out_THROW_EXCEPTION("StateSet::write(): Could not cast this osg::StateSet to an osg::Object.");
33
34    // Write render bin bin details.
35    switch(getRenderBinMode()){
36        case osg::StateSet::INHERIT_RENDERBIN_DETAILS:        out->writeChar((char)0);break;
37        case osg::StateSet::USE_RENDERBIN_DETAILS:            out->writeChar((char)1);break;
38        case osg::StateSet::OVERRIDE_RENDERBIN_DETAILS:        out->writeChar((char)2);break;
39        default: out_THROW_EXCEPTION("Unknown RenderBinMode in StateSet::write()");
40    }
41    out->writeInt(getBinNumber());
42    out->writeString(getBinName());
43
44
45    StateSet::ModeList ml = getModeList();
46    StateSet::AttributeList al = getAttributeList();
47
48    // Write stateset modes, this could for instance be GL_NORMALIZE, GL_LIGHT, etc.
49    out->writeInt(ml.size());
50    for(StateSet::ModeList::const_iterator mitr=ml.begin(); mitr!=ml.end(); ++mitr){
51        out->writeInt((unsigned int)mitr->first);
52        out->writeInt((unsigned int)mitr->second);
53    }
54
55    // Write stateset attributes, this could for instance be alphafunctions, materials, etc.
56    out->writeInt(al.size());
57    for(StateSet::AttributeList::iterator aitr=al.begin(); aitr!=al.end(); ++aitr){
58        out->writeStateAttribute(aitr->second.first.get());
59        out->writeInt(aitr->second.second);
60    }
61
62    StateSet::TextureModeList tml = getTextureModeList();
63    StateSet::TextureAttributeList tal = getTextureAttributeList();
64
65    // Write texture stateset modes, this could for instance be TEXGEN ON, TEXTURECUBEMAP ON, etc.
66    int nUnits = tml.size();
67    out->writeInt(nUnits);
68        int unit;
69    for(unit=0;unit<nUnits;unit++){
70        ml = tml[unit];
71        out->writeInt(ml.size());
72        for(StateSet::ModeList::const_iterator mitr=ml.begin(); mitr!=ml.end(); ++mitr){
73            out->writeInt((unsigned int)mitr->first);
74            out->writeInt((unsigned int)mitr->second);
75        }
76    }
77
78    // Write texture attributes, this could for instance texture2D, texturecubemap
79    nUnits = tal.size();
80    out->writeInt(nUnits);
81    for(unit=0;unit<nUnits;unit++){
82        al = tal[unit];
83        out->writeInt(al.size());
84        for(StateSet::AttributeList::iterator aitr=al.begin(); aitr!=al.end(); ++aitr){
85            out->writeStateAttribute(aitr->second.first.get());
86            out->writeInt(aitr->second.second);
87        }
88    }
89
90    if ( out->getVersion() >= VERSION_0010 )
91    {
92        // Write stateset uniforms
93        StateSet::UniformList ul = getUniformList();
94        out->writeInt(ul.size());
95        for(StateSet::UniformList::iterator uitr=ul.begin(); uitr!=ul.end(); ++uitr)
96        {
97            out->writeUniform(uitr->second.first.get());
98            out->writeInt(uitr->second.second);
99        }
100    }
101}
102
103void StateSet::read(DataInputStream* in){
104    // Read StateSet's identification.
105    int id = in->peekInt();
106    if(id == IVESTATESET){
107        // Code to read StateSet's properties.
108        id = in->readInt();
109
110        // If the osg class is inherited by any other class we should also read this from file.
111        osg::Object*  obj = dynamic_cast<osg::Object*>(this);
112        if(obj){
113            ((ive::Object*)(obj))->read(in);;
114        }
115        else
116            in_THROW_EXCEPTION("StateSet::read(): Could not cast this osg::StateSet to an osg::Object.");
117
118        // Read render bin details.
119        char c = in->readChar();
120        int num = in->readInt();
121        std::string name = in->readString();
122        switch((int)c){
123            case 0:
124                setRenderBinDetails(num, name, osg::StateSet::INHERIT_RENDERBIN_DETAILS);
125                break;
126            case 1:
127                setRenderBinDetails(num, name, osg::StateSet::USE_RENDERBIN_DETAILS);
128                break;
129            case 2:
130                setRenderBinDetails(num, name, osg::StateSet::OVERRIDE_RENDERBIN_DETAILS);
131                break;
132            case 3:
133                setRenderBinDetails(num, name, osg::StateSet::USE_RENDERBIN_DETAILS);
134                break;
135            default: in_THROW_EXCEPTION("Unknown RenderBinMode in StateSet::read()");
136        }
137
138        // Read stateset modes.
139        int size = in->readInt();
140        int i;
141        for(i=0;i<size;i++){
142            int mode = in->readInt();
143            int value = in->readInt();
144            setMode((osg::StateAttribute::GLMode)mode, (osg::StateAttribute::GLModeValue)value);
145        }
146
147        // Read state attributes
148        size = in->readInt();
149        for(i=0;i<size;i++){
150            osg::StateAttribute* attribute = in->readStateAttribute();
151            setAttribute(attribute, (osg::StateAttribute::OverrideValue)in->readInt());
152        }
153
154        // Read texture stateset mode.
155        int nUnits = in->readInt();
156        int unit;
157        for(unit=0;unit<nUnits;unit++){
158            size = in->readInt();
159            for(i=0;i<size;i++){
160                int mode = in->readInt();
161                int value = in->readInt();
162                setTextureMode(unit,(osg::StateAttribute::GLMode)mode, (osg::StateAttribute::GLModeValue)value);
163            }
164        }
165
166        // Read texture attributes.
167        nUnits = in->readInt();
168        for(unit=0;unit<nUnits;unit++)
169        {
170            size = in->readInt();
171            for(i=0;i<size;i++)
172            {
173                osg::StateAttribute* attribute = in->readStateAttribute();
174                setTextureAttribute(unit, attribute, (osg::StateAttribute::OverrideValue)in->readInt());
175            }
176        }
177
178        if ( in->getVersion() >= VERSION_0010 )
179        {
180            // Read uniforms
181            size = in->readInt();
182            for(i=0;i<size;i++)
183            {
184                osg::Uniform* uniform = in->readUniform();
185                addUniform(uniform, (osg::StateAttribute::OverrideValue)in->readInt());
186            }
187        }
188    }
189    else{
190        in_THROW_EXCEPTION("StateSet::read(): Expected StateSet identification");
191    }
192}
Note: See TracBrowser for help on using the browser.