XNA系列—3d模型的导入

using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; using Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; namespace My3DGame { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Model terrain; //地形的模型 Vector3 terrainPosition; //地形的世界坐标位置 Matrix cameraViewMatrix; //照相机视图矩阵 Matrix cameraProjectionMatrix; //照相机投影矩阵 public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); terrain = Content.Load<Model>("Models\\terrain"); terrainPosition = Vector3.Zero; //初始化摄像机的视图矩阵 参数: 摄像机位置,摄像机目标位置,看的方向 cameraViewMatrix = Matrix.CreateLookAt( new Vector3(0f, 60f, 160f), new Vector3(0f, 50f, 0f), Vector3.Up); //初始化摄像机的投影矩阵 参数:摄像机看的范围、屏幕的比例、照相机看的最近的距离、照相机看的最远的距离 cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView( MathHelper.PiOver4, graphics.GraphicsDevice.Viewport.AspectRatio, 1f, 10000f); } protected override void UnloadContent() { } protected override void Update(GameTime gameTime) { base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); foreach (ModelMesh mesh in terrain.Meshes) { foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.PreferPerPixelLighting = true; effect.World = Matrix.CreateTranslation(terrainPosition); //将世界坐标转化成矩阵坐标 effect.View = cameraViewMatrix; effect.Projection = cameraProjectionMatrix; } mesh.Draw(); } base.Draw(gameTime); } } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值