int X3DXIOTNodeHandler::startTransform(const X3DAttributes &attr) { #ifdef _DEBUG std::cout << "Start Transform" << std::endl; #endif // Get all the X3D tranformation data SFVec3f translation; SFRotation rotation; int index = attr.getAttributeIndex(ID::translation); if (index != -1) { attr.getSFVec3f(index,translation); } index = attr.getAttributeIndex(ID::rotation); if (index != -1) { attr.getSFRotation(index,rotation); } assert(m_currentLeaf); if (m_currentLeaf) { ccGLMatrix mat; mat.initFromParameters(rotation.angle, CCVector3(rotation.x,rotation.y,rotation.z), CCVector3(translation.x,translation.y,translation.z)); m_currentLeaf->setGLTransformation(mat); } return CONTINUE; }
int OgreNodeHandler::startIndexedLineSet(const X3DAttributes &attr) { _currentGeometry = new IndexedGeometry(createUniqueName(attr, "indexedLineSet")); int index = attr.getAttributeIndex(ID::coordIndex); if (index != -1) _currentGeometry->setCoordIndex(attr.getMFInt32(index)); index = attr.getAttributeIndex(ID::colorPerVertex); _currentGeometry->setColorPerVertex(index != -1 ? attr.getSFBool(index) : true); return CONTINUE; }
virtual int startBox(const X3DAttributes& attr) { testEvent(14, "start Box"); int index = attr.getAttributeIndex(ID::size); assert(index != -1); return CONTINUE; }
int OgreNodeHandler::startIndexedFaceSet(const X3DAttributes &attr) { std::cout << "Start IndexedFaceSet" << std::endl; _currentGeometry = new IndexedGeometry(createUniqueName(attr, "indexedFaceSet")); int index = attr.getAttributeIndex(ID::coordIndex); if (index != -1) _currentGeometry->setCoordIndex(attr.getMFInt32(index)); index = attr.getAttributeIndex(ID::normalIndex); if (index != -1) _currentGeometry->setNormalIndex(attr.getMFInt32(index)); index = attr.getAttributeIndex(ID::normalPerVertex); _currentGeometry->setNormalPerVertex(index != -1 ? attr.getSFBool(index) : true); return 1; }
int OgreNodeHandler::startDirectionalLight(const X3DAttributes &attr) { Light* light = _sceneManager->createLight(createUniqueName(attr, "directionalLight")); light->setType(Ogre::Light::LT_DIRECTIONAL); // Get the X3D values; SFVec3f direction; SFColor color; int index = attr.getAttributeIndex(ID::direction); if (index != -1) { direction = attr.getSFVec3f(index); } else direction.z = -1; index = attr.getAttributeIndex(ID::color); if (index != -1) { color = attr.getSFColor(index); } else color.r = color.g = color.b = 1; index = attr.getAttributeIndex(ID::intensity); float intensity = (index == -1) ? 1 : attr.getSFFloat(index); index = attr.getAttributeIndex(ID::on); bool on = (index == -1) ? true : attr.getSFBool(index); // Set the Ogre values light->setDirection(direction.x, direction.y, direction.z); Ogre::ColourValue colourValue(color.r, color.g, color.b); colourValue *= intensity; light->setDiffuseColour(colourValue); light->setSpecularColour(colourValue); light->setVisible(on); _nodeStack.top()->attachObject(light); return 1; }
virtual int startMaterial(const X3DAttributes& attr) { testEvent(11, "start Material"); int index = attr.getAttributeIndex(ID::diffuseColor); assert(index != -1); SFColor diffuseColor; attr.getSFColor(index, diffuseColor); assert(diffuseColor.r == 1.0); assert(diffuseColor.g == 0.0); assert(diffuseColor.b == 0.0); cout << "Diffuse Color is: " << diffuseColor.r << " " << diffuseColor.g << " " << diffuseColor.b << endl; index = attr.getAttributeIndex(ID::transparency); assert(index != -1); float transparency = attr.getSFFloat(index); assert(transparency == 0.1f); return CONTINUE; }
int OgreNodeHandler::startColor(const X3DAttributes &attr) { if(!_currentGeometry) throw std::runtime_error("Color currently only supported for IndexedFaceSets"); int index = attr.getAttributeIndex(ID::color); if (index != -1) _currentGeometry->setColors(attr.getMFColor(index)); else throw std::runtime_error("No color given within Color node"); return 1; }
int OgreNodeHandler::startNormal(const X3DAttributes &attr) { if(!_currentGeometry) throw std::runtime_error("Normal currently only supported for IndexedFaceSets"); int index = attr.getAttributeIndex(ID::vector); if (index != -1) _currentGeometry->setNormals(attr.getMFVec3f(index)); else throw std::runtime_error("No points given within Coordinate node"); return 1; }
int OgreNodeHandler::startSphere(const X3DAttributes &attr) { std::cout << "Start Sphere\n"; int index = attr.getAttributeIndex(ID::radius); float radius = index == -1 ? 1.0 : attr.getSFFloat(index); const string name = createUniqueName(attr, "sphere"); GeomUtils::createSphere(name , radius, 20, 20, true, false); _currentMesh = MeshManager::getSingleton().getByName(name); _currentEntity = _sceneManager->createEntity(_shapeName, name); return 1; }
int OgreNodeHandler::startTransform(const X3DAttributes &attr) { std::cout << "Start Transform" << std::endl; // Get all the X3D tranformation data SFVec3f translation; SFRotation rotation; int index = attr.getAttributeIndex(ID::translation); if (index != -1) { translation = attr.getSFVec3f(index); } index = attr.getAttributeIndex(ID::rotation); if (index != -1) { rotation = attr.getSFRotation(index); } SceneNode* node = _nodeStack.top()->createChildSceneNode(createUniqueName(attr, "transform")); node->resetToInitialState(); node->translate(translation.x, translation.y, translation.z); node->rotate(Vector3(rotation.x, rotation.y, rotation.z), Radian(rotation.angle)); _nodeStack.push(node); 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; }
int OgreNodeHandler::startMaterial(const X3DAttributes &attr) { std::cout << "Start Material" << std::endl; if (!_currentMaterial.isNull()) { Pass* pass = _currentMaterial->getTechnique(0)->getPass(0); int index = attr.getAttributeIndex(ID::ambientIntensity); float ambientIntensity = (index == -1) ? 0.2f : attr.getSFFloat(index); index = attr.getAttributeIndex(ID::transparency); float transparency = (index == -1) ? 0.0f : attr.getSFFloat(index); SFColor diffuseColor; index = attr.getAttributeIndex(ID::diffuseColor); if (index != -1) { diffuseColor = attr.getSFColor(index); } else { diffuseColor.r = diffuseColor.g = diffuseColor.b = 0.8; } SFColor specularColor; index = attr.getAttributeIndex(ID::specularColor); if (index != -1) { specularColor = attr.getSFColor(index); } SFColor emissiveColor; index = attr.getAttributeIndex(ID::emissiveColor); if (index != -1) { emissiveColor = attr.getSFColor(index); } index = attr.getAttributeIndex(ID::shininess); float shininess = (index == -1) ? 0.2f : attr.getSFFloat(index); shininess = Math::Clamp(shininess * 128.0f, 0.0f, 128.0f); pass->setAmbient(ambientIntensity * diffuseColor.r, ambientIntensity * diffuseColor.g, ambientIntensity * diffuseColor.b); pass->setDiffuse(diffuseColor.r, diffuseColor.g, diffuseColor.b, 1.0f - transparency); pass->setSpecular(specularColor.r, specularColor.g, specularColor.b, 1.0f - transparency); pass->setSelfIllumination(emissiveColor.r, emissiveColor.g, emissiveColor.b); pass->setShininess( shininess ); pass->setLightingEnabled(true); } return 1; }