XNA 中3D模型的显示:
ModelMeshPart[] meshParts;
Model start_model;
Matrix[] dq_model_transforms;
Matrix view = Matrix.CreateLookAt(new Vector3(72.93132f, 67.70515f, 101.329f), new Vector3(7.16512f, 66.22025f, 5.214687f), new Vector3(-0.01481795f, 0.9998762f, -0.0053083f));
Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(88f), 800f / 600f, 0.1f, 1000000f);
Matrix[] dq_transforms;
Dictionary
Dictionary
public void add_Set_model(string model_name)
{
Model model = contentManager.Load
start_model = model;
Set_mesh(start_model, model_name);
}
public void Set_mesh(Model model, string model_name)
{
dq_transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(dq_transforms);
//dq_model_transforms = dq_transforms;
foreach (ModelMesh mesh in model.Meshes)
{
if (!dq_transforms_d.ContainsKey(mesh.Name))
{
dq_transforms_d.Add(mesh.Name, dq_transforms[mesh.ParentBone.Index]);
}
//dq_model_transforms_All.Add(dq_model_transforms);
foreach (ModelMeshPart part in mesh.MeshParts)
{
if (!dq_ModelMeshPart_d.ContainsKey(mesh.Name))
{
dq_ModelMeshPart_d.Add(mesh.Name, part);
}
}
}
}
protected override void Draw()
{
Color backColor = new Color(BackColor.R, BackColor.G, BackColor.B);
GraphicsDevice.Clear(Color.Black);
#region ModelMeshPart画模型
//foreach (Effect effect_ls in mesh.Effects)
foreach (var dq_part in dq_ModelMeshPart_d)
{
//dq_part.Value.Effect
BasicEffect myBasicEffect = dq_part.Value.Effect as BasicEffect;
//dq_transforms_d[dq_part.Key]
myBasicEffect.World = dq_transforms_d[dq_part.Key];
myBasicEffect.View = view;
myBasicEffect.Projection = projection;
myBasicEffect.EnableDefaultLighting();
myBasicEffect.PreferPerPixelLighting = true;
myBasicEffect.SpecularPower = 16;
//获取或设置活动技术。 获取此渲染技术所需的EffectPass对象的集合。 开始这个
myBasicEffect.CurrentTechnique.Passes[0].Apply();
GraphicsDevice.Indices = dq_part.Value.IndexBuffer;
GraphicsDevice.SetVertexBuffer(dq_part.Value.VertexBuffer);
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, dq_part.Value.VertexOffset, 0, dq_part.Value.NumVertices, dq_part.Value.StartIndex, dq_part.Value.PrimitiveCount);
}
#endregion
#region model.Draw画模型
//foreach (ModelMesh mesh in start_model.Meshes)
//{
// foreach (Effect effect_ls in mesh.Effects)
// {
// Type effects_type = effect_ls.GetType();
// if (effects_type.Name == "BasicEffect")
// {
// BasicEffect effect = (BasicEffect)effect_ls;
// effect.World = dq_model_transforms[mesh.ParentBone.Index];
// effect.View = view;
// effect.Projection = projection;
// effect.EnableDefaultLighting();
// effect.PreferPerPixelLighting = true;
// effect.SpecularPower = 16;
// }
// }
// if (!line_mode_flag)
// {
// mesh.Draw();
// }
// else
// {
// foreach (ModelMeshPart part in mesh.MeshParts)
// {
// BasicEffect myBasicEffect = part.Effect as BasicEffect;
// myBasicEffect.World = dq_model_transforms[mesh.ParentBone.Index];
// //myBasicEffect.View = view;
// //myBasicEffect.Projection = projection;
// // myBasicEffect.TextureEnabled = true;
// myBasicEffect.CurrentTechnique.Passes[0].Apply();
// //myBasicEffect.Alpha = dq_Alpha;
// GraphicsDevice.Indices = part.IndexBuffer;
// GraphicsDevice.SetVertexBuffer(part.VertexBuffer);
// GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, part.VertexOffset, 0, part.NumVertices, part.StartIndex, part.PrimitiveCount);
// }
// }
#endregion
}
}
}
}
注:Model model = contentManager.Load