void Plugin::ReadNode(domNodeRef node, ISceneNode *parentNode) { MapNode::iterator it = _mapNode.find(node->getId()); if (it != _mapNode.end()) return; LOG_DEBUG << "Reading Scene Node " << node->getId() << std::endl; Object *newNode = new Object(Box3f(), _globalMatrix); if (parentNode != NULL) parentNode->As<Object>()->AddChild(newNode); _mapNode[node->getId()] = newNode; // get back the matrix tranformation ReadNodeTranforms(newNode, node, (parentNode != NULL) ? parentNode->As<Object>() : NULL); // Process Instance Geometries for (unsigned int i = 0; i < node->getInstance_geometry_array().getCount(); i++) { domInstance_geometryRef lib = node->getInstance_geometry_array()[i]; xsAnyURI &urltype = lib->getUrl(); urltype.resolveElement(); domElement *element = (domElement*)urltype.getElement(); if (element == NULL) // this instance geometry is not found skip to the next one continue; ReadDrawable((domGeometry*)element, newNode); } if (node->getInstance_geometry_array().getCount() > 0) newNode->ChooseDefaultMaterial(); /* // Process Instance Controllers for (int i = 0; i < node->getInstance_controller_array().getCount(); i++) { domInstance_controller *icontroller = node->getInstance_controller_array()[i]; CrtInstanceController * instanceController = ReadInstanceController(icontroller); if (instanceController==NULL) //if instance Controller can not be created, skip to the next one continue; instanceController->Parent = crtNode; crtNode->InstanceControllers.push_back(instanceController); ControllerInstances.push_back(instanceController); } */ // read childrens for (unsigned int i = 0; i < node->getNode_array().getCount(); i++) ReadNode(node->getNode_array()[i], newNode); // read children <instance_nodes> for (unsigned int i = 0; i < node->getInstance_node_array().getCount(); i++) { domInstance_node *instance_node = node->getInstance_node_array()[i]; domNode *urlnode = (domNode*) (domElement*) instance_node->getUrl().getElement(); if (urlnode) ReadNode(urlnode, newNode); } }
/*! Add a transform node to the OpenSG tree representing this <node>. Also finalizes transform animations. */ void ColladaNode::appendStackedXForm(TransformationElement *transformElement, domNodeRef node) { OSG_ASSERT(getGlobal()->getOptions()->getFlattenNodeXForms()); StackedTransformUnrecPtr bottomTrans(NULL); if(_bottomN != NULL) { if(_bottomN->getCore()->getType().isDerivedFrom(StackedTransform::getClassType())) { bottomTrans = dynamic_cast<StackedTransform*>(_bottomN->getCore()); } else { bottomTrans = StackedTransform::create(); NodeUnrecPtr xformN = makeNodeFor(bottomTrans); setName(xformN, node->getName()); _bottomN->addChild(xformN); } } else { bottomTrans = StackedTransform::create(); _bottomN = makeNodeFor(bottomTrans); setName(_bottomN, node->getName()); } bottomTrans->pushToTransformElements(transformElement); if(_topN == NULL) { _topN = _bottomN; } }
void getAnimationChannels(const domNodeRef& node, std::list<domChannelRef>& channels) { assert(node->getId()); std::string nodeIdSlash (node->getId()); nodeIdSlash.append("/"); domCOLLADA* root = (domCOLLADA*)node->getDocument()->getDomRoot(); domLibrary_animations_Array& animationLibrary = root->getLibrary_animations_array(); size_t animationLibraryCount = animationLibrary.getCount(); for (size_t i = 0; i < animationLibraryCount; ++i) { domLibrary_animationsRef& animationsRef = animationLibrary.get(i); domAnimation_Array& animationArray = animationsRef->getAnimation_array(); size_t animationCount = animationArray.getCount(); for (size_t j = 0; j < animationCount; ++j) { domAnimationRef& animationRef = animationArray.get(j); getAnimationChannels(animationRef, nodeIdSlash, channels); } } // Recursively do the same for all nodes daeTArray< daeSmartRef<daeElement> > children; node->getChildren(children); size_t childCount = children.getCount(); for (size_t i = 0; i < childCount; ++i) { daeElementRef childElement = children[i]; if (childElement->typeID() == COLLADA_TYPE::NODE) { domNodeRef childNode = daeSafeCast<domNode>(childElement); getAnimationChannels(childNode, channels); } } }
IZ_BOOL CColladaAnimation::ReadJoint(domNodeRef pNode) { domNodeType type = pNode->getType(); if (type == NODETYPE_JOINT) { SJoint sJoint; sJoint.name = pNode->getName(); for (size_t n = 0; n < pNode->getContents().getCount(); n++) { domElement* pContent = pNode->getContents().get(n); daeString strType = pContent->getTypeName(); if (izanagi::tool::CString::CmpStr(strType, "node")) { // terminate... continue; } SJointTransform sTransform; sTransform.type = GetTransformType(strType); switch (sTransform.type) { case E_TRANSFROM_TYPE_TRANSLATE: GetTransform<domTranslate, 3>(pContent, sTransform); break; case E_TRANSFROM_TYPE_QUARTANION: GetTransform<domRotate, 4>(pContent, sTransform); break; case E_TRANSFROM_TYPE_SCALE: GetTransform<domScale, 3>(pContent, sTransform); break; default: VRETURN(IZ_FALSE); break; } sJoint.transforms.push_back(sTransform); } if (!sJoint.transforms.empty()) { m_Joints.push_back(sJoint); } } size_t nChildNodeCnt = pNode->getNode_array().getCount(); for (size_t n = 0; n < nChildNodeCnt; n++) { domNodeRef node = pNode->getNode_array().get(n); VRETURN(ReadJoint(node)); } return IZ_TRUE; }
int getIndex(const domInstance_controller::domSkeleton_Array& skeletonArray, const domNodeRef& node) { const std::string nodeId = node->getId(); size_t count = skeletonArray.getCount(); for (size_t i = 0; i < count; ++i) { const domInstance_controller::domSkeletonRef& skeleton = skeletonArray.get(i); daeElementRef element = skeleton->getValue().getElement(); if (element->typeID() == COLLADA_TYPE::NODE) { domNodeRef targetNode = daeSafeCast<domNode>(element); if (nodeId.compare(targetNode->getId()) == 0) { return i; } } } return -1; }
void Plugin::ReadNodeTranforms(Object *node, domNodeRef domNode, Object *) { // load the node transformations as they are to be able to // handle any matrix stack configurations independant of the tools for (unsigned int i = 0; i < domNode->getContents().getCount(); i++) { // get the component type string char *typeName = (char*)domNode->getContents()[i]->getTypeName(); // set the matrix transformation with the good attributes if (strcmp(typeName, "rotate") == 0) { // load rotation domRotateRef rotateArray = (domRotate*)(domElement*)domNode->getContents()[i]; if (rotateArray->getValue().getCount() != 4) LOG_DEBUG << "something wrong when we load the rotation matrix transformation" << std::endl; else { node->Matrix.AddRotation(Vector3f((float)rotateArray->getValue()[0], (float)rotateArray->getValue()[1], (float)rotateArray->getValue()[2]), (float)rotateArray->getValue()[3]); //transform->SetRotate( rot ); // this will be used to bind to an animation later //sid = (CrtChar *)rotateArray->getSid(); //if ( sid ) // transform->SetSid( sid ); //crtNode->Transforms.push_back(transform); } } else if (strcmp(typeName, "translate") == 0) { // load translation domTranslateRef translateArray = (domTranslate*)(domElement*)domNode->getContents()[i]; if (translateArray->getValue().getCount() != 3) LOG_DEBUG << "something wrong when we load the rotation matrix transformation" << std::endl; else { // get the transation data node->Matrix.AddTranslation((float)translateArray->getValue()[0], (float)translateArray->getValue()[1], (float)translateArray->getValue()[2]); //crtNode->Transforms.push_back(transform); } } else if (strcmp(typeName, "scale") == 0) { // load scale domScaleRef scaleArray = (domScale*)(domElement*)domNode->getContents()[i]; if (scaleArray->getValue().getCount() != 3) LOG_DEBUG << "something wrong when we load the rotation matrix transformation" << std::endl; else { // get the rotation data node->Matrix.AddScale((float)scaleArray->getValue()[0], (float)scaleArray->getValue()[1], (float)scaleArray->getValue()[2]); //crtNode->Transforms.push_back(transform); } } else if (strcmp(typeName, "matrix") == 0) { domMatrixRef matrixArray = (domMatrix*)(domElement*)domNode->getContents()[i]; TMatrix mat; for (int j = 0; j < 4; ++j) for (int k = 0; k < 4; ++k) mat[j][k] = (float)matrixArray->getValue()[(j*4) + k]; node->Matrix.AddTransformation(mat); //crtNode->Transforms.push_back(transform); } /* for (unsigned int i=0; i<Animations.size(); i++) { CrtAnimation * anim = Animations[i]; if (anim->HasFoundTarget()) continue; CrtBool found_target = CrtFalse; for (CrtUInt j=0; j<anim->Channels.size(); j++) { CrtChar * target_id = anim->Channels[j]->GetTargetID(); CrtChar * target_sid = anim->Channels[j]->GetTargetSID(); CrtChar * id = crtNode->GetId(); if (target_id && target_sid && id && sid) { if (CrtICmp(target_id, id) && CrtICmp(target_sid, sid)) { anim->SetFoundTarget(); transform->SetAnimation(anim); transform->AddChannelId(j); found_target = CrtTrue; crtNode->SetAnimation( anim ); // don't want to break, though there maybe be multiple anim targets NumAnimatedNodes++; break; } } } if (found_target) break; } */ } }
void processNode(domNodeRef node, DAE &dae, const char *cmdlntexfn) { daeTArray<daeElementRef> nodeChildren = node->getChildren(); for (size_t j = 0; j < nodeChildren.getCount(); j++) { const daeElementRef nodeChild = nodeChildren[j]; const daeString nodeChildKind = nodeChild->getElementName(); if (!strcmp(nodeChildKind, "node")) processNode(daeSafeCast<domNode>(nodeChild), dae, cmdlntexfn); else if (!strcmp(nodeChildKind, "instance_geometry")) { const daeTArray<daeElementRef> geometryChildren = daeSafeCast<domInstance_geometry>(nodeChild)->getUrl().getElement()->getChildren(); for (size_t k = 0; k < geometryChildren.getCount(); k++) { const daeElementRef geometryChild = geometryChildren[k]; const daeString geometryChildKind = geometryChild->getElementName(); if (!strcmp(geometryChildKind, "mesh")) { const domMeshRef mesh = daeSafeCast<domMesh>(geometryChild); const domTriangles_Array trianglesArray = mesh->getTriangles_array(); for (size_t i = 0; i < trianglesArray.getCount(); i++) { char *effect = NULL; string windex; string texturefnstr; const char *texturefn = cmdlntexfn; daeElement *squeegee = nodeChild->getChild("bind_material"); if (squeegee) { squeegee = squeegee->getChild("technique_common"); if (squeegee) { squeegee = squeegee->getChild("instance_material"); if (squeegee) { windex = squeegee->getAttribute("target"); effect = (char*)malloc(windex.length()+1); if (!effect) { perror("malloc"); exit(0); } strcpy(effect, windex.c_str()); daeElement *effectel = daeSafeCast<domInstance_material>(squeegee)->getTarget().getElement(); if (effectel) { daeElement *profile_COMMON = effectel->getChild("profile_COMMON"); if (profile_COMMON) { daeElement *technique = profile_COMMON->getChild("technique"); if (technique) { daeElement *phong = technique->getChild("phong"); if (phong) { daeElement *diffuse = phong->getChild("diffuse"); if (diffuse) { daeElement *texture = diffuse->getChild("texture"); if (texture) { const string samplerSid = texture->getAttribute("texture"); daeElement *samplernewparam = daeSidRef(samplerSid, effectel, "COMMON").resolve().elt; if (samplernewparam) { daeElement *sampler2D = samplernewparam->getChild("sampler2D"); if (sampler2D) { daeElement *samplersrcel = sampler2D->getChild("source"); daeElement *minfilterel = sampler2D->getChild("minfilter"); daeElement *magfilterel = sampler2D->getChild("magfilter"); if (samplersrcel && minfilterel && magfilterel) { const string surfSid = samplersrcel->getCharData(); daeElement *surfnewparam = daeSidRef(surfSid, effectel, "COMMON").resolve().elt; if (surfnewparam) { daeElement *surface = surfnewparam->getChild("surface"); if (surface) { daeElement *init_from = surface->getChild("init_from"); if (init_from) { daeElement *imageel = dae.getDatabase()->idLookup(init_from->getCharData(), init_from->getDocument()); if (imageel) { daeElement *imageinit_from = imageel->getChild("init_from"); if (imageinit_from) { texturefnstr = imageinit_from->getCharData(); texturefn = texturefnstr.c_str(); } } } } } } } } } } } } } } } } } TriangleMesh x(trianglesArray[i], effect); x.transform(texturefn); } } } } } }
SceneGraph::Vertex Scene::readNode(const domNodeRef node, SceneGraph::Vertex &parent) { // just to experiment with fonts bool is_font_node = false; std::string::size_type pos = std::string(node->getId()).find(SceneGraph::font_name); if(pos != std::string::npos) is_font_node = true; SceneGraph::IdVertexMap::iterator findnode = this->scene_graph.id_vertex_map.find(node->getId()); if(findnode != this->scene_graph.id_vertex_map.end()) return findnode->second; this->scene_graph.printStatusMessage(std::string(" reading scene node ") + node->getID()); size_t i; // Insert new node and connect to parent SceneGraph::NodeInfo new_node_info = {node->getId(), CheckString(node->getSid())}; this->scene_graph.insertNode(parent, CheckString(node->getName()), node->getId(), CheckString(node->getSid())); SceneGraph::node_id_map_t node_id_map = get(SceneGraph::node_info_t(), this->scene_graph.node_graph); // Read node transformations const SceneGraph::NodeInfo &parent_node_info = node_id_map[parent]; const Node *parent_node = this->scene_graph.all_nodes.find(parent_node_info.id.c_str()); Node ¤t_node = *this->scene_graph.all_nodes.find(new_node_info.id.c_str()); current_node.is_font_node = is_font_node; SceneGraph::Vertex node_vertex = this->scene_graph.id_vertex_map.find(new_node_info.id)->second; this->readNodeTranforms(current_node, *parent_node, node); // Process Instance Geometries const domInstance_geometry_Array &geom_instances = node->getInstance_geometry_array(); for(i = 0; i < geom_instances.getCount(); ++i) this->readInstanceGeometry(current_node, geom_instances[i]); // Process Instance Lights const domInstance_light_Array &lia = node->getInstance_light_array(); for(i = 0; i < lia.getCount(); ++i) this->readInstanceLight(current_node, lia[i]); if(!current_node.instance_lights.empty()) this->scene_graph.light_nodes.push_back(new_node_info.id); // Process Instance Cameras const domInstance_camera_Array &ica = node->getInstance_camera_array(); for(i = 0; i < ica.getCount(); ++i) this->readInstanceCamera(current_node, ica[i]); // Read in each child and recursively it's children const domNode_Array &nodes = node->getNode_array(); for(i = 0; i < nodes.getCount(); i++) this->readNode(nodes[i], node_vertex); // Read children <instance_nodes>, can be 0 or more const domInstance_node_Array &instance_nodes = node->getInstance_node_array(); for(i = 0; i < instance_nodes.getCount(); i++) { domInstance_node *instance_node = instance_nodes[i]; domNode *urlnode = (domNode*)(domElement*)instance_node->getUrl().getElement(); if(urlnode) this->readNode(urlnode, node_vertex); } return node_vertex; }
void processNode(domNodeRef node, Matrix4 m_trans, vector<AmbientLight> *ambientLights, vector<PointLight> *pointLights, vector<Cube> *cubes, vector<Sphere> *spheres, vector<Cylinder> *cylinders, vector<Cone> *cones, vector<Torus> *toruses, vector<PartialTorus> *partialToruses, vector<TaperedCylinder> *taperedCylinders, vector<Square> *squares) { daeTArray<daeElementRef> nodeChildren = node->getChildren(); for (size_t j = 0; j < nodeChildren.getCount(); j++) { const daeElementRef nodeChild = nodeChildren[j]; const daeString nodeChildKind = nodeChild->getElementName(); if (!strcmp(nodeChildKind, "node")) processNode(daeSafeCast<domNode>(nodeChild), m_trans, ambientLights, pointLights, cubes, spheres, cylinders, cones, toruses, partialToruses, taperedCylinders, squares); else if (!strcmp(nodeChildKind, "translate")) { const domFloat3 val = daeSafeCast<domTranslate>(nodeChild)->getValue(); m_trans = m_trans * trans_mat(Vector3(val[0], val[1], val[2])); } else if (!strcmp(nodeChildKind, "rotate")) { const domFloat3 val = daeSafeCast<domRotate>(nodeChild)->getValue(); m_trans = m_trans * rot_mat(Point3(), Vector3(val[0], val[1], val[2]), val[3]); } else if (!strcmp(nodeChildKind, "scale")) { const domFloat3 val = daeSafeCast<domScale>(nodeChild)->getValue(); m_trans = m_trans * scale_mat(Vector3(val[0], val[1], val[2])); } else if (!strcmp(nodeChildKind, "instance_geometry")) { const daeTArray<daeElementRef> geometryChildren = daeSafeCast<domInstance_geometry>(nodeChild)->getUrl().getElement()->getChildren(); for (size_t k = 0; k < geometryChildren.getCount(); k++) { const daeElementRef geometryChild = geometryChildren[k]; const daeString geometryChildKind = geometryChild->getElementName(); double KdR = 0.8, KdG = 0.8, KdB = 0.8, KsR = 1.f, KsG = 1.f, KsB = 1.f, shiny = 25.f, reflectivity = 1.f, transparency = 0.f, refractiveIndex = 1.f; daeElement *squeegee = nodeChild->getChild("bind_material"); if (squeegee) { squeegee = squeegee->getChild("technique_common"); if (squeegee) { squeegee = squeegee->getChild("instance_material"); if (squeegee) { squeegee = daeSafeCast<domInstance_material>(squeegee)->getTarget().getElement(); if (squeegee) { squeegee = squeegee->getChild("instance_effect"); if (squeegee) { daeElement *effectel = daeSafeCast<domInstance_effect>(squeegee)->getUrl().getElement(); if (effectel) { daeElement *profile_COMMON = effectel->getChild("profile_COMMON"); if (profile_COMMON) { daeElement *technique = profile_COMMON->getChild("technique"); if (technique) { daeElement *phong = technique->getChild("phong"); if (phong) { daeElement *diffuse = phong->getChild("diffuse"); daeElement *specular = phong->getChild("specular"); daeElement *shininess = phong->getChild("shininess"); daeElement *reflectivityel = phong->getChild("reflectivity"); daeElement *transparencyel = phong->getChild("transparency"); daeElement *index_of_refraction = phong->getChild("index_of_refraction"); if (diffuse && specular && shininess && reflectivityel && transparencyel && index_of_refraction) { daeElement *diffusecolorel = diffuse->getChild("color"); daeElement *specularcolorel = specular->getChild("color"); daeElement *shininessfloatel = shininess->getChild("float"); daeElement *reflectivityfloatel = reflectivityel->getChild("float"); daeElement *transparencyfloatel = transparencyel->getChild("float"); daeElement *index_of_refractionfloatel = index_of_refraction->getChild("float"); if (diffusecolorel && specularcolorel && shininessfloatel && reflectivityfloatel && transparencyfloatel && index_of_refractionfloatel) { istringstream parsediffusecolorel(diffusecolorel->getCharData()); parsediffusecolorel >> KdR; parsediffusecolorel >> KdG; parsediffusecolorel >> KdB; istringstream parsespecularcolorel(specularcolorel->getCharData()); parsespecularcolorel >> KsR; parsespecularcolorel >> KsG; parsespecularcolorel >> KsB; istringstream parseshininessfloatel(shininessfloatel->getCharData()); parseshininessfloatel >> shiny; istringstream parsereflectivityfloatel(reflectivityfloatel->getCharData()); parsereflectivityfloatel >> reflectivity; istringstream parsetransparencyfloatel(transparencyfloatel->getCharData()); parsetransparencyfloatel >> transparency; istringstream parseindex_of_refractionfloatel(index_of_refractionfloatel->getCharData()); parseindex_of_refractionfloatel >> refractiveIndex; } } } } } } } } } } } #define INJECTMESH \ mesh.transform(m_trans); \ mesh.KdR = KdR; \ mesh.KdG = KdG; \ mesh.KdB = KdB; \ mesh.KsR = KsR; \ mesh.KsG = KsG; \ mesh.KsB = KsB; \ mesh.shiny = shiny; \ mesh.reflectivity = reflectivity; \ mesh.transparency = transparency; \ mesh.refractiveIndex = refractiveIndex; \ buf->addObject(mesh); if (!strcmp(geometryChildKind, "block")) { const domBlockRef block = daeSafeCast<domBlock>(geometryChild); cubes->push_back(Cube(block->getEdgelen(), KdR, KdG, KdB, KsR, KsG, KsB, shiny, reflectivity, transparency, refractiveIndex, m_trans)); TriangleMesh mesh(block); INJECTMESH } else if (!strcmp(geometryChildKind, "ball")) { const domBallRef ball = daeSafeCast<domBall>(geometryChild); spheres->push_back(Sphere(ball->getRadius(), KdR, KdG, KdB, KsR, KsG, KsB, shiny, reflectivity, transparency, refractiveIndex, m_trans)); TriangleMesh mesh(ball); INJECTMESH } else if (!strcmp(geometryChildKind, "rod")) {