using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace XnaMesh
{
class Box
{
public Matrix world;
public Vector3 diffuse;
}
class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch sprite;
SpriteFont font;
DateTime prevTime;
int draw = 0;
int fps = 0;
Box[] boxList;
Matrix view;
Matrix proj;
Model model;
float fDist; // 原点との距離
int nLat; // 緯度
int nLong; // 経度
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
prevTime = DateTime.Now;
fDist = 5.0f;
nLat = 30;
nLong = 120;
proj = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
GraphicsDevice.Viewport.AspectRatio, 1.0f, 10.0f);
base.Initialize();
}
protected override void LoadContent()
{
sprite = new SpriteBatch(GraphicsDevice);
// ContentにSprite Fontを追加しておく
font = Content.Load<SpriteFont>("SpriteFont1");
// Contentに.xファイルを追加しておく
model = Content.Load<Model>("Box");
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
}
}
boxList = new Box[3];
boxList[0] = new Box();
boxList[0].world = Matrix.CreateTranslation(new Vector3(0.5f, 0.5f, 0.5f));
boxList[0].diffuse = new Vector3(0.8f, 0.0f, 0.0f);
boxList[1] = new Box();
boxList[1].world = Matrix.CreateTranslation(new Vector3(2.5f, 0.5f, 0.5f));
boxList[1].diffuse = new Vector3(0.0f, 0.8f, 0.0f);
boxList[2] = new Box();
boxList[2].world = Matrix.CreateTranslation(new Vector3(0.5f, 0.5f, 2.5f));
boxList[2].diffuse = new Vector3(0.0f, 0.0f, 0.8f);
base.LoadContent();
}
protected override void Update(GameTime gameTime)
{
// キー入力
KeyboardState state = Keyboard.GetState();
if (state[Keys.Left] == KeyState.Down)
{
nLong += 2;
}
if (state[Keys.Right] == KeyState.Down)
{
nLong -= 2;
}
if (state[Keys.Up] == KeyState.Down)
{
nLat += 2;
}
if (state[Keys.Down] == KeyState.Down)
{
nLat -= 2;
}
if (state[Keys.PageUp] == KeyState.Down)
{
fDist -= 0.1f;
}
if (state[Keys.PageDown] == KeyState.Down)
{
fDist += 0.1f;
}
if (state[Keys.Escape] == KeyState.Down)
{
Exit();
}
// 視点
float fRad = MathHelper.ToRadians(nLat);
float fY = (float)Math.Sin(fRad) * fDist;
float fR = (float)Math.Cos(fRad) * fDist;
fRad = MathHelper.ToRadians(nLong);
float fX = (float)Math.Cos(fRad) * fR;
float fZ = (float)Math.Sin(fRad) * fR;
view = Matrix.CreateLookAt(new Vector3(fX, fY, fZ), Vector3.Zero, Vector3.Up);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
// モデル描画
foreach (Box box in boxList)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.DiffuseColor = box.diffuse;
}
}
model.Draw(box.world, view, proj);
}
// フレームレート
draw++;
DateTime now = DateTime.Now;
TimeSpan t = now - prevTime;
if (t.TotalMilliseconds >= 1000)
{
fps = draw;
draw = 0;
prevTime = now;
}
sprite.Begin();
string text = "fps=" + fps + " lat=" + nLat + " long=" + nLong
+ " dist=" + fDist.ToString();
sprite.DrawString(font, text, new Vector2(0, 0), Color.White);
sprite.End();
base.Draw(gameTime);
}
}
}
xof 0303txt 0032
template Vector {
<3d82ab5e-62da-11cf-ab39-0020af71e433>
FLOAT x;
FLOAT y;
FLOAT z;
}
template MeshFace {
<3d82ab5f-62da-11cf-ab39-0020af71e433>
DWORD nFaceVertexIndices;
array DWORD faceVertexIndices[nFaceVertexIndices];
}
template Mesh {
<3d82ab44-62da-11cf-ab39-0020af71e433>
DWORD nVertices;
array Vector vertices[nVertices];
DWORD nFaces;
array MeshFace faces[nFaces];
[...]
}
template MeshNormals {
<f6f23f43-7686-11cf-8f52-0040333594a3>
DWORD nNormals;
array Vector normals[nNormals];
DWORD nFaceNormals;
array MeshFace faceNormals[nFaceNormals];
}
template VertexDuplicationIndices {
<b8d65549-d7c9-4995-89cf-53a9a8b031e3>
DWORD nIndices;
DWORD nOriginalVertices;
array DWORD indices[nIndices];
}
Mesh {
24;
-0.500000;-0.500000;-0.500000;,
-0.500000;-0.500000;0.500000;,
-0.500000;0.500000;0.500000;,
-0.500000;0.500000;-0.500000;,
-0.500000;0.500000;-0.500000;,
-0.500000;0.500000;0.500000;,
0.500000;0.500000;0.500000;,
0.500000;0.500000;-0.500000;,
0.500000;0.500000;-0.500000;,
0.500000;0.500000;0.500000;,
0.500000;-0.500000;0.500000;,
0.500000;-0.500000;-0.500000;,
-0.500000;-0.500000;0.500000;,
-0.500000;-0.500000;-0.500000;,
0.500000;-0.500000;-0.500000;,
0.500000;-0.500000;0.500000;,
-0.500000;-0.500000;0.500000;,
0.500000;-0.500000;0.500000;,
0.500000;0.500000;0.500000;,
-0.500000;0.500000;0.500000;,
-0.500000;-0.500000;-0.500000;,
-0.500000;0.500000;-0.500000;,
0.500000;0.500000;-0.500000;,
0.500000;-0.500000;-0.500000;;
12;
3;0,1,2;,
3;2,3,0;,
3;4,5,6;,
3;6,7,4;,
3;8,9,10;,
3;10,11,8;,
3;12,13,14;,
3;14,15,12;,
3;16,17,18;,
3;18,19,16;,
3;20,21,22;,
3;22,23,20;;
MeshNormals {
24;
-1.000000;0.000000;0.000000;,
-1.000000;0.000000;0.000000;,
-1.000000;0.000000;0.000000;,
-1.000000;0.000000;0.000000;,
0.000000;1.000000;0.000000;,
0.000000;1.000000;0.000000;,
0.000000;1.000000;0.000000;,
0.000000;1.000000;0.000000;,
1.000000;0.000000;0.000000;,
1.000000;0.000000;0.000000;,
1.000000;0.000000;0.000000;,
1.000000;0.000000;0.000000;,
0.000000;-1.000000;0.000000;,
0.000000;-1.000000;0.000000;,
0.000000;-1.000000;0.000000;,
0.000000;-1.000000;0.000000;,
0.000000;0.000000;1.000000;,
0.000000;0.000000;1.000000;,
0.000000;0.000000;1.000000;,
0.000000;0.000000;1.000000;,
0.000000;0.000000;-1.000000;,
0.000000;0.000000;-1.000000;,
0.000000;0.000000;-1.000000;,
0.000000;0.000000;-1.000000;;
12;
3;0,1,2;,
3;2,3,0;,
3;4,5,6;,
3;6,7,4;,
3;8,9,10;,
3;10,11,8;,
3;12,13,14;,
3;14,15,12;,
3;16,17,18;,
3;18,19,16;,
3;20,21,22;,
3;22,23,20;;
}
VertexDuplicationIndices {
24;
8;
0,
1,
2,
3,
3,
2,
6,
7,
7,
6,
10,
11,
1,
0,
11,
10,
1,
10,
6,
2,
0,
3,
7,
11;
}
}