| [10932] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | #include <osg/io_utils> |
|---|
| 17 | #include <osg/CullFace> |
|---|
| 18 | #include <osgDB/WriteFile> |
|---|
| 19 | |
|---|
| 20 | #include "WriterNodeVisitor.h" |
|---|
| 21 | #include <assert.h> |
|---|
| 22 | #include <string.h> |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | void copyOsgMatrixToLib3dsMatrix(Lib3dsMatrix lib3ds_matrix, const osg::Matrix& osg_matrix) |
|---|
| 26 | { |
|---|
| 27 | for(int row=0; row<4; ++row) { |
|---|
| 28 | lib3ds_matrix[row][0] = osg_matrix.ptr()[row*4+0]; |
|---|
| 29 | lib3ds_matrix[row][1] = osg_matrix.ptr()[row*4+1]; |
|---|
| 30 | lib3ds_matrix[row][2] = osg_matrix.ptr()[row*4+2]; |
|---|
| 31 | lib3ds_matrix[row][3] = osg_matrix.ptr()[row*4+3]; |
|---|
| 32 | } |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | inline void copyOsgVectorToLib3dsVector(Lib3dsVector lib3ds_vector, const osg::Vec3f& osg_vector) { |
|---|
| 36 | lib3ds_vector[0] = osg_vector[0]; |
|---|
| 37 | lib3ds_vector[1] = osg_vector[1]; |
|---|
| 38 | lib3ds_vector[2] = osg_vector[2]; |
|---|
| 39 | } |
|---|
| 40 | inline void copyOsgVectorToLib3dsVector(Lib3dsVector lib3ds_vector, const osg::Vec3d& osg_vector) { |
|---|
| 41 | lib3ds_vector[0] = osg_vector[0]; |
|---|
| 42 | lib3ds_vector[1] = osg_vector[1]; |
|---|
| 43 | lib3ds_vector[2] = osg_vector[2]; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | inline void copyOsgColorToLib3dsColor(Lib3dsVector lib3ds_vector, const osg::Vec4f& osg_vector) { |
|---|
| 47 | lib3ds_vector[0] = osg_vector[0]; |
|---|
| 48 | lib3ds_vector[1] = osg_vector[1]; |
|---|
| 49 | lib3ds_vector[2] = osg_vector[2]; |
|---|
| 50 | } |
|---|
| 51 | inline void copyOsgColorToLib3dsColor(Lib3dsVector lib3ds_vector, const osg::Vec4d& osg_vector) { |
|---|
| 52 | lib3ds_vector[0] = osg_vector[0]; |
|---|
| 53 | lib3ds_vector[1] = osg_vector[1]; |
|---|
| 54 | lib3ds_vector[2] = osg_vector[2]; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | inline void copyOsgQuatToLib3dsQuat(float lib3ds_vector[4], const osg::Quat& osg_quat) { |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | osg::Quat::value_type angle, x, y, z; |
|---|
| 64 | osg_quat.getRotate(angle, x, y, z); |
|---|
| 65 | lib3ds_vector[0] = static_cast<float>(x); |
|---|
| 66 | lib3ds_vector[1] = static_cast<float>(y); |
|---|
| 67 | lib3ds_vector[2] = static_cast<float>(z); |
|---|
| 68 | lib3ds_vector[3] = static_cast<float>(-angle); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | std::string getFileName(const std::string & path) { |
|---|
| 72 | unsigned int slashPos = path.find_last_of("/\\"); |
|---|
| 73 | if (slashPos == std::string::npos) return path; |
|---|
| 74 | return path.substr(slashPos+1); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | bool is83(const std::string & s) { |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | if (s.find_first_of("/\\") != std::string::npos) return false; |
|---|
| 83 | unsigned int len = s.length(); |
|---|
| 84 | if (len > 12 || len == 0) return false; |
|---|
| 85 | unsigned int pointPos = s.rfind('.'); |
|---|
| 86 | if (pointPos == std::string::npos) return len <= 8; |
|---|
| 87 | |
|---|
| 88 | if (pointPos > 8) return false; |
|---|
| 89 | if (len-1 - pointPos > 3) return false; |
|---|
| 90 | return true; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | |
|---|
| [11056] | 94 | bool is3DSpath(const std::string & s, bool extendedFilePaths) { |
|---|
| [10932] | 95 | unsigned int len = s.length(); |
|---|
| 96 | if (len >= 64 || len == 0) return false; |
|---|
| [11056] | 97 | if (extendedFilePaths) return true; |
|---|
| [10932] | 98 | |
|---|
| 99 | unsigned int tokenBegin = 0; |
|---|
| 100 | for (unsigned int tokenEnd=0; tokenEnd != std::string::npos; tokenBegin = tokenEnd+1) { |
|---|
| 101 | tokenEnd = s.find_first_of("/\\", tokenBegin); |
|---|
| 102 | if ( !is83(s.substr(tokenBegin, tokenEnd-tokenBegin-1)) ) return false; |
|---|
| 103 | } |
|---|
| 104 | return true; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | class PrimitiveIndexWriter : public osg::PrimitiveIndexFunctor { |
|---|
| 111 | public: |
|---|
| 112 | PrimitiveIndexWriter(osg::Geometry * geo, |
|---|
| 113 | ListTriangle & listTriangles, |
|---|
| 114 | unsigned int drawable_n, |
|---|
| 115 | unsigned int material) : |
|---|
| 116 | osg::PrimitiveIndexFunctor(), |
|---|
| 117 | _drawable_n(drawable_n), |
|---|
| 118 | _listTriangles(listTriangles), |
|---|
| 119 | _hasNormalCoords(geo->getNormalArray() != NULL), |
|---|
| 120 | _hasTexCoords(geo->getTexCoordArray(0) != NULL), |
|---|
| 121 | _geo(geo), |
|---|
| 122 | _lastFaceIndex(0), |
|---|
| 123 | _material(material) |
|---|
| 124 | { |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | unsigned int getNextFaceIndex() { return _lastFaceIndex; } |
|---|
| 128 | |
|---|
| 129 | virtual void setVertexArray(unsigned int,const osg::Vec2*) {} |
|---|
| 130 | |
|---|
| 131 | virtual void setVertexArray(unsigned int count,const osg::Vec3* vecs) {} |
|---|
| 132 | |
|---|
| 133 | virtual void setVertexArray(unsigned int,const osg::Vec4* ) {} |
|---|
| 134 | |
|---|
| 135 | virtual void setVertexArray(unsigned int,const osg::Vec2d*) {} |
|---|
| 136 | |
|---|
| 137 | virtual void setVertexArray(unsigned int ,const osg::Vec3d* ) {} |
|---|
| 138 | virtual void setVertexArray(unsigned int,const osg::Vec4d* ) {} |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | void writeTriangle(unsigned int i1, unsigned int i2, unsigned int i3) |
|---|
| 143 | { |
|---|
| 144 | Triangle triangle; |
|---|
| 145 | triangle.t1 = i1; |
|---|
| 146 | triangle.t2 = i2; |
|---|
| 147 | triangle.t3 = i3; |
|---|
| 148 | triangle.material = _material; |
|---|
| [11056] | 149 | _listTriangles.push_back(std::pair<Triangle, unsigned int>(triangle, _drawable_n)); |
|---|
| [10932] | 150 | } |
|---|
| 151 | virtual void begin(GLenum mode) |
|---|
| 152 | { |
|---|
| 153 | _modeCache = mode; |
|---|
| 154 | _indexCache.clear(); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | virtual void vertex(unsigned int vert) |
|---|
| 158 | { |
|---|
| 159 | _indexCache.push_back(vert); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | virtual void end() |
|---|
| 163 | { |
|---|
| 164 | if (!_indexCache.empty()) |
|---|
| 165 | { |
|---|
| 166 | drawElements(_modeCache,_indexCache.size(),&_indexCache.front()); |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | virtual void drawArrays(GLenum mode,GLint first,GLsizei count); |
|---|
| 171 | |
|---|
| 172 | virtual void drawElements(GLenum mode,GLsizei count,const GLubyte* indices) |
|---|
| 173 | { |
|---|
| 174 | drawElementsImplementation<GLubyte>(mode, count, indices); |
|---|
| 175 | } |
|---|
| 176 | virtual void drawElements(GLenum mode,GLsizei count,const GLushort* indices) |
|---|
| 177 | { |
|---|
| 178 | drawElementsImplementation<GLushort>(mode, count, indices); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | virtual void drawElements(GLenum mode,GLsizei count,const GLuint* indices) |
|---|
| 182 | { |
|---|
| 183 | drawElementsImplementation<GLuint>(mode, count, indices); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | protected: |
|---|
| 187 | |
|---|
| 188 | template<typename T>void drawElementsImplementation(GLenum mode, GLsizei count, const T* indices) |
|---|
| 189 | { |
|---|
| 190 | if (indices==0 || count==0) return; |
|---|
| 191 | |
|---|
| 192 | typedef const T* IndexPointer; |
|---|
| 193 | |
|---|
| 194 | switch(mode) |
|---|
| 195 | { |
|---|
| 196 | case(GL_TRIANGLES): |
|---|
| 197 | { |
|---|
| 198 | |
|---|
| 199 | IndexPointer ilast = &indices[count]; |
|---|
| 200 | for(IndexPointer iptr=indices;iptr<ilast;iptr+=3) |
|---|
| 201 | writeTriangle(*iptr,*(iptr+1),*(iptr+2)); |
|---|
| 202 | |
|---|
| 203 | break; |
|---|
| 204 | } |
|---|
| 205 | case(GL_TRIANGLE_STRIP): |
|---|
| 206 | { |
|---|
| 207 | |
|---|
| 208 | IndexPointer iptr = indices; |
|---|
| 209 | for(GLsizei i=2;i<count;++i,++iptr) |
|---|
| 210 | { |
|---|
| 211 | if ((i%2)) writeTriangle(*(iptr),*(iptr+2),*(iptr+1)); |
|---|
| 212 | else writeTriangle(*(iptr),*(iptr+1),*(iptr+2)); |
|---|
| 213 | } |
|---|
| 214 | break; |
|---|
| 215 | } |
|---|
| 216 | case(GL_QUADS): |
|---|
| 217 | { |
|---|
| 218 | |
|---|
| 219 | IndexPointer iptr = indices; |
|---|
| 220 | for(GLsizei i=3;i<count;i+=4,iptr+=4) |
|---|
| 221 | { |
|---|
| 222 | writeTriangle(*(iptr),*(iptr+1),*(iptr+2)); |
|---|
| 223 | writeTriangle(*(iptr),*(iptr+2),*(iptr+3)); |
|---|
| 224 | } |
|---|
| 225 | break; |
|---|
| 226 | } |
|---|
| 227 | case(GL_QUAD_STRIP): |
|---|
| 228 | { |
|---|
| 229 | |
|---|
| 230 | IndexPointer iptr = indices; |
|---|
| 231 | for(GLsizei i=3;i<count;i+=2,iptr+=2) |
|---|
| 232 | { |
|---|
| 233 | writeTriangle(*(iptr),*(iptr+1),*(iptr+2)); |
|---|
| 234 | writeTriangle(*(iptr+1),*(iptr+3),*(iptr+2)); |
|---|
| 235 | } |
|---|
| 236 | break; |
|---|
| 237 | } |
|---|
| 238 | case(GL_POLYGON): |
|---|
| 239 | case(GL_TRIANGLE_FAN): |
|---|
| 240 | { |
|---|
| 241 | |
|---|
| 242 | IndexPointer iptr = indices; |
|---|
| 243 | unsigned int first = *iptr; |
|---|
| 244 | ++iptr; |
|---|
| 245 | for(GLsizei i=2;i<count;++i,++iptr) |
|---|
| 246 | { |
|---|
| 247 | writeTriangle(first,*(iptr),*(iptr+1)); |
|---|
| 248 | } |
|---|
| 249 | break; |
|---|
| 250 | } |
|---|
| 251 | case(GL_POINTS): |
|---|
| 252 | case(GL_LINES): |
|---|
| 253 | case(GL_LINE_STRIP): |
|---|
| 254 | case(GL_LINE_LOOP): |
|---|
| 255 | |
|---|
| 256 | break; |
|---|
| 257 | |
|---|
| 258 | default: |
|---|
| 259 | |
|---|
| 260 | break; |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | private: |
|---|
| 265 | |
|---|
| 266 | PrimitiveIndexWriter& operator = (const PrimitiveIndexWriter&) { return *this; } |
|---|
| 267 | |
|---|
| 268 | unsigned int _drawable_n; |
|---|
| 269 | ListTriangle & _listTriangles; |
|---|
| 270 | GLenum _modeCache; |
|---|
| 271 | std::vector<GLuint> _indexCache; |
|---|
| 272 | bool _hasNormalCoords, _hasTexCoords; |
|---|
| 273 | osg::Geometry* _geo; |
|---|
| 274 | unsigned int _lastFaceIndex; |
|---|
| 275 | unsigned int _material; |
|---|
| 276 | }; |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | void PrimitiveIndexWriter::drawArrays(GLenum mode,GLint first,GLsizei count) |
|---|
| 280 | { |
|---|
| 281 | switch(mode) |
|---|
| 282 | { |
|---|
| 283 | case(GL_TRIANGLES): |
|---|
| 284 | { |
|---|
| 285 | unsigned int pos=first; |
|---|
| 286 | for(GLsizei i=2;i<count;i+=3,pos+=3) |
|---|
| 287 | { |
|---|
| 288 | writeTriangle(pos,pos+1,pos+2); |
|---|
| 289 | } |
|---|
| 290 | break; |
|---|
| 291 | } |
|---|
| 292 | case(GL_TRIANGLE_STRIP): |
|---|
| 293 | { |
|---|
| 294 | unsigned int pos=first; |
|---|
| 295 | for(GLsizei i=2;i<count;++i,++pos) |
|---|
| 296 | { |
|---|
| 297 | if ((i%2)) writeTriangle(pos,pos+2,pos+1); |
|---|
| 298 | else writeTriangle(pos,pos+1,pos+2); |
|---|
| 299 | } |
|---|
| 300 | break; |
|---|
| 301 | } |
|---|
| 302 | case(GL_QUADS): |
|---|
| 303 | { |
|---|
| 304 | unsigned int pos=first; |
|---|
| 305 | for(GLsizei i=3;i<count;i+=4,pos+=4) |
|---|
| 306 | { |
|---|
| 307 | writeTriangle(pos,pos+1,pos+2); |
|---|
| 308 | writeTriangle(pos,pos+2,pos+3); |
|---|
| 309 | } |
|---|
| 310 | break; |
|---|
| 311 | } |
|---|
| 312 | case(GL_QUAD_STRIP): |
|---|
| 313 | { |
|---|
| 314 | unsigned int pos=first; |
|---|
| 315 | for(GLsizei i=3;i<count;i+=2,pos+=2) |
|---|
| 316 | { |
|---|
| 317 | writeTriangle(pos,pos+1,pos+2); |
|---|
| 318 | writeTriangle(pos+1,pos+3,pos+2); |
|---|
| 319 | } |
|---|
| 320 | break; |
|---|
| 321 | } |
|---|
| 322 | case(GL_POLYGON): |
|---|
| 323 | case(GL_TRIANGLE_FAN): |
|---|
| 324 | { |
|---|
| 325 | unsigned int pos=first+1; |
|---|
| 326 | for(GLsizei i=2;i<count;++i,++pos) |
|---|
| 327 | { |
|---|
| 328 | writeTriangle(first,pos,pos+1); |
|---|
| 329 | } |
|---|
| 330 | break; |
|---|
| 331 | } |
|---|
| 332 | case(GL_POINTS): |
|---|
| 333 | case(GL_LINES): |
|---|
| 334 | case(GL_LINE_STRIP): |
|---|
| 335 | case(GL_LINE_LOOP): |
|---|
| 336 | |
|---|
| 337 | default: |
|---|
| [11056] | 338 | osg::notify(osg::WARN) << "3DS WriterNodeVisitor: can't handle mode " << mode << std::endl; |
|---|
| [10932] | 339 | break; |
|---|
| 340 | } |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | WriterNodeVisitor::Material::Material(WriterNodeVisitor & writerNodeVisitor, osg::StateSet * stateset, osg::Material* mat, osg::Texture* tex, int index) : |
|---|
| 346 | index(index), |
|---|
| 347 | diffuse(1,1,1,1), |
|---|
| 348 | ambient(0.2,0.2,0.2,1), |
|---|
| 349 | specular(0,0,0,1), |
|---|
| 350 | shininess(0), |
|---|
| 351 | transparency(0), |
|---|
| 352 | double_sided(false), |
|---|
| 353 | image(NULL), |
|---|
| 354 | texture_transparency(false), |
|---|
| 355 | texture_no_tile(false) |
|---|
| 356 | { |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | if (mat) { |
|---|
| 360 | assert(stateset); |
|---|
| 361 | diffuse = mat->getDiffuse(osg::Material::FRONT); |
|---|
| 362 | ambient = mat->getAmbient(osg::Material::FRONT); |
|---|
| 363 | specular = mat->getSpecular(osg::Material::FRONT); |
|---|
| 364 | shininess = mat->getShininess(osg::Material::FRONT); |
|---|
| 365 | transparency = 1-diffuse.w(); |
|---|
| 366 | name = writerNodeVisitor.getUniqueName(mat->getName(),"mat"); |
|---|
| 367 | osg::StateAttribute * attribute = stateset->getAttribute(osg::StateAttribute::CULLFACE); |
|---|
| 368 | if (!attribute) { |
|---|
| 369 | double_sided = true; |
|---|
| 370 | } else { |
|---|
| 371 | assert(dynamic_cast<osg::CullFace *>(attribute)); |
|---|
| 372 | osg::CullFace::Mode mode = static_cast<osg::CullFace *>(attribute)->getMode(); |
|---|
| 373 | if (mode == osg::CullFace::BACK) double_sided = false; |
|---|
| 374 | else if (mode == osg::CullFace::FRONT) { |
|---|
| 375 | osg::notify(osg::WARN) << "3DS Writer: Reversed face (culled FRONT) not supported yet." << std::endl; |
|---|
| 376 | double_sided = false; |
|---|
| 377 | } |
|---|
| 378 | else { |
|---|
| 379 | assert(mode == osg::CullFace::FRONT_AND_BACK); |
|---|
| 380 | osg::notify(osg::WARN) << "3DS Writer: Invisible face (culled FRONT_AND_BACK) not supported yet." << std::endl; |
|---|
| 381 | double_sided = false; |
|---|
| 382 | } |
|---|
| 383 | } |
|---|
| 384 | } |
|---|
| 385 | if (tex) { |
|---|
| 386 | osg::Image* img = tex->getImage(0); |
|---|
| 387 | if(img) |
|---|
| 388 | { |
|---|
| 389 | texture_transparency = (stateset->getMode(GL_BLEND) == osg::StateAttribute::ON); |
|---|
| 390 | texture_no_tile = (tex->getWrap(osg::Texture2D::WRAP_S) == osg::Texture2D::CLAMP); |
|---|
| 391 | image = img; |
|---|
| 392 | } |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | if (name.empty()) { |
|---|
| 396 | std::stringstream ss; |
|---|
| 397 | ss << "m" << index; |
|---|
| 398 | name = ss.str(); |
|---|
| 399 | } |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | |
|---|
| [11056] | 403 | |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | std::string getPathRelative(const std::string& from, |
|---|
| 407 | const std::string& to) |
|---|
| [10932] | 408 | { |
|---|
| 409 | |
|---|
| [11056] | 410 | std::string::size_type slash = to.find_last_of('/'); |
|---|
| 411 | std::string::size_type backslash = to.find_last_of('\\'); |
|---|
| 412 | if (slash == std::string::npos) |
|---|
| 413 | { |
|---|
| 414 | if (backslash == std::string::npos) return to; |
|---|
| 415 | slash = backslash; |
|---|
| 416 | } |
|---|
| 417 | else if (backslash != std::string::npos && backslash > slash) |
|---|
| 418 | { |
|---|
| 419 | slash = backslash; |
|---|
| 420 | } |
|---|
| [10932] | 421 | |
|---|
| [11056] | 422 | if (from.empty() || from.length() > to.length()) |
|---|
| 423 | return osgDB::getSimpleFileName(to); |
|---|
| 424 | |
|---|
| 425 | std::string::const_iterator itTo = to.begin(); |
|---|
| 426 | for (std::string::const_iterator itFrom = from.begin(); |
|---|
| 427 | itFrom != from.end(); ++itFrom, ++itTo) |
|---|
| [10932] | 428 | { |
|---|
| [11056] | 429 | char a = tolower(*itFrom), b = tolower(*itTo); |
|---|
| 430 | if (a == '\\') a = '/'; |
|---|
| 431 | if (b == '\\') b = '/'; |
|---|
| 432 | if (a != b || itTo == to.begin() + slash + 1) |
|---|
| 433 | { |
|---|
| 434 | return osgDB::getSimpleFileName(to); |
|---|
| 435 | } |
|---|
| [10932] | 436 | } |
|---|
| [11056] | 437 | |
|---|
| 438 | while (itTo != to.end() && (*itTo == '\\' || *itTo == '/')) |
|---|
| 439 | { |
|---|
| 440 | ++itTo; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | return std::string(itTo, to.end()); |
|---|
| [10932] | 444 | } |
|---|
| 445 | |
|---|
| [10938] | 446 | |
|---|
| [11056] | 447 | std::string convertExt(const std::string & path, bool extendedFilePaths) |
|---|
| [10938] | 448 | { |
|---|
| [11056] | 449 | if (extendedFilePaths) return path; |
|---|
| 450 | |
|---|
| [10938] | 451 | std::string ext = osgDB::getFileExtensionIncludingDot(path); |
|---|
| 452 | if (ext == ".tiff") ext = ".tif"; |
|---|
| 453 | else if (ext == ".jpeg") ext = ".jpg"; |
|---|
| 454 | else if (ext == ".jpeg2000" || ext == ".jpg2000") ext = ".jpc"; |
|---|
| 455 | return osgDB::getNameLessExtension(path) + ext; |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| [11056] | 458 | |
|---|
| 459 | WriterNodeVisitor::WriterNodeVisitor(Lib3dsFile * file3ds, const std::string & fileName, |
|---|
| 460 | const osgDB::ReaderWriter::Options* options, |
|---|
| 461 | const std::string & srcDirectory) : |
|---|
| 462 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
|---|
| 463 | _suceedLastApply(true), |
|---|
| 464 | _srcDirectory(srcDirectory), |
|---|
| 465 | file3ds(file3ds), |
|---|
| 466 | _currentStateSet(new osg::StateSet()), |
|---|
| 467 | _lastMaterialIndex(0), |
|---|
| 468 | _lastMeshIndex(0), |
|---|
| 469 | _cur3dsNode(NULL), |
|---|
| 470 | options(options), |
|---|
| 471 | _imageCount(0), |
|---|
| 472 | _extendedFilePaths(false) |
|---|
| 473 | { |
|---|
| 474 | if (!fileName.empty()) |
|---|
| 475 | _directory = options->getDatabasePathList().empty() ? osgDB::getFilePath(fileName) : options->getDatabasePathList().front(); |
|---|
| 476 | |
|---|
| 477 | if (options) { |
|---|
| 478 | std::istringstream iss(options->getOptionString()); |
|---|
| 479 | std::string opt; |
|---|
| 480 | while (iss >> opt) |
|---|
| 481 | { |
|---|
| 482 | if (opt == "extended3dsFilePaths" || opt == "extended3DSFilePaths") |
|---|
| 483 | _extendedFilePaths = true; |
|---|
| 484 | } |
|---|
| 485 | } |
|---|
| 486 | } |
|---|
| 487 | |
|---|
| 488 | |
|---|
| [10932] | 489 | void WriterNodeVisitor::writeMaterials() |
|---|
| 490 | { |
|---|
| 491 | unsigned int nbMat = _materialMap.size(); |
|---|
| 492 | lib3ds_file_reserve_materials(file3ds, nbMat, 1); |
|---|
| 493 | |
|---|
| 494 | for (unsigned int iMat=0; iMat<nbMat; ++iMat) |
|---|
| 495 | { |
|---|
| 496 | bool found = false; |
|---|
| 497 | for(MaterialMap::iterator itr = _materialMap.begin(); itr != _materialMap.end(); ++itr) |
|---|
| 498 | { |
|---|
| 499 | const Material & mat = itr->second; |
|---|
| 500 | if (mat.index != static_cast<int>(iMat)) continue; |
|---|
| 501 | found = true; |
|---|
| 502 | |
|---|
| 503 | assert(mat.index>=0 && mat.index < static_cast<int>(_materialMap.size())); |
|---|
| 504 | Lib3dsMaterial * mat3ds = lib3ds_material_new(getFileName(mat.name).c_str()); |
|---|
| 505 | copyOsgColorToLib3dsColor(mat3ds->ambient, mat.ambient); |
|---|
| 506 | copyOsgColorToLib3dsColor(mat3ds->diffuse, mat.diffuse); |
|---|
| 507 | copyOsgColorToLib3dsColor(mat3ds->specular, mat.specular); |
|---|
| 508 | mat3ds->shininess = mat.shininess; |
|---|
| 509 | mat3ds->transparency = mat.transparency; |
|---|
| 510 | mat3ds->two_sided = mat.double_sided ? 1 : 0; |
|---|
| 511 | if (mat.image) |
|---|
| 512 | { |
|---|
| 513 | Lib3dsTextureMap & tex = mat3ds->texture1_map; |
|---|
| 514 | std::string path; |
|---|
| 515 | if(mat.image->getFileName().empty()) |
|---|
| 516 | { |
|---|
| 517 | std::ostringstream oss; |
|---|
| 518 | oss << "Image_" << _imageCount++ << ".rgb"; |
|---|
| 519 | path = oss.str(); |
|---|
| 520 | } |
|---|
| 521 | else { |
|---|
| 522 | path = getPathRelative(_srcDirectory, mat.image->getFileName()); |
|---|
| 523 | } |
|---|
| [11056] | 524 | path = convertExt(path, _extendedFilePaths); |
|---|
| [10938] | 525 | |
|---|
| [11056] | 526 | if(!is3DSpath(path, _extendedFilePaths)) { |
|---|
| [10932] | 527 | path = getUniqueName(path, "", true); |
|---|
| 528 | |
|---|
| 529 | } |
|---|
| 530 | |
|---|
| 531 | strcpy(tex.name, path.c_str()); |
|---|
| 532 | path = osgDB::concatPaths(_directory, path); |
|---|
| 533 | osgDB::makeDirectoryForFile(path); |
|---|
| 534 | |
|---|
| 535 | |
|---|
| [11056] | 536 | if(_imageSet.find(mat.image.get()) == _imageSet.end()) |
|---|
| 537 | { |
|---|
| 538 | _imageSet.insert(mat.image.get()); |
|---|
| 539 | osgDB::writeImageFile(*(mat.image), path); |
|---|
| 540 | } |
|---|
| [10932] | 541 | if (mat.texture_transparency) tex.flags |= LIB3DS_TEXTURE_ALPHA_SOURCE; |
|---|
| 542 | if (mat.texture_no_tile) tex.flags |= LIB3DS_TEXTURE_NO_TILE; |
|---|
| 543 | } |
|---|
| 544 | if (!suceedLastApply()) |
|---|
| 545 | return; |
|---|
| 546 | lib3ds_file_insert_material(file3ds, mat3ds, itr->second.index); |
|---|
| 547 | break; |
|---|
| 548 | } |
|---|
| 549 | if (!found) throw "Implementation error"; |
|---|
| 550 | } |
|---|
| 551 | } |
|---|
| 552 | |
|---|
| 553 | |
|---|
| 554 | std::string WriterNodeVisitor::getUniqueName(const std::string& _defaultValue, const std::string & _defaultPrefix, bool nameIsPath) { |
|---|
| [11056] | 555 | static const unsigned int MAX_PREFIX_LEGNTH = 4; |
|---|
| 556 | if (_defaultPrefix.length()>MAX_PREFIX_LEGNTH) throw "Default prefix is too long"; |
|---|
| [10932] | 557 | |
|---|
| 558 | |
|---|
| 559 | bool defaultIs83 = is83(_defaultValue); |
|---|
| [11056] | 560 | bool defaultIsValid = nameIsPath ? is3DSpath(_defaultValue, _extendedFilePaths) : defaultIs83; |
|---|
| [10932] | 561 | if (defaultIsValid && _nameMap.find(_defaultValue) == _nameMap.end()) { |
|---|
| 562 | _nameMap.insert(_defaultValue); |
|---|
| 563 | return _defaultValue; |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | |
|---|
| [11056] | 567 | |
|---|
| [10932] | 568 | std::string defaultValue(nameIsPath ? osgDB::getSimpleFileName(_defaultValue) : _defaultValue); |
|---|
| 569 | std::string ext(nameIsPath ? osgDB::getFileExtensionIncludingDot(_defaultValue).substr(0, std::min<unsigned int>(_defaultValue.size(), 4)) : ""); |
|---|
| [11056] | 570 | if (ext == ".") ext = ""; |
|---|
| [10932] | 571 | |
|---|
| 572 | std::string defaultPrefix(_defaultPrefix.empty() ? "_" : _defaultPrefix); |
|---|
| 573 | |
|---|
| 574 | unsigned int max_val = 0; |
|---|
| 575 | std::string truncDefaultValue = ""; |
|---|
| [11056] | 576 | for (unsigned int i = 0; i < std::min<unsigned int>(defaultValue.size(), MAX_PREFIX_LEGNTH); ++i) |
|---|
| [10932] | 577 | { |
|---|
| 578 | if (defaultValue[i] == '.') |
|---|
| 579 | { |
|---|
| 580 | truncDefaultValue = defaultValue.substr(0, i); |
|---|
| 581 | break; |
|---|
| 582 | } |
|---|
| 583 | } |
|---|
| 584 | if (truncDefaultValue.empty()) |
|---|
| [11056] | 585 | truncDefaultValue = defaultValue.substr(0, std::min<unsigned int>(defaultValue.size(), MAX_PREFIX_LEGNTH)); |
|---|
| 586 | assert(truncDefaultValue.size() <= MAX_PREFIX_LEGNTH); |
|---|
| [10932] | 587 | std::map<std::string, unsigned int>::iterator pairPrefix; |
|---|
| [11056] | 588 | |
|---|
| 589 | |
|---|
| [10932] | 590 | defaultIs83 = is83(truncDefaultValue); |
|---|
| 591 | if (defaultIs83) |
|---|
| 592 | { |
|---|
| [11056] | 593 | max_val = static_cast<unsigned int>(pow(10., 8. - truncDefaultValue.length())) -1; |
|---|
| [10932] | 594 | pairPrefix = _mapPrefix.find(truncDefaultValue); |
|---|
| 595 | } |
|---|
| 596 | |
|---|
| [11056] | 597 | if (defaultIs83 && (pairPrefix == _mapPrefix.end() || pairPrefix->second <= max_val)) |
|---|
| [10932] | 598 | { |
|---|
| 599 | defaultPrefix = truncDefaultValue; |
|---|
| 600 | } |
|---|
| 601 | else |
|---|
| 602 | { |
|---|
| [11056] | 603 | max_val = static_cast<unsigned int>(pow(10., 8. - defaultPrefix.length())) -1; |
|---|
| [10932] | 604 | pairPrefix = _mapPrefix.find(defaultPrefix); |
|---|
| 605 | } |
|---|
| 606 | |
|---|
| 607 | unsigned int searchStart = 0; |
|---|
| [11056] | 608 | if (pairPrefix != _mapPrefix.end()) { |
|---|
| [10932] | 609 | searchStart = pairPrefix->second; |
|---|
| [11056] | 610 | } |
|---|
| [10932] | 611 | |
|---|
| 612 | for(unsigned int i = searchStart; i <= max_val; ++i) { |
|---|
| 613 | std::stringstream ss; |
|---|
| [11056] | 614 | ss << defaultPrefix << i; |
|---|
| [10932] | 615 | const std::string & res = ss.str(); |
|---|
| 616 | if (_nameMap.find(res) == _nameMap.end()) { |
|---|
| [11056] | 617 | if (pairPrefix != _mapPrefix.end()) { |
|---|
| [10932] | 618 | pairPrefix->second = i + 1; |
|---|
| [11056] | 619 | } else { |
|---|
| 620 | _mapPrefix.insert(std::pair<std::string, unsigned int>(defaultPrefix, i + 1)); |
|---|
| [10932] | 621 | } |
|---|
| 622 | _nameMap.insert(res); |
|---|
| 623 | return res + ext; |
|---|
| 624 | } |
|---|
| 625 | } |
|---|
| [11056] | 626 | |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | if (defaultPrefix.length()>1) return getUniqueName(_defaultValue, defaultPrefix.substr(0, defaultPrefix.length()-1), nameIsPath); |
|---|
| 630 | |
|---|
| 631 | if (defaultPrefix != std::string("_")) return getUniqueName(_defaultValue, "_", nameIsPath); |
|---|
| [10932] | 632 | throw "No more names available! Is default prefix too long?"; |
|---|
| 633 | } |
|---|
| 634 | |
|---|
| 635 | int WriterNodeVisitor::processStateSet(osg::StateSet* ss) |
|---|
| 636 | { |
|---|
| 637 | MaterialMap::const_iterator itr = _materialMap.find(ss); |
|---|
| 638 | if (itr != _materialMap.end()) { |
|---|
| 639 | assert(itr->second.index>=0); |
|---|
| 640 | return itr->second.index; |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | osg::Material* mat = dynamic_cast<osg::Material*>(ss->getAttribute(osg::StateAttribute::MATERIAL)); |
|---|
| 644 | osg::Texture* tex = dynamic_cast<osg::Texture*>(ss->getTextureAttribute(0, osg::StateAttribute::TEXTURE)); |
|---|
| 645 | |
|---|
| 646 | if (mat || tex) |
|---|
| 647 | { |
|---|
| 648 | int matNum = _lastMaterialIndex; |
|---|
| 649 | _materialMap.insert(std::make_pair(osg::ref_ptr<osg::StateSet>(ss), Material(*this, ss, mat, tex, matNum) )); |
|---|
| 650 | ++_lastMaterialIndex; |
|---|
| 651 | return matNum; |
|---|
| 652 | } |
|---|
| 653 | return -1; |
|---|
| 654 | } |
|---|
| 655 | |
|---|
| 656 | |
|---|
| 657 | |
|---|
| 658 | |
|---|
| 659 | |
|---|
| 660 | |
|---|
| 661 | |
|---|
| 662 | |
|---|
| 663 | unsigned int |
|---|
| 664 | WriterNodeVisitor::getMeshIndexForGeometryIndex(MapIndices & index_vert, |
|---|
| 665 | unsigned int index, |
|---|
| 666 | unsigned int drawable_n) |
|---|
| 667 | { |
|---|
| [11056] | 668 | MapIndices::iterator itIndex = index_vert.find(std::pair<unsigned int, unsigned int>(index, drawable_n)); |
|---|
| [10932] | 669 | if (itIndex == index_vert.end()) { |
|---|
| 670 | unsigned int indexMesh = index_vert.size(); |
|---|
| [11056] | 671 | index_vert.insert(std::make_pair(std::pair<unsigned int, unsigned int>(index, drawable_n), indexMesh)); |
|---|
| [10932] | 672 | return indexMesh; |
|---|
| 673 | } |
|---|
| 674 | return itIndex->second; |
|---|
| 675 | } |
|---|
| 676 | |
|---|
| 677 | |
|---|
| 678 | void |
|---|
| 679 | WriterNodeVisitor::buildMesh(osg::Geode & geo, |
|---|
| 680 | MapIndices & index_vert, |
|---|
| 681 | bool texcoords, |
|---|
| 682 | Lib3dsMesh * mesh) |
|---|
| 683 | { |
|---|
| 684 | osg::notify(osg::DEBUG_INFO) << "Building Mesh" << std::endl; |
|---|
| 685 | |
|---|
| 686 | if (!mesh) throw "Allocation error"; |
|---|
| 687 | |
|---|
| 688 | lib3ds_mesh_resize_vertices(mesh, index_vert.size(), texcoords ? 1 : 0, 0); |
|---|
| 689 | |
|---|
| 690 | |
|---|
| 691 | for(MapIndices::iterator it = index_vert.begin(); it != index_vert.end();++it) |
|---|
| 692 | { |
|---|
| 693 | osg::Geometry *g = geo.getDrawable( it->first.second )->asGeometry(); |
|---|
| 694 | assert(g->getVertexArray()); |
|---|
| 695 | if (g->getVertexArray()->getType() != osg::Array::Vec3ArrayType) |
|---|
| 696 | throw "Vertex array is not Vec3. Not implemented"; |
|---|
| 697 | const osg::Vec3Array & vecs= *static_cast<osg::Vec3Array *>(g->getVertexArray()); |
|---|
| 698 | copyOsgVectorToLib3dsVector(mesh->vertices[it->second], vecs[it->first.first]); |
|---|
| 699 | } |
|---|
| 700 | |
|---|
| 701 | |
|---|
| 702 | if (texcoords) |
|---|
| 703 | { |
|---|
| 704 | for(MapIndices::iterator it = index_vert.begin(); it != index_vert.end(); ++it) |
|---|
| 705 | { |
|---|
| 706 | osg::Geometry *g = geo.getDrawable( it->first.second )->asGeometry(); |
|---|
| 707 | osg::Array * array = g->getTexCoordArray(0); |
|---|
| 708 | if(array) |
|---|
| 709 | { |
|---|
| 710 | if (g->getTexCoordArray(0)->getType() != osg::Array::Vec2ArrayType) |
|---|
| 711 | throw "Texture coords array is not Vec2. Not implemented"; |
|---|
| 712 | const osg::Vec2Array & vecs= *static_cast<osg::Vec2Array *>(array); |
|---|
| 713 | mesh->texcos[it->second][0] = vecs[it->first.first][0]; |
|---|
| 714 | mesh->texcos[it->second][1] = vecs[it->first.first][1]; |
|---|
| 715 | } |
|---|
| 716 | } |
|---|
| 717 | } |
|---|
| 718 | lib3ds_file_insert_mesh(file3ds, mesh, _lastMeshIndex); |
|---|
| 719 | ++_lastMeshIndex; |
|---|
| 720 | |
|---|
| 721 | Lib3dsMeshInstanceNode * node3ds = lib3ds_node_new_mesh_instance(mesh, mesh->name, NULL, NULL, NULL); |
|---|
| 722 | lib3ds_file_append_node(file3ds, reinterpret_cast<Lib3dsNode*>(node3ds), reinterpret_cast<Lib3dsNode*>(_cur3dsNode)); |
|---|
| 723 | } |
|---|
| 724 | |
|---|
| 725 | unsigned int |
|---|
| 726 | WriterNodeVisitor::calcVertices(osg::Geode & geo) |
|---|
| 727 | { |
|---|
| 728 | unsigned int numVertice = 0; |
|---|
| 729 | for (unsigned int i = 0; i < geo.getNumDrawables(); ++i) |
|---|
| 730 | { |
|---|
| 731 | osg::Geometry *g = geo.getDrawable( i )->asGeometry(); |
|---|
| 732 | assert(g->getVertexArray()); |
|---|
| 733 | if (g->getVertexArray()->getType() != osg::Array::Vec3ArrayType) |
|---|
| 734 | throw "Vertex array is not Vec3. Not implemented"; |
|---|
| 735 | const osg::Vec3Array & vecs= *static_cast<osg::Vec3Array *>(g->getVertexArray()); |
|---|
| 736 | numVertice += vecs.getNumElements(); |
|---|
| 737 | } |
|---|
| 738 | return numVertice; |
|---|
| 739 | } |
|---|
| 740 | |
|---|
| 741 | |
|---|
| 742 | void |
|---|
| 743 | WriterNodeVisitor::buildFaces(osg::Geode & geo, |
|---|
| 744 | ListTriangle & listTriangles, |
|---|
| 745 | bool texcoords) |
|---|
| 746 | { |
|---|
| 747 | MapIndices index_vert; |
|---|
| 748 | unsigned int nbFace = 0; |
|---|
| 749 | Lib3dsMesh *mesh = lib3ds_mesh_new( getUniqueName(geo.getName().empty() ? geo.className() : geo.getName(), "geo").c_str() ); |
|---|
| 750 | unsigned int nbTriangles = listTriangles.size(); |
|---|
| 751 | |
|---|
| 752 | lib3ds_mesh_resize_faces(mesh, nbTriangles); |
|---|
| 753 | |
|---|
| 754 | unsigned int nbVertices = calcVertices(geo); |
|---|
| 755 | if (listTriangles.size() >= MAX_FACES-2 || |
|---|
| 756 | ((nbVertices) >= MAX_VERTICES-2)) |
|---|
| 757 | { |
|---|
| [11056] | 758 | osg::notify(osg::INFO) << "Sorting elements..." << std::endl; |
|---|
| [10932] | 759 | WriterCompareTriangle cmp(geo, nbVertices); |
|---|
| 760 | std::sort(listTriangles.begin(), listTriangles.end(), cmp); |
|---|
| 761 | } |
|---|
| 762 | |
|---|
| 763 | for (ListTriangle::iterator it = listTriangles.begin(); it != listTriangles.end(); ++it) |
|---|
| 764 | { |
|---|
| 765 | |
|---|
| 766 | if ((index_vert.size() >= MAX_VERTICES-2 || |
|---|
| 767 | nbFace >= MAX_FACES-2)) |
|---|
| 768 | { |
|---|
| 769 | |
|---|
| 770 | lib3ds_mesh_resize_faces(mesh, nbFace); |
|---|
| 771 | buildMesh(geo, index_vert, texcoords, mesh); |
|---|
| 772 | |
|---|
| 773 | |
|---|
| 774 | index_vert.clear(); |
|---|
| 775 | mesh = lib3ds_mesh_new( getUniqueName(geo.getName().empty() ? geo.className() : geo.getName(), "geo").c_str()); |
|---|
| 776 | nbTriangles -= nbFace; |
|---|
| 777 | nbFace = 0; |
|---|
| 778 | lib3ds_mesh_resize_faces(mesh, nbTriangles); |
|---|
| 779 | } |
|---|
| 780 | Lib3dsFace & face = mesh->faces[nbFace++]; |
|---|
| 781 | face.index[0] = getMeshIndexForGeometryIndex(index_vert, it->first.t1, it->second); |
|---|
| 782 | face.index[1] = getMeshIndexForGeometryIndex(index_vert, it->first.t2, it->second); |
|---|
| 783 | face.index[2] = getMeshIndexForGeometryIndex(index_vert, it->first.t3, it->second); |
|---|
| 784 | face.material = it->first.material; |
|---|
| 785 | } |
|---|
| 786 | buildMesh(geo, index_vert, texcoords, mesh); |
|---|
| 787 | } |
|---|
| 788 | |
|---|
| 789 | void |
|---|
| 790 | WriterNodeVisitor::createListTriangle(osg::Geometry * geo, |
|---|
| 791 | ListTriangle & listTriangles, |
|---|
| 792 | bool & texcoords, |
|---|
| 793 | unsigned int & drawable_n) |
|---|
| 794 | { |
|---|
| 795 | unsigned int nbVertices = 0; |
|---|
| 796 | { |
|---|
| 797 | if (geo->getVertexArray() && geo->getVertexArray()->getType() != osg::Array::Vec3ArrayType) |
|---|
| 798 | throw "Vertex array is not Vec3. Not implemented"; |
|---|
| 799 | const osg::Vec3Array * vecs = geo->getVertexArray() ? static_cast<osg::Vec3Array *>(geo->getVertexArray()) : NULL; |
|---|
| 800 | if (vecs) |
|---|
| 801 | { |
|---|
| 802 | nbVertices = geo->getVertexArray()->getNumElements(); |
|---|
| 803 | |
|---|
| 804 | if (geo->getTexCoordArray(0) && geo->getTexCoordArray(0)->getType() != osg::Array::Vec2ArrayType) |
|---|
| 805 | throw "Texture coords array is not Vec2. Not implemented"; |
|---|
| 806 | const osg::Vec2Array * texvecs = geo->getTexCoordArray(0) ? static_cast<osg::Vec2Array *>(geo->getTexCoordArray(0)) : NULL; |
|---|
| 807 | if (texvecs) |
|---|
| 808 | { |
|---|
| 809 | unsigned int nb = geo->getTexCoordArray(0)->getNumElements(); |
|---|
| 810 | if (nb != geo->getVertexArray()->getNumElements()) throw "There are more/less texture coords than vertices!"; |
|---|
| 811 | texcoords = true; |
|---|
| 812 | } |
|---|
| 813 | } |
|---|
| 814 | } |
|---|
| 815 | |
|---|
| 816 | if (nbVertices==0) return; |
|---|
| 817 | |
|---|
| 818 | int material = processStateSet(_currentStateSet.get()); |
|---|
| 819 | |
|---|
| 820 | for(unsigned int i = 0; i < geo->getNumPrimitiveSets(); ++i) |
|---|
| 821 | { |
|---|
| 822 | osg::PrimitiveSet* ps = geo->getPrimitiveSet(i); |
|---|
| 823 | PrimitiveIndexWriter pif(geo, listTriangles, drawable_n, material); |
|---|
| 824 | ps->accept(pif); |
|---|
| 825 | } |
|---|
| 826 | } |
|---|
| 827 | |
|---|
| 828 | bool WriterNodeVisitor::suceedLastApply() const |
|---|
| 829 | { |
|---|
| 830 | return _suceedLastApply; |
|---|
| 831 | } |
|---|
| 832 | |
|---|
| 833 | void WriterNodeVisitor::failedApply() |
|---|
| 834 | { |
|---|
| 835 | _suceedLastApply = false; |
|---|
| 836 | osg::notify(osg::NOTICE) << "Error going through node" << std::endl; |
|---|
| 837 | } |
|---|
| 838 | |
|---|
| 839 | void WriterNodeVisitor::apply( osg::Geode &node ) |
|---|
| 840 | { |
|---|
| 841 | pushStateSet(node.getStateSet()); |
|---|
| 842 | |
|---|
| 843 | |
|---|
| 844 | unsigned int count = node.getNumDrawables(); |
|---|
| 845 | ListTriangle listTriangles; |
|---|
| 846 | bool texcoords = false; |
|---|
| 847 | for ( unsigned int i = 0; i < count; i++ ) |
|---|
| 848 | { |
|---|
| 849 | osg::Geometry *g = node.getDrawable( i )->asGeometry(); |
|---|
| 850 | if ( g != NULL ) |
|---|
| 851 | { |
|---|
| 852 | pushStateSet(g->getStateSet()); |
|---|
| 853 | createListTriangle(g, listTriangles, texcoords, i); |
|---|
| 854 | popStateSet(g->getStateSet()); |
|---|
| 855 | } |
|---|
| 856 | } |
|---|
| 857 | if (count > 0) |
|---|
| 858 | { |
|---|
| 859 | buildFaces(node, listTriangles, texcoords); |
|---|
| 860 | } |
|---|
| 861 | popStateSet(node.getStateSet()); |
|---|
| 862 | |
|---|
| 863 | if (suceedLastApply()) |
|---|
| 864 | traverse(node); |
|---|
| 865 | } |
|---|
| 866 | |
|---|
| 867 | void WriterNodeVisitor::apply(osg::Group &node) |
|---|
| 868 | { |
|---|
| 869 | Lib3dsMeshInstanceNode * parent = _cur3dsNode; |
|---|
| 870 | Lib3dsMeshInstanceNode * node3ds = lib3ds_node_new_mesh_instance(NULL, getUniqueName(node.getName().empty() ? node.className() : getFileName(node.getName()), "grp").c_str(), NULL, NULL, NULL); |
|---|
| 871 | lib3ds_file_append_node(file3ds, reinterpret_cast<Lib3dsNode*>(node3ds), reinterpret_cast<Lib3dsNode*>(parent)); |
|---|
| 872 | _cur3dsNode = node3ds; |
|---|
| 873 | if (suceedLastApply()) |
|---|
| 874 | traverse(node); |
|---|
| 875 | _cur3dsNode = parent; |
|---|
| 876 | } |
|---|
| 877 | |
|---|
| 878 | void WriterNodeVisitor::apply(osg::MatrixTransform &node) |
|---|
| 879 | { |
|---|
| 880 | Lib3dsMeshInstanceNode * parent = _cur3dsNode; |
|---|
| 881 | |
|---|
| 882 | const osg::Matrix & m = node.getMatrix(); |
|---|
| 883 | |
|---|
| 884 | |
|---|
| 885 | |
|---|
| 886 | float pos[3]; |
|---|
| 887 | float scl[3]; |
|---|
| 888 | float rot[4]; |
|---|
| 889 | osg::Vec3 osgScl, osgPos; |
|---|
| 890 | osg::Quat osgRot, osgSo; |
|---|
| 891 | m.decompose(osgPos, osgRot, osgScl, osgSo); |
|---|
| 892 | copyOsgVectorToLib3dsVector(pos, osgPos); |
|---|
| 893 | copyOsgVectorToLib3dsVector(scl, osgScl); |
|---|
| 894 | copyOsgQuatToLib3dsQuat(rot, osgRot); |
|---|
| 895 | Lib3dsMeshInstanceNode * node3ds = lib3ds_node_new_mesh_instance |
|---|
| 896 | (NULL, getUniqueName(node.getName().empty() ? node.className() : node.getName(), "mtx").c_str(), pos, scl, rot); |
|---|
| 897 | |
|---|
| 898 | |
|---|
| 899 | |
|---|
| 900 | |
|---|
| 901 | |
|---|
| 902 | |
|---|
| 903 | lib3ds_file_append_node(file3ds, reinterpret_cast<Lib3dsNode*>(node3ds), reinterpret_cast<Lib3dsNode*>(parent)); |
|---|
| 904 | _cur3dsNode = node3ds; |
|---|
| 905 | if (suceedLastApply()) |
|---|
| 906 | traverse(node); |
|---|
| 907 | _cur3dsNode = parent; |
|---|
| 908 | } |
|---|