static void initWave(void) { _size = (int) sqrt((float) _geo->getPositions()->getSize()); _surf.resize(_size); for(UInt32 i=0;i<_size;++i) _surf[i].resize(_size); _force.resize(_size); for(UInt32 i=0;i<_size;++i) _force[i].resize(_size); _veloc.resize(_size); for(UInt32 i=0;i<_size;++i) _veloc[i].resize(_size); _surfo.resize(_size); for(UInt32 i=0;i<_size;++i) _surfo[i].resize(_size); GeoPositions3fPtr pos = GeoPositions3fPtr::dcast(_geo->getPositions()); MFPnt3f *p = pos->getFieldPtr(); beginEditCP(pos); { int c = 0; for(int i=0;i<_size;i++) { for(int j=0;j<_size;j++) _surfo[i][j] = (*p)[c++][2]; } } endEditCP(pos); }
void display(void) { Real32 time = glutGet(GLUT_ELAPSED_TIME); updateMesh(time); // we extract the core out of the root node // as we now this is a geometry node GeometryPtr geo = GeometryPtr::dcast(scene->getChild(0)->getCore()); //now modify it's content // first we need a pointer to the position data field GeoPositions3fPtr pos = GeoPositions3fPtr::dcast(geo->getPositions()); //get the data field the pointer is pointing at GeoPositions3f::StoredFieldType *posfield = pos->getFieldPtr(); //get some iterators GeoPositions3f::StoredFieldType::iterator last, it; // set the iterator to the first data it = posfield->begin(); beginEditCP(pos, GeoPositions3f::GeoPropDataFieldMask); //now simply run over all entires in the array for (int x = 0; x < N; x++) for (int z = 0; z < N; z++){ (*it) = Pnt3f(x, wMesh[x][z], z); it++; } endEditCP(pos, GeoPositions3f::GeoPropDataFieldMask); mgr->redraw(); }
static NodePtr makePerturbedUniform (UInt16 numSubdiv, Real32 radius, Real32 rate = 0.1f) { static Real32 factor = 1.1f; NodePtr sphereNode = makeSphere(numSubdiv, radius); GeometryPtr sphere = GeometryPtr::dcast(sphereNode->getCore()); GeoPositionsPtr points = sphere->getPositions(); beginEditCP(points); for (UInt32 i=0; i<points->size(); ++i) { Real32 random = (rand()/Real32(RAND_MAX)); if (random <= rate) { points->setValue(factor*points->getValue(i), i); } } endEditCP(points); NodePtr node = Node::create(); beginEditCP(node); node->setCore(Transform::create()); node->addChild(sphereNode); endEditCP(node); return node; }
// redraw the window void display( void ) { // create the matrix Matrix m; Real32 t = glutGet(GLUT_ELAPSED_TIME ); m.setTransform(Quaternion( Vec3f(0,1,0), t / 1000.f)); // set the transform's matrix beginEditCP(trans, Transform::MatrixFieldMask); { trans->setMatrix(m); } endEditCP (trans, Transform::MatrixFieldMask); /* Manipulate the geometry. The OpenSG geometry structure is pretty flexible. The disadvantage of all this flexibility is that it can be hard to write generic tools, as pretty much all the used types can be one of a number of variants. To simplify that, every kind of GeoProperty has a generic type, e.g. the generic type for positions is Pnt3f, for colors it's Color3f. No matter the internal data representation looks like, all GeoProperties have the generic interface. As does the abstract parent class of every kind of property. Thus it's possible to access the data of an arbitrary geometry using the generic interface. */ // note that this is the abstract parent class, it doesn't have a specific // type GeoPositionsPtr pos = geo->getPositions(); beginEditCP(pos); for(UInt32 i = 0; i < pos->getSize(); i++) { Pnt3f p; pos->getValue(p, i); p[0] += osgsin(t / 300) * p[1] / 100; p[1] += osgsin(t / 300) * p[2] / 100; p[2] += osgsin(t / 300) * p[0] / 100; pos->setValue(p, i); } endEditCP (pos); // right now the geometry doesn't notice changes to the properties, it has // to be notified explicitly beginEditCP(geo, Geometry::PositionsFieldMask); endEditCP (geo, Geometry::PositionsFieldMask); mgr->redraw(); }
Action::ResultE changeGeo(NodePtr& node) { GeometryPtr geo = GeometryPtr::dcast(node->getCore()); if(geo == NullFC) return Action::Continue; GeoColors3fPtr colors = GeoColors3fPtr::dcast(geo->getColors()); if(colors == NullFC) { colors = GeoColors3f::create(); colors->resize(geo->getPositions()->getSize()); // Change the geometry to use the new colors beginEditCP(geo, Geometry::ColorsFieldMask); geo->setColors(colors); // If multi-indexed, make the colors use the same index as // the geometry if(geo->getMFIndexMapping()->size() > 0) { Int16 pind = geo->calcMappingIndex(Geometry::MapPosition); if(pind < 0) { FFATAL(("Multi-indexed, but no positions index???\n")); return Action::Continue; } // This makes the colors use the same indices as the positions geo->editIndexMapping(pind) |= Geometry::MapColor; } endEditCP (geo, Geometry::ColorsFieldMask); } beginEditCP(geo, Geometry::ColorsFieldMask); beginEditCP(colors); Real32 size = colors->getSize(); for(UInt32 i=0;i<size;++i) { Color3f c; c[0] = ((Real32) i) / size; c[1] = 0.0f; c[2] = 0.0f; colors->setValue(c, i); } endEditCP(colors); endEditCP(geo, Geometry::ColorsFieldMask); return Action::Continue; }
static void updateGeometry(GeometryPtr geo) { GeoPositions3fPtr pos = GeoPositions3fPtr::dcast(geo->getPositions()); // p->setValue() is faster than pos->setValue() MFPnt3f *p = pos->getFieldPtr(); beginEditCP(pos); int c = 0; for(int i=0;i<_size;++i) { for(int j=0;j<_size;++j) { Pnt3f &pp = (*p)[c++]; pp[2] = _surfo[i][j] + _surf[i][j]; } } endEditCP(pos); }
static NodePtr makePerturbedAll (UInt16 numSubdiv, Real32 radius, Real32 stdDeviation = 0.1f) { NodePtr sphereNode = makeSphere(numSubdiv, radius); GeometryPtr sphere = GeometryPtr::dcast(sphereNode->getCore()); GeoPositionsPtr points = sphere->getPositions(); beginEditCP(points); for (UInt32 i=0; i<points->size(); ++i) { Real32 factor = 1.0f + stdDeviation * (rand()/Real32(RAND_MAX) - 0.5f); points->setValue(factor*points->getValue(i), i); } endEditCP(points); NodePtr node = Node::create(); beginEditCP(node); node->setCore(Transform::create()); node->addChild(sphereNode); endEditCP(node); return node; }
void display(void) { Real32 time = glutGet(GLUT_ELAPSED_TIME); updateMesh(time); // we extract the core out of the root node // as we now this is a geometry node GeometryPtr geo = GeometryPtr::dcast(scene->getCore()); //now modify it's content // first we need a pointer to the position data field GeoPositions3fPtr pos = GeoPositions3fPtr::dcast(geo->getPositions()); //this loop is similar to when we generted the data during createScenegraph() beginEditCP(pos, GeoPositions3f::GeoPropDataFieldMask); // here they all come for (int x = 0; x < N; x++) for (int z = 0; z < N; z++) pos->setValue(Pnt3f(x, wMesh[x][z], z), N*x+z); endEditCP(pos, GeoPositions3f::GeoPropDataFieldMask); mgr->redraw(); }
bool VerifyGraphOp::verifyIndexMap(GeometryPtr &geo, bool &repair) { repair = false; if(geo == NullFC) return true; if(geo->getIndices() == NullFC) return true; if(!geo->getMFIndexMapping()->empty()) return true; if(geo->getPositions() == NullFC) return true; UInt32 positions_size = geo->getPositions()->getSize(); UInt32 normals_size = 0; if(geo->getNormals() != NullFC) normals_size = geo->getNormals()->getSize(); UInt32 colors_size = 0; if(geo->getColors() != NullFC) colors_size = geo->getColors()->getSize(); UInt32 secondary_colors_size = 0; if(geo->getSecondaryColors() != NullFC) secondary_colors_size = geo->getSecondaryColors()->getSize(); UInt32 texccords_size = 0; if(geo->getTexCoords() != NullFC) texccords_size = geo->getTexCoords()->getSize(); UInt32 texccords1_size = 0; if(geo->getTexCoords1() != NullFC) texccords1_size = geo->getTexCoords1()->getSize(); UInt32 texccords2_size = 0; if(geo->getTexCoords2() != NullFC) texccords2_size = geo->getTexCoords2()->getSize(); UInt32 texccords3_size = 0; if(geo->getTexCoords3() != NullFC) texccords3_size = geo->getTexCoords3()->getSize(); /* printf("sizes: %u %u %u %u %u %u %u %u\n", positions_size, normals_size, colors_size, secondary_colors_size, texccords_size, texccords1_size, texccords2_size, texccords3_size); */ if((positions_size == normals_size || normals_size == 0) && (positions_size == colors_size || colors_size == 0) && (positions_size == secondary_colors_size || secondary_colors_size == 0) && (positions_size == texccords_size || texccords_size == 0) && (positions_size == texccords1_size || texccords1_size == 0) && (positions_size == texccords2_size || texccords2_size == 0) && (positions_size == texccords3_size || texccords3_size == 0) ) { UInt16 indexmap = 0; if(positions_size > 0) indexmap |= Geometry::MapPosition; if(normals_size > 0) indexmap |= Geometry::MapNormal; if(colors_size > 0) indexmap |= Geometry::MapColor; if(secondary_colors_size > 0) indexmap |= Geometry::MapSecondaryColor; if(texccords_size > 0) indexmap |= Geometry::MapTexCoords; if(texccords1_size > 0) indexmap |= Geometry::MapTexCoords1; if(texccords2_size > 0) indexmap |= Geometry::MapTexCoords2; if(texccords3_size > 0) indexmap |= Geometry::MapTexCoords3; beginEditCP(geo, Geometry::IndexMappingFieldMask); geo->editMFIndexMapping()->push_back(indexmap); endEditCP(geo, Geometry::IndexMappingFieldMask); repair = true; return false; } else { return false; } }
/** Verify geometry method. */ Action::ResultE VerifyGraphOp::verifyGeometry(NodePtr &node) { GeometryPtr geo = GeometryPtr::dcast(node->getCore()); if(geo == NullFC) return Action::Continue; if(geo->getPositions() == NullFC) return Action::Continue; UInt32 start_errors = _numErrors; Int32 positions_size = geo->getPositions()->getSize(); Int32 normals_size = 0; if(geo->getNormals() != NullFC) normals_size = geo->getNormals()->getSize(); Int32 colors_size = 0; if(geo->getColors() != NullFC) colors_size = geo->getColors()->getSize(); Int32 secondary_colors_size = 0; if(geo->getSecondaryColors() != NullFC) secondary_colors_size = geo->getSecondaryColors()->getSize(); Int32 texccords_size = 0; if(geo->getTexCoords() != NullFC) texccords_size = geo->getTexCoords()->getSize(); Int32 texccords1_size = 0; if(geo->getTexCoords1() != NullFC) texccords1_size = geo->getTexCoords1()->getSize(); Int32 texccords2_size = 0; if(geo->getTexCoords2() != NullFC) texccords2_size = geo->getTexCoords2()->getSize(); Int32 texccords3_size = 0; if(geo->getTexCoords3() != NullFC) texccords3_size = geo->getTexCoords3()->getSize(); UInt32 pos_errors = 0; UInt32 norm_errors = 0; UInt32 col_errors = 0; UInt32 col2_errors = 0; UInt32 tex0_errors = 0; UInt32 tex1_errors = 0; UInt32 tex2_errors = 0; UInt32 tex3_errors = 0; PrimitiveIterator it; for(it = geo->beginPrimitives(); it != geo->endPrimitives(); ++it) { for(UInt32 v=0; v < it.getLength(); ++v) { if(it.getPositionIndex(v) >= positions_size) ++pos_errors; if(it.getNormalIndex(v) >= normals_size) ++norm_errors; if(it.getColorIndex(v) >= colors_size) ++col_errors; if(it.getSecondaryColorIndex(v) >= secondary_colors_size) ++col2_errors; if(it.getTexCoordsIndex(v) >= texccords_size) ++tex0_errors; if(it.getTexCoordsIndex1(v) >= texccords1_size) ++tex1_errors; if(it.getTexCoordsIndex2(v) >= texccords2_size) ++tex2_errors; if(it.getTexCoordsIndex3(v) >= texccords3_size) ++tex3_errors; } } if(norm_errors > 0) { norm_errors = 0; if(_verbose) SINFO << "removed corrupted normals!\n"; beginEditCP(geo); geo->setNormals(NullFC); endEditCP(geo); } if(col_errors > 0) { col_errors = 0; if(_verbose) SINFO << "removed corrupted colors!\n"; beginEditCP(geo); geo->setColors(NullFC); endEditCP(geo); } if(tex0_errors > 0) { tex0_errors = 0; if(_verbose) SINFO << "removed corrupted tex coords0!\n"; beginEditCP(geo); geo->setTexCoords(NullFC); endEditCP(geo); } _numErrors += (pos_errors + norm_errors + col_errors + col2_errors + tex0_errors + tex1_errors + tex2_errors + tex3_errors); // found some errors. if(_numErrors > start_errors) { _corruptedGeos.push_back(geo); } // ok we found no errors now check for missing index map. bool need_repair(false); if(!verifyIndexMap(geo, need_repair)) { if(need_repair) { SINFO << "verifyGeometry : added missing index map!" << endLog; } else { SINFO << "verifyGeometry : couldn't add missing index map!\n" << endLog; } } return Action::Continue; }
/* Aufruf dieser Funktion erfolgt bei Traversierung des Szenengraphen mittels OpenSG-Funktion traverse(). Enthaelt ein Knoten verwertbare Geometrieinformation so tragen wir Zeiger auf seine Geometrie (OpenSG-Strukturen) im array gla_meshInfo_ ein. Nebenbei bestimmen wir für die Geometrie auch noch die World-Space- Transformation (evtl. existiert eine OpenSG-Funktion um diese Information zu erhalten, der Autor hat keine in der OpenSG-API entdeckt). */ Action::ResultE enter(NodePtr &node) { int i, j, h; Pnt3f v; int numFaces, numFaceVertices, vId, size; MeshInfo meshInfo; TinyMatrix transf; FaceIterator fit; int numQuads; NamePtr namePtr; char name[255]; namePtr = NamePtr::dcast(node->findAttachment(Name::getClassType().getGroupId())); if(namePtr == osg::NullFC) strcpy(name, ""); else { strcpy(name, namePtr->getFieldPtr()->getValue().c_str()); } SINFO << "Node name = '" << name << "'" << endl << endLog; GeometryPtr geo = GeometryPtr::dcast(node->getCore()); if(geo != NullFC) { GeoPLengthsUI32Ptr pLength = GeoPLengthsUI32Ptr::dcast(geo->getLengths()); GeoPTypesUI8Ptr pTypes = GeoPTypesUI8Ptr::dcast(geo->getTypes()); /* pLength and pTypes should not be NullFC, however VRML Importer/Exporter code is instable by now, so this can happen */ if((pLength != NullFC) && (pTypes != NullFC)) { GeoPLengthsUI32::StoredFieldType * pLengthField = pLength->getFieldPtr(); GeoPTypesUI8::StoredFieldType * pTypeField = pTypes->getFieldPtr(); size = pLengthField->size(); for(h = 0; h < size; h++) { if(((*pTypeField)[h] == GL_TRIANGLES) || ((*pTypeField)[h] == GL_QUADS)) { /* may quads appear in GL_TRIANGLES ? */ /* check if all triangles have three vertices */ numQuads = 0; fit = geo->beginFaces(); while(fit != geo->endFaces()) { numFaceVertices = fit.getLength(); if(numFaceVertices == 4) numQuads++; if(numFaceVertices > 4) { SWARNING << "More than 4 vertices in face!" << endl << endLog; return Action::Continue; // exit(1); } ++fit; } if(numQuads > 0) { SWARNING << "Quad encountered" << endl << endLog; } if(gl_sga->nodeDepth_ > 0) { for(i = 0; i < gl_sga->nodeDepth_; i++) { meshInfo.transf = meshInfo.transf * gl_sga->transf_[i]; } } else meshInfo.transf.identity(); /* access to vertices */ GeoPositions3fPtr pPos = GeoPositions3fPtr::dcast(geo->getPositions()); GeoPositions3f::StoredFieldType * pPosField = pPos->getFieldPtr(); /* access to faces */ numFaces = 0; fit = geo->beginFaces(); for(fit = geo->beginFaces(); fit != geo->endFaces(); ++fit) { numFaceVertices = fit.getLength(); for(j = 0; j < numFaceVertices; j++) { vId = fit.getPositionIndex(j); } numFaces++; } /* for fit */ /* set other mesh attributes */ meshInfo.numQuads = numQuads; meshInfo.geoPtr = geo; meshInfo.vPtr = pPosField; meshInfo.triangularFaces = (numQuads == 0); meshInfo.numVertices = pPosField->size(); meshInfo.numFaces = numFaces; gl_sga->meshInfo_.push_back(meshInfo); gl_sga->numGeometryNodes_++; } else { // SWARNING << "Neither triangle nor quad. Field type = " << // (*pTypeField)[h] << endl << endLog; } } /* for h */ } /* if pLength!=NullFC */ } else if(node->getCore()->getType().isDerivedFrom(Transform::getClassType())) { TransformPtr t = TransformPtr::dcast(node->getCore()); Matrix ma; ma = t->getMatrix(); SINFO << "Node type derived from transform, skipping children" << endl << endLog; for(i = 0; i < 4; i++) { for(j = 0; j < 4; j++) { transf[i][j] = ma[j][i]; /* the matrix is stored as columns/rows ? */ } } if(gl_sga->nodeDepth_ > gl_sga->maxNodeDepth_) { gl_sga->maxNodeDepth_ = gl_sga->nodeDepth_; gl_sga->transf_.push_back(transf); } else { gl_sga->transf_[gl_sga->nodeDepth_] = transf; } gl_sga->nodeDepth_++; } return Action::Continue; }
static void calcVertexNormals(GeometryPtr geo) { GeoNormals3fPtr norms = GeoNormals3fPtr::dcast(geo->getNormals()); GeoPositions3fPtr pos = GeoPositions3fPtr::dcast(geo->getPositions()); MFPnt3f *p = pos->getFieldPtr(); MFVec3f *n = norms->getFieldPtr(); beginEditCP(norms); Vec3f a, b, c; int l = 0; for(int i=0; i<_size; ++i) { for(int j=0; j<_size; ++j) { int m = i*_size+j; if (i!=_size-1 && j!=_size-1) { a = (*p)[l+m+1] - (*p)[l+m]; b = (*p)[l+m+_size] - (*p)[l+m]; } else { a = (*p)[l+m-1] - (*p)[l+m]; int index = l+m-_size; if(index < 0) index += norms->getSize(); b = (*p)[index] - (*p)[l+m]; } c = a.cross(b); c.normalize(); if (i==0 && j==_size-1) { a = (*p)[l+m-1] - (*p)[l+m]; b = (*p)[l+m+_size] - (*p)[l+m]; c = a.cross(b); c.normalize(); c.negate(); } if (i==_size-1 && j==0) { a = (*p)[l+m-_size] - (*p)[l+m]; b = (*p)[l+m+1] - (*p)[l+m]; c = a.cross(b); c.normalize(); } (*n)[l+m] = c; } } l += _size*_size; endEditCP(norms); }
/***************************************************************************\ * Field Set * \***************************************************************************/ void PhysicsTriMeshGeom::setGeometryNode(NodePtr& node) { PhysicsTriMeshGeomPtr tmpPtr(*this); GeometryPtr geo = GeometryPtr::dcast(node->getCore()); if(geo!=NullFC) { calcVertexNormals(geo, deg2rad( 30)); separateProperties(geo); createSingleIndex(geo); GeoPositions3f::StoredFieldType* positions = GeoPositions3fPtr::dcast( geo->getPositions())->getFieldPtr(); GeoIndicesUI32::StoredFieldType* indices = GeoIndicesUI32Ptr::dcast( geo->getIndices())->getFieldPtr(); GeoNormals3f::StoredFieldType* normals = GeoNormals3fPtr::dcast( geo->getNormals())->getFieldPtr(); GeoPTypesPtr geoTypes = geo->getTypes(); bool triangles = false; //has to be some triangle soup! for( Int32 i=0; i < geoTypes->size(); ++i) { switch( geoTypes->getValue(i)) { case GL_TRIANGLES: triangles=true; break; case GL_TRIANGLE_STRIP: triangles=true; break; case GL_TRIANGLE_FAN: triangles=true; break; } } UInt32 vertexCount = GeoPositions3fPtr::dcast(geo->getPositions())->getSize(); UInt32 vertexStride = 3*sizeof(Real32); UInt32 indexCount = GeoIndicesUI32Ptr::dcast(geo->getIndices())->getSize(); UInt32 indexStride = 3*sizeof(UInt32); //pass the pointers to ODE if(tmpPtr->data) dGeomTriMeshDataDestroy(tmpPtr->data); tmpPtr->data = dGeomTriMeshDataCreate(); if(triangles) { dGeomTriMeshDataBuildSingle(tmpPtr->data, (Real32*)&positions->front(), vertexStride, vertexCount, (Int32*)&indices->front(), indexCount, indexStride/* just can't use this, (Real32*)&normals->front()*/); tmpPtr->setData(tmpPtr->data); /* use this method if you build with single precision dGeomTriMeshDataBuildSingle1(tmpPtr->data, (Real32*)&positions->front(), vertexStride, vertexCount, (Int32*)&indices->front(), indexCount, indexStride, (Real32*)&normals->front()); tmpPtr->setData(tmpPtr->data); */ } else { FWARNING(("No triangle mesh given to ODE! Convert to triangles first!\n")); tmpPtr->setData(tmpPtr->data); } } tmpPtr->geoNode=node; PhysicsTriMeshGeomBase::setGeometryNode(node); }
Action::ResultE OOCOSGFeeder::enter(NodePtr& node) { GeometryPtr geo = GeometryPtr::dcast(node->getCore()); if(geo == NullFC) return Action::Continue; GeoPositionsPtr pos = geo->getPositions(); UInt32 pntindexbase; if(_poss.find(pos) != _poss.end()) { pntindexbase = _poss[pos]; pos = NullFC; } else { pntindexbase = _pntindexbase; _poss[pos] = _pntindexbase; _pntindexbase += pos->getSize(); } UInt32 matind = MaterialPool::addMaterial(geo->getMaterial()); if(pos != NullFC) { if(_pfunc != NULL) { Pnt3f p; UInt32 s = pos->getSize(); for(UInt32 i = 0; i < s; ++i) { if(_npts != 0) { ++_pntprog; Real32 prog = _pntprog / (Real32)_npts; if(prog > _nextpntprog) { PLOG << _nextpntprog * 100 << "%.."; _nextpntprog += 0.1; } } pos->getValue(p, i); _pfunc(_rec, p); } } } if(_tfunc != NULL) { TriangleIterator it, end = geo->endTriangles(); for(it = geo->beginTriangles(); it != end; ++it) { if(_ntris != 0) { ++_triprog; Real32 prog = _triprog / (Real32)_ntris; if(prog > _nexttriprog) { PLOG << _nexttriprog * 100 << "%.."; _nexttriprog += 0.1; } } _tfunc(_rec, it.getPositionIndex(0) + pntindexbase, it.getPositionIndex(1) + pntindexbase, it.getPositionIndex(2) + pntindexbase, matind); } } return Action::Continue; }
bool SplitGraphOp::splitNode(NodePtr& node, std::vector<NodePtr> &split) { //split it only if it is a non special geometry leaf if (!isLeaf(node) || isInExcludeList(node) || !node->getCore()->getType().isDerivedFrom(Geometry::getClassType())) return false; GeometryPtr geo = GeometryPtr::dcast(node->getCore()); if ( geo->getPositions() == NullFC || geo->getPositions()->size() == 0 || geo->getLengths() == NullFC || geo->getLengths()->size() == 0 || geo->getTypes() == NullFC || geo->getTypes()->size() == 0 ) return false; //get all center points std::vector<Pnt3f> centers; int ind; PrimitiveIterator it(geo); while (!it.isAtEnd()) { switch(it.getType()) { case GL_POINTS: case GL_LINES: case GL_LINE_STRIP: case GL_LINE_LOOP: case GL_TRIANGLE_FAN: case GL_TRIANGLE_STRIP: case GL_QUAD_STRIP: case GL_POLYGON: { Pnt3f center(0,0,0); for (UInt32 i=0; i<it.getLength(); i++) center+=Vec3f(it.getPosition(i)); center/=Real32(it.getLength()); centers.push_back(center); } break; case GL_TRIANGLES: ind=0; while(it.getLength()-ind>=3) { Pnt3f center(0,0,0); for (UInt32 i=0; i<3; i++, ind++) center+=Vec3f(it.getPosition(ind)); center/=3; centers.push_back(center); } break; case GL_QUADS: ind=0; while(it.getLength()-ind>=4) { Pnt3f center(0,0,0); for (UInt32 i=0; i<4; i++, ind++) center+=Vec3f(it.getPosition(ind)); center/=4; centers.push_back(center); } break; default: SWARNING << "SplitGraphOp::splitLeave: encountered " << "unknown primitive type " << it.getType() << ", ignoring!" << std::endl; break; } ++it; } std::vector<int> order; for (UInt32 i=0; i<centers.size(); i++) order.push_back(i); Pnt3fComparator comp(centers); std::sort(order.begin(), order.end(), comp); //now we need (centers.size()/_max_polygons) amount of new geometries int ngeos=int(ceil(double(centers.size())/double(_max_polygons))); if (ngeos<=1) return false; GeometryPtr *geos = new GeometryPtr[ngeos]; GeoPTypesPtr *types = new GeoPTypesPtr[ngeos]; GeoPLengthsPtr *lens = new GeoPLengthsPtr[ngeos]; GeoPositionsPtr *pnts = new GeoPositionsPtr[ngeos]; GeoNormalsPtr *normals = new GeoNormalsPtr[ngeos]; GeoColorsPtr *colors = new GeoColorsPtr[ngeos]; GeoColorsPtr *scolors = new GeoColorsPtr[ngeos]; GeoTexCoordsPtr *tex = new GeoTexCoordsPtr[ngeos]; GeoTexCoordsPtr *tex1 = new GeoTexCoordsPtr[ngeos]; GeoTexCoordsPtr *tex2 = new GeoTexCoordsPtr[ngeos]; GeoTexCoordsPtr *tex3 = new GeoTexCoordsPtr[ngeos]; GeoIndicesPtr *indices = new GeoIndicesPtr[ngeos]; int **pni = new int*[ngeos]; int **nni = new int*[ngeos]; int **cni = new int*[ngeos]; int **sni = new int*[ngeos]; int **tni = new int*[ngeos]; int **t1ni = new int*[ngeos]; int **t2ni = new int*[ngeos]; int **t3ni = new int*[ngeos]; for (Int32 i=0; i<ngeos; i++) { geos[i] = Geometry::create(); beginEditCP(geos[i]); // Keep open until the end geos[i]->setMaterial(geo->getMaterial()); if(geo->getMFIndexMapping() != NULL) geos[i]->editMFIndexMapping()->setValues(*(geo->getMFIndexMapping())); types[i] = GeoPTypesPtr::dcast(geo->getTypes()->getType().createFieldContainer()); lens[i] = GeoPLengthsPtr::dcast(geo->getLengths()->getType().createFieldContainer()); if (geo->getIndices()!=NullFC) { indices[i] = GeoIndicesPtr::dcast(geo->getIndices()->getType().createFieldContainer()); beginEditCP(indices[i]); // Keep open until the end } else indices[i] = NullFC; beginEditCP(types[i]); // Keep open until the end beginEditCP(lens[i]); // Keep open until the end setupAttr( GeoPositionsPtr , pnts , pni , getPositions ); setupAttr( GeoNormalsPtr , normals , nni , getNormals ); setupAttr( GeoColorsPtr , colors , cni , getColors ); setupAttr( GeoColorsPtr , scolors , sni , getSecondaryColors ); setupAttr( GeoTexCoordsPtr , tex , tni , getTexCoords ); setupAttr( GeoTexCoordsPtr , tex1 , t1ni , getTexCoords1 ); setupAttr( GeoTexCoordsPtr , tex2 , t2ni , getTexCoords2 ); setupAttr( GeoTexCoordsPtr , tex3 , t3ni , getTexCoords3 ); } ind=0; it.setToBegin(); while (!it.isAtEnd()) { switch(it.getType()) { case GL_POINTS: case GL_LINES: case GL_LINE_STRIP: case GL_LINE_LOOP: case GL_TRIANGLE_FAN: case GL_TRIANGLE_STRIP: case GL_QUAD_STRIP: case GL_POLYGON: { int geoIndex=order[ind]/_max_polygons; types[geoIndex]->push_back(it.getType()); lens[geoIndex]->push_back(it.getLength()); addPoints( 0 , it.getLength() ); ++ind; } break; case GL_TRIANGLES: { UInt32 i=0; while(it.getLength()-i>=3) { i+=3; ++ind; } } break; case GL_QUADS: { UInt32 i=0; while(it.getLength()-i>=4) { i+=4; ++ind; } } break; default: SWARNING << "SplitGraphOp::splitLeave: encountered " << "unknown primitive type " << it.getType() << ", ignoring!" << std::endl; break; } ++it; } ind=0; it.setToBegin(); while (!it.isAtEnd()) { switch(it.getType()) { case GL_POINTS: case GL_LINES: case GL_LINE_STRIP: case GL_LINE_LOOP: case GL_TRIANGLE_FAN: case GL_TRIANGLE_STRIP: case GL_QUAD_STRIP: case GL_POLYGON: { ++ind; } break; case GL_TRIANGLES: { UInt32 i=0; int geoIndex; while(it.getLength()-i>=3) { geoIndex = order[ind]/_max_polygons; if (types[geoIndex]->size()>0 && types[geoIndex]->getValue(types[geoIndex]->size()-1) == GL_TRIANGLES) { int lind; if ((lind=lens[geoIndex]->size()-1)>=0) lens[geoIndex]->setValue(lens[geoIndex]->getValue(lind)+3, lind); else lens[geoIndex]->push_back(3); } else { types[geoIndex]->push_back(GL_TRIANGLES); lens[geoIndex]->push_back(3); } addPoints( i ,3 ); i+=3; ++ind; } } break; case GL_QUADS: { UInt32 i=0; while(it.getLength()-i>=4) { i+=4; ++ind; } } break; default: SWARNING << "SplitGraphOp::splitLeave: encountered " << "unknown primitive type " << it.getType() << ", ignoring!" << std::endl; break; } ++it; } ind=0; it.setToBegin(); while (!it.isAtEnd()) { switch(it.getType()) { case GL_POINTS: case GL_LINES: case GL_LINE_STRIP: case GL_LINE_LOOP: case GL_TRIANGLE_FAN: case GL_TRIANGLE_STRIP: case GL_QUAD_STRIP: case GL_POLYGON: { ++ind; } break; case GL_TRIANGLES: { UInt32 i=0; while(it.getLength()-i>=3) { i+=3; ++ind; } } break; case GL_QUADS: { UInt32 i=0; int geoIndex; while(it.getLength()-i>=4) { geoIndex = order[ind]/_max_polygons; if (types[geoIndex]->size()>0 && types[geoIndex]->getValue(types[geoIndex]->size()-1) == GL_QUADS) { int lind; if ((lind=lens[geoIndex]->size()-1)>=0) lens[geoIndex]->setValue(lens[geoIndex]->getValue(lind)+4, lind); else lens[geoIndex]->push_back(4); } else { types[geoIndex]->push_back(GL_QUADS); lens[geoIndex]->push_back(4); } addPoints( i , 4 ); i+=4; ++ind; } } break; default: SWARNING << "SplitGraphOp::splitLeave: encountered " << "unknown primitive type " << it.getType() << ", ignoring!" << std::endl; break; } ++it; } for (Int32 i=0; i<ngeos; i++) { geos[i]->setTypes(types[i]); geos[i]->setLengths(lens[i]); geos[i]->setPositions(pnts[i]); // Now close the open FCs endEditCP(types[i]); endEditCP(lens[i]); endEditCP(pnts[i]); if (indices[i]!=NullFC) { geos[i]->setIndices(indices[i]); endEditCP(indices[i]); } if (normals[i]!=NullFC) { geos[i]->setNormals(normals[i]); endEditCP(normals[i]); } if (colors[i]!=NullFC) { geos[i]->setColors(colors[i]); endEditCP(colors[i]); } if (scolors[i]!=NullFC) { geos[i]->setSecondaryColors(scolors[i]); endEditCP(scolors[i]); } if (tex[i]!=NullFC) { geos[i]->setTexCoords(tex[i]); endEditCP(tex[i]); } if (tex1[i]!=NullFC) { geos[i]->setTexCoords1(tex1[i]); endEditCP(tex1[i]); } if (tex2[i]!=NullFC) { geos[i]->setTexCoords2(tex2[i]); endEditCP(tex2[i]); } if (tex3[i]!=NullFC) { geos[i]->setTexCoords3(tex3[i]); endEditCP(tex3[i]); } endEditCP(geos[i]); if (node->getParent()!=NullFC) { NodePtr n=Node::create(); beginEditCP(n, Node::CoreFieldMask); n->setCore(geos[i]); endEditCP (n, Node::CoreFieldMask); split.push_back(n); } } for (Int32 i=0; i<ngeos; i++) { if (pni[i]) delete [] pni[i]; if (nni[i]) delete [] nni[i]; if (cni[i]) delete [] cni[i]; if (sni[i]) delete [] sni[i]; if (tni[i]) delete [] tni[i]; if (t1ni[i]) delete [] t1ni[i]; if (t2ni[i]) delete [] t2ni[i]; if (t3ni[i]) delete [] t3ni[i]; } delete [] pni; delete [] nni; delete [] cni; delete [] sni; delete [] tni; delete [] t1ni; delete [] t2ni; delete [] t3ni; return true; }