Beispiel #1
0
 void convertOpenCOLLADAMesh(COLLADAFW::Mesh* openCOLLADAMesh,
                             MeshVector &meshes)
 {
     shared_ptr <GLTF::GLTFMesh> cvtMesh(new GLTF::GLTFMesh());
     
     cvtMesh->setID(openCOLLADAMesh->getOriginalId());
     cvtMesh->setName(openCOLLADAMesh->getName());
     
     const COLLADAFW::MeshPrimitiveArray& primitives =  openCOLLADAMesh->getMeshPrimitives();
     size_t primitiveCount = primitives.getCount();
     
     std::vector< shared_ptr<IndicesVector> > allPrimitiveIndicesVectors;
     
     // get all primitives
     for (size_t i = 0 ; i < primitiveCount ; i++) {
         const COLLADAFW::MeshPrimitive::PrimitiveType primitiveType = primitives[i]->getPrimitiveType();
         if ((primitiveType != COLLADAFW::MeshPrimitive::TRIANGLES) &&
             (primitiveType != COLLADAFW::MeshPrimitive::TRIANGLE_STRIPS) &&
             (primitiveType != COLLADAFW::MeshPrimitive::POLYLIST) &&
             (primitiveType != COLLADAFW::MeshPrimitive::POLYGONS))
             continue;
         
         shared_ptr <GLTF::IndicesVector> primitiveIndicesVector(new GLTF::IndicesVector());
         allPrimitiveIndicesVectors.push_back(primitiveIndicesVector);
         
         shared_ptr <GLTF::GLTFPrimitive> primitive = ConvertOpenCOLLADAMeshPrimitive(primitives[i],*primitiveIndicesVector);
         cvtMesh->appendPrimitive(primitive);
         
         VertexAttributeVector vertexAttributes = primitive->getVertexAttributes();
         primitiveIndicesVector = allPrimitiveIndicesVectors[i];
         
         // once we got a primitive, keep track of its meshAttributes
         std::vector< shared_ptr<GLTF::GLTFIndices> > allIndices = *primitiveIndicesVector;
         for (size_t k = 0 ; k < allIndices.size() ; k++) {
             shared_ptr<GLTF::GLTFIndices> indices = allIndices[k];
             GLTF::Semantic semantic = vertexAttributes[k]->getSemantic();
             GLTF::IndexSetToMeshAttributeHashmap& meshAttributes = cvtMesh->getMeshAttributesForSemantic(semantic);
             
             switch (semantic) {
                 case GLTF::POSITION:
                     ConvertOpenCOLLADAMeshVertexDataToGLTFMeshAttributes(openCOLLADAMesh->getPositions(), meshAttributes);
                     break;
                     
                 case GLTF::NORMAL:
                     ConvertOpenCOLLADAMeshVertexDataToGLTFMeshAttributes(openCOLLADAMesh->getNormals(), meshAttributes);
                     break;
                     
                 case GLTF::TEXCOORD:
                     ConvertOpenCOLLADAMeshVertexDataToGLTFMeshAttributes(openCOLLADAMesh->getUVCoords(), meshAttributes);
                     break;
                     
                 case GLTF::COLOR:
                     ConvertOpenCOLLADAMeshVertexDataToGLTFMeshAttributes(openCOLLADAMesh->getColors(), meshAttributes);
                     break;
                     
                 default:
                     break;
             }
             
             cvtMesh->setMeshAttributesForSemantic(semantic, meshAttributes);
         }
     }
     
     //https://github.com/KhronosGroup/collada2json/issues/41
     //Goes through all texcoord and invert V
     GLTF::IndexSetToMeshAttributeHashmap& texcoordMeshAttributes = cvtMesh->getMeshAttributesForSemantic(GLTF::TEXCOORD);
     GLTF::IndexSetToMeshAttributeHashmap::const_iterator meshAttributeIterator;
     
     //FIXME: consider turn this search into a method for mesh
     for (meshAttributeIterator = texcoordMeshAttributes.begin() ; meshAttributeIterator != texcoordMeshAttributes.end() ; meshAttributeIterator++) {
         //(*it).first;             // the key value (of type Key)
         //(*it).second;            // the mapped value (of type T)
         shared_ptr <GLTF::GLTFMeshAttribute> meshAttribute = (*meshAttributeIterator).second;
         
         meshAttribute->apply(__InvertV, NULL);
     }
     
     if (cvtMesh->getPrimitives().size() > 0) {
         //After this point cvtMesh should be referenced anymore and will be deallocated
         shared_ptr <GLTF::GLTFMesh> unifiedMesh = createUnifiedIndexesMeshFromMesh(cvtMesh.get(), allPrimitiveIndicesVectors);
         if  (createMeshesWithMaximumIndicesCountFromMeshIfNeeded(unifiedMesh.get(), 65535, meshes) == false) {
             meshes.push_back(unifiedMesh);
         }
     }
 }
Beispiel #2
0
    shared_ptr<GLTFMesh> convertOpenCOLLADAMesh(COLLADAFW::Mesh* openCOLLADAMesh, GLTFAsset* asset) {
        shared_ptr <GLTF::GLTFMesh> cvtMesh(new GLTF::GLTFMesh());
        
        cvtMesh->setID(openCOLLADAMesh->getOriginalId());
        cvtMesh->setName(openCOLLADAMesh->getName());
        
        const COLLADAFW::MeshPrimitiveArray& primitives =  openCOLLADAMesh->getMeshPrimitives();
        size_t primitiveCount = primitives.getCount();
        
        std::vector< shared_ptr<IndicesVector> > allPrimitiveIndicesVectors;
                
        // get all primitives
        for (size_t i = 0 ; i < primitiveCount ; i++) {
            
            const COLLADAFW::MeshPrimitive::PrimitiveType primitiveType = primitives[i]->getPrimitiveType();
            if ((primitiveType != COLLADAFW::MeshPrimitive::TRIANGLES) &&
                //(primitiveType != COLLADAFW::MeshPrimitive::TRIANGLE_STRIPS) &&
                (primitiveType != COLLADAFW::MeshPrimitive::POLYLIST) &&
                (primitiveType != COLLADAFW::MeshPrimitive::POLYGONS)) {
                
                
                static bool printedOnce = false;
                if (!printedOnce) {
                    if (asset->converterConfig()->boolForKeyPath("verboseLogging")) {
                        asset->log("WARNING: some primitives failed to convert\nCurrently supported are TRIANGLES, POLYLIST and POLYGONS\nMore: https://github.com/KhronosGroup/glTF/issues/129\nand https://github.com/KhronosGroup/glTF/issues/135\n");
                        printedOnce = true;
                    }
                }
                
                continue;
            }
            
            shared_ptr <GLTF::IndicesVector> primitiveIndicesVector(new GLTF::IndicesVector());
            allPrimitiveIndicesVectors.push_back(primitiveIndicesVector);
            
            shared_ptr <GLTF::GLTFPrimitive> primitive = __ConvertOpenCOLLADAMeshPrimitive(primitives[i],*primitiveIndicesVector, asset->profile());
            cvtMesh->appendPrimitive(primitive);
            
            VertexAttributeVector vertexAttributes = primitive->getVertexAttributes();
            primitiveIndicesVector = allPrimitiveIndicesVectors[allPrimitiveIndicesVectors.size()-1];
            
            // once we got a primitive, keep track of its meshAttributes
            std::vector< shared_ptr<GLTF::GLTFAccessor> > allIndices = *primitiveIndicesVector;
            for (size_t k = 0 ; k < allIndices.size() ; k++) {
                shared_ptr<GLTF::GLTFAccessor> indices = allIndices[k];
                GLTF::Semantic semantic = vertexAttributes[k]->getSemantic();
                
                switch (semantic) {
                    case GLTF::POSITION:
                        __ConvertOpenCOLLADAMeshVertexDataToGLTFAccessors(openCOLLADAMesh->getPositions(), cvtMesh.get(), GLTF::POSITION, 3, asset->profile());
                        break;
                        
                    case GLTF::NORMAL:
                        __ConvertOpenCOLLADAMeshVertexDataToGLTFAccessors(openCOLLADAMesh->getNormals(), cvtMesh.get(), GLTF::NORMAL, 3, asset->profile());
                        break;
                        
                    case GLTF::TEXCOORD:
                        __ConvertOpenCOLLADAMeshVertexDataToGLTFAccessors(openCOLLADAMesh->getUVCoords(), cvtMesh.get(), GLTF::TEXCOORD, 2, asset->profile());
                        break;
                        
                    case GLTF::COLOR:
                        __ConvertOpenCOLLADAMeshVertexDataToGLTFAccessors(openCOLLADAMesh->getColors(), cvtMesh.get(), GLTF::COLOR, 4, asset->profile());
                        break;

                    case GLTF::TEXBINORMAL:
                        __ConvertOpenCOLLADAMeshVertexDataToGLTFAccessors(openCOLLADAMesh->getBinormals(), cvtMesh.get(), GLTF::TEXBINORMAL, 3, asset->profile());
                        break;
                    case GLTF::TEXTANGENT:
                        __ConvertOpenCOLLADAMeshVertexDataToGLTFAccessors(openCOLLADAMesh->getTangents(), cvtMesh.get(), GLTF::TEXTANGENT, 3, asset->profile());
                        break;

                    default:
                        break;
                }
            }
        }
                
        if (cvtMesh->getPrimitivesCount() > 0) {
            //After this point cvtMesh should be referenced anymore and will be deallocated
            return createUnifiedIndexesMeshFromMesh(cvtMesh.get(), allPrimitiveIndicesVectors, asset->profile());
        }

        return nullptr;
    }