▼XNA Game Studioメモ▼
点の表示


点の表示を行うプログラムを作成する。



ソースコードの編集
Game.cs
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace PointEx {
    public class Game : Microsoft.Xna.Framework.Game {
        //システム
        private GraphicsDeviceManager graphics;   //グラフィックス
        private float                 aspectRatio;//アスペクト比

        //頂点
        private VertexDeclaration vertexDeclaration;//頂点定義
        private VertexBuffer      vertexBuffer;     //頂点バッファ
        private BasicEffect       basicEffect;      //基本エフェクト
                
        //コンストラクタ
        public Game() {
            graphics=new GraphicsDeviceManager(this);
            Content.RootDirectory="Content";
        }

        //初期化
        protected override void Initialize() {
            base.Initialize();
        }

        //グラフィクスコンテントの読み込み
        protected override void LoadContent() {
            //アスペクト比
            aspectRatio=
                (float)GraphicsDevice.Viewport.Width/
                (float)GraphicsDevice.Viewport.Height;
       
            // エフェクトの生成
            basicEffect=new BasicEffect(GraphicsDevice,null);
            basicEffect.VertexColorEnabled=true;
            //ビュー変換行列
            basicEffect.View = Matrix.CreateLookAt(
                new Vector3(2.0f,2.0f,2.0f),//カメラ位置
                Vector3.Zero,               //参照点
                Vector3.Up                  //UPベクトル
            );
            //射影変換行列
            basicEffect.Projection=Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(45.0f),//視野角
                aspectRatio,                //アスペクト比
                0.005f,                     //ニアクリップ面の距離
                1000.0f                     //ファークリップ面の距離
            );

            //頂点定義の生成
            vertexDeclaration=new VertexDeclaration(
                GraphicsDevice,VertexPositionColor.VertexElements);
            
            //頂点バッファの生成
            vertexBuffer=new VertexBuffer(GraphicsDevice,
                VertexPositionColor.SizeInBytes*7,BufferUsage.None);
            
            //頂点データの生成
            VertexPositionColor[] pointList=new VertexPositionColor[7];
            pointList[0]=new VertexPositionColor(new Vector3(0.0f,0.0f,0.0f),Color.White);
            pointList[1]=new VertexPositionColor(new Vector3(0.5f,0.0f,0.0f),Color.Red);
            pointList[2]=new VertexPositionColor(new Vector3(1.0f,0.0f,0.0f),Color.Red);
            pointList[3]=new VertexPositionColor(new Vector3(0.0f,0.5f,0.0f),Color.Green);
            pointList[4]=new VertexPositionColor(new Vector3(0.0f,1.0f,0.0f),Color.Green);
            pointList[5]=new VertexPositionColor(new Vector3(0.0f,0.0f,0.5f),Color.Blue);
            pointList[6]=new VertexPositionColor(new Vector3(0.0f,0.0f,1.0f),Color.Blue);
            vertexBuffer.SetData<VertexPositionColor>(pointList);        
        }

        //グラフィクスコンテントの解放
        protected override void UnloadContent() {
        }

        //更新
        protected override void Update(GameTime gameTime) {
            base.Update(gameTime);
        }

        //描画
        protected override void Draw(GameTime gameTime) {
            //背景色
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
                                    
            //頂点の設定
            GraphicsDevice.VertexDeclaration=vertexDeclaration;
            GraphicsDevice.Vertices[0].SetSource(
                vertexBuffer,0,VertexPositionColor.SizeInBytes);
            GraphicsDevice.RenderState.PointSize=10;

            //頂点の描画
            basicEffect.Begin();
            foreach (EffectPass pass in this.basicEffect.CurrentTechnique.Passes) {
                pass.Begin();
                GraphicsDevice.DrawPrimitives(PrimitiveType.PointList,0,7);
                pass.End();
            }
            basicEffect.End();

            //ベースの描画
            base.Draw(gameTime);
        } 
    }
}



−戻る−