- Timestamp:
- Oct 20, 2008, 5:40:38 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/physics/src/bullet/BulletSoftBody/btSoftBodyHelpers.cpp
r1963 r1972 23 23 // 24 24 static void drawVertex( btIDebugDraw* idraw, 25 26 {27 idraw->drawLine(x-btVector3(s,0,0),x+btVector3(s,0,0),c);28 idraw->drawLine(x-btVector3(0,s,0),x+btVector3(0,s,0),c);29 idraw->drawLine(x-btVector3(0,0,s),x+btVector3(0,0,s),c);30 }25 const btVector3& x,btScalar s,const btVector3& c) 26 { 27 idraw->drawLine(x-btVector3(s,0,0),x+btVector3(s,0,0),c); 28 idraw->drawLine(x-btVector3(0,s,0),x+btVector3(0,s,0),c); 29 idraw->drawLine(x-btVector3(0,0,s),x+btVector3(0,0,s),c); 30 } 31 31 32 32 // 33 33 static void drawBox( btIDebugDraw* idraw, 34 const btVector3& mins,35 const btVector3& maxs,36 const btVector3& color)37 { 38 39 btVector3(maxs.x(),mins.y(),mins.z()),40 btVector3(maxs.x(),maxs.y(),mins.z()),41 btVector3(mins.x(),maxs.y(),mins.z()),42 btVector3(mins.x(),mins.y(),maxs.z()),43 btVector3(maxs.x(),mins.y(),maxs.z()),44 btVector3(maxs.x(),maxs.y(),maxs.z()),45 btVector3(mins.x(),maxs.y(),maxs.z())};46 47 48 49 50 51 34 const btVector3& mins, 35 const btVector3& maxs, 36 const btVector3& color) 37 { 38 const btVector3 c[]={ btVector3(mins.x(),mins.y(),mins.z()), 39 btVector3(maxs.x(),mins.y(),mins.z()), 40 btVector3(maxs.x(),maxs.y(),mins.z()), 41 btVector3(mins.x(),maxs.y(),mins.z()), 42 btVector3(mins.x(),mins.y(),maxs.z()), 43 btVector3(maxs.x(),mins.y(),maxs.z()), 44 btVector3(maxs.x(),maxs.y(),maxs.z()), 45 btVector3(mins.x(),maxs.y(),maxs.z())}; 46 idraw->drawLine(c[0],c[1],color);idraw->drawLine(c[1],c[2],color); 47 idraw->drawLine(c[2],c[3],color);idraw->drawLine(c[3],c[0],color); 48 idraw->drawLine(c[4],c[5],color);idraw->drawLine(c[5],c[6],color); 49 idraw->drawLine(c[6],c[7],color);idraw->drawLine(c[7],c[4],color); 50 idraw->drawLine(c[0],c[4],color);idraw->drawLine(c[1],c[5],color); 51 idraw->drawLine(c[2],c[6],color);idraw->drawLine(c[3],c[7],color); 52 52 } 53 53 54 54 // 55 55 static void drawTree( btIDebugDraw* idraw, 56 57 58 59 60 61 62 { 63 64 { 65 66 { 67 68 69 } 70 71 { 72 73 74 75 56 const btDbvtNode* node, 57 int depth, 58 const btVector3& ncolor, 59 const btVector3& lcolor, 60 int mindepth, 61 int maxdepth) 62 { 63 if(node) 64 { 65 if(node->isinternal()&&((depth<maxdepth)||(maxdepth<0))) 66 { 67 drawTree(idraw,node->childs[0],depth+1,ncolor,lcolor,mindepth,maxdepth); 68 drawTree(idraw,node->childs[1],depth+1,ncolor,lcolor,mindepth,maxdepth); 69 } 70 if(depth>=mindepth) 71 { 72 const btScalar scl=(btScalar)(node->isinternal()?1:1); 73 const btVector3 mi=node->volume.Center()-node->volume.Extents()*scl; 74 const btVector3 mx=node->volume.Center()+node->volume.Extents()*scl; 75 drawBox(idraw,mi,mx,node->isleaf()?lcolor:ncolor); 76 76 } 77 77 } … … 82 82 static inline T sum(const btAlignedObjectArray<T>& items) 83 83 { 84 85 86 { 87 88 89 { 90 91 } 92 } 93 84 T v; 85 if(items.size()) 86 { 87 v=items[0]; 88 for(int i=1,ni=items.size();i<ni;++i) 89 { 90 v+=items[i]; 91 } 92 } 93 return(v); 94 94 } 95 95 … … 98 98 static inline void add(btAlignedObjectArray<T>& items,const Q& value) 99 99 { 100 101 { 102 100 for(int i=0,ni=items.size();i<ni;++i) 101 { 102 items[i]+=value; 103 103 } 104 104 } … … 108 108 static inline void mul(btAlignedObjectArray<T>& items,const Q& value) 109 109 { 110 111 { 112 110 for(int i=0,ni=items.size();i<ni;++i) 111 { 112 items[i]*=value; 113 113 } 114 114 } … … 118 118 static inline T average(const btAlignedObjectArray<T>& items) 119 119 { 120 121 120 const btScalar n=(btScalar)(items.size()>0?items.size():1); 121 return(sum(items)/n); 122 122 } 123 123 … … 137 137 #if 0 138 138 static btVector3 stresscolor(btScalar stress) 139 {139 { 140 140 static const btVector3 spectrum[]= { btVector3(1,0,1), 141 btVector3(0,0,1),142 btVector3(0,1,1),143 btVector3(0,1,0),144 btVector3(1,1,0),145 btVector3(1,0,0),146 btVector3(1,0,0)};141 btVector3(0,0,1), 142 btVector3(0,1,1), 143 btVector3(0,1,0), 144 btVector3(1,1,0), 145 btVector3(1,0,0), 146 btVector3(1,0,0)}; 147 147 static const int ncolors=sizeof(spectrum)/sizeof(spectrum[0])-1; 148 148 static const btScalar one=1; … … 151 151 const btScalar frc=stress-sel; 152 152 return(spectrum[sel]+(spectrum[sel+1]-spectrum[sel])*frc); 153 }153 } 154 154 #endif 155 155 156 156 // 157 157 void btSoftBodyHelpers::Draw( btSoftBody* psb, 158 159 158 btIDebugDraw* idraw, 159 int drawflags) 160 160 { 161 161 const btScalar scl=(btScalar)0.1; … … 252 252 const btVector3 c=(x[0]+x[1]+x[2])/3; 253 253 idraw->drawTriangle((x[0]-c)*scl+c, 254 (x[1]-c)*scl+c,255 (x[2]-c)*scl+c,256 col,alp);254 (x[1]-c)*scl+c, 255 (x[2]-c)*scl+c, 256 col,alp); 257 257 } 258 258 } 259 259 /* Clusters */ 260 260 if(0!=(drawflags&fDrawFlags::Clusters)) 261 {261 { 262 262 srand(1806); 263 263 for(i=0;i<psb->m_clusters.size();++i) 264 {264 { 265 265 if(psb->m_clusters[i]->m_collide) 266 {266 { 267 267 btVector3 color( rand()/(btScalar)RAND_MAX, 268 rand()/(btScalar)RAND_MAX,269 rand()/(btScalar)RAND_MAX);268 rand()/(btScalar)RAND_MAX, 269 rand()/(btScalar)RAND_MAX); 270 270 color=color.normalized()*0.75; 271 271 btAlignedObjectArray<btVector3> vertices; 272 272 vertices.resize(psb->m_clusters[i]->m_nodes.size()); 273 273 for(j=0,nj=vertices.size();j<nj;++j) 274 {274 { 275 275 vertices[j]=psb->m_clusters[i]->m_nodes[j]->m_x; 276 }276 } 277 277 HullDesc hdsc(QF_TRIANGLES,vertices.size(),&vertices[0]); 278 278 HullResult hres; … … 285 285 add(hres.m_OutputVertices,center); 286 286 for(j=0;j<(int)hres.mNumFaces;++j) 287 {287 { 288 288 const int idx[]={hres.m_Indices[j*3+0],hres.m_Indices[j*3+1],hres.m_Indices[j*3+2]}; 289 289 idraw->drawTriangle(hres.m_OutputVertices[idx[0]], 290 hres.m_OutputVertices[idx[1]], 291 hres.m_OutputVertices[idx[2]], 292 color,1); 290 hres.m_OutputVertices[idx[1]], 291 hres.m_OutputVertices[idx[2]], 292 color,1); 293 } 294 hlib.ReleaseResult(hres); 293 295 } 294 hlib.ReleaseResult(hres);295 }296 296 /* Velocities */ 297 #if 0297 #if 0 298 298 for(int j=0;j<psb->m_clusters[i].m_nodes.size();++j) 299 {299 { 300 300 const btSoftBody::Cluster& c=psb->m_clusters[i]; 301 301 const btVector3 r=c.m_nodes[j]->m_x-c.m_com; 302 302 const btVector3 v=c.m_lv+cross(c.m_av,r); 303 303 idraw->drawLine(c.m_nodes[j]->m_x,c.m_nodes[j]->m_x+v,btVector3(1,0,0)); 304 }305 #endif304 } 305 #endif 306 306 /* Frame */ 307 307 btSoftBody::Cluster& c=*psb->m_clusters[i]; … … 309 309 idraw->drawLine(c.m_com,c.m_framexform*btVector3(0,10,0),btVector3(0,1,0)); 310 310 idraw->drawLine(c.m_com,c.m_framexform*btVector3(0,0,10),btVector3(0,0,1)); 311 }312 }311 } 312 } 313 313 /* Notes */ 314 314 if(0!=(drawflags&fDrawFlags::Notes)) … … 319 319 btVector3 p=n.m_offset; 320 320 for(int j=0;j<n.m_rank;++j) 321 {321 { 322 322 p+=n.m_nodes[j]->m_x*n.m_coords[j]; 323 }323 } 324 324 idraw->draw3dText(p,n.m_text); 325 325 } … … 334 334 if(0!=(drawflags&fDrawFlags::Joints)) 335 335 { 336 337 { 338 339 336 for(i=0;i<psb->m_joints.size();++i) 337 { 338 const btSoftBody::Joint* pj=psb->m_joints[i]; 339 switch(pj->Type()) 340 340 { 341 341 case btSoftBody::Joint::eType::Linear: 342 342 { 343 344 345 346 347 348 349 343 const btSoftBody::LJoint* pjl=(const btSoftBody::LJoint*)pj; 344 const btVector3 a0=pj->m_bodies[0].xform()*pjl->m_refs[0]; 345 const btVector3 a1=pj->m_bodies[1].xform()*pjl->m_refs[1]; 346 idraw->drawLine(pj->m_bodies[0].xform().getOrigin(),a0,btVector3(1,1,0)); 347 idraw->drawLine(pj->m_bodies[1].xform().getOrigin(),a1,btVector3(0,1,1)); 348 drawVertex(idraw,a0,0.25,btVector3(1,1,0)); 349 drawVertex(idraw,a1,0.25,btVector3(0,1,1)); 350 350 } 351 351 break; 352 352 case btSoftBody::Joint::eType::Angular: 353 353 { 354 355 356 357 358 359 360 361 362 354 const btSoftBody::AJoint* pja=(const btSoftBody::AJoint*)pj; 355 const btVector3 o0=pj->m_bodies[0].xform().getOrigin(); 356 const btVector3 o1=pj->m_bodies[1].xform().getOrigin(); 357 const btVector3 a0=pj->m_bodies[0].xform().getBasis()*pj->m_refs[0]; 358 const btVector3 a1=pj->m_bodies[1].xform().getBasis()*pj->m_refs[1]; 359 idraw->drawLine(o0,o0+a0*10,btVector3(1,1,0)); 360 idraw->drawLine(o0,o0+a1*10,btVector3(1,1,0)); 361 idraw->drawLine(o1,o1+a0*10,btVector3(0,1,1)); 362 idraw->drawLine(o1,o1+a1*10,btVector3(0,1,1)); 363 363 } 364 364 } … … 369 369 // 370 370 void btSoftBodyHelpers::DrawInfos( btSoftBody* psb, 371 372 373 374 371 btIDebugDraw* idraw, 372 bool masses, 373 bool areas, 374 bool /*stress*/) 375 375 { 376 376 for(int i=0;i<psb->m_nodes.size();++i) … … 395 395 // 396 396 void btSoftBodyHelpers::DrawNodeTree( btSoftBody* psb, 397 btIDebugDraw* idraw,398 int mindepth,399 int maxdepth)400 { 401 397 btIDebugDraw* idraw, 398 int mindepth, 399 int maxdepth) 400 { 401 drawTree(idraw,psb->m_ndbvt.m_root,0,btVector3(1,0,1),btVector3(1,1,1),mindepth,maxdepth); 402 402 } 403 403 404 404 // 405 405 void btSoftBodyHelpers::DrawFaceTree( btSoftBody* psb, 406 btIDebugDraw* idraw,407 int mindepth,408 int maxdepth)409 { 410 406 btIDebugDraw* idraw, 407 int mindepth, 408 int maxdepth) 409 { 410 drawTree(idraw,psb->m_fdbvt.m_root,0,btVector3(0,1,0),btVector3(1,0,0),mindepth,maxdepth); 411 411 } 412 412 413 413 // 414 414 void btSoftBodyHelpers::DrawClusterTree( btSoftBody* psb, 415 416 417 418 { 419 415 btIDebugDraw* idraw, 416 int mindepth, 417 int maxdepth) 418 { 419 drawTree(idraw,psb->m_cdbvt.m_root,0,btVector3(0,1,1),btVector3(1,0,0),mindepth,maxdepth); 420 420 } 421 421 422 422 // 423 423 void btSoftBodyHelpers::DrawFrame( btSoftBody* psb, 424 424 btIDebugDraw* idraw) 425 425 { 426 426 if(psb->m_pose.m_bframe) … … 446 446 // 447 447 btSoftBody* btSoftBodyHelpers::CreateRope( btSoftBodyWorldInfo& worldInfo, const btVector3& from, 448 449 450 448 const btVector3& to, 449 int res, 450 int fixeds) 451 451 { 452 452 /* Create nodes */ … … 478 478 // 479 479 btSoftBody* btSoftBodyHelpers::CreatePatch(btSoftBodyWorldInfo& worldInfo,const btVector3& corner00, 480 481 482 483 484 485 486 480 const btVector3& corner10, 481 const btVector3& corner01, 482 const btVector3& corner11, 483 int resx, 484 int resy, 485 int fixeds, 486 bool gendiags) 487 487 { 488 488 #define IDX(_x_,_y_) ((_y_)*rx+(_x_)) … … 554 554 555 555 // 556 btSoftBody* btSoftBodyHelpers::CreatePatchUV(btSoftBodyWorldInfo& worldInfo,557 const btVector3& corner00,558 const btVector3& corner10,559 const btVector3& corner01,560 const btVector3& corner11,561 int resx,562 int resy,563 int fixeds,564 bool gendiags,565 float* tex_coords)566 {567 568 /*569 *570 * corners:571 *572 * [0][0] corner00 ------- corner01 [resx][0]573 * | |574 * | |575 * [0][resy] corner10 -------- corner11 [resx][resy]576 *577 *578 *579 *580 *581 *582 * "fixedgs" map:583 *584 * corner00 --> +1585 * corner01 --> +2586 * corner10 --> +4587 * corner11 --> +8588 * upper middle --> +16589 * left middle --> +32590 * right middle --> +64591 * lower middle --> +128592 * center --> +256593 *594 *595 * tex_coords size (resx-1)*(resy-1)*12596 *597 *598 *599 * SINGLE QUAD INTERNALS600 *601 * 1) btSoftBody's nodes and links,602 * diagonal link is optional ("gendiags")603 *604 *605 * node00 ------ node01606 * | .607 * | .608 * | .609 * | .610 * | .611 * node10 node11612 *613 *614 *615 * 2) Faces:616 * two triangles,617 * UV Coordinates (hier example for single quad)618 *619 * (0,1) (0,1) (1,1)620 * 1 |\ 3 \-----| 2621 * | \ \ |622 * | \ \ |623 * | \ \ |624 * | \ \ |625 * 2 |-----\ 3 \| 1626 * (0,0) (1,0) (1,0)627 *628 *629 *630 *631 *632 *633 */634 635 #define IDX(_x_,_y_) ((_y_)*rx+(_x_))636 /* Create nodes */637 if((resx<2)||(resy<2)) return(0);638 const int rx=resx;639 const int ry=resy;640 const int tot=rx*ry;641 btVector3* x=new btVector3[tot];642 btScalar* m=new btScalar[tot];643 644 for(int iy=0;iy<ry;++iy)645 {646 const btScalar ty=iy/(btScalar)(ry-1);647 const btVector3 py0=lerp(corner00,corner01,ty);648 const btVector3 py1=lerp(corner10,corner11,ty);649 for(int ix=0;ix<rx;++ix)650 {651 const btScalar tx=ix/(btScalar)(rx-1);652 x[IDX(ix,iy)]=lerp(py0,py1,tx);653 m[IDX(ix,iy)]=1;654 }655 }656 btSoftBody* psb=new btSoftBody(&worldInfo,tot,x,m);657 if(fixeds&1) psb->setMass(IDX(0,0),0);658 if(fixeds&2) psb->setMass(IDX(rx-1,0),0);659 if(fixeds&4) psb->setMass(IDX(0,ry-1),0);660 if(fixeds&8) psb->setMass(IDX(rx-1,ry-1),0);661 if(fixeds&16) psb->setMass(IDX((rx-1)/2,0),0);662 if(fixeds&32) psb->setMass(IDX(0,(ry-1)/2),0);663 if(fixeds&64) psb->setMass(IDX(rx-1,(ry-1)/2),0);664 if(fixeds&128) psb->setMass(IDX((rx-1)/2,ry-1),0);665 if(fixeds&256) psb->setMass(IDX((rx-1)/2,(ry-1)/2),0);666 delete[] x;667 delete[] m;668 669 670 int z = 0;671 /* Create links and faces */672 for(int iy=0;iy<ry;++iy)673 {674 for(int ix=0;ix<rx;++ix)675 {676 const bool mdx=(ix+1)<rx;677 const bool mdy=(iy+1)<ry;678 679 int node00=IDX(ix,iy);680 int node01=IDX(ix+1,iy);681 int node10=IDX(ix,iy+1);682 int node11=IDX(ix+1,iy+1);683 684 if(mdx) psb->appendLink(node00,node01);685 if(mdy) psb->appendLink(node00,node10);686 if(mdx&&mdy)687 {688 psb->appendFace(node00,node10,node11);689 if (tex_coords) {690 tex_coords[z+0]=CalculateUV(resx,resy,ix,iy,0);691 tex_coords[z+1]=CalculateUV(resx,resy,ix,iy,1);692 tex_coords[z+2]=CalculateUV(resx,resy,ix,iy,0);693 tex_coords[z+3]=CalculateUV(resx,resy,ix,iy,2);694 tex_coords[z+4]=CalculateUV(resx,resy,ix,iy,3);695 tex_coords[z+5]=CalculateUV(resx,resy,ix,iy,2);696 }697 psb->appendFace(node11,node01,node00);698 if (tex_coords) {699 tex_coords[z+6 ]=CalculateUV(resx,resy,ix,iy,3);700 tex_coords[z+7 ]=CalculateUV(resx,resy,ix,iy,2);701 tex_coords[z+8 ]=CalculateUV(resx,resy,ix,iy,3);702 tex_coords[z+9 ]=CalculateUV(resx,resy,ix,iy,1);703 tex_coords[z+10]=CalculateUV(resx,resy,ix,iy,0);704 tex_coords[z+11]=CalculateUV(resx,resy,ix,iy,1);705 }706 if (gendiags) psb->appendLink(node00,node11);707 z += 12;708 }709 }710 }711 /* Finished */712 #undef IDX713 return(psb);714 }715 716 float btSoftBodyHelpers::CalculateUV(int resx,int resy,int ix,int iy,int id)717 {718 719 /*720 *721 *722 * node00 --- node01723 * | |724 * node10 --- node11725 *726 *727 * ID map:728 *729 * node00 s --> 0730 * node00 t --> 1731 *732 * node01 s --> 3733 * node01 t --> 1734 *735 * node10 s --> 0736 * node10 t --> 2737 *738 * node11 s --> 3739 * node11 t --> 2740 *741 *742 */743 744 float tc=0.0f;745 if (id == 0) {746 tc = (1.0f/((resx-1))*ix);747 }748 else if (id==1) {749 tc = (1.0f/((resy-1))*(resy-1-iy));750 }751 else if (id==2) {752 tc = (1.0f/((resy-1))*(resy-1-iy-1));753 }754 else if (id==3) {755 tc = (1.0f/((resx-1))*(ix+1));756 }757 return tc;758 }759 //760 556 btSoftBody* btSoftBodyHelpers::CreateEllipsoid(btSoftBodyWorldInfo& worldInfo,const btVector3& center, 761 762 557 const btVector3& radius, 558 int res) 763 559 { 764 560 struct Hammersley … … 791 587 // 792 588 btSoftBody* btSoftBodyHelpers::CreateFromTriMesh(btSoftBodyWorldInfo& worldInfo,const btScalar* vertices, 793 794 589 const int* triangles, 590 int ntriangles) 795 591 { 796 592 int maxidx=0; … … 833 629 // 834 630 btSoftBody* btSoftBodyHelpers::CreateFromConvexHull(btSoftBodyWorldInfo& worldInfo, const btVector3* vertices, 835 631 int nvertices) 836 632 { 837 633 HullDesc hdsc(QF_TRIANGLES,nvertices,vertices);
Note: See TracChangeset
for help on using the changeset viewer.