開発環境 |
Microsoft Visual C# 2010 Express (SP1) |
実行環境 |
Microsoft Windows XP Home Edition (SP3) |
プロジェクトの種類 |
Windows Game (4.0) |
プロジェクト名 |
CustomVertex |
参考
VertexPositionNormalColor.cs
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace CustomVertex
{
struct VertexPositionNormalColor : IVertexType
{
Vector3 Position;
Vector3 Normal;
Color Color;
public VertexPositionNormalColor(Vector3 position, Vector3 normal, Color color)
{
Position = position;
Normal = normal;
Color = color;
}
static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration(
new VertexElement(0,
VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
new VertexElement(sizeof(float) * 3,
VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
new VertexElement(sizeof(float) * 6,
VertexElementFormat.Color, VertexElementUsage.Color, 0));
VertexDeclaration IVertexType.VertexDeclaration { get { return VertexDeclaration; } }
}
}
Game1.cs
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace CustomVertex
{
class Game1 : Game
{
GraphicsDeviceManager graphics;
BasicEffect effect;
MouseState mStateOld = new MouseState();
float yaw = 0;
float pitch = 0;
float roll = 0;
VertexPositionNormalColor[] vertices =
{
new VertexPositionNormalColor(new Vector3(0, 1, 0), Vector3.Backward, Color.Red),
new VertexPositionNormalColor(new Vector3(1, 0, 0), Vector3.Backward, Color.Red),
new VertexPositionNormalColor(new Vector3(-1, 0, 0), Vector3.Backward, Color.Red),
new VertexPositionNormalColor(new Vector3(0, 1, 0), Vector3.Forward, Color.Blue),
new VertexPositionNormalColor(new Vector3(-1, 0, 0), Vector3.Forward, Color.Blue),
new VertexPositionNormalColor(new Vector3(1, 0, 0), Vector3.Forward, Color.Blue),
};
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
IsMouseVisible = true;
}
protected override void LoadContent()
{
effect = new BasicEffect(GraphicsDevice)
{
VertexColorEnabled = true,
View = Matrix.CreateLookAt(new Vector3(0, 0, 3), Vector3.Zero, Vector3.Up),
Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45),
GraphicsDevice.Viewport.AspectRatio, 1, 100)
};
effect.EnableDefaultLighting();
base.LoadContent();
}
protected override void Update(GameTime gameTime)
{
KeyboardState kState = Keyboard.GetState();
if (kState[Keys.Escape] == KeyState.Down) Exit();
MouseState mState = Mouse.GetState();
if (mState.LeftButton == ButtonState.Pressed)
{
yaw += MathHelper.ToRadians(mStateOld.X - mState.X);
roll += MathHelper.ToRadians(mStateOld.Y - mState.Y);
}
pitch = MathHelper.ToRadians(mState.ScrollWheelValue / -10);
effect.World = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll);
mStateOld = mState;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
foreach (var pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawUserPrimitives<VertexPositionNormalColor>(
PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3);
}
base.Draw(gameTime);
}
}
}
最終更新:2012年12月09日 10:43