XNA中的显示

 

          XNA中的显示可分为2D图片的显示和模型的显示。

12D图片的显示

                   XNA2D图片是通过精灵(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);

           33D模型的显示
  
  
                 XNA3D模型的显示是通过遍历并显示该模型的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();

            }
   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值