using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace XnaCube
{
class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch sprite;
SpriteFont font;
BasicEffect effect;
VertexBuffer vertexBuffer;
IndexBuffer indexBuffer;
Texture2D texture;
// fps
int sec;
int draw = 0;
int fps = 0;
// カメラ
int camLat = 0;
int camLong = 90;
float camDist = 5;
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");
texture = Content.Load<Texture2D>("Palette16");
sprite = new SpriteBatch(GraphicsDevice);
effect = new BasicEffect(GraphicsDevice);
//effect.VertexColorEnabled = true;
//effect.EnableDefaultLighting();
effect.TextureEnabled = true;
effect.Texture = texture;
effect.Projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45), GraphicsDevice.Viewport.AspectRatio, 1, 100);
// 頂点バッファ
int face = 6;
float f = 4;
VertexPositionTexture[] vertices = new VertexPositionTexture[4 * face];
for (int n = 0; n < 4; n++)
{
int u = n & 1;
int v = (n >> 1) & 1;
int ru = 1 - u * 2;
int rv = 1 - v * 2;
// +X面
vertices[0 + n] = new VertexPositionTexture(
new Vector3(1, rv, ru), new Vector2(u / f, v / f));
// -X面
vertices[4 + n] = new VertexPositionTexture(
new Vector3(-1, -ru, -rv), new Vector2((1 + u) / f, v / f));
// +Y面
vertices[8 + n] = new VertexPositionTexture(
new Vector3(ru, 1, rv), new Vector2((2 + u) / f, v / f));
// -Y面
vertices[12 + n] = new VertexPositionTexture(
new Vector3(-rv, -1, -ru), new Vector2((3 + u) / f, v / f));
// +Z面
vertices[16 + n] = new VertexPositionTexture(
new Vector3(rv, ru, 1), new Vector2(u / f, (1 + v) / f));
// -Z面
vertices[20 + n] = new VertexPositionTexture(
new Vector3(-ru, -rv, -1), new Vector2((1 + u) / f, (1 + v) / f));
}
vertexBuffer = new VertexBuffer(GraphicsDevice,
typeof(VertexPositionTexture), 4 * face, BufferUsage.WriteOnly);
vertexBuffer.SetData(vertices);
// 索引バッファ
short[] indices = new short[6 * face];
int i = 0;
for (int m = 0; m < face; m++)
{
for (int n = 0; n < 6; n++)
{
indices[i++] = (short)(4 * m + (n < 3 ? n : 6 - n));
}
}
indexBuffer = new IndexBuffer(GraphicsDevice,
typeof(short), 6 * face, BufferUsage.WriteOnly);
indexBuffer.SetData(indices);
base.LoadContent();
}
protected override void Update(GameTime gameTime)
{
KeyboardState kState = Keyboard.GetState();
if (kState.IsKeyDown(Keys.Escape)) Exit();
if (kState.IsKeyDown(Keys.Up)) camLat++;
if (kState.IsKeyDown(Keys.Down)) camLat--;
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);
// カメラ位置
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);
GraphicsDevice.SetVertexBuffer(vertexBuffer);
GraphicsDevice.Indices = indexBuffer;
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,
0, 0, vertexBuffer.VertexCount, 0, vertexBuffer.VertexCount / 2);
}
// 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);
}
}
}