| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include <osg/MatrixTransform> |
|---|
| 21 | #include <osg/Texture2D> |
|---|
| 22 | #include <osg/TexEnv> |
|---|
| 23 | |
|---|
| 24 | #include "Registry.h" |
|---|
| 25 | #include "Document.h" |
|---|
| 26 | #include "RecordInputStream.h" |
|---|
| 27 | |
|---|
| 28 | namespace flt { |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | class Comment : public Record |
|---|
| 33 | { |
|---|
| 34 | public: |
|---|
| 35 | |
|---|
| 36 | Comment() {} |
|---|
| 37 | |
|---|
| 38 | META_Record(Comment) |
|---|
| 39 | |
|---|
| 40 | protected: |
|---|
| 41 | |
|---|
| 42 | virtual ~Comment() {} |
|---|
| 43 | |
|---|
| 44 | virtual void readRecord(RecordInputStream& in, Document& ) |
|---|
| 45 | { |
|---|
| 46 | std::streamsize size = in.getRecordSize(); |
|---|
| 47 | std::string commentfield = in.readString(size-4); |
|---|
| 48 | |
|---|
| 49 | if (_parent.valid()) |
|---|
| 50 | { |
|---|
| 51 | #if 0 |
|---|
| 52 | _parent->setComment(commentfield); |
|---|
| 53 | #else |
|---|
| 54 | unsigned int front_of_line = 0; |
|---|
| 55 | unsigned int end_of_line = 0; |
|---|
| 56 | while (end_of_line<commentfield.size()) |
|---|
| 57 | { |
|---|
| 58 | if (commentfield[end_of_line]=='\r') |
|---|
| 59 | { |
|---|
| 60 | _parent->setComment( std::string( commentfield, front_of_line, end_of_line-front_of_line) ); |
|---|
| 61 | |
|---|
| 62 | if (end_of_line+1<commentfield.size() && |
|---|
| 63 | commentfield[end_of_line+1]=='\n') ++end_of_line; |
|---|
| 64 | |
|---|
| 65 | ++end_of_line; |
|---|
| 66 | front_of_line = end_of_line; |
|---|
| 67 | } |
|---|
| 68 | else if (commentfield[end_of_line]=='\n') |
|---|
| 69 | { |
|---|
| 70 | _parent->setComment( std::string( commentfield, front_of_line, end_of_line-front_of_line) ); |
|---|
| 71 | ++end_of_line; |
|---|
| 72 | front_of_line = end_of_line; |
|---|
| 73 | } |
|---|
| 74 | else ++end_of_line; |
|---|
| 75 | } |
|---|
| 76 | if (front_of_line<end_of_line) |
|---|
| 77 | { |
|---|
| 78 | _parent->setComment( std::string( commentfield, front_of_line, end_of_line-front_of_line) ); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | } |
|---|
| 82 | #endif |
|---|
| 83 | |
|---|
| 84 | } |
|---|
| 85 | }; |
|---|
| 86 | |
|---|
| 87 | RegisterRecordProxy<Comment> g_Comment(COMMENT_OP); |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | class LongID : public Record |
|---|
| 93 | { |
|---|
| 94 | public: |
|---|
| 95 | |
|---|
| 96 | LongID() {} |
|---|
| 97 | |
|---|
| 98 | META_Record(LongID) |
|---|
| 99 | |
|---|
| 100 | protected: |
|---|
| 101 | |
|---|
| 102 | virtual ~LongID() {} |
|---|
| 103 | |
|---|
| 104 | virtual void readRecord(RecordInputStream& in, Document& ) |
|---|
| 105 | { |
|---|
| 106 | std::streamsize size = in.getRecordSize(); |
|---|
| 107 | std::string id = in.readString(size-4); |
|---|
| 108 | |
|---|
| 109 | if (_parent.valid()) |
|---|
| 110 | _parent->setID(id); |
|---|
| 111 | } |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | RegisterRecordProxy<LongID> g_LongID(LONG_ID_OP); |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | class Matrix : public Record |
|---|
| 120 | { |
|---|
| 121 | public: |
|---|
| 122 | |
|---|
| 123 | Matrix() {} |
|---|
| 124 | |
|---|
| 125 | META_Record(Matrix) |
|---|
| 126 | |
|---|
| 127 | protected: |
|---|
| 128 | |
|---|
| 129 | virtual ~Matrix() {} |
|---|
| 130 | |
|---|
| 131 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 132 | { |
|---|
| 133 | osg::Matrix matrix; |
|---|
| 134 | for (int i=0; i<4; ++i) |
|---|
| 135 | { |
|---|
| 136 | for (int j=0; j<4; ++j) |
|---|
| 137 | { |
|---|
| 138 | matrix(i,j) = in.readFloat32(); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | osg::Vec3 pos = matrix.getTrans(); |
|---|
| 144 | matrix *= osg::Matrix::translate(-pos); |
|---|
| 145 | pos *= (float)document.unitScale(); |
|---|
| 146 | matrix *= osg::Matrix::translate(pos); |
|---|
| 147 | |
|---|
| 148 | if (_parent.valid()) |
|---|
| 149 | _parent->setMatrix(matrix); |
|---|
| 150 | } |
|---|
| 151 | }; |
|---|
| 152 | |
|---|
| 153 | RegisterRecordProxy<Matrix> g_Matrix(MATRIX_OP); |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | class Multitexture : public Record |
|---|
| 159 | { |
|---|
| 160 | public: |
|---|
| 161 | |
|---|
| 162 | Multitexture() {} |
|---|
| 163 | |
|---|
| 164 | META_Record(Multitexture) |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | enum EffectMode |
|---|
| 168 | { |
|---|
| 169 | TEXTURE_ENVIRONMENT = 0, |
|---|
| 170 | BUMP_MAP = 1 |
|---|
| 171 | }; |
|---|
| 172 | |
|---|
| 173 | protected: |
|---|
| 174 | |
|---|
| 175 | virtual ~Multitexture() {} |
|---|
| 176 | |
|---|
| 177 | virtual void readRecord(RecordInputStream& in, Document& document) |
|---|
| 178 | { |
|---|
| 179 | osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet; |
|---|
| 180 | |
|---|
| 181 | uint32 mask = in.readUInt32(); |
|---|
| 182 | for (int layer=1; layer<8; layer++) |
|---|
| 183 | { |
|---|
| 184 | uint32 layerBit = 0x80000000u >> (layer-1); |
|---|
| 185 | if (mask & layerBit) |
|---|
| 186 | { |
|---|
| 187 | int16 textureIndex = in.readInt16(); |
|---|
| 188 | int16 effect = in.readInt16(); |
|---|
| 189 | in.readInt16(); |
|---|
| 190 | in.readUInt16(); |
|---|
| 191 | |
|---|
| 192 | osg::ref_ptr<osg::StateSet> texturePoolStateset = document.getOrCreateTexturePool()->get(textureIndex); |
|---|
| 193 | if (stateset.valid() && texturePoolStateset.valid()) |
|---|
| 194 | { |
|---|
| 195 | |
|---|
| 196 | osg::Texture* texture = dynamic_cast<osg::Texture*>(texturePoolStateset->getTextureAttribute(0,osg::StateAttribute::TEXTURE)); |
|---|
| 197 | if (texture) |
|---|
| 198 | stateset->setTextureAttributeAndModes(layer,texture,osg::StateAttribute::ON); |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | switch (effect) |
|---|
| 202 | { |
|---|
| 203 | case TEXTURE_ENVIRONMENT: |
|---|
| 204 | { |
|---|
| 205 | |
|---|
| 206 | osg::TexEnv* texenv = dynamic_cast<osg::TexEnv*>(texturePoolStateset->getTextureAttribute(0,osg::StateAttribute::TEXENV)); |
|---|
| 207 | if (texenv) |
|---|
| 208 | stateset->setTextureAttribute(layer,texenv); |
|---|
| 209 | } |
|---|
| 210 | break; |
|---|
| 211 | case BUMP_MAP: |
|---|
| 212 | { |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | } |
|---|
| 220 | break; |
|---|
| 221 | } |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | if (_parent.valid()) |
|---|
| 227 | _parent->setMultitexture(*stateset); |
|---|
| 228 | } |
|---|
| 229 | }; |
|---|
| 230 | |
|---|
| 231 | RegisterRecordProxy<Multitexture> g_Multitexture(MULTITEXTURE_OP); |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | class UVList : public Record |
|---|
| 238 | { |
|---|
| 239 | public: |
|---|
| 240 | |
|---|
| 241 | UVList() {} |
|---|
| 242 | |
|---|
| 243 | META_Record(UVList) |
|---|
| 244 | |
|---|
| 245 | protected: |
|---|
| 246 | |
|---|
| 247 | virtual ~UVList() {} |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | int bitCount(uint32 mask) |
|---|
| 251 | { |
|---|
| 252 | int count = 0; |
|---|
| 253 | while (mask) |
|---|
| 254 | { |
|---|
| 255 | if (mask & 0x0001) |
|---|
| 256 | ++count; |
|---|
| 257 | mask >>= 1; |
|---|
| 258 | } |
|---|
| 259 | return count; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | virtual void readRecord(RecordInputStream& in, Document& ) |
|---|
| 263 | { |
|---|
| 264 | uint32 mask = in.readUInt32(0); |
|---|
| 265 | |
|---|
| 266 | int numLayers = bitCount(mask); |
|---|
| 267 | int numVertices = (in.getRecordSize()-8) / (8 * numLayers); |
|---|
| 268 | for (int n=0; n < numVertices; ++n) |
|---|
| 269 | { |
|---|
| 270 | for (unsigned int layer=1; layer<8; layer++) |
|---|
| 271 | { |
|---|
| 272 | uint32 layerBit = 0x80000000u >> (layer-1); |
|---|
| 273 | if (mask & layerBit) |
|---|
| 274 | { |
|---|
| 275 | float32 u = in.readFloat32(); |
|---|
| 276 | float32 v = in.readFloat32(); |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | if (_parent.valid()) |
|---|
| 280 | _parent->addVertexUV(layer,osg::Vec2(u,v)); |
|---|
| 281 | } |
|---|
| 282 | } |
|---|
| 283 | } |
|---|
| 284 | } |
|---|
| 285 | }; |
|---|
| 286 | |
|---|
| 287 | RegisterRecordProxy<UVList> g_UVList(UV_LIST_OP); |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | class Replicate : public Record |
|---|
| 293 | { |
|---|
| 294 | public: |
|---|
| 295 | |
|---|
| 296 | Replicate() {} |
|---|
| 297 | |
|---|
| 298 | META_Record(Replicate) |
|---|
| 299 | |
|---|
| 300 | protected: |
|---|
| 301 | |
|---|
| 302 | virtual ~Replicate() {} |
|---|
| 303 | |
|---|
| 304 | virtual void readRecord(RecordInputStream& in, Document& ) |
|---|
| 305 | { |
|---|
| 306 | int16 replicate = in.readInt16(); |
|---|
| 307 | |
|---|
| 308 | if (_parent.valid()) |
|---|
| 309 | _parent->setNumberOfReplications((int)replicate); |
|---|
| 310 | } |
|---|
| 311 | }; |
|---|
| 312 | |
|---|
| 313 | RegisterRecordProxy<Replicate> g_Replicate(REPLICATE_OP); |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | RegisterRecordProxy<DummyRecord> g_OldTranslate(OLD_TRANSLATE2_OP); |
|---|
| 318 | RegisterRecordProxy<DummyRecord> g_OldRotateAboutPoint(OLD_ROTATE_ABOUT_POINT_OP); |
|---|
| 319 | RegisterRecordProxy<DummyRecord> g_OldRotateAboutEdge(OLD_ROTATE_ABOUT_EDGE_OP); |
|---|
| 320 | RegisterRecordProxy<DummyRecord> g_OldScale(OLD_SCALE_OP); |
|---|
| 321 | RegisterRecordProxy<DummyRecord> g_OldTranslate2(OLD_TRANSLATE_OP); |
|---|
| 322 | RegisterRecordProxy<DummyRecord> g_OldNonuniformScale(OLD_NONUNIFORM_SCALE_OP); |
|---|
| 323 | RegisterRecordProxy<DummyRecord> g_OldRotateAboutPoint2(OLD_ROTATE_ABOUT_POINT2_OP); |
|---|
| 324 | RegisterRecordProxy<DummyRecord> g_OldRotateScaleToPoint(OLD_ROTATE_SCALE_TO_POINT_OP); |
|---|
| 325 | RegisterRecordProxy<DummyRecord> g_OldPutTransform(OLD_PUT_TRANSFORM_OP); |
|---|
| 326 | RegisterRecordProxy<DummyRecord> g_OldBoundingBox(OLD_BOUNDING_BOX_OP); |
|---|
| 327 | RegisterRecordProxy<DummyRecord> g_IndexedString(INDEXED_STRING_OP); |
|---|
| 328 | RegisterRecordProxy<DummyRecord> g_RoadZone(ROAD_ZONE_OP); |
|---|
| 329 | RegisterRecordProxy<DummyRecord> g_RotateAboutEdge(ROTATE_ABOUT_EDGE_OP); |
|---|
| 330 | RegisterRecordProxy<DummyRecord> g_Translate(TRANSLATE_OP); |
|---|
| 331 | RegisterRecordProxy<DummyRecord> g_Scale(NONUNIFORM_SCALE_OP); |
|---|
| 332 | RegisterRecordProxy<DummyRecord> g_RotateAboutPoint(ROTATE_ABOUT_POINT_OP); |
|---|
| 333 | RegisterRecordProxy<DummyRecord> g_RotateScaleToPoint(ROTATE_SCALE_TO_POINT_OP); |
|---|
| 334 | RegisterRecordProxy<DummyRecord> g_PutTransform(PUT_TRANSFORM_OP); |
|---|
| 335 | RegisterRecordProxy<DummyRecord> g_GeneralMatrix(GENERAL_MATRIX_OP); |
|---|
| 336 | RegisterRecordProxy<DummyRecord> g_Vector(VECTOR_OP); |
|---|
| 337 | RegisterRecordProxy<DummyRecord> g_BoundingBox(BOUNDING_BOX_OP); |
|---|
| 338 | RegisterRecordProxy<DummyRecord> g_BoundingSphere(BOUNDING_SPHERE_OP); |
|---|
| 339 | RegisterRecordProxy<DummyRecord> g_BoundingCylinder(BOUNDING_CYLINDER_OP); |
|---|
| 340 | RegisterRecordProxy<DummyRecord> g_BoundingConvexHull(BOUNDING_CONVEX_HULL_OP); |
|---|
| 341 | RegisterRecordProxy<DummyRecord> g_BoundingHistogram(BOUNDING_HISTOGRAM); |
|---|
| 342 | RegisterRecordProxy<DummyRecord> g_BoundingVolumeCenter(BOUNDING_VOLUME_CENTER_OP); |
|---|
| 343 | RegisterRecordProxy<DummyRecord> g_BoundingVolumeOrientation(BOUNDING_VOLUME_ORIENTATION_OP); |
|---|
| 344 | RegisterRecordProxy<DummyRecord> g_HistogramBoundingVolume(HISTOGRAM_BOUNDING_VOLUME_OP); |
|---|
| 345 | |
|---|
| 346 | } |
|---|