[Solved]Mogre Navmesh

Problems building or running the engine, queries about how to use features etc.
Post Reply
randomcode
Kobold
Posts: 25
Joined: Wed Nov 11, 2015 11:31 am
Location: Universe
x 1
Contact:

[Solved]Mogre Navmesh

Post by randomcode » Thu Apr 26, 2018 3:22 pm

I tried to make a function to convert Mogre Entity to Navmesh using critterai, but it noticed "error: more than 2 vertices shared between polys 0 and 1", I use MeshToNavmesh tool to convert my entity.

Code: Select all

	vertices.Clear();
        faces.Clear();
        MeshPtr mesh = ent.GetMesh();
        Mesh.SubMeshIterator subIterator = mesh.GetSubMeshIterator();

        uint vertexNum = mesh.sharedVertexData.vertexCount;
        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;
            HardwareIndexBufferSharedPtr indexBuffer = subMesh.indexData.indexBuffer;
            HardwareIndexBuffer.IndexType indexType = indexBuffer.Type;
            uint indexCount = subMesh.indexData.indexCount;
            uint[] indcies=new uint[indexCount];
            uint indexOffset = subMesh.indexData.indexStart;

            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;
                    }
                }
            }
            for (int i = 0; i < indcies.Length; 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();
        
`vertices` and `faces` are member variable used by creating navmesh

Thanks if anyone can help me
0 x
AMGE(Open Source C# Role-play Engine)
https://github.com/cookgreen/AMGE

Post Reply