// XnaCube2 立方体(法線)
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using CustomVertex;
namespace XnaCube
{
class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch sprite;
SpriteFont font;
BasicEffect effect;
VertexPositionNormalColor[] vertices24;
short[] indices24;
VertexPositionNormalColor[] vertices8;
short[] indices8;
// fps
int sec;
int draw = 0;
int fps = 0;
// カメラ
int camLat = 0;
int camLong = 90;
float camDist = 8;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void LoadContent()
{
font = Content.Load<SpriteFont>("SpriteFont1");
sprite = new SpriteBatch(GraphicsDevice);
effect = new BasicEffect(GraphicsDevice);
effect.VertexColorEnabled = true;
effect.EnableDefaultLighting();
effect.Projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 1, 100);
// 8頂点
Vector3[] cube =
{
new Vector3(1, 1, 1), new Vector3(1, 1, -1),
new Vector3(1, -1, 1), new Vector3(1, -1, -1),
new Vector3(-1, -1, -1), new Vector3(-1, 1, -1),
new Vector3(-1, -1, 1), new Vector3(-1, 1, 1),
};
vertices8 = new VertexPositionNormalColor[8];
for (int n = 0; n < 8; n++)
{
vertices8[n] = new VertexPositionNormalColor(
cube[n], Vector3.Normalize(cube[n]), Color.Yellow);
}
indices8 = new short[]
{
0, 1, 2, 3, 2, 1, // +X面
4, 5, 6, 7, 6, 5, // -X面
0, 7, 1, 5, 1, 7, // +Y面
4, 6, 3, 2, 3, 6, // -Y面
0, 2, 7, 6, 7, 2, // +Z面
4, 3, 5, 1, 5, 3, // -Z面
};
// 24頂点
vertices24 = new VertexPositionNormalColor[24];
for (int n = 0; n < 4; n++)
{
int u = 1 - (n & 1) * 2;
int v = 1 - ((n >> 1) & 1) * 2;
// +X面
vertices24[0 + n] = new VertexPositionNormalColor(
new Vector3(1, v, u), new Vector3(1, 0, 0), Color.Yellow);
// -X面
vertices24[4 + n] = new VertexPositionNormalColor(
new Vector3(-1, -u, -v), new Vector3(-1, 0, 0), Color.Yellow);
// +Y面
vertices24[8 + n] = new VertexPositionNormalColor(
new Vector3(u, 1, v), new Vector3(0, 1, 0), Color.Yellow);
// -Y面
vertices24[12 + n] = new VertexPositionNormalColor(
new Vector3(-v, -1, -u), new Vector3(0, -1, 0), Color.Yellow);
// +Z面
vertices24[16 + n] = new VertexPositionNormalColor(
new Vector3(v, u, 1), new Vector3(0, 0, 1), Color.Yellow);
// -Z面
vertices24[20 + n] = new VertexPositionNormalColor(
new Vector3(-u, -v, -1), new Vector3(0, 0, -1), Color.Yellow);
}
indices24 = new short[36];
for (int i = 0; i < 36; i++)
{
int n = i % 6;
indices24[i] = (short)(i / 6 * 4 + (n < 3 ? n : 6 - n));
}
base.LoadContent();
}
protected override void Update(GameTime gameTime)
{
KeyboardState kState = Keyboard.GetState();
if (kState.IsKeyDown(Keys.Escape)) Exit();
if (kState.IsKeyDown(Keys.Up)) camLat = Math.Min(camLat + 1, 89);
if (kState.IsKeyDown(Keys.Down)) camLat = Math.Max(camLat - 1, -89);
if (kState.IsKeyDown(Keys.Left)) camLong = (camLong + 1) % 360;
if (kState.IsKeyDown(Keys.Right)) camLong = (camLong + 359) % 360;
if (kState.IsKeyDown(Keys.PageUp)) camDist -= 0.1f;
if (kState.IsKeyDown(Keys.PageDown)) camDist += 0.1f;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
// カメラ位置
float rad = MathHelper.ToRadians(camLat);
float y = (float)Math.Sin(rad) * camDist;
float r = (float)Math.Cos(rad) * camDist;
rad = MathHelper.ToRadians(camLong);
float x = (float)Math.Cos(rad) * r;
float z = (float)Math.Sin(rad) * r;
effect.View = Matrix.CreateLookAt(new Vector3(x, y, z), Vector3.Zero, Vector3.Up);
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
effect.World = Matrix.CreateTranslation(-2, 0, 0);
pass.Apply();
GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList,
vertices8, 0, 8, indices8, 0, 12);
effect.World = Matrix.CreateTranslation(2, 0, 0);
pass.Apply();
GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList,
vertices24, 0, 24, indices24, 0, 12);
}
// fps
draw++;
if (gameTime.TotalGameTime.Seconds != sec)
{
fps = draw;
draw = 0;
sec = gameTime.TotalGameTime.Seconds;
}
sprite.Begin();
string text = String.Format(
"fps={0} lat={1} long={2} dist={3:f1}", fps, camLat, camLong, camDist);
sprite.DrawString(font, text, new Vector2(0, 0), Color.White);
sprite.End();
base.Draw(gameTime);
}
}
}