開発環境 |
Microsoft Visual C# 2010 Express (SP1) |
実行環境 |
Microsoft Windows XP Home Edition (SP3) |
プロジェクトの種類 |
Windows Game (4.0) |
プロジェクト名 |
XnaModel |
Game1.cs
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace XnaModel
{
class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch sprite;
SpriteFont font;
Model model;
Matrix[] worlds;
Matrix proj;
MouseState mStateOld = new MouseState();
// fps
int sec;
int draw = 0;
int fps = 0;
// カメラ
float camLat = 30;
float camLon = -150;
float camDist = 10;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void LoadContent()
{
sprite = new SpriteBatch(GraphicsDevice);
font = Content.Load<SpriteFont>("SpriteFont1");
model = Content.Load<Model>("aircraft");
worlds = new Matrix[3];
worlds[0] = Matrix.CreateTranslation(new Vector3(0, 0, 0));
worlds[1] = Matrix.CreateTranslation(new Vector3(10, 0, -10));
worlds[2] = Matrix.CreateTranslation(new Vector3(10, 0, 10));
proj = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 0.1f, 1000);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
//effect.DiffuseColor = new Vector3(0.8f, 0.8f, 0.8f);
}
}
base.LoadContent();
}
protected override void Update(GameTime gameTime)
{
float deltaLat = 0;
float deltaLon = 0;
float deltaDist = 0;
// キーボード
KeyboardState kState = Keyboard.GetState();
if (kState.IsKeyDown(Keys.Escape)) Exit();
if (kState.IsKeyDown(Keys.Up)) deltaLat = 1;
if (kState.IsKeyDown(Keys.Down)) deltaLat = -1;
if (kState.IsKeyDown(Keys.Left)) deltaLon = 1;
if (kState.IsKeyDown(Keys.Right)) deltaLon = -1;
if (kState.IsKeyDown(Keys.PageUp)) deltaDist = -0.2f;
if (kState.IsKeyDown(Keys.PageDown)) deltaDist = 0.2f;
// マウス
MouseState mState = Mouse.GetState();
if (mState.LeftButton == ButtonState.Pressed)
{
deltaLat = (mState.Y - mStateOld.Y) * 0.5f;
deltaLon = (mState.X - mStateOld.X) * 0.5f;
}
if (mState.ScrollWheelValue != mStateOld.ScrollWheelValue)
{
deltaDist = (mState.ScrollWheelValue - mStateOld.ScrollWheelValue) / -120;
}
mStateOld = mState;
// カメラ移動
if (deltaLat != 0)
{
camLat = MathHelper.Clamp(camLat + deltaLat, -89.9f, 89.9f);
}
if (deltaLon != 0)
{
camLon += deltaLon;
if (camLon <= -180) camLon += 360;
if (180 < camLon) camLon -= 360;
}
if (deltaDist != 0)
{
camDist = MathHelper.Clamp(camDist + deltaDist, 1, 1000);
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.BlendState = BlendState.AlphaBlend;
//GraphicsDevice.RasterizerState = new RasterizerState { FillMode = FillMode.WireFrame};
// カメラ位置
float rad = MathHelper.ToRadians(camLat);
float y = (float)Math.Sin(rad) * camDist;
float r = (float)Math.Cos(rad) * camDist;
rad = MathHelper.ToRadians(camLon);
float x = (float)Math.Cos(rad) * r;
float z = (float)Math.Sin(rad) * r;
Matrix view = Matrix.CreateLookAt(new Vector3(x, y, z), Vector3.Zero, Vector3.Up);
foreach (Matrix world in worlds)
{
model.Draw(world, view, proj);
}
// fps
draw++;
if (gameTime.TotalGameTime.Seconds != sec)
{
fps = draw;
draw = 0;
sec = gameTime.TotalGameTime.Seconds;
}
// 2D描画
sprite.Begin();
string text = String.Format(
"fps={0} lat={1} long={2} dist={3:f1}", fps, camLat, camLon, camDist);
sprite.DrawString(font, text, new Vector2(0, 0), Color.White);
sprite.End();
base.Draw(gameTime);
}
}
}
最終更新:2012年12月26日 21:31