| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | #include "osg/Uniform" |
|---|
| 5 | #include "osg/io_utils" |
|---|
| 6 | #include "osg/Notify" |
|---|
| 7 | |
|---|
| 8 | #include "osgDB/Registry" |
|---|
| 9 | #include "osgDB/Input" |
|---|
| 10 | #include "osgDB/Output" |
|---|
| 11 | |
|---|
| 12 | #include "Matrix.h" |
|---|
| 13 | |
|---|
| 14 | using namespace osg; |
|---|
| 15 | using namespace osgDB; |
|---|
| 16 | using namespace std; |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | bool Array_writeLocalData(const Array& array,Output& fw); |
|---|
| 20 | Array* Array_readLocalData(Input& fr); |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | bool Uniform_readLocalData(Object& obj, Input& fr); |
|---|
| 24 | bool Uniform_writeLocalData(const Object& obj, Output& fw); |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | REGISTER_DOTOSGWRAPPER(Uniform) |
|---|
| 28 | ( |
|---|
| 29 | new osg::Uniform, |
|---|
| 30 | "Uniform", |
|---|
| 31 | "Object Uniform", |
|---|
| 32 | &Uniform_readLocalData, |
|---|
| 33 | &Uniform_writeLocalData |
|---|
| 34 | ); |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | bool Uniform_readLocalData(Object& obj, Input& fr) |
|---|
| 38 | { |
|---|
| 39 | bool iteratorAdvanced = false; |
|---|
| 40 | Uniform& uniform = static_cast<Uniform&>(obj); |
|---|
| 41 | |
|---|
| 42 | if (fr[0].matchWord("type")) |
|---|
| 43 | { |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | ++fr; |
|---|
| 47 | |
|---|
| 48 | if (fr.matchSequence("unsigned int")) |
|---|
| 49 | { |
|---|
| 50 | uniform.setType( Uniform::getTypeId( "unsigned int" ) ); |
|---|
| 51 | fr += 2; |
|---|
| 52 | } |
|---|
| 53 | else |
|---|
| 54 | { |
|---|
| 55 | uniform.setType( Uniform::getTypeId( fr[0].getStr() ) ); |
|---|
| 56 | ++fr; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | unsigned int numElements; |
|---|
| 60 | fr[0].getUInt(numElements); |
|---|
| 61 | uniform.setNumElements( numElements ); |
|---|
| 62 | |
|---|
| 63 | ++fr; |
|---|
| 64 | iteratorAdvanced = true; |
|---|
| 65 | |
|---|
| 66 | Array* data = Array_readLocalData(fr); |
|---|
| 67 | uniform.setArray( dynamic_cast<FloatArray*>(data) ); |
|---|
| 68 | uniform.setArray( dynamic_cast<IntArray*>(data) ); |
|---|
| 69 | uniform.setArray( dynamic_cast<UIntArray*>(data) ); |
|---|
| 70 | } |
|---|
| 71 | #if 1 //[ |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | else |
|---|
| 75 | { |
|---|
| 76 | |
|---|
| 77 | uniform.setType( Uniform::getTypeId(fr[0].getStr()) ); |
|---|
| 78 | fr+=1; |
|---|
| 79 | iteratorAdvanced = true; |
|---|
| 80 | |
|---|
| 81 | switch( Uniform::getGlApiType(uniform.getType()) ) |
|---|
| 82 | { |
|---|
| 83 | case(osg::Uniform::FLOAT): |
|---|
| 84 | { |
|---|
| 85 | float value; |
|---|
| 86 | if (fr[0].getFloat(value)) |
|---|
| 87 | { |
|---|
| 88 | uniform.set(value); |
|---|
| 89 | fr+=1; |
|---|
| 90 | iteratorAdvanced = true; |
|---|
| 91 | } |
|---|
| 92 | break; |
|---|
| 93 | } |
|---|
| 94 | case(osg::Uniform::FLOAT_VEC2): |
|---|
| 95 | { |
|---|
| 96 | osg::Vec2 value; |
|---|
| 97 | if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1])) |
|---|
| 98 | { |
|---|
| 99 | uniform.set(value); |
|---|
| 100 | fr+=2; |
|---|
| 101 | iteratorAdvanced = true; |
|---|
| 102 | } |
|---|
| 103 | break; |
|---|
| 104 | } |
|---|
| 105 | case(osg::Uniform::FLOAT_VEC3): |
|---|
| 106 | { |
|---|
| 107 | osg::Vec3 value; |
|---|
| 108 | if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]) && fr[2].getFloat(value[2])) |
|---|
| 109 | { |
|---|
| 110 | uniform.set(value); |
|---|
| 111 | fr+=3; |
|---|
| 112 | iteratorAdvanced = true; |
|---|
| 113 | } |
|---|
| 114 | break; |
|---|
| 115 | } |
|---|
| 116 | case(osg::Uniform::FLOAT_VEC4): |
|---|
| 117 | { |
|---|
| 118 | osg::Vec4 value; |
|---|
| 119 | if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]) && fr[2].getFloat(value[2]) && fr[3].getFloat(value[3])) |
|---|
| 120 | { |
|---|
| 121 | uniform.set(value); |
|---|
| 122 | fr+=4; |
|---|
| 123 | iteratorAdvanced = true; |
|---|
| 124 | } |
|---|
| 125 | break; |
|---|
| 126 | } |
|---|
| 127 | case(osg::Uniform::INT): |
|---|
| 128 | { |
|---|
| 129 | int value; |
|---|
| 130 | if (fr[0].getInt(value)) |
|---|
| 131 | { |
|---|
| 132 | uniform.set(value); |
|---|
| 133 | fr+=1; |
|---|
| 134 | iteratorAdvanced = true; |
|---|
| 135 | } |
|---|
| 136 | break; |
|---|
| 137 | } |
|---|
| 138 | case(osg::Uniform::INT_VEC2): |
|---|
| 139 | { |
|---|
| 140 | int value[2]; |
|---|
| 141 | if (fr[0].getInt(value[0]) && fr[1].getInt(value[1])) |
|---|
| 142 | { |
|---|
| 143 | uniform.set(value[0],value[1]); |
|---|
| 144 | fr+=2; |
|---|
| 145 | iteratorAdvanced = true; |
|---|
| 146 | } |
|---|
| 147 | break; |
|---|
| 148 | } |
|---|
| 149 | case(osg::Uniform::INT_VEC3): |
|---|
| 150 | { |
|---|
| 151 | int value[3]; |
|---|
| 152 | if (fr[0].getInt(value[0]) && fr[1].getInt(value[1]) && fr[2].getInt(value[2])) |
|---|
| 153 | { |
|---|
| 154 | uniform.set(value[0],value[1],value[2]); |
|---|
| 155 | fr+=3; |
|---|
| 156 | iteratorAdvanced = true; |
|---|
| 157 | } |
|---|
| 158 | break; |
|---|
| 159 | } |
|---|
| 160 | case(osg::Uniform::INT_VEC4): |
|---|
| 161 | { |
|---|
| 162 | int value[4]; |
|---|
| 163 | if (fr[0].getInt(value[0]) && fr[1].getInt(value[1]) && fr[2].getInt(value[2]) && fr[3].getInt(value[3])) |
|---|
| 164 | { |
|---|
| 165 | uniform.set(value[0],value[1],value[2],value[3]); |
|---|
| 166 | fr+=4; |
|---|
| 167 | iteratorAdvanced = true; |
|---|
| 168 | } |
|---|
| 169 | break; |
|---|
| 170 | } |
|---|
| 171 | case(osg::Uniform::FLOAT_MAT2): |
|---|
| 172 | { |
|---|
| 173 | osg::Matrix2 value; |
|---|
| 174 | if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]) && |
|---|
| 175 | fr[2].getFloat(value[2]) && fr[3].getFloat(value[3])) |
|---|
| 176 | { |
|---|
| 177 | uniform.set(value); |
|---|
| 178 | fr+=4; |
|---|
| 179 | iteratorAdvanced = true; |
|---|
| 180 | } |
|---|
| 181 | break; |
|---|
| 182 | } |
|---|
| 183 | case(osg::Uniform::FLOAT_MAT3): |
|---|
| 184 | { |
|---|
| 185 | osg::Matrix3 value; |
|---|
| 186 | if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]) && fr[2].getFloat(value[2]) && |
|---|
| 187 | fr[3].getFloat(value[3]) && fr[4].getFloat(value[4]) && fr[5].getFloat(value[5]) && |
|---|
| 188 | fr[6].getFloat(value[6]) && fr[7].getFloat(value[7]) && fr[8].getFloat(value[8])) |
|---|
| 189 | { |
|---|
| 190 | uniform.set(value); |
|---|
| 191 | fr+=9; |
|---|
| 192 | iteratorAdvanced = true; |
|---|
| 193 | } |
|---|
| 194 | break; |
|---|
| 195 | } |
|---|
| 196 | case(osg::Uniform::FLOAT_MAT4): |
|---|
| 197 | { |
|---|
| 198 | Matrix value; |
|---|
| 199 | if( readMatrix(value,fr) ) |
|---|
| 200 | { |
|---|
| 201 | uniform.set(value); |
|---|
| 202 | iteratorAdvanced = true; |
|---|
| 203 | } |
|---|
| 204 | break; |
|---|
| 205 | } |
|---|
| 206 | default: |
|---|
| 207 | break; |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | #endif //] |
|---|
| 211 | |
|---|
| 212 | static ref_ptr<Uniform::Callback> s_callback = new osg::Uniform::Callback; |
|---|
| 213 | while (fr.matchSequence("UpdateCallback {")) |
|---|
| 214 | { |
|---|
| 215 | |
|---|
| 216 | fr += 2; |
|---|
| 217 | Uniform::Callback* callback = dynamic_cast<Uniform::Callback*>(fr.readObjectOfType(*s_callback)); |
|---|
| 218 | if (callback) { |
|---|
| 219 | uniform.setUpdateCallback(callback); |
|---|
| 220 | } |
|---|
| 221 | iteratorAdvanced = true; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | while (fr.matchSequence("EventCallback {")) |
|---|
| 225 | { |
|---|
| 226 | |
|---|
| 227 | fr += 2; |
|---|
| 228 | Uniform::Callback* callback = dynamic_cast<Uniform::Callback*>(fr.readObjectOfType(*s_callback)); |
|---|
| 229 | if (callback) { |
|---|
| 230 | uniform.setEventCallback(callback); |
|---|
| 231 | } |
|---|
| 232 | iteratorAdvanced = true; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | return iteratorAdvanced; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | bool Uniform_writeLocalData(const Object& obj,Output& fw) |
|---|
| 239 | { |
|---|
| 240 | const Uniform& uniform = static_cast<const Uniform&>(obj); |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | fw.indent() << "type " |
|---|
| 244 | << Uniform::getTypename( uniform.getType() ) << " " |
|---|
| 245 | << uniform.getNumElements() << " "; |
|---|
| 246 | |
|---|
| 247 | if( uniform.getFloatArray() ) Array_writeLocalData( *uniform.getFloatArray(), fw ); |
|---|
| 248 | if( uniform.getIntArray() ) Array_writeLocalData( *uniform.getIntArray(), fw ); |
|---|
| 249 | if( uniform.getUIntArray() ) Array_writeLocalData( *uniform.getUIntArray(), fw ); |
|---|
| 250 | |
|---|
| 251 | if (uniform.getUpdateCallback()) |
|---|
| 252 | { |
|---|
| 253 | fw.indent() << "UpdateCallback {" << std::endl; |
|---|
| 254 | fw.moveIn(); |
|---|
| 255 | fw.writeObject(*uniform.getUpdateCallback()); |
|---|
| 256 | fw.moveOut(); |
|---|
| 257 | fw.indent() << "}" << std::endl; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | if (uniform.getEventCallback()) |
|---|
| 261 | { |
|---|
| 262 | fw.indent() << "EventCallback {" << std::endl; |
|---|
| 263 | fw.moveIn(); |
|---|
| 264 | fw.writeObject(*uniform.getEventCallback()); |
|---|
| 265 | fw.moveOut(); |
|---|
| 266 | fw.indent() << "}" << std::endl; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | return true; |
|---|
| 270 | } |
|---|