XNA中的显示可分为2D图片的显示和模型的显示。
1、2D图片的显示
XNA中2D图片是通过精灵(SpriteBatch)来显示的的。
(1)、在类中声明精灵对象
SpriteBatch spriteBatch;
(2)、在LoadContent函数中构造精灵对象、加载2D图片信息
spriteBatch = new SpriteBatch(GraphicsDevice);
(3)、在Update函数中根据输入信息计算模型的屏幕坐标及旋转角度
(4)、在Draw函数中通过spriteBatch显示myTexture信息
spriteBatch.Draw(myTexture, spritePosition, null, Color.White,
0.5f , new Vector2(), 1.0f , SpriteEffects.None, 0.0f );
2、字体的显示
XNA中字体的显示与2D图片的显示一样,也是通过SpriteBatch类实现的。
public void DrawString (
SpriteFont spriteFont, /*字体*/
string text, /*输出字符串*/
Vector2 position, /*显示位置*/
Color color, /*颜色*/
float rotation, /*旋转*/
Vector2 origin, /*原点*/
Vector2 scale, /*缩放*/
SpriteEffects effects, /*特效*/
float layerDepth /*深度*/
)
显示字符串时,首先按照与2D图片显示相同的方法声明并初始化SpriteBatch对象,之后通过该对象的DrawString方法显示字符串。
spriteBatch.DrawString(spriteFont, "hello world !",
new Vector2(10, 20), Color.White);
3、3D模型的显示
XNA中3D模型的显示是通过遍历并显示该模型的ModelMesh来实现的
foreach (ModelMesh mesh in model.Meshes)
{
/*设置mesh的特效*/
foreach (BasicEffect effect in mesh.Effects)
{
/*投影变换*/
effect.World = world * boneTransforms[mesh.ParentBone.Index];
effect.View = camera.View;
effect.Projection = camera.Proj;
/*灯光效果*/
effect.LightingEnabled = true;
effect.DiffuseColor = new Vector3( 1.2f );
effect.AmbientLightColor = new Vector3( 0.5f );
/*雾化效果*/
effect.FogEnabled = true;
effect.FogStart = 6000;
effect.FogEnd = 10000;
effect.FogColor = Color.CornflowerBlue.ToVector3();
}
/*显示mesh*/
mesh.Draw();
}