Ejemplo n.º 1
0
 static void __HandleIndexList(unsigned int idx,
                               COLLADAFW::IndexList *indexList,
                               Semantic semantic,
                               bool shouldTriangulate,
                               unsigned int count,
                               unsigned int vcount,
                               unsigned int *verticesCountArray,
                               shared_ptr <GLTF::GLTFPrimitive> cvtPrimitive,
                               IndicesVector &primitiveIndicesVector
                               )
 {
     unsigned int triangulatedIndicesCount = 0;
     bool ownData = false;
     unsigned int *indices = indexList->getIndices().getData();
     
     if (shouldTriangulate) {
         indices = createTrianglesFromPolylist(verticesCountArray, indices, vcount, &triangulatedIndicesCount);
         count = triangulatedIndicesCount;
         ownData = true;
     }
     
     //Why is OpenCOLLADA doing this ? why adding an offset the indices ??
     //We need to offset it backward here.
     unsigned int initialIndex = indexList->getInitialIndex();
     if (initialIndex != 0) {
         unsigned int *bufferDestination = 0;
         if (!ownData) {
             bufferDestination = (unsigned int*)malloc(sizeof(unsigned int) * count);
             ownData = true;
         } else {
             bufferDestination = indices;
         }
         for (size_t idx = 0 ; idx < count ; idx++) {
             bufferDestination[idx] = indices[idx] - initialIndex;
         }
         indices = bufferDestination;
     }
     
     shared_ptr <GLTF::GLTFBufferView> uvBuffer = createBufferViewWithAllocatedBuffer(indices, 0, count * sizeof(unsigned int), ownData);
     
     //FIXME: Looks like for texcoord indexSet begin at 1, this is out of the sync with the index used in ConvertOpenCOLLADAMeshVertexDataToGLTFMeshAttributes that begins at 0
     //for now forced to 0, to be fixed for multi texturing.
     
     //unsigned int idx = (unsigned int)indexList->getSetIndex();
     
     shared_ptr <GLTFIndices> jsonIndices(new GLTFIndices(uvBuffer, count));
     __AppendIndices(cvtPrimitive, primitiveIndicesVector, jsonIndices, semantic, idx);
 }
Ejemplo n.º 2
0
    static void __HandleIndexList(unsigned int idx,
                                  COLLADAFW::IndexList *indexList,
                                  Semantic semantic,
                                  bool shouldTriangulate,
                                  unsigned int count,
                                  unsigned int vcount,
                                  unsigned int *verticesCountArray,
                                  shared_ptr <GLTF::GLTFPrimitive> cvtPrimitive,
                                  IndicesVector &primitiveIndicesVector,
                                  shared_ptr<GLTFProfile> profile)
    {
        unsigned int triangulatedIndicesCount = 0;
        bool ownData = false;
        unsigned int *indices = indexList->getIndices().getData();
        
        if (shouldTriangulate) {
            indices = createTrianglesFromPolylist(verticesCountArray, indices, vcount, &triangulatedIndicesCount);
            count = triangulatedIndicesCount;
            ownData = true;
        }
        
        //Why is OpenCOLLADA doing this ? why adding an offset the indices ??
        //We need to offset it backward here.
		unsigned int initialIndex = (unsigned int)indexList->getInitialIndex();
        if (initialIndex != 0) {
            unsigned int *bufferDestination = 0;
            if (!ownData) {
                bufferDestination = (unsigned int*)malloc(sizeof(unsigned int) * count);
                ownData = true;
            } else {
                bufferDestination = indices;
            }
            for (size_t idx = 0 ; idx < count ; idx++) {
                bufferDestination[idx] = indices[idx] - initialIndex;
            }
            indices = bufferDestination;
        }
        
        shared_ptr <GLTF::GLTFBufferView> uvBuffer = createBufferViewWithAllocatedBuffer(indices, 0, count * sizeof(unsigned int), ownData);
        shared_ptr <GLTFAccessor> accessor(new GLTFAccessor(profile, profile->getGLenumForString("UNSIGNED_SHORT")));
        
        accessor->setBufferView(uvBuffer);
        accessor->setCount(count);
        
        __AppendIndices(cvtPrimitive, primitiveIndicesVector, accessor, semantic, idx);
    }
Ejemplo n.º 3
0
 static shared_ptr <GLTF::GLTFPrimitive> ConvertOpenCOLLADAMeshPrimitive(
                                                                         COLLADAFW::MeshPrimitive *openCOLLADAMeshPrimitive,
                                                                         IndicesVector &primitiveIndicesVector)
 {
     shared_ptr <GLTF::GLTFPrimitive> cvtPrimitive(new GLTF::GLTFPrimitive());
     
     // We want to match OpenGL/ES mode , as WebGL spec points to OpenGL/ES spec...
     // "Symbolic constants GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, and GL_TRIANGLES are accepted."
     std::string type;
     bool shouldTriangulate = false;
     
     switch(openCOLLADAMeshPrimitive->getPrimitiveType()) {
             //these 2 requires transforms
         case COLLADAFW::MeshPrimitive::POLYLIST:
         case COLLADAFW::MeshPrimitive::POLYGONS:
             // FIXME: perform conversion, but until not done report error
             //these mode are supported by WebGL
             shouldTriangulate = true;
             //force triangles
             type = "TRIANGLES";
             break;
         case  COLLADAFW::MeshPrimitive::LINES:
             type = "LINES";
             break;
         case  COLLADAFW::MeshPrimitive::LINE_STRIPS:
             type = "LINE_STRIP";
             break;
             
         case  COLLADAFW::MeshPrimitive::TRIANGLES:
             type = "TRIANGLES";
             break;
             
         case  COLLADAFW::MeshPrimitive::TRIANGLE_FANS:
             type = "TRIANGLE_FANS";
             break;
             
         case  COLLADAFW::MeshPrimitive::TRIANGLE_STRIPS:
             type = "TRIANGLE_STRIPS";
             break;
             
         case  COLLADAFW::MeshPrimitive::POINTS:
             type = "POINTS";
             break;
         default:
             break;
     }
     
     cvtPrimitive->setMaterialObjectID((unsigned int)openCOLLADAMeshPrimitive->getMaterialId());
     cvtPrimitive->setType(type);
     
     //count of indices , it must be the same for all kind of indices
     size_t count = openCOLLADAMeshPrimitive->getPositionIndices().getCount();
     
     //vertex
     //IndexList &positionIndexList = openCOLLADAMeshPrimitive->getPositionIndices();
     unsigned int *indices = openCOLLADAMeshPrimitive->getPositionIndices().getData();
     unsigned int *verticesCountArray = 0;
     unsigned int vcount = 0;   //count of elements in the array containing the count of indices per polygon & polylist.
     
     if (shouldTriangulate) {
         unsigned int triangulatedIndicesCount = 0;
         //We have to upcast to polygon to retrieve the array of vertexCount
         //OpenCOLLADA use polylist as polygon.
         COLLADAFW::Polygons *polygon = (COLLADAFW::Polygons*)openCOLLADAMeshPrimitive;
         const COLLADAFW::Polygons::VertexCountArray& vertexCountArray = polygon->getGroupedVerticesVertexCountArray();
         vcount = (unsigned int)vertexCountArray.getCount();
         verticesCountArray = (unsigned int*)malloc(sizeof(unsigned int) * vcount);
         for (size_t i = 0; i < vcount; i++) {
             verticesCountArray[i] = polygon->getGroupedVerticesVertexCount(i);;
         }
         indices = createTrianglesFromPolylist(verticesCountArray, indices, vcount, &triangulatedIndicesCount);
         count = triangulatedIndicesCount;
     }
     
     shared_ptr <GLTFBufferView> positionBuffer = createBufferViewWithAllocatedBuffer(indices, 0, count * sizeof(unsigned int), shouldTriangulate ? true : false);
     
     shared_ptr <GLTF::GLTFIndices> positionIndices(new GLTF::GLTFIndices(positionBuffer,count));
     
     __AppendIndices(cvtPrimitive, primitiveIndicesVector, positionIndices, POSITION, 0);
     
     if (openCOLLADAMeshPrimitive->hasNormalIndices()) {
         unsigned int triangulatedIndicesCount = 0;
         indices = openCOLLADAMeshPrimitive->getNormalIndices().getData();
         if (shouldTriangulate) {
             indices = createTrianglesFromPolylist(verticesCountArray, indices, vcount, &triangulatedIndicesCount);
             count = triangulatedIndicesCount;
         }
         
         shared_ptr <GLTF::GLTFBufferView> normalBuffer = createBufferViewWithAllocatedBuffer(indices, 0, count * sizeof(unsigned int), shouldTriangulate ? true : false);
         shared_ptr <GLTF::GLTFIndices> normalIndices(new GLTF::GLTFIndices(normalBuffer,
                                                                            count));
         __AppendIndices(cvtPrimitive, primitiveIndicesVector, normalIndices, NORMAL, 0);
     }
     
     if (openCOLLADAMeshPrimitive->hasColorIndices()) {
         COLLADAFW::IndexListArray& colorListArray = openCOLLADAMeshPrimitive->getColorIndicesArray();
         for (size_t i = 0 ; i < colorListArray.getCount() ; i++) {
             COLLADAFW::IndexList* indexList = openCOLLADAMeshPrimitive->getColorIndices(i);
             __HandleIndexList(i,
                               indexList,
                               GLTF::COLOR,
                               shouldTriangulate,
                               count,
                               vcount,
                               verticesCountArray,
                               cvtPrimitive,
                               primitiveIndicesVector);
         }
     }
     
     if (openCOLLADAMeshPrimitive->hasUVCoordIndices()) {
         COLLADAFW::IndexListArray& uvListArray = openCOLLADAMeshPrimitive->getUVCoordIndicesArray();
         for (size_t i = 0 ; i < uvListArray.getCount() ; i++) {
             COLLADAFW::IndexList* indexList = openCOLLADAMeshPrimitive->getUVCoordIndices(i);
             __HandleIndexList(i,
                               indexList,
                               GLTF::TEXCOORD,
                               shouldTriangulate,
                               count,
                               vcount,
                               verticesCountArray,
                               cvtPrimitive,
                               primitiveIndicesVector);
         }
     }
     
     if (verticesCountArray) {
         free(verticesCountArray);
     }
     
     return cvtPrimitive;
 }