男は黙ってナマポリゴン
■なにはともあれ、テクスチャを使用しないポリゴンを表示したい。(シェーダーを使わないで)
XNAでは、簡単に表示する関数は用意されていないようですので、最短で表示する方法を考えてみました。
namespace WindowsGame { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; ContentManager content; VertexPositionColor[] vertices = { new VertexPositionColor( new Vector3( 1.0f, 0.0f, 0.0f ), new Color(255, 255, 50, 100) ), new VertexPositionColor( new Vector3( -1.0f, -1.0f, 0.0f ), new Color(255, 255, 50, 255) ), new VertexPositionColor( new Vector3( -0.5f, 1.0f, 0.0f ), new Color(255, 255, 255, 100) ), }; VertexDeclaration vdecl; BasicEffect basicEffect; public Game1() { graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); } protected override void Initialize() { vdecl = new VertexDeclaration( graphics.GraphicsDevice,VertexPositionColor.VertexElements); basicEffect = new BasicEffect( graphics.GraphicsDevice, null); basicEffect.VertexColorEnabled = true; base.Initialize(); } protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); basicEffect.Begin(); basicEffect.CurrentTechnique.Passes[0].Begin(); graphics.GraphicsDevice.VertexDeclaration = vdecl; graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>( PrimitiveType.TriangleList, vertices, 0, 1); basicEffect.CurrentTechnique.Passes[0].End(); basicEffect.End(); base.Draw(gameTime); } } }
■ソース説明
表示したい三角形の3頂点を定義します。「VertexPositionColor」の頂点タイプは「頂点座標」「頂点の色」を指定してください。
VertexPositionColor[] vertices = { new VertexPositionColor( new Vector3( 1.0f, 0.0f, 0.0f ), new Color(255, 255, 50, 100) ), new VertexPositionColor( new Vector3( -1.0f, -1.0f, 0.0f ), new Color(255, 255, 50, 255) ), new VertexPositionColor( new Vector3( -0.5f, 1.0f, 0.0f ), new Color(255, 255, 255, 100) ), };
頂点の型を設定
VertexDeclaration vdecl;
vdecl = new VertexDeclaration( graphics.GraphicsDevice, VertexPositionColor.VertexElements);
BasicEffect basicEffect; basicEffect = new BasicEffect( graphics.GraphicsDevice, null); basicEffect.VertexColorEnabled = true;
初期がが終わったら描画をしよう
basicEffect.Begin(); // エフェクト開始 basicEffect.CurrentTechnique.Passes[0].Begin(); // graphics.GraphicsDevice.VertexDeclaration = vdecl; // 頂点の型指定 graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>( // 頂点情報などを流し込む PrimitiveType.TriangleList, vertices, 0, 1); basicEffect.CurrentTechnique.Passes[0].End(); // basicEffect.End(); // エフェクト終了