| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include "Exception.h" |
|---|
| 16 | #include "Uniform.h" |
|---|
| 17 | #include "Object.h" |
|---|
| 18 | #include <osg/Notify> |
|---|
| 19 | |
|---|
| 20 | using namespace ive; |
|---|
| 21 | |
|---|
| 22 | void Uniform::write(DataOutputStream* out){ |
|---|
| 23 | |
|---|
| 24 | out->writeInt(IVEUNIFORM); |
|---|
| 25 | |
|---|
| 26 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 27 | if(obj) |
|---|
| 28 | { |
|---|
| 29 | ((ive::Object*)(obj))->write(out); |
|---|
| 30 | } |
|---|
| 31 | else |
|---|
| 32 | out_THROW_EXCEPTION("Uniform::write(): Could not cast this osg::Uniform to an osg::Object."); |
|---|
| 33 | |
|---|
| 34 | out->writeInt(getType()); |
|---|
| 35 | |
|---|
| 36 | if ( out->getVersion() < VERSION_0012 ) |
|---|
| 37 | { |
|---|
| 38 | out->writeString(getName()); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | if ( out->getVersion() >= VERSION_0016 ) |
|---|
| 42 | { |
|---|
| 43 | out->writeUInt(getNumElements()); |
|---|
| 44 | |
|---|
| 45 | if( getFloatArray() ) out->writeArray( getFloatArray() ); |
|---|
| 46 | if( getIntArray() ) out->writeArray( getIntArray() ); |
|---|
| 47 | } |
|---|
| 48 | else |
|---|
| 49 | { |
|---|
| 50 | switch( Uniform::getGlApiType(getType()) ) |
|---|
| 51 | { |
|---|
| 52 | case(osg::Uniform::FLOAT): |
|---|
| 53 | { |
|---|
| 54 | float value; |
|---|
| 55 | get(value); |
|---|
| 56 | out->writeFloat(value); |
|---|
| 57 | break; |
|---|
| 58 | } |
|---|
| 59 | case(osg::Uniform::FLOAT_VEC2): |
|---|
| 60 | { |
|---|
| 61 | osg::Vec2 value; |
|---|
| 62 | get(value); |
|---|
| 63 | out->writeVec2(value); |
|---|
| 64 | break; |
|---|
| 65 | } |
|---|
| 66 | case(osg::Uniform::FLOAT_VEC3): |
|---|
| 67 | { |
|---|
| 68 | osg::Vec3 value; |
|---|
| 69 | get(value); |
|---|
| 70 | out->writeVec3(value); |
|---|
| 71 | break; |
|---|
| 72 | } |
|---|
| 73 | case(osg::Uniform::FLOAT_VEC4): |
|---|
| 74 | { |
|---|
| 75 | osg::Vec4 value; |
|---|
| 76 | get(value); |
|---|
| 77 | out->writeVec4(value); |
|---|
| 78 | break; |
|---|
| 79 | } |
|---|
| 80 | case(osg::Uniform::INT): |
|---|
| 81 | { |
|---|
| 82 | int i0; |
|---|
| 83 | get(i0); |
|---|
| 84 | out->writeInt(i0); |
|---|
| 85 | break; |
|---|
| 86 | } |
|---|
| 87 | case(osg::Uniform::INT_VEC2): |
|---|
| 88 | { |
|---|
| 89 | int i0, i1; |
|---|
| 90 | get(i0, i1); |
|---|
| 91 | out->writeInt(i0); |
|---|
| 92 | out->writeInt(i1); |
|---|
| 93 | break; |
|---|
| 94 | } |
|---|
| 95 | case(osg::Uniform::INT_VEC3): |
|---|
| 96 | { |
|---|
| 97 | int i0, i1, i2; |
|---|
| 98 | get(i0, i1, i2); |
|---|
| 99 | out->writeInt(i0); |
|---|
| 100 | out->writeInt(i1); |
|---|
| 101 | out->writeInt(i2); |
|---|
| 102 | break; |
|---|
| 103 | } |
|---|
| 104 | case(osg::Uniform::INT_VEC4): |
|---|
| 105 | { |
|---|
| 106 | int i0, i1, i2, i3; |
|---|
| 107 | get(i0, i1, i2, i3); |
|---|
| 108 | out->writeInt(i0); |
|---|
| 109 | out->writeInt(i1); |
|---|
| 110 | out->writeInt(i2); |
|---|
| 111 | out->writeInt(i3); |
|---|
| 112 | break; |
|---|
| 113 | } |
|---|
| 114 | case(osg::Uniform::FLOAT_MAT2): |
|---|
| 115 | { |
|---|
| 116 | osg::Matrix2 m2; |
|---|
| 117 | get(m2); |
|---|
| 118 | for(int i=0; i<4; ++i) out->writeFloat(m2[i]); |
|---|
| 119 | break; |
|---|
| 120 | } |
|---|
| 121 | case(osg::Uniform::FLOAT_MAT3): |
|---|
| 122 | { |
|---|
| 123 | osg::Matrix3 m3; |
|---|
| 124 | get(m3); |
|---|
| 125 | for(int i=0; i<9; ++i) out->writeFloat(m3[i]); |
|---|
| 126 | break; |
|---|
| 127 | } |
|---|
| 128 | case(osg::Uniform::FLOAT_MAT4): |
|---|
| 129 | { |
|---|
| 130 | osg::Matrixf matrix; |
|---|
| 131 | get(matrix); |
|---|
| 132 | out->writeMatrixf(matrix); |
|---|
| 133 | break; |
|---|
| 134 | } |
|---|
| 135 | default: |
|---|
| 136 | { |
|---|
| 137 | OSG_WARN<<"Warning : uniform "<<getType()<<"type not supported for writing."<<std::endl; |
|---|
| 138 | break; |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | void Uniform::read(DataInputStream* in) |
|---|
| 145 | { |
|---|
| 146 | |
|---|
| 147 | int id = in->peekInt(); |
|---|
| 148 | if(id == IVEUNIFORM) |
|---|
| 149 | { |
|---|
| 150 | |
|---|
| 151 | id = in->readInt(); |
|---|
| 152 | |
|---|
| 153 | osg::Object* obj = dynamic_cast<osg::Object*>(this); |
|---|
| 154 | if(obj) |
|---|
| 155 | { |
|---|
| 156 | ((ive::Object*)(obj))->read(in); |
|---|
| 157 | } |
|---|
| 158 | else |
|---|
| 159 | in_THROW_EXCEPTION("Uniform::read(): Could not cast this osg::Uniform to an osg::Object."); |
|---|
| 160 | |
|---|
| 161 | } |
|---|
| 162 | else |
|---|
| 163 | { |
|---|
| 164 | in_THROW_EXCEPTION("Uniform::read(): Expected Uniform identification."); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | setType(static_cast<Type>(in->readInt())); |
|---|
| 168 | |
|---|
| 169 | if ( in->getVersion() < VERSION_0012 ) |
|---|
| 170 | { |
|---|
| 171 | setName(in->readString()); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | if ( in->getVersion() >= VERSION_0016 ) |
|---|
| 175 | { |
|---|
| 176 | setNumElements( in->readUInt() ); |
|---|
| 177 | |
|---|
| 178 | osg::Array* data = in->readArray(); |
|---|
| 179 | setArray( dynamic_cast<osg::FloatArray*>(data) ); |
|---|
| 180 | setArray( dynamic_cast<osg::IntArray*>(data) ); |
|---|
| 181 | } |
|---|
| 182 | else |
|---|
| 183 | { |
|---|
| 184 | switch( Uniform::getGlApiType(getType()) ) |
|---|
| 185 | { |
|---|
| 186 | case(osg::Uniform::FLOAT): |
|---|
| 187 | { |
|---|
| 188 | set(in->readFloat()); |
|---|
| 189 | break; |
|---|
| 190 | } |
|---|
| 191 | case(osg::Uniform::FLOAT_VEC2): |
|---|
| 192 | { |
|---|
| 193 | set(in->readVec2()); |
|---|
| 194 | break; |
|---|
| 195 | } |
|---|
| 196 | case(osg::Uniform::FLOAT_VEC3): |
|---|
| 197 | { |
|---|
| 198 | set(in->readVec3()); |
|---|
| 199 | break; |
|---|
| 200 | } |
|---|
| 201 | case(osg::Uniform::FLOAT_VEC4): |
|---|
| 202 | { |
|---|
| 203 | set(in->readVec4()); |
|---|
| 204 | break; |
|---|
| 205 | } |
|---|
| 206 | case(osg::Uniform::INT): |
|---|
| 207 | { |
|---|
| 208 | set(in->readInt()); |
|---|
| 209 | break; |
|---|
| 210 | } |
|---|
| 211 | case(osg::Uniform::INT_VEC2): |
|---|
| 212 | { |
|---|
| 213 | int i0 = in->readInt(); |
|---|
| 214 | int i1 = in->readInt(); |
|---|
| 215 | set(i0,i1); |
|---|
| 216 | break; |
|---|
| 217 | } |
|---|
| 218 | case(osg::Uniform::INT_VEC3): |
|---|
| 219 | { |
|---|
| 220 | int i0 = in->readInt(); |
|---|
| 221 | int i1 = in->readInt(); |
|---|
| 222 | int i2 = in->readInt(); |
|---|
| 223 | set(i0,i1,i2); |
|---|
| 224 | break; |
|---|
| 225 | } |
|---|
| 226 | case(osg::Uniform::INT_VEC4): |
|---|
| 227 | { |
|---|
| 228 | int i0 = in->readInt(); |
|---|
| 229 | int i1 = in->readInt(); |
|---|
| 230 | int i2 = in->readInt(); |
|---|
| 231 | int i3 = in->readInt(); |
|---|
| 232 | set(i0,i1,i2,i3); |
|---|
| 233 | break; |
|---|
| 234 | } |
|---|
| 235 | case(osg::Uniform::FLOAT_MAT2): |
|---|
| 236 | { |
|---|
| 237 | osg::Matrix2 m2; |
|---|
| 238 | for(int i=0; i<9; ++i) m2[i]=in->readFloat(); |
|---|
| 239 | set(m2); |
|---|
| 240 | break; |
|---|
| 241 | } |
|---|
| 242 | case(osg::Uniform::FLOAT_MAT3): |
|---|
| 243 | { |
|---|
| 244 | osg::Matrix3 m3; |
|---|
| 245 | for(int i=0; i<9; ++i) m3[i]=in->readFloat(); |
|---|
| 246 | set(m3); |
|---|
| 247 | break; |
|---|
| 248 | } |
|---|
| 249 | case(osg::Uniform::FLOAT_MAT4): |
|---|
| 250 | { |
|---|
| 251 | set( in->readMatrixf() ); |
|---|
| 252 | break; |
|---|
| 253 | } |
|---|
| 254 | default: |
|---|
| 255 | { |
|---|
| 256 | OSG_WARN<<"Warning : uniform "<<getType()<<"type not supported for reading."<<std::endl; |
|---|
| 257 | break; |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | } |
|---|