Exemple #1
0
        /// <summary>
        /// Loads a batch renderer for a doodad in a render context
        /// </summary>
        /// <param name="mgr">The render context</param>
        /// <param name="doodadName">Name of the doodad (case insensitive)</param>
        public M2BatchRender(string doodadName)
        {
            Intersector = new ModelIntersector(this);
            ModelName   = doodadName;
            M2Info inf = Game.GameManager.M2ModelCache.GetInfo(doodadName);

            mModelInfo = inf;
            Game.GameManager.GraphicsThread.CallOnThread(
                () =>
            {
                for (int i = 0; i < inf.Passes.Count; ++i)
                {
                    Models.MDX.M2RenderPass pass = inf.Passes[i];
                    Mesh mesh = new Mesh(
                        Game.GameManager.GraphicsThread.GraphicsManager.Device,
                        pass.Vertices.Length / 3,
                        pass.Vertices.Length,
                        MeshFlags.Managed,
                        MdxVertex.FVF
                        );

                    VertexBuffer vb = new VertexBuffer(Game.GameManager.GraphicsThread.GraphicsManager.Device, pass.Vertices.Length * Marshal.SizeOf(typeof(Models.MDX.MdxVertex)), Usage.None,
                                                       Models.MDX.MdxVertex.FVF, Pool.Managed);
                    DataStream strm = vb.Lock(0, 0, LockFlags.None);
                    strm.WriteRange(pass.Vertices);
                    vb.Unlock();

                    strm = mesh.VertexBuffer.Lock(0, 0, LockFlags.None);
                    strm.WriteRange(pass.Vertices);
                    mesh.VertexBuffer.Unlock();

                    IndexBuffer ib = new IndexBuffer(Game.GameManager.GraphicsThread.GraphicsManager.Device, pass.Vertices.Length * 2, Usage.None, Pool.Managed, true);
                    strm           = ib.Lock(0, 0, LockFlags.None);
                    for (int j = 0; j < pass.Vertices.Length; ++j)
                    {
                        strm.Write((short)j);
                    }
                    ib.Unlock();

                    strm = mesh.IndexBuffer.Lock(0, 0, LockFlags.None);
                    for (int j = 0; j < pass.Vertices.Length; ++j)
                    {
                        strm.Write((short)j);
                    }
                    mesh.IndexBuffer.Unlock();

                    try
                    {
                        Textures.Add(Video.TextureManager.GetTexture(pass.Texture));
                    }
                    catch (Exception)
                    {
                        Textures.Add(Video.TextureManager.Default.ErrorTexture);
                    }
                    NumTriangles.Add(pass.Vertices.Length / 3);
                    Indices.Add(ib);
                    Meshes.Add(vb);
                    mMeshPasses.Add(mesh);
                }
            }
                );
            InstanceDeclaration = new VertexDeclaration(Game.GameManager.GraphicsThread.GraphicsManager.Device, ElemDecl);
            InstanceLoader      = new M2InstanceLoader(this);

            Game.GameManager.GraphicsThread.OnFrame += new Game.VideoThread.FrameRenderDelegate(_OnFrame);
        }
Exemple #2
0
        /// <summary>
        /// Loads a batch renderer for a doodad in a render context
        /// </summary>
        /// <param name="mgr">The render context</param>
        /// <param name="doodadName">Name of the doodad (case insensitive)</param>
        public M2BatchRender(string doodadName)
        {
            Intersector = new ModelIntersector(this);
            ModelName = doodadName;
            M2Info inf = Game.GameManager.M2ModelCache.GetInfo(doodadName);
            mModelInfo = inf;
            Game.GameManager.GraphicsThread.CallOnThread(
                () =>
                {
                    for (int i = 0; i < inf.Passes.Count; ++i)
                    {
                        Models.MDX.M2RenderPass pass = inf.Passes[i];
                        Mesh mesh = new Mesh(
                           Game.GameManager.GraphicsThread.GraphicsManager.Device,
                           pass.Vertices.Length / 3,
                           pass.Vertices.Length,
                           MeshFlags.Managed,
                           MdxVertex.FVF
                       );

                        VertexBuffer vb = new VertexBuffer(Game.GameManager.GraphicsThread.GraphicsManager.Device, pass.Vertices.Length * Marshal.SizeOf(typeof(Models.MDX.MdxVertex)), Usage.None,
                            Models.MDX.MdxVertex.FVF, Pool.Managed);
                        DataStream strm = vb.Lock(0, 0, LockFlags.None);
                        strm.WriteRange(pass.Vertices);
                        vb.Unlock();

                        strm = mesh.VertexBuffer.Lock(0, 0, LockFlags.None);
                        strm.WriteRange(pass.Vertices);
                        mesh.VertexBuffer.Unlock();

                        IndexBuffer ib = new IndexBuffer(Game.GameManager.GraphicsThread.GraphicsManager.Device, pass.Vertices.Length * 2, Usage.None, Pool.Managed, true);
                        strm = ib.Lock(0, 0, LockFlags.None);
                        for (int j = 0; j < pass.Vertices.Length; ++j)
                            strm.Write((short)j);
                        ib.Unlock();

                        strm = mesh.IndexBuffer.Lock(0, 0, LockFlags.None);
                        for (int j = 0; j < pass.Vertices.Length; ++j)
                            strm.Write((short)j);
                        mesh.IndexBuffer.Unlock();

                        try
                        {
                            Textures.Add(Video.TextureManager.GetTexture(pass.Texture));
                        }
                        catch (Exception)
                        {
                            Textures.Add(Video.TextureManager.Default.ErrorTexture);
                        }
                        NumTriangles.Add(pass.Vertices.Length / 3);
                        Indices.Add(ib);
                        Meshes.Add(vb);
                        mMeshPasses.Add(mesh);
                    }
                }
            );
            InstanceDeclaration = new VertexDeclaration(Game.GameManager.GraphicsThread.GraphicsManager.Device, ElemDecl);
            InstanceLoader = new M2InstanceLoader(this);

            Game.GameManager.GraphicsThread.OnFrame += new Game.VideoThread.FrameRenderDelegate(_OnFrame);
        }