Esempio 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);
        }