Ejemplo n.º 1
0
        public unsafe bool BuildClusterFromMeshSource(CRenderContext rc, RName meshSourceName)
        {
            Graphics.Mesh.CGfxMeshDataProvider meshSource = new Graphics.Mesh.CGfxMeshDataProvider();
            var meshPrimitive = CEngine.Instance.MeshPrimitivesManager.GetMeshPrimitives(rc, meshSourceName, true);

            meshSource.InitFromMesh(rc, meshPrimitive);

            uint a, b, c;

            a = 0;
            b = 0;
            c = 0;
            List <Cluster> clusterArray = new List <Cluster>();
            int            vertNum      = meshSource.VertexNumber;

            Vector3 *pPos    = (Vector3 *)meshSource.GetVertexPtr(EVertexSteamType.VST_Position, 0).ToPointer();
            Vector3 *pNor    = (Vector3 *)meshSource.GetVertexPtr(EVertexSteamType.VST_Normal, 0).ToPointer();
            Vector4 *pTan    = (Vector4 *)meshSource.GetVertexPtr(EVertexSteamType.VST_Tangent, 0).ToPointer();
            Vector2 *pDiffUV = (Vector2 *)meshSource.GetVertexPtr(EVertexSteamType.VST_UV, 0).ToPointer();

            GpuSceneVertex vert = new GpuSceneVertex();

            for (int i = 0; i < vertNum; i++)
            {
                vert.Reset();
                if (pPos != null)
                {
                    vert.Position = pPos[i];
                }
                if (pNor != null)
                {
                    vert.Normal = pNor[i];
                }
                if (pTan != null)
                {
                    vert.Tangent = pTan[i];
                }
                if (pDiffUV != null)
                {
                    vert.DiffuseU = pDiffUV[i].X;
                    vert.DiffuseV = pDiffUV[i].Y;
                }

                MeshVertices.Add(vert);
            }

            for (int i = 0; i < meshSource.TriangleNumber; i++)
            {
                meshSource.GetTriangle(i, ref a, ref b, ref c);
                IndexBuffer.Add(a);
                IndexBuffer.Add(b);
                IndexBuffer.Add(c);
            }

            Cluster  curCluster = null;
            Vector3 *pPosPtr    = (Vector3 *)meshSource.GetVertexPtr(EVertexSteamType.VST_Position, 0).ToPointer();

            for (uint i = 0; i < meshSource.AtomNumber; i++)
            {
                CDrawPrimitiveDesc desc = meshSource[i, 0];

                if (desc.PrimitiveType != EPrimitiveType.EPT_TriangleList)
                {
                    return(false);
                }

                uint startFace = desc.StartIndex / 3;

                for (uint j = 0; j < desc.NumPrimitives; j++)
                {
                    if (curCluster == null || curCluster.Data.FaceCount >= 64)
                    {
                        curCluster = new Cluster();
                        curCluster.Data.InstanceId     = i;
                        curCluster.Data.StartFaceIndex = startFace;
                        curCluster.Data.FaceCount      = 0;

                        clusterArray.Add(curCluster);
                    }
                    startFace++;
                    curCluster.Data.FaceCount++;
                }
            }

            ClusterDatas.Clear();
            for (int i = 0; i < clusterArray.Count; i++)
            {
                clusterArray[i].Proccess(pPosPtr, IndexBuffer);

                ClusterDatas.Add(clusterArray[i].Data);
            }
            return(true);
        }
Ejemplo n.º 2
0
        public bool LoadClusteredMesh(RName name)
        {
            Name = name;

            Cleanup();

            var xnd = IO.XndHolder.SyncLoadXND(name.Address);

            IO.XndNode node = xnd.Node;

            var attr = node.FindAttrib("Vertices");

            if (attr == null)
            {
                return(false);
            }
            attr.BeginRead();
            int count = 0;

            attr.Read(out count);
            for (int i = 0; i < count; i++)
            {
                GpuSceneVertex vert;
                attr.Read(out vert);
                MeshVertices.Add(vert);
            }
            attr.EndRead();

            attr = node.FindAttrib("ClusterDatas");
            if (attr == null)
            {
                return(false);
            }
            attr.BeginRead();
            attr.Read(out count);
            for (int i = 0; i < count; i++)
            {
                GpuCluster cluster;
                attr.Read(out cluster);
                ClusterDatas.Add(cluster);
            }
            attr.EndRead();

            attr = node.FindAttrib("IndexBuffer");
            attr.BeginRead();
            attr.Read(out count);
            for (int i = 0; i < count; i++)
            {
                uint index;
                attr.Read(out index);
                IndexBuffer.Add(index);
            }
            attr.EndRead();

            unsafe
            {
                ResourceState.ResourceSize = (uint)(MeshVertices.Count * sizeof(GpuSceneVertex) + ClusterDatas.Count * sizeof(GpuCluster) + IndexBuffer.Count * sizeof(uint));
            }

            return(true);
        }