Esempio n. 1
0
    /**************** New Functions Begin ***************/
    /// <summary>
    /// Generate navmesh by entity
    /// </summary>
    /// <param name="ent">Ogre Entity</param>
    /// <returns>Navmesh</returns>

    public static Navmesh LoadNavmesh(Entity ent)
    {
        bool addedSharedVertex = false;

        vertices.Clear();
        faces.Clear();
        MeshPtr mesh = ent.GetMesh();

        Mesh.SubMeshIterator subIterator = mesh.GetSubMeshIterator();

        uint vertexNum    = 0;
        uint vertexOffset = mesh.sharedVertexData.vertexStart;

        MyVector3 <float>[]           verticeArray = new MyVector3 <float> [vertexNum];
        VertexElement                 posElem      = mesh.sharedVertexData.vertexDeclaration.FindElementBySemantic(VertexElementSemantic.VES_POSITION);
        HardwareVertexBufferSharedPtr vertexBuffer = mesh.sharedVertexData.vertexBufferBinding.GetBuffer(posElem.Source);

        while (subIterator.MoveNext())
        {
            SubMesh subMesh = subIterator.Current;

            VertexData vertexData = subMesh.useSharedVertices ? mesh.sharedVertexData : subMesh.vertexData;

            HardwareIndexBufferSharedPtr  indexBuffer = subMesh.indexData.indexBuffer;
            HardwareIndexBuffer.IndexType indexType   = indexBuffer.Type;
            uint indexCount = subMesh.indexData.indexCount;

            uint trisNum = indexCount / 3;

            uint[] indcies     = new uint[indexCount];
            uint   indexOffset = subMesh.indexData.indexStart;

            if (subMesh.useSharedVertices)
            {
                if (!addedSharedVertex)
                {
                    vertexNum        += mesh.sharedVertexData.vertexCount;
                    addedSharedVertex = true;
                }
            }
            else
            {
                vertexNum += subMesh.vertexData.vertexCount;
            }

            unsafe
            {
                uint *  pLong  = (uint *)(indexBuffer.Lock(HardwareBuffer.LockOptions.HBL_READ_ONLY));
                ushort *pShort = (ushort *)pLong;
                for (int i = 0; i < indexCount; i++)
                {
                    if (indexType == HardwareIndexBuffer.IndexType.IT_32BIT)
                    {
                        indcies[indexOffset] = pLong[i] + vertexNum;
                    }
                    else
                    {
                        indcies[indexOffset] = pShort[i] + vertexNum;
                    }
                    indexOffset++;
                }
            }

            int indexLength = indcies.Length / 3;
            for (int i = 0; i < indexLength; i++)
            {
                faces.Add(new MyVector3 <ushort>(
                              (ushort)indcies[i * 3 + 0],
                              (ushort)indcies[i * 3 + 1],
                              (ushort)indcies[i * 3 + 2]
                              ));
            }

            indexBuffer.Unlock();

            if (subMesh.vertexData != null)
            {
                vertexNum    = subMesh.vertexData.vertexCount;
                vertexOffset = subMesh.vertexData.vertexStart;
                verticeArray = new MyVector3 <float> [vertexNum];
                posElem      = subMesh.vertexData.vertexDeclaration.FindElementBySemantic(VertexElementSemantic.VES_POSITION);
                vertexBuffer = subMesh.vertexData.vertexBufferBinding.GetBuffer(posElem.Source);
                unsafe
                {
                    byte * vertexMemory = (byte *)vertexBuffer.Lock(HardwareBuffer.LockOptions.HBL_READ_ONLY);
                    float *pVertexBuffer;
                    for (int i = 0; i < vertexNum; i++)
                    {
                        posElem.BaseVertexPointerToElement(vertexMemory, &pVertexBuffer);
                        verticeArray[vertexOffset] = (new MyVector3 <float>(
                                                          pVertexBuffer[0],
                                                          pVertexBuffer[1],
                                                          pVertexBuffer[2]
                                                          ));
                        vertexMemory += vertexBuffer.VertexSize;
                        vertexOffset++;
                    }
                }
                for (int i = 0; i < verticeArray.Length; i++)
                {
                    vertices.Add(verticeArray[i]);
                }
                vertexBuffer.Unlock();
            }
        }

        vertexNum    = mesh.sharedVertexData.vertexCount;
        vertexOffset = mesh.sharedVertexData.vertexStart;
        verticeArray = new MyVector3 <float> [vertexNum];
        posElem      = mesh.sharedVertexData.vertexDeclaration.FindElementBySemantic(VertexElementSemantic.VES_POSITION);
        vertexBuffer = mesh.sharedVertexData.vertexBufferBinding.GetBuffer(posElem.Source);

        unsafe
        {
            byte * vertexMemory = (byte *)vertexBuffer.Lock(HardwareBuffer.LockOptions.HBL_READ_ONLY);
            float *pVertexBuffer;
            for (int i = 0; i < vertexNum; i++)
            {
                posElem.BaseVertexPointerToElement(vertexMemory, &pVertexBuffer);
                verticeArray[vertexOffset] = (new MyVector3 <float>(
                                                  pVertexBuffer[0],
                                                  pVertexBuffer[1],
                                                  pVertexBuffer[2]
                                                  ));
                vertexMemory += vertexBuffer.VertexSize;
                vertexOffset++;
            }
        }
        for (int i = 0; i < verticeArray.Length; i++)
        {
            vertices.Add(verticeArray[i]);
        }
        vertexBuffer.Unlock();

        return(GenerateNavmesh());
    }