1
votes
private void DrawModel()
    {
        Matrix worldMatrix = Matrix.CreateScale(0.0005f, 0.0005f, 0.0005f) * Matrix.CreateRotationZ(MathHelper.Pi) * Matrix.CreateTranslation(new Vector3(19, 12, -5));

        Matrix[] modelTransforms = new Matrix[testModel.Bones.Count];
        testModel.CopyAbsoluteBoneTransformsTo(modelTransforms);
        foreach (ModelMesh mesh in testModel.Meshes)
        {
            foreach (Effect currentEffect in mesh.Effects)
            {
                currentEffect.CurrentTechnique = currentEffect.Techniques["Colored"];
                currentEffect.Parameters["xWorld"].SetValue(modelTransforms[mesh.ParentBone.Index] * worldMatrix);
                currentEffect.Parameters["xView"].SetValue(viewMatrix);
                currentEffect.Parameters["xProjection"].SetValue(projectionMatrix);
            }
            mesh.Draw();
        }
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);
        DepthStencilState depthBufferState = new DepthStencilState();
        depthBufferState.DepthBufferEnable = true;
        GraphicsDevice.DepthStencilState = depthBufferState;
        RasterizerState rs = new RasterizerState();
        if (wireframeMode)
            rs.FillMode = FillMode.WireFrame;
        if (showAllTriangles)
            rs.CullMode = CullMode.None;//DO NOT INCLUDE IN FINAL PRODUCT--DRAWS ALL TRIANGLES
        GraphicsDevice.RasterizerState = rs;
        Matrix worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, -terrainHeight / 2.0f, 0) * Matrix.CreateRotationZ(angle);
        effect.CurrentTechnique = effect.Techniques["Colored"];
        effect.Parameters["xView"].SetValue(viewMatrix);
        effect.Parameters["xProjection"].SetValue(projectionMatrix);
        effect.Parameters["xWorld"].SetValue(worldMatrix);
        //lighting (ambient)
        Vector3 lightDirection = new Vector3(1.0f, -1.0f, -1.0f);
        lightDirection.Normalize();
        effect.Parameters["xLightDirection"].SetValue(lightDirection);
        effect.Parameters["xAmbient"].SetValue(0.1f);
        effect.Parameters["xEnableLighting"].SetValue(true);
        //drawing
        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();

        }
        GraphicsDevice.Indices = indexBuffer;
        GraphicsDevice.SetVertexBuffer(vertexBuffer);
        GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, indices.Length / 3);
        DrawModel();
        base.Draw(gameTime);
    }

This is the code I am using to draw a 3D object. The problem occurs on mesh.Draw();

The error is: The current vertex declaration does not include all the elements required by the current vertex shader. Color0 is missing.

I have tried to figure out what is going on, to no avail. Even if you can just tell me where to look, it will be a massive help!

EDIT: the .fx file is here.

1
I'm not 100% sure and I wouldn't be leaving this comment if you had already received responses, but I'm thinking that the HLSL is expecting you to set effect.Parameters["Color0"] to something. You'd have to check the .fx file.itsme86
Thanks! I will check it out.Halleflux
I don't think that's it... I added the .fx file in, so tell me if I'm wrong, but this is really making no sense.Halleflux
The vertex shader for the effect you're using expects the Vertex Declaration to be of the form VertexPositionNormalColor. I'm not sure what vertex declaration you're using but it's not what the effect is expecting. (I.E. For each vertex getting passed to it it also wants a color to associate with that vertex)Matt Razza
Thanks... but I'm not using any form of VertexPositionSomethingElseHere, just mesh.Draw();, so is there any way to change that? If I put a skin on the model, will that change anything?Halleflux

1 Answers

0
votes

I needed to use VertexPositionNormalTexture, not VertexPositionColorNormal. Meshes can not handle colors, but they can handle textures.