566
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
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.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace WPGameTest1
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Camera camera;//
VertexPositionTexture[] verts;////
VertexBuffer vertexBuffer;//
BasicEffect effect;//
Matrix worldTranslation = Matrix.Identity;//
Matrix worldRotation = Matrix.Identity;//
List<String> btnArr = new List<String>();//这是定义MessageBox.Show()的按钮数组
Texture2D texture;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
TargetElapsedTime = TimeSpan.FromTicks(333333);
}
protected override void Initialize()
{
camera = new Camera(this, new Vector3(0, 0, 5), Vector3.Zero, Vector3.Up);//
Components.Add(camera);//
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
texture = Content.Load<Texture2D>(@"Textures\Trees");
verts = new VertexPositionTexture[4];
verts[0] = new VertexPositionTexture(
new Vector3(-1, 1, 0), new Vector2(0, 0));
verts[1] = new VertexPositionTexture(
new Vector3(1, 1, 0), new Vector2(1, 0));
verts[2] = new VertexPositionTexture(
new Vector3(-1, -1, 0), new Vector2(0, 1));
verts[3] = new VertexPositionTexture(
new Vector3(1, -1, 0), new Vector2(1, 1));
//设置数据 到VertexBuffer
vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture),
verts.Length, BufferUsage.None);////
vertexBuffer.SetData(verts);//
// Initialize the BasicEffect
effect = new BasicEffect(GraphicsDevice);//
btnArr.Add("确定");//
btnArr.Add("取消");//
RasterizerState rs = new RasterizerState();
rs.CullMode = CullMode.None;
GraphicsDevice.RasterizerState = rs;
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
worldTranslation *= Matrix.CreateTranslation(-0.002f, 0, 0);
worldRotation *= Matrix.CreateFromYawPitchRoll(MathHelper.PiOver4 / 60,0,0);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.SetVertexBuffer(vertexBuffer);//
effect.World = worldRotation * worldTranslation;
effect.View = camera.view;
effect.Projection = camera.projection;
effect.Texture = texture;
effect.TextureEnabled = true;
// Begin effect and draw for each pass
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>
(PrimitiveType.TriangleStrip, verts, 0, 2);
}
base.Draw(gameTime);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
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;
namespace WPGameTest1
{
/// <summary>
/// This is a game component that implements IUpdateable.
/// </summary>
public class Camera : Microsoft.Xna.Framework.GameComponent
{
public Matrix view { get; protected set; }
public Matrix projection { get; protected set; }
public Camera(Game game, Vector3 pos, Vector3 target, Vector3 up)
: base(game)
{
view = Matrix.CreateLookAt(pos, target, up);//摄像机的:坐标 目标 朝向
projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.PiOver4,
(float)Game.Window.ClientBounds.Width /
(float)Game.Window.ClientBounds.Height,
1, 100);//摄像机的:视角弧度,通常是45度或者π/4 摄像机长宽比,通常使用屏幕宽度除以屏幕高度 最近看清距离 最远看清距离
}
/// <summary>
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
/// </summary>
public override void Initialize()
{
// TODO: Add your initialization code here
base.Initialize();
}
/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
base.Update(gameTime);
}
}
}