Пример #1
0
 //-----------------
 public unsafe void addRenderPrimitive(BRenderPrimitive prim)
 {
     mPrimitives.Add(prim);
 }
Пример #2
0
        static private unsafe void processMesh(ref BRenderGrannyMesh mesh, granny_mesh *grannyMesh)
        {
            BRenderPrimitive prim = new BRenderPrimitive();

            //indicies
            prim.mNumInds = GrannyGetMeshTriangleCount(grannyMesh) * 3;
            int[] indData = new int[prim.mNumInds];

            int    indexSize          = prim.mNumInds * sizeof(int);
            IntPtr pMarshaledIndexMem = System.Runtime.InteropServices.Marshal.AllocHGlobal(indexSize);

            GrannyCopyMeshIndices(grannyMesh, 4, (int *)pMarshaledIndexMem);
            System.Runtime.InteropServices.Marshal.Copy(pMarshaledIndexMem, indData, 0, prim.mNumInds);
            System.Runtime.InteropServices.Marshal.FreeHGlobal(pMarshaledIndexMem);


            //ib's
            prim.mIB = new IndexBuffer(typeof(int), prim.mNumInds, BRenderDevice.getDevice(), Usage.None, Pool.Managed);
            GraphicsStream stream  = prim.mIB.Lock(0, 0, LockFlags.None);
            int *          outInds = (int *)stream.InternalDataPointer;

            for (int q = 0; q < prim.mNumInds; q++)
            {
                outInds[q] = indData[q];
            }

            prim.mIB.Unlock();
            stream.Close();


            //verticies
            prim.mVertexSize = 0;


            granny_data_type_definition *grnyVertTypeDef = GrannyGetMeshVertexType(grannyMesh);

            granny_data_type_definition[] grnDTD = null;
            VertexDeclaration             grnVD  = null;

            if (!getVertexTypeFromGranny(grnyVertTypeDef, ref prim.mVertexSize, ref grnDTD, ref grnVD, ref prim.mVDecl))
            {
                //already logged
                //CoreGlobals.getErrorManager().OnSimpleWarning(String.Format("Error loading {0} getVertexTypeFromGranny failed", filename));

                return;
            }

            prim.mNumVerts = GrannyGetMeshVertexCount(grannyMesh);

            {
                int    size          = prim.mNumVerts * prim.mVertexSize;
                IntPtr pMarshaledMem = System.Runtime.InteropServices.Marshal.AllocHGlobal(size);
                byte[] tGrnVerts     = new byte[size];


                fixed(granny_data_type_definition *grnPD = grnDTD)
                GrannyCopyMeshVertices(grannyMesh, grnPD /*grnyVertTypeDef*/, (void *)pMarshaledMem);

                System.Runtime.InteropServices.Marshal.Copy(pMarshaledMem, tGrnVerts, 0, size);
                System.Runtime.InteropServices.Marshal.FreeHGlobal(pMarshaledMem);

                byte[] d3dVerts = new byte[size];

                //swizzle the granny verts to be d3d friendly before copying them to the device
                swzzlGrnyVertsToD3DVerts(tGrnVerts, grnVD, prim.mVertexSize, prim.mNumVerts,
                                         ref d3dVerts, prim.mVDecl);



                prim.mVB = new VertexBuffer(BRenderDevice.getDevice(), (int)prim.mNumVerts * prim.mVertexSize, Usage.None, VertexFormats.None, Pool.Managed);
                stream   = prim.mVB.Lock(0, 0, LockFlags.None);

                stream.Write(d3dVerts, 0, size);
                prim.mVB.Unlock();
                stream.Close();


                tGrnVerts = null;
                grnVD.Dispose();
                grnVD = null;
            }
            //SUB GROUPS
            int groupCount = GrannyGetMeshTriangleGroupCount(grannyMesh);
            granny_tri_material_group *GrannyMatGroups = GrannyGetMeshTriangleGroups(grannyMesh);


            //process our material groups for this mesh


            for (int k = 0; k < groupCount; k++)
            {
                BRenderMaterialGroup group = new BRenderMaterialGroup();


                group.mStartIndex = GrannyMatGroups[k].TriFirst * 3;
                group.mPrimCount  = GrannyMatGroups[k].TriCount;

                //load your texture here.
                prim.mGroups.Add(group);
            }


            mesh.addRenderPrimitive(prim);
        }