void TSRGeometryContainer::ProcessPolygonsArrayDOM( const domMeshRef& _mesh )
{
    // there will be multiple triangles to process
    domPolygons_Array& polygons_array = _mesh->getPolygons_array();

    unsigned int PolygonsCount = (unsigned int)polygons_array.getCount();

    // loop on each group of triangles groups...
    // same should be done when there are polygons..
    for ( unsigned int i = 0; i < PolygonsCount; i++ )
    {
        TSRModelGeometryTriangleSet newSet;
        newSet.m_uiStartIndex = ( int )m_pTwisterGeometry->GetIndexCount();
        newSet.m_uiMaterialIndex = 0;
        domPolygonsRef currPolygons = polygons_array[ i ];
        xsNCName materialName =  currPolygons->getMaterial();
        int materialIndex = m_pExporter->FindMaterialIndexByName( materialName );
        // material 0 is the default if no material was found...
        if ( materialIndex > -1 )
        {
            newSet.m_uiMaterialIndex = materialIndex + 1;
        }
        ProcessPolygonsDOM( currPolygons );
        newSet.m_uiIndexCount = ( int ) m_pTwisterGeometry->GetIndexCount()  - newSet.m_uiStartIndex;
        m_pTwisterGeometry->m_TriangleSets.push_back( newSet );
    }
}
/// process lines array
void TSRGeometryContainer::ProcessLinesArrayDOM( const domMeshRef& _mesh )
{
    // there will be multiple line lists to process
    domLines_Array& lines_array = _mesh->getLines_array();

    unsigned int lineListsCount = ( unsigned int ) lines_array.getCount();

    // loop on each line list
    for ( unsigned int i = 0; i < lineListsCount; i++ )
    {
        TSRModelGeometryTriangleSet newSet;
        newSet.m_uiStartIndex = ( int ) m_pTwisterGeometry->GetIndexCount();
        newSet.m_uiMaterialIndex = 0;
        domLinesRef currLines = lines_array[ i ];
        xsNCName materialName = currLines->getMaterial();
        int materialIndex = m_pExporter->FindMaterialIndexByName( materialName );
        // material 0 is the default if no material was found...
        if ( materialIndex > -1 )
        {
            newSet.m_uiMaterialIndex = materialIndex + 1;
        }
        ProcessLinesDOM( currLines );
        newSet.m_uiIndexCount = ( int ) m_pTwisterGeometry->GetIndexCount()  - newSet.m_uiStartIndex;

        newSet.m_renderMode = TWISTER_RENDERMODE_LINELIST;
        m_pTwisterGeometry->m_TriangleSets.push_back( newSet );
    }
}
Ejemplo n.º 3
0
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);
          }
        }
      }
    }
  }
}
Ejemplo n.º 4
0
void	ColladaConverter::ConvertRigidBodyRef( btRigidBodyInput& rbInput,btRigidBodyOutput& rbOutput)
{

	const domRigid_body::domTechnique_commonRef techniqueRef = rbInput.m_rigidBodyRef2->getTechnique_common();
	if (techniqueRef)
	{

		if (techniqueRef->getMass())
		{
			rbOutput.m_mass = (float) techniqueRef->getMass()->getValue();
		}
		if (techniqueRef->getDynamic())
		{
			rbOutput.m_isDynamics = techniqueRef->getDynamic()->getValue();
		}
		//a hack to interpret <extra> PhysX profile:
		//when <kinematic> is true, make <dynamic> false...
		//using the DOM is a pain...
		const domExtra_Array& extraArray = rbInput.m_rigidBodyRef2->getExtra_array();
		unsigned int s=0;

		for (s = 0;s< extraArray.getCount();s++)
		{
			const domExtraRef extraRef = extraArray[s];
			const domTechnique_Array techniqueArray = extraRef->getTechnique_array();
			unsigned int t=0;
			for (t=0;t<techniqueArray.getCount();t++)
			{
				const domTechniqueRef techRef = techniqueArray[t];
				const daeElementRefArray elemRefArray = techRef->getContents();
				unsigned int u = 0;
				for (u=0;u<elemRefArray.getCount();u++)
				{
					daeElementRef elemRef = elemRefArray[u];
					daeString elemName = elemRef->getElementName();
					if (elemName && !strcmp(elemName,"kinematic"))
					{
						//how can I make this cast safe?
						const domAny* myAny = (const domAny*)elemRef.cast();
						daeString myVal = myAny->getValue();
						if (myVal)
						{
							if (!strcmp(myVal,"true"))
							{
								printf("revert bug in PhysX .dae export -> <kinematic>true</kinematic> means <dynamic>false</dynamic>\n");
								rbOutput.m_isDynamics = false;
							}
						}
					}
				}
			}
		}

		//shapes
		for (s=0;s<techniqueRef->getShape_array().getCount();s++)
		{
			domRigid_body::domTechnique_common::domShapeRef shapeRef = techniqueRef->getShape_array()[s];

			if (shapeRef->getPlane())
			{
				domPlaneRef planeRef = shapeRef->getPlane();
				if (planeRef->getEquation())
				{
					const domFloat4 planeEq = planeRef->getEquation()->getValue();
					btVector3 planeNormal((btScalar)planeEq.get(0),(btScalar)planeEq.get(1),(btScalar)planeEq.get(2));
					btScalar planeConstant = (btScalar)planeEq.get(3)*(btScalar)m_unitMeterScaling;
					rbOutput.m_colShape = new btStaticPlaneShape(planeNormal,planeConstant);
				}

			}

			if (shapeRef->getBox())
			{
				domBoxRef boxRef = shapeRef->getBox();
				domBox::domHalf_extentsRef	domHalfExtentsRef = boxRef->getHalf_extents();
				domFloat3& halfExtents = domHalfExtentsRef->getValue();
				btScalar x = (btScalar)halfExtents.get(0)*m_unitMeterScaling;
				btScalar y = (btScalar)halfExtents.get(1)*m_unitMeterScaling;
				btScalar z = (btScalar)halfExtents.get(2)*m_unitMeterScaling;
				rbOutput.m_colShape = new btBoxShape(btVector3(x,y,z));
			}
			if (shapeRef->getSphere())
			{
				domSphereRef sphereRef = shapeRef->getSphere();
				domSphere::domRadiusRef radiusRef = sphereRef->getRadius();
				btScalar radius = (btScalar)radiusRef->getValue()*m_unitMeterScaling;
				rbOutput.m_colShape = new btSphereShape(radius);
			}

			if (shapeRef->getCylinder())
			{
				domCylinderRef cylinderRef = shapeRef->getCylinder();
				domFloat height = cylinderRef->getHeight()->getValue()*m_unitMeterScaling;
				domFloat2 radius2 = cylinderRef->getRadius()->getValue();
				domFloat radius0 = radius2.get(0)*m_unitMeterScaling;

				//Cylinder around the local Y axis
				rbOutput.m_colShape = new btCylinderShapeZ(btVector3((btScalar)radius0,(btScalar)height,(btScalar)radius0));

			}

			if (shapeRef->getInstance_geometry())
			{
				const domInstance_geometryRef geomInstRef = shapeRef->getInstance_geometry();
				daeElement* geomElem = geomInstRef->getUrl().getElement();
				//elemRef->getTypeName();
				domGeometry* geom = (domGeometry*) geomElem;
				if (geom && geom->getMesh())
				{
					const domMeshRef meshRef = geom->getMesh();

					//it can be either triangle mesh, or we just pick the vertices/positions

					if (meshRef->getTriangles_array().getCount())
					{

						btTriangleMesh* trimesh = new btTriangleMesh();

						for (unsigned int tg = 0;tg<meshRef->getTriangles_array().getCount();tg++)
						{


							domTrianglesRef triRef = meshRef->getTriangles_array()[tg];
							const domPRef pRef = triRef->getP();
							btIndexedMesh meshPart;
							meshPart.m_triangleIndexStride=0;



							int vertexoffset = -1;
							domInputLocalOffsetRef indexOffsetRef;


							for (unsigned int w=0;w<triRef->getInput_array().getCount();w++)
							{
								domUint offset = triRef->getInput_array()[w]->getOffset();
								daeString str = triRef->getInput_array()[w]->getSemantic();
								if (!strcmp(str,"VERTEX"))
								{
									indexOffsetRef = triRef->getInput_array()[w];
									vertexoffset = (int) offset;
								}
								if ((int) offset > (int) meshPart.m_triangleIndexStride)
								{
									meshPart.m_triangleIndexStride = (int) offset;
								}
							}
							meshPart.m_triangleIndexStride++;
							domListOfUInts indexArray =triRef->getP()->getValue();

							//int*		m_triangleIndexBase;



							meshPart.m_numTriangles = (int) triRef->getCount();

							const domVerticesRef vertsRef = meshRef->getVertices();
							size_t numInputs = vertsRef->getInput_array().getCount();
							for (size_t i=0;i<numInputs;i++)
							{
								domInputLocalRef localRef = vertsRef->getInput_array()[i];
								daeString str = localRef->getSemantic();
								if ( !strcmp(str,"POSITION"))
								{
									domURIFragmentType& frag = localRef->getSource();

									daeElementConstRef constElem = frag.getElement();

									const domSourceRef node = *(const domSourceRef*)&constElem;
									const domFloat_arrayRef flArray = node->getFloat_array();
									if (flArray)
									{
										const domListOfFloats& listFloats = flArray->getValue();

										int k=vertexoffset;
										int t=0;
										int vertexStride = 3;//instead of hardcoded stride, should use the 'accessor'
										for (;t<meshPart.m_numTriangles;t++)
										{
											btVector3 verts[3];
											int index0;
											for (int i=0;i<3;i++)
											{
												index0 = (int) indexArray.get(k)*vertexStride;
												domFloat fl0 = listFloats.get(index0);
												domFloat fl1 = listFloats.get(index0+1);
												domFloat fl2 = listFloats.get(index0+2);
												k+=meshPart.m_triangleIndexStride;
												verts[i].setValue((btScalar)fl0*m_unitMeterScaling,(btScalar)fl1*m_unitMeterScaling,(btScalar)fl2*m_unitMeterScaling);
											}
											trimesh->addTriangle(verts[0],verts[1],verts[2]);
										}
									}
								}
							}



							if (rbOutput.m_isDynamics)
							{
								printf("moving concave <mesh> not supported, transformed into convex\n");
								rbOutput.m_colShape = new btConvexTriangleMeshShape(trimesh);
							} else
							{
								printf("static concave triangle <mesh> added\n");
								bool useQuantizedAabbCompression = false;
								rbOutput.m_colShape = new btBvhTriangleMeshShape(trimesh,useQuantizedAabbCompression);

							}

						}
					} else
						{

							btConvexHullShape* convexHull = new btConvexHullShape();
							int numAddedVerts = 0;

							const domVerticesRef vertsRef = meshRef->getVertices();
							size_t numInputs = vertsRef->getInput_array().getCount();
							for (size_t i=0;i<numInputs;i++)
							{
								domInputLocalRef localRef = vertsRef->getInput_array()[i];
								daeString str = localRef->getSemantic();
								if ( !strcmp(str,"POSITION"))
								{
									domURIFragmentType& frag = localRef->getSource();

									daeElementConstRef constElem = frag.getElement();

									const domSourceRef node = *(const domSourceRef*)&constElem;
									const domFloat_arrayRef flArray = node->getFloat_array();
									if (flArray)
									{
										const domListOfFloats& listFloats = flArray->getValue();
										int vertexStride = 3;//instead of hardcoded stride, should use the 'accessor'
										size_t vertIndex = 0;
										for (vertIndex = 0;vertIndex < listFloats.getCount();vertIndex+=vertexStride)
										{
											domFloat fl0 = listFloats.get(vertIndex);
											domFloat fl1 = listFloats.get(vertIndex+1);
											domFloat fl2 = listFloats.get(vertIndex+2);
											convexHull->addPoint(btPoint3((btScalar)fl0,(btScalar)fl1,(btScalar)fl2) * m_unitMeterScaling);
										}
									}
								}
							}
							//convexHull->addPoint();
							if (numAddedVerts > 0)
							{
								rbOutput.m_colShape = convexHull;
							} else
							{
								delete convexHull;
								printf("no vertices found for convex hull\n");
							}

					}


				}

				if (geom && geom->getConvex_mesh())
				{

					{
						const domConvex_meshRef convexRef = geom->getConvex_mesh();
						daeElementRef otherElemRef = convexRef->getConvex_hull_of().getElement();
						if ( otherElemRef != NULL )
						{
							domGeometryRef linkedGeom = *(domGeometryRef*)&otherElemRef;
							printf( "otherLinked\n");
						} else
						{
							printf("convexMesh polyCount = %i\n",convexRef->getPolygons_array().getCount());
							printf("convexMesh triCount = %i\n",convexRef->getTriangles_array().getCount());

						}
					}



					btConvexHullShape* convexHullShape = new btConvexHullShape(0,0);

					//it is quite a trick to get to the vertices, using Collada.
					//we are not there yet...

					const domConvex_meshRef convexRef = geom->getConvex_mesh();
					//daeString urlref = convexRef->getConvex_hull_of().getURI();
					daeString urlref2 = convexRef->getConvex_hull_of().getOriginalURI();
					if (urlref2)
					{
						daeElementRef otherElemRef = convexRef->getConvex_hull_of().getElement();
						//	if ( otherElemRef != NULL )
						//	{
						//		domGeometryRef linkedGeom = *(domGeometryRef*)&otherElemRef;

						// Load all the geometry libraries
						for ( unsigned int i = 0; i < m_dom->getLibrary_geometries_array().getCount(); i++)
						{
							domLibrary_geometriesRef libgeom = m_dom->getLibrary_geometries_array()[i];
							//int index = libgeom->findLastIndexOf(urlref2);
							//can't find it

							for ( unsigned int  i = 0; i < libgeom->getGeometry_array().getCount(); i++)
							{
								//ReadGeometry(  );
								domGeometryRef lib = libgeom->getGeometry_array()[i];
								if (!strcmp(lib->getId(),urlref2))
								{
									//found convex_hull geometry
									domMesh			*meshElement		= lib->getMesh();//linkedGeom->getMesh();
									if (meshElement)
									{
										const domVerticesRef vertsRef = meshElement->getVertices();
										size_t numInputs = vertsRef->getInput_array().getCount();
										for (size_t i=0;i<numInputs;i++)
										{
											domInputLocalRef localRef = vertsRef->getInput_array()[i];
											daeString str = localRef->getSemantic();
											if ( !strcmp(str,"POSITION"))
											{
												domURIFragmentType& frag = localRef->getSource();

												daeElementConstRef constElem = frag.getElement();

												const domSourceRef node = *(const domSourceRef*)&constElem;
												const domFloat_arrayRef flArray = node->getFloat_array();
												if (flArray)
												{
													int numElem = (int) flArray->getCount();
													const domListOfFloats& listFloats = flArray->getValue();

													for (int k=0;k+2<numElem;k+=3)
													{
														domFloat fl0 = listFloats.get(k);
														domFloat fl1 = listFloats.get(k+1);
														domFloat fl2 = listFloats.get(k+2);
														//printf("float %f %f %f\n",fl0,fl1,fl2);

														convexHullShape->addPoint(btPoint3((btScalar)fl0,(btScalar)fl1,(btScalar)fl2) * m_unitMeterScaling);
													}
												}
											}
										}
									}
								}
							}
						}
					} else
					{
						//no getConvex_hull_of but direct vertices
						const domVerticesRef vertsRef = convexRef->getVertices();
						size_t numInputs = vertsRef->getInput_array().getCount();
						for (size_t i=0;i<numInputs;i++)
						{
							domInputLocalRef localRef = vertsRef->getInput_array()[i];
							daeString str = localRef->getSemantic();
							if ( !strcmp(str,"POSITION"))
							{
								domURIFragmentType& frag = localRef->getSource();

								daeElementConstRef constElem = frag.getElement();

								const domSourceRef node = *(const domSourceRef*)&constElem;
								const domFloat_arrayRef flArray = node->getFloat_array();
								if (flArray)
								{
									int numElem = (int) flArray->getCount();
									const domListOfFloats& listFloats = flArray->getValue();

									for (int k=0;k+2<numElem;k+=3)
									{
										domFloat fl0 = listFloats.get(k);
										domFloat fl1 = listFloats.get(k+1);
										domFloat fl2 = listFloats.get(k+2);
										//printf("float %f %f %f\n",fl0,fl1,fl2);

										convexHullShape->addPoint(btPoint3((btScalar)fl0,(btScalar)fl1,(btScalar)fl2)*m_unitMeterScaling);
									}

								}

							}


						}


					}

					if (convexHullShape->getNumVertices())
					{
						rbOutput.m_colShape = convexHullShape;
						printf("created convexHullShape with %i points\n",convexHullShape->getNumVertices());
					} else
					{
						delete convexHullShape;
						printf("failed to create convexHullShape\n");
					}


					//domGeometryRef linkedGeom = *(domGeometryRef*)&otherElemRef;

					printf("convexmesh\n");

				}
			}

			//if more then 1 shape, or a non-identity local shapetransform
			//use a compound

			bool hasShapeLocalTransform = ((shapeRef->getRotate_array().getCount() > 0) ||
				(shapeRef->getTranslate_array().getCount() > 0));

			if (rbOutput.m_colShape)
			{
				if ((techniqueRef->getShape_array().getCount()>1) ||
					(hasShapeLocalTransform))
				{

					if (!rbOutput.m_compoundShape)
					{
						rbOutput.m_compoundShape = new btCompoundShape();
					}

					btTransform localTransform;
					localTransform.setIdentity();
					if (hasShapeLocalTransform)
					{
					localTransform = GetbtTransformFromCOLLADA_DOM(
						emptyMatrixArray,
						shapeRef->getRotate_array(),
						shapeRef->getTranslate_array(),
						m_unitMeterScaling
						);
					}

					rbOutput.m_compoundShape->addChildShape(localTransform,rbOutput.m_colShape);
					rbOutput.m_colShape = 0;
				}
			}


		}//for each shape
	}


}