開発環境 Microsoft Visual C# 2010 Express (SP1)
実行環境 Microsoft Windows XP Home Edition (SP3)
プロジェクトの種類 Windows Game (4.0)
プロジェクト名 XnaSample

Xファイルをメタセコイアなどの3Dポリゴンモデラー、DirectX SDKサンプルなどから用意する。
ソリューションエクスプローラーのXnaSampleContent (Content)を右クリックして[追加]-[既存の項目]を選択する。

参考

Program_2.cs
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
 
class Game1 : Game
{
	GraphicsDeviceManager gdm;
	Model model;
	Matrix world, view, proj;
	float rotate = 0.0f;
 
	SpriteBatch sprBat;
	SpriteFont sprFont;
	DateTime prevTime;
	int count = 0;
	int fps = 0;
 
	public Game1()
	{
		gdm = new GraphicsDeviceManager(this);
		Content.RootDirectory = "Content";
	}
 
	protected override void Initialize()
	{
		prevTime = DateTime.Now;
 
		world = Matrix.Identity;
		proj = Matrix.CreatePerspectiveFieldOfView(
			MathHelper.ToRadians(45.0f), GraphicsDevice.Viewport.AspectRatio,
			1.0f, 1000.0f);
		view = Matrix.CreateLookAt(new Vector3(2.0f, 2.0f, 5.0f),
			Vector3.Zero, Vector3.Up);
 
		base.Initialize();
	}
 
	protected override void LoadContent()
	{
		sprBat = new SpriteBatch(GraphicsDevice);
		// ContentにSprite Fontを追加しておく
		sprFont = Content.Load<SpriteFont>("SpriteFont1");
		// ContentにXファイルを追加しておく
		model = Content.Load<Model>("teapot");
 
		base.LoadContent();
	}
 
	protected override void Update(GameTime gameTime)
	{
		rotate -= (float)gameTime.ElapsedGameTime.TotalSeconds;
		world = Matrix.CreateRotationY(rotate);
 
		foreach (ModelMesh mesh in model.Meshes) {
			foreach (ModelMeshPart meshPart in mesh.MeshParts) {
				BasicEffect effect = meshPart.Effect as BasicEffect;
//				effect.World = world;
//				effect.View = view;
//				effect.Projection = proj;
				effect.LightingEnabled = true;
			}
		}
 
		base.Update(gameTime);
	}
 
	protected override void Draw(GameTime gameTime)
	{
		GraphicsDevice.Clear(Color.CornflowerBlue);
 
		// モデル描画
		model.Draw(world, view, proj);
/*
		foreach (ModelMesh mesh in model.Meshes) {
			foreach (ModelMeshPart meshPart in mesh.MeshParts) {
				GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer);
				GraphicsDevice.Indices = meshPart.IndexBuffer;
				foreach (EffectTechnique tech in meshPart.Effect.Techniques) {
					foreach (EffectPass pass in tech.Passes) {
						pass.Apply();
						GraphicsDevice.DrawIndexedPrimitives(
							PrimitiveType.TriangleList,
							meshPart.VertexOffset, 0,
							meshPart.NumVertices,
							meshPart.StartIndex,
							meshPart.PrimitiveCount);
					}
				}
			}
		}
*/
		// フレームレート
		count++;
		DateTime now = DateTime.Now;
		TimeSpan t = now - prevTime;
		if (t.TotalMilliseconds >= 1000) {
			fps = count;
			count = 0;
			prevTime = now;
		}
		sprBat.Begin();
		sprBat.DrawString(sprFont, "fps = " + fps, new Vector2(0, 0), Color.White);
		sprBat.End();
 
		base.Draw(gameTime);
	}
}
 
class Program
{
	static void Main()
	{
		using (Game1 game = new Game1()) {
			game.IsMouseVisible = true;
			game.Run();
		}
	}
}
 
最終更新:2012年11月05日 16:45