【发布时间】:2013-01-17 14:36:01
【问题描述】:
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();
错误是: 当前顶点声明不包括当前顶点着色器所需的所有元素。缺少 Color0。
我试图弄清楚发生了什么,但无济于事。即使你能告诉我去哪里看,也会有很大的帮助!
编辑:.fx 文件是here。
【问题讨论】:
-
我不确定 100%,如果您已经收到回复,我不会留下此评论,但我认为 HLSL 期望您设置 effect.Parameters["Color0 "] 到某事。您必须检查 .fx 文件。
-
谢谢!我去看看。
-
我不认为是这样...我添加了 .fx 文件,如果我错了,请告诉我,但这真的没有意义。
-
您正在使用的效果的顶点着色器要求顶点声明的形式为 VertexPositionNormalColor。我不确定您使用的是什么顶点声明,但这不是预期的效果。 (即,对于传递给它的每个顶点,它还需要一种与该顶点相关联的颜色)
-
谢谢...但我没有使用任何形式的 VertexPositionSomethingElseHere,只是 mesh.Draw();,有什么办法可以改变它吗?如果我在模型上贴上皮肤,会改变什么吗?