Re: [Plib-users] triangles from branches
Brought to you by:
sjbaker
From: Paolo S. <ax...@ti...> - 2004-10-26 08:53:59
|
Steve Baker wrote: ... > Take a look at the TORCS project - it uses PLIB and ODE. I will. Until now I saw the nick's solution and I tried to implement it, since it doesn't need to access the triangle data. ... > Well, you need to walk the tree structure of the model using things > like getNumKids() and getKid(n) at each node and using isAKindOf to > recognise whether you have a leaf node or a branch node...for example: > > > void processObj ( ssgEntity *n ) > { > if ( n == NULL ) return ; > > if ( n -> isAKindOf ( ssgTypeLeaf() ) ) > { > ssgLeaf *l = (ssgLeaf *) n ; > ....DO SOMETHING WITH THE TRIANGLES... > return ; > } > > ssgBranch *b = (ssgBranch *) n ; > > for ( int i = 0 ; i < b -> getNumKids () ; i++ ) > processObj ( b -> getKid ( i ) ) ; > } > > ssgLeaf nodes contain the triangles - there are member functions > to get the indices of the three vertices of the N'th triangle > (getTriangle) and to retrieve the vertex, colour, normal and > texture coordinate given a vertex index: getVertex()/getColour()/ > getNormal()/getTexCoord(). I wrote a function that uses the algorithm that you and Wolfram suggest: static void getTriangleData( ssgEntity * ent, dReal * vertices, int * indices, int *vert_offset, int *ind_offset ) { int i,j; if (ent->getNumKids() == 0){ ssgLeaf * le = (ssgLeaf *) ent; int ind_start = *ind_offset; int ind_max = 0; for (i=0; i<le->getNumTriangles(); i++) { short int ind[3]; le->getTriangle(i, &ind[0], &ind[1], &ind[2]); for (j=0; j<3; j++){ indices[*ind_offset+j] = ind[j]; if (indices[*ind_offset+j]>ind_max) ind_max=indices[*ind_offset+j]; indices[*ind_offset+j] += ind_start; } (*ind_offset)+=3; } for (i = 0; i < ind_max; i++) { float *vert; vert = le->getVertex(i); for (j=0; j<3; j++){ vertices[*vert_offset+i*3+j] = (dReal) vert[j]; } (*vert_offset)+=3; } } else { ssgBranch * br = (ssgBranch *) ent; for (i=0; i<br->getNumKids(); i++){ getTriangleData(br->getKid(i), vertices, indices, vert_offset, ind_offset); } } } but I fear that I have to handle also the transformation nodes to get right vertices positions. And I have no idea how to calculate the exact dimensions of the arrays I should pass to this function. Just for the indices I used: static int getNumIndices(ssgEntity * ent) { int i, num = 0; if (ent->getNumKids() == 0){ ssgLeaf * le = (ssgLeaf *) ent; return (le->getNumTriangles())*3; } else { ssgBranch * br = (ssgBranch *) ent; for (i=0; i<br->getNumKids(); i++){ num += getNumIndices(br->getKid(i)); } return num; } } Since I didn't find any simple way to check if the triangle data I get with this method are correct (of course when I use this data I get wreid collisions :)) I gave up... Maybe torcs people have found a solution. -- Paolo Sacconier |