std::string OgreNodeHandler::createUniqueName(const X3DAttributes &attr, const std::string& prefix) { if (attr.isDEF()) return attr.getDEF(); string result; result.append(prefix); result.append(StringConverter::toString(++_objCount)); return result; }
int X3DXIOTNodeHandler::startShape(const X3DAttributes &attr) { #ifdef _DEBUG std::cout << "Start Shape event" << std::endl; for (size_t i = 0; i < static_cast<int>(attr.getLength()); i++) std::cout << attr.getAttributeName(static_cast<int>(i)) << std::endl; if (attr.isDEF()) std::cout << attr.getDEF() << std::endl; #endif return CONTINUE; }
int OgreNodeHandler::startShape(const X3DAttributes &attr) { std::cout << "Start Shape" << std::endl; if (attr.isUSE()) { _currentEntity = _sceneManager->getEntity(attr.getUSE())->clone(createUniqueName(attr, "shapeUSE")); } else { // We can not create a entity yet, because Ogre does not // allow an entity without a mesh. So we create the entity as // soon as we have a mesh _shapeName = attr.isDEF() ? attr.getDEF() : createUniqueName(attr, "shape"); } return 1; }
int X3DXIOTNodeHandler::startCoordinate(const X3DAttributes &attr) { int index = attr.getAttributeIndex(ID::point); if (index == -1) { //TODO: warn user instead //throw std::runtime_error("No points given within Coordinate node"); return SKIP_CHILDREN; } MFVec3f points; attr.getMFVec3f(index,points); unsigned count = points.size(); if (count == 0) return SKIP_CHILDREN; ccPointCloud* cloud = new ccPointCloud(attr.isDEF() ? attr.getDEF().c_str() : 0); if (!cloud->reserve(count)) { //not enough memory! delete cloud; return SKIP_CHILDREN; } for (MFVec3f::const_iterator it=points.begin(); it!=points.end(); ++it) cloud->addPoint(CCVector3(it->x,it->y,it->z)); assert(m_currentLeaf); m_currentLeaf->addChild(cloud); //cloud = parent vertices? if (m_currentLeaf->isKindOf(CC_MESH)) { ccGenericMesh* mesh = ccHObjectCaster::ToGenericMesh(m_currentLeaf); if (mesh->getAssociatedCloud()==0) { mesh->setAssociatedCloud(cloud); cloud->setVisible(false); } } m_currentLeaf = cloud; return CONTINUE; }
int X3DXIOTNodeHandler::startIndexedFaceSet(const X3DAttributes &attr) { std::cout << "Start IndexedFaceSet" << std::endl; ccMesh* mesh = new ccMesh(0); unsigned realFaceNumber=0; if (attr.isDEF()) mesh->setName(attr.getDEF().c_str()); //Vertices indexes int vertIndex = attr.getAttributeIndex(ID::coordIndex); if (vertIndex != -1) { MFInt32 streamIndexes; attr.getMFInt32(vertIndex,streamIndexes); int triIndexes[3]; int pos=0; for (MFInt32::const_iterator it = streamIndexes.begin(); it != streamIndexes.end(); ++it) { if (*it == -1) //end of polygon { if (pos<3) { //incomplete dataset? //TODO: warn user } pos = 0; ++realFaceNumber; } else //new vertex index { if (pos<3) { triIndexes[pos]=*it; } else { //FIXME: simplistic fan triangulation (hum, hum) triIndexes[1]=triIndexes[2]; triIndexes[2]=*it; } ++pos; } //new face if (pos==3) { //we check that we are at the end of the polygon (we don't handle non triangular meshes yet!) if (it+1 == streamIndexes.end() || *(it+1)==-1) { //we must reserve some more space for storage! if (mesh->size() == mesh->maxSize()) { if (!mesh->reserve(mesh->maxSize() + 100)) { delete mesh; return ABORT; //not enough memory! } } mesh->addTriangle(triIndexes[0],triIndexes[1],triIndexes[2]); } else { //TODO: we don't handle non triangle faces yet! } } } if (mesh->size() < mesh->maxSize()) { //unhandled type of mesh if (mesh->size()==0) { delete mesh; return SKIP_CHILDREN; } mesh->resize(mesh->size()); } } //Normals (per face) int normIndex = attr.getAttributeIndex(ID::normalIndex); if (normIndex != -1) { //per-triangle normals! if (mesh->size() == realFaceNumber) { if (mesh->reservePerTriangleNormalIndexes()) { MFInt32 perFaceNormIndexes; attr.getMFInt32(normIndex,perFaceNormIndexes); for (unsigned i=0;i<perFaceNormIndexes.size();++i) mesh->addTriangleNormalIndexes(i,i,i); mesh->showTriNorms(true); } else { //TODO: not enough memory! } } else { //TODO: we can't load per-face normals with non-triangular meshes yet! } } //Normals (per vertex) // normIndex = attr.getAttributeIndex(ID::normalPerVertex); // bool perVertexNormals=(normIndex != -1 ? attr.getSFBool(normIndex) : true); //DGM: in fact we don't care assert(m_currentLeaf); mesh->setVisible(true); m_currentLeaf->addChild(mesh); m_currentLeaf = mesh; return CONTINUE; }