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

这是我用来绘制 3D 对象的代码。问题发生在 mesh.Draw();

错误是:当前顶点声明不包括当前顶点着色器所需的所有元素。缺少颜色 0。

我试图弄清楚发生了什么,但无济于事。即使你能告诉我在哪里看,这将是一个巨大的帮助!

编辑:.fx 文件在这里

4

1 回答 1

0

我需要使用 VertexPositionNormalTexture,而不是 VertexPositionColorNormal。网格不能处理颜色,但它们可以处理纹理。

于 2013-01-17T06:36:01.263 回答