0

I'm trying to implement the InstancedModelSample code into my game, and it's worked perfectly except for one problem - The edges on my icospheres are being drawn between the wrong vertices and it's just drawing a garbled mess.

When I used to draw the 300 or so models with individual Draw() calls, it renders fine, but not with this new code. I'm new to this stuff - how do I get the method to fill in the right gaps with faces when this seems to be a generic method useable with anything?

My code below is only slightly adjusted from the original sample, with parts that I can see aren't necessary to me (like bone transforms) excluded.

    private void DrawSeriesOfModels(Model model, Matrix[] matrices, Matrix view, Matrix Projection)
    {
        // Set up vertex declaration.
        VertexDeclaration vertexDeclaration = new VertexDeclaration
        (
            new VertexElement(0, VertexElementFormat.Vector4, VertexElementUsage.BlendWeight, 0),
            new VertexElement(16, VertexElementFormat.Vector4, VertexElementUsage.BlendWeight, 1),
            new VertexElement(32, VertexElementFormat.Vector4, VertexElementUsage.BlendWeight, 2),
            new VertexElement(48, VertexElementFormat.Vector4, VertexElementUsage.BlendWeight, 3)
        );

        // Set up dynamic vertic buffer.
        DynamicVertexBuffer vertexBuffer = new DynamicVertexBuffer(GraphicsDevice, vertexDeclaration, matrices.Length, BufferUsage.WriteOnly);

        // Transfer matrices into buffer.
        vertexBuffer.SetData(matrices, 0, matrices.Length, SetDataOptions.Discard);

        foreach (ModelMesh mesh in model.Meshes)
        {
            foreach (ModelMeshPart meshPart in mesh.MeshParts)
            {
                GraphicsDevice.SetVertexBuffers(new VertexBufferBinding(meshPart.VertexBuffer, meshPart.VertexOffset, 0),
                                                new VertexBufferBinding(vertexBuffer, 0, 1));

                // Set up instance rendering effect.
                Effect effect = meshPart.Effect;
                effect.CurrentTechnique = effect.Techniques["HardwareInstancing"];
                effect.Parameters["World"].SetValue(Matrix.Identity);
                effect.Parameters["View"].SetValue(view);
                effect.Parameters["Projection"].SetValue(Projection);
                effect.Parameters["AmbientLight"].SetValue(1);      // Make model solid white.


                // Draw everything in a single call.
                foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    pass.Apply();
                    GraphicsDevice.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0, meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount, matrices.Length);
                }
            }
        }
        amountOfDraws++;
    }



Thanks in advance to anyone who can help me out :)

4

1 回答 1

0

好吧,恐怕这是我的粗心造成的,但是如果其他人有类似问题的可能性很小,问题是我没有包括该行

    GraphicsDevice.Indices = meshPart.IndexBuffer;

就在这条线之后

    GraphicsDevice.SetVertexBuffers(new VertexBufferBinding(meshPart.VertexBuffer, meshPart.VertexOffset, 0),
                                            new VertexBufferBinding(vertexBuffer, 0, 1));

...我的错。

于 2014-08-21T21:43:31.090 回答