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

[ソリューションのディレクトリを作成]にチェックを付けないと.slnファイルが指定した場所の直下に作られる。

参考

Game1.cs
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
 
namespace HardwareInstancing
{
    struct VertexPosition : IVertexType
    {
        public Vector3 Position;
 
        public VertexPosition(Vector3 position)
        {
            Position = position;
        }
 
        static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration(
            new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0));
 
        VertexDeclaration IVertexType.VertexDeclaration { get { return VertexDeclaration; } }
    };
 
    class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        Effect effect;
        VertexBuffer triangleVertexBuffer;
        IndexBuffer indexBuffer;
        VertexBuffer positionVertexBuffer;
        VertexBufferBinding[] bindings;
 
        VertexPositionColor[] vertices =
        {
            new VertexPositionColor(new Vector3(-0.1f, 0.1f, 0), Color.Blue),
            new VertexPositionColor(new Vector3(0.1f, 0.1f, 0), Color.White),
            new VertexPositionColor(new Vector3(0.1f, -0.1f, 0), Color.Red),
        };
        VertexPosition[] trianglePositions =
        {
            new VertexPosition(new Vector3()),
            new VertexPosition(new Vector3(0.1f, 0.1f, 0)),
        };
 
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }
 
        protected override void LoadContent()
        {
            // ContentにHardwareInstancing.fxを追加する
            effect = Content.Load<Effect>("HardwareInstancing");
 
            triangleVertexBuffer = new VertexBuffer(GraphicsDevice,
                typeof(VertexPositionColor), vertices.Length, BufferUsage.None);
            triangleVertexBuffer.SetData<VertexPositionColor>(vertices);
 
            indexBuffer = new IndexBuffer(GraphicsDevice,
                typeof(int), 3, BufferUsage.None);
            indexBuffer.SetData<int>(new int[] { 0, 1, 2 });
 
            positionVertexBuffer = new VertexBuffer(GraphicsDevice,
                typeof(VertexPosition), trianglePositions.Length, BufferUsage.None);
            positionVertexBuffer.SetData<VertexPosition>(trianglePositions);
 
            bindings = new VertexBufferBinding[2];
            bindings[0] = new VertexBufferBinding(triangleVertexBuffer);
            bindings[1] = new VertexBufferBinding(positionVertexBuffer, 0, 1);
 
            base.LoadContent();
        }
 
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
 
            GraphicsDevice.SetVertexBuffers(bindings);
            GraphicsDevice.Indices = indexBuffer;
 
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                GraphicsDevice.DrawInstancedPrimitives(
                    PrimitiveType.TriangleList,
                    0, // baseVertex
                    0,
                    vertices.Length,
                    0,
                    vertices.Length / 3,
                    2);
            }
 
            base.Draw(gameTime);
        }
    }
}
 

HardwareInstancing.fx
/*
Effect File
 
プロジェクトのプロパティ
[XNA Game Studio]タブ
Use HiDef to access the complete API
*/
 
struct VertexPositionColor
{
	float4 Position : POSITION0;
	float4 Color : COLOR;
};
 
VertexPositionColor VertexShaderFunction(VertexPositionColor input, float3 position : POSITION1)
{
	input.Position.xyz += position;
	return input;
}
 
float4 PixelShaderFunction(float4 color : COLOR) : COLOR0
{
	return color;
}
 
technique HardwareInstancing
{
	pass Pass1
	{
		VertexShader = compile vs_3_0 VertexShaderFunction();
		PixelShader = compile ps_3_0 PixelShaderFunction();
	}
}
 
最終更新:2012年11月30日 22:55