Changeset 10941
- Timestamp:
- 01/11/10 17:09:18 (3 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgPlugins/3ds/ReaderWriter3DS.cpp
r10932 r10941 36 36 using namespace std; 37 37 using namespace osg; 38 39 /// Implementation borrowed from boost and slightly modified 40 template<class T> class scoped_array // noncopyable 41 { 42 private: 43 T * px; 44 scoped_array(scoped_array const &); 45 scoped_array & operator=(scoped_array const &); 46 typedef scoped_array<T> this_type; 47 48 void operator==( scoped_array const& ) const; 49 void operator!=( scoped_array const& ) const; 50 51 public: 52 typedef T element_type; 53 explicit scoped_array( T * p = 0 ) : px( p ) {} 54 55 ~scoped_array() { delete[] px; } 56 57 void reset(T * p = 0) { 58 assert( p == 0 || p != px ); // catch self-reset errors 59 this_type(p).swap(*this); 60 } 61 62 T & operator[](std::ptrdiff_t i) const // never throws 63 { 64 assert( px != 0 ); 65 assert( i >= 0 ); 66 return px[i]; 67 } 68 69 T * get() const // never throws 70 { 71 return px; 72 } 73 74 void swap(scoped_array & b) // never throws 75 { 76 T * tmp = b.px; 77 b.px = px; 78 px = tmp; 79 } 80 }; 81 38 82 39 83 … … 316 360 FaceList& smoothFaceMap = sitr->second; 317 361 osg::ref_ptr<osg::Drawable> drawable = createDrawable(mesh,smoothFaceMap,matrix); 318 if (drawable )362 if (drawable.valid()) 319 363 { 320 364 if (stateSet) 321 365 drawable->setStateSet(stateSet); 322 geode->addDrawable(drawable );366 geode->addDrawable(drawable.get()); 323 367 } 324 368 } … … 327 371 { 328 372 osg::ref_ptr<osg::Drawable> drawable = createDrawable(mesh,faceList,matrix); 329 if (drawable )373 if (drawable.valid()) 330 374 { 331 375 if (stateSet) 332 376 drawable->setStateSet(stateSet); 333 geode->addDrawable(drawable );377 geode->addDrawable(drawable.get()); 334 378 } 335 379 } … … 701 745 osg::Drawable* ReaderWriter3DS::ReaderObject::createDrawable(Lib3dsMesh *m,FaceList& faceList, const osg::Matrix * matrix) 702 746 { 703 704 osg::Geometry* geom = new osg::Geometry; 747 osg::Geometry * geom = new osg::Geometry; 705 748 unsigned int i; 706 749 … … 731 774 // create vertices. 732 775 733 osg:: Vec3Array*osg_coords = new osg::Vec3Array(noVertex);734 geom->setVertexArray(osg_coords );776 osg::ref_ptr<osg::Vec3Array> osg_coords = new osg::Vec3Array(noVertex); 777 geom->setVertexArray(osg_coords.get()); 735 778 736 779 for (i=0; i<m->nvertices; ++i) … … 753 796 if (m->texcos) 754 797 { 755 osg:: Vec2Array*osg_tcoords = new osg::Vec2Array(noVertex);756 geom->setTexCoordArray(0, osg_tcoords);798 osg::ref_ptr<osg::Vec2Array> osg_tcoords = new osg::Vec2Array(noVertex); 799 geom->setTexCoordArray(0, osg_tcoords.get()); 757 800 for (i=0; i<m->nvertices; ++i) 758 801 { … … 768 811 //Lib3dsVector * normals = new Lib3dsVector[m->nfaces*3]; 769 812 //lib3ds_mesh_calculate_vertex_normals(m, normals); 770 Lib3dsVector * normals = new Lib3dsVector[m->nfaces];771 lib3ds_mesh_calculate_face_normals(m, normals );772 osg:: Vec3Array*osg_normals = new osg::Vec3Array(noVertex);813 scoped_array<Lib3dsVector> normals( new Lib3dsVector[m->nfaces] ); // Temporary array 814 lib3ds_mesh_calculate_face_normals(m, normals.get()); 815 osg::ref_ptr<osg::Vec3Array> osg_normals = new osg::Vec3Array(noVertex); 773 816 774 817 // initialize normal list to zero's. … … 791 834 } 792 835 793 geom->setNormalArray(osg_normals );836 geom->setNormalArray(osg_normals.get()); 794 837 geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX); 795 796 838 } 797 839 else 798 840 { 799 Lib3dsVector * normals = new Lib3dsVector[m->nfaces];841 scoped_array<Lib3dsVector> normals ( new Lib3dsVector[m->nfaces] ); 800 842 lib3ds_mesh_calculate_face_normals(m, normals); 801 osg:: Vec3Array*osg_normals = new osg::Vec3Array(faceList.size());843 osg::ref_ptr<osg::Vec3Array> osg_normals = new osg::Vec3Array(faceList.size()); 802 844 osg::Vec3Array::iterator normal_itr = osg_normals->begin(); 803 845 for (fitr=faceList.begin(); … … 810 852 *(normal_itr++) = osgNormal; 811 853 } 812 geom->setNormalArray(osg_normals );854 geom->setNormalArray(osg_normals.get()); 813 855 geom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE); 814 856 } 815 857 816 osg:: Vec4ubArray*osg_colors = new osg::Vec4ubArray(1);858 osg::ref_ptr<osg::Vec4ubArray> osg_colors = new osg::Vec4ubArray(1); 817 859 (*osg_colors)[0].set(255,255,255,255); 818 geom->setColorArray(osg_colors );860 geom->setColorArray(osg_colors.get()); 819 861 geom->setColorBinding(osg::Geometry::BIND_OVERALL); 820 821 862 822 863 // create primitives 823 864 int numIndices = faceList.size()*3; 824 DrawElementsUShort*elements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES,numIndices);865 osg::ref_ptr<DrawElementsUShort> elements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES,numIndices); 825 866 DrawElementsUShort::iterator index_itr = elements->begin(); 826 867 … … 830 871 { 831 872 Lib3dsFace& face = m->faces[*fitr]; 832 833 873 *(index_itr++) = orig2NewMapping[face.index[0]]; 834 874 *(index_itr++) = orig2NewMapping[face.index[1]]; … … 836 876 } 837 877 838 geom->addPrimitiveSet(elements );878 geom->addPrimitiveSet(elements.get()); 839 879 840 880 #if 0 … … 1035 1075 local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); 1036 1076 1037 if (!createFileObject(node, file3ds, fileName, local_opt )) ok = false;1077 if (!createFileObject(node, file3ds, fileName, local_opt.get())) ok = false; 1038 1078 if (ok && !lib3ds_file_save(file3ds, fileName.c_str())) ok = false; 1039 1079 } catch (...) { … … 1070 1110 local_opt->getDatabasePathList().push_front(osgDB::getFilePath(optFileName)); 1071 1111 1072 if (!createFileObject(node, file3ds, optFileName, local_opt )) ok = false;1112 if (!createFileObject(node, file3ds, optFileName, local_opt.get())) ok = false; 1073 1113 if (ok && !lib3ds_file_write(file3ds, &io)) ok = false; 1074 1114
