■XNAで三角ポリゴン | Prev Top Next |
いよいよ、ポリゴンの描画です。といっても三角ポリゴン一個ですが。
namespace Tutrial { ////// This is the main type for your game /// public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; ContentManager content; //**************************************************************** //オブジェクトの定義 //**************************************************************** private VertexDeclaration vertexDeclaration = null; //頂点データを定義 private BasicEffect basicEffect = null; //単純なエフェクト。固定機能パイプラインの感覚で使用できると思う。 private VertexPositionColor[] vertices = null; //頂点データ。頂点座標と頂点カラーを持つ。 public Game1() { graphics = new GraphicsDeviceManager(this); content = new ContentManager(Services); } ////// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// protected override void Initialize() { //**************************************************************** //オブジェクトの初期化 //**************************************************************** //頂点データを頂点座標と頂点カラーで定義する this.vertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionColor.VertexElements); //エフェクトを作成 this.basicEffect = new BasicEffect(graphics.GraphicsDevice, null); //エフェクトで頂点カラーを有効にする this.basicEffect.VertexColorEnabled = true; //ライティングを無効にする this.basicEffect.LightingEnabled = false; base.Initialize(); } ////// Load your graphics content. If loadAllContent is true, you should /// load content from both ResourceManagementMode pools. Otherwise, just /// load ResourceManagementMode.Manual content. /// /// Which type of content to load. protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { // TODO: Load any ResourceManagementMode.Automatic content } // TODO: Load any ResourceManagementMode.Manual content } ////// Unload your graphics content. If unloadAllContent is true, you should /// unload content from both ResourceManagementMode pools. Otherwise, just /// unload ResourceManagementMode.Manual content. Manual content will get /// Disposed by the GraphicsDevice during a Reset. /// /// Which type of content to unload. protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent == true) { content.Unload(); } } ////// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input and playing audio. /// /// Provides a snapshot of timing values. protected override void Update(GameTime gameTime) { // Allows the default game to exit on Xbox 360 and Windows if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: Add your update logic here base.Update(gameTime); } ////// This is called when the game should draw itself. /// /// Provides a snapshot of timing values. protected override void Draw(GameTime gameTime) { //**************************************************************** //レンダリング //**************************************************************** //背景色を黒で塗りつぶす graphics.GraphicsDevice.Clear(Color.Black); //射影座標変換 this.basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45.0f), //視野角 (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height, //スクリーンサイズのアスペクト比 1.0f, //前方クリップ位置 100.0f //後方クリップ位置 ); //ビュー座標変換(注意1) this.basicEffect.View = Matrix.CreateLookAt( new Vector3(0.0f, 0.0f, 10.0f), //カメラ位置 Vector3.Zero, //カメラの注視点 Vector3.Up //カメラの上方向 ); //ワールド座標系 this.basicEffect.World = Matrix.Identity; //単位行列を設定 //頂点データ(三角ポリゴン)を作成(注意2) this.vertices = new VertexPositionColor[3]; this.vertices[0] = new VertexPositionColor(new Vector3(0.0f, 3.0f, -2.0f), Color.Red); this.vertices[1] = new VertexPositionColor(new Vector3(3.0f, -2.0f, -2.0f), Color.Green); this.vertices[2] = new VertexPositionColor(new Vector3(-3.0f, -2.0f, -2.0f), Color.Blue); //描画する頂点データの定義を設定 graphics.GraphicsDevice.VertexDeclaration = this.vertexDeclaration; //エフェクトの使用を開始 this.basicEffect.Begin(); //パスの数だけ繰り返し描画 foreach (EffectPass pass in this.basicEffect.CurrentTechnique.Passes) { // パスの開始 pass.Begin(); // ポリゴンを描画 graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>( PrimitiveType.TriangleList, vertices, 0, 1 ); // パスの終了 pass.End(); } // エフェクトの使用を終了 this.basicEffect.End(); base.Draw(gameTime); } } }
無事、三角ポリゴンが表示されました。今回のPG作成でいきなりはまったのですがXNAでは右手座標系を採用しているらしいです。 Direct3Dでは左手座標系なのに。OpenGLにあわせるためか理由は不明なのですが、わざわざ変更しなくてもと思うのは自分だけでしょうか。
(注意1) 右手座標系なのでZ値は手前方向が+になります。サンプルPGではカメラを奥方向に向けています。
(注意2) 右手座標系なのでポリゴンを時計回りに指定します。