XNA Vertex Position Color 3 D n Vertex

  • Slides: 51
Download presentation

XNA內定的頂點格式 Vertex. Position. Color 3 D 座標和顏色 n Vertex. Position. Texture 3 D 座標和一組紋理

XNA內定的頂點格式 Vertex. Position. Color 3 D 座標和顏色 n Vertex. Position. Texture 3 D 座標和一組紋理 圖UV座標 n Vertex. Position. Color. Texture 3 D 座標、顏色 和一組紋理圖UV座標 n Vertex. Position. Normal. Texture 3 D 座標、法 向量和一組紋理圖UV座標 n 3

Vertex. Position. Color public struct Vertex. Position. Color { public Vector 3 Position; //

Vertex. Position. Color public struct Vertex. Position. Color { public Vector 3 Position; // 3 D 座標 public Color; // 顏色 public static readonly Vertex. Element[] Vertex. Elements; // 頂點格式宣告 public Vertex. Position. Color(Vector 3 position, Color color); //建構元 public static int Size. In. Bytes { get; }); // 每個頂點大小. . . } 4

Vertex. Position. Texture public struct Vertex. Position. Texture { public Vector 3 Position; //

Vertex. Position. Texture public struct Vertex. Position. Texture { public Vector 3 Position; // 3 D 座標 public Vector 2 Texture. Coordinate; // 紋理圖UV座標 public static readonly Vertex. Element[] Vertex. Elements; // 頂點格式宣告 public Vertex. Position. Texture (Vector 3 position, Vector 2 texture. Coordinate; ); //建構元 public static int Size. In. Bytes { get; } // 每個頂點大小. . . } 5

Vertex. Position. Color. Texture public struct Vertex. Position. Color. Texture { public Vector 3

Vertex. Position. Color. Texture public struct Vertex. Position. Color. Texture { public Vector 3 Position; // 3 D 座標 public Color; // 顏色 public Vector 2 Texture. Coordinate; // 紋理圖UV座標 public static readonly Vertex. Element[] Vertex. Elements; //頂點格式宣 告 public Vertex. Position. Color. Texture(Vector 3 position, Color color, Vector 2 texture. Coordinate; ); //建構元 public static int Size. In. Bytes { get; } // 每個頂點大小. . . } 6

Vertex. Position. Normal. Texture public struct Vertex. Position. Normal. Texture { public Vector 3

Vertex. Position. Normal. Texture public struct Vertex. Position. Normal. Texture { public Vector 3 Position; // 3 D 座標 public Vector 3 Normal; // 法向量 public Vector 2 Texture. Coordinate; // 紋理圖UV座標 public static readonly Vertex. Element[] Vertex. Elements; //頂點格式宣 告 public Vertex. Position. Normal. Texture(Vector 3 position, Vector 3 normal, Vector 2 texture. Coordinate; ); //建構元 public static int Size. In. Bytes { get; } // 每個頂點大小. . . } 7

頂點陣列 (宣告六個頂點) Vertex. Position. Color[] vertices = new Vertex. Position. Color[6]; vertices[0] = new

頂點陣列 (宣告六個頂點) Vertex. Position. Color[] vertices = new Vertex. Position. Color[6]; vertices[0] = new Vertex. Position. Color(new Vector 3(-1, 0. 0 f), Color. White); vertices[1] = new Vertex. Position. Color(new Vector 3(1, 0. 0 f), Color. White); …. . 8

將頂點陣列的內容複製到頂點緩衝 private Vertex. Buffer vertex. Buffer; // 頂點緩衝區 vertex. Buffer = new Vertex. Buffer(this.

將頂點陣列的內容複製到頂點緩衝 private Vertex. Buffer vertex. Buffer; // 頂點緩衝區 vertex. Buffer = new Vertex. Buffer(this. Graphics. Device, 6 * Vertex. Position. Color. Size. In. Bytes, // 總共要多大的空間 Buffer. Usage. Write. Only); // 使用方式--寫入 // 把頂點陣列複製到頂點緩衝區內 vertex. Buffer. Set. Data<Vertex. Position. Color>(vertices); 9

繪出前的頂點設定 // 頂點格式宣告 graphics. Graphics. Device. Vertex. Declaration = new Vertex. Declaration(graphics. Graphics. Device,

繪出前的頂點設定 // 頂點格式宣告 graphics. Graphics. Device. Vertex. Declaration = new Vertex. Declaration(graphics. Graphics. Device, Vertex. Position. Color. Vertex. Elements); // 設定 頂點資料流 編號 0 就是第一條 的 來源 graphics. Graphics. Device. Vertices[0]. Set. Source( vertex. Buffer, // 使用 vertex. Buffer 頂點緩衝區 0, // 從頭開始 Vertex. Position. Color. Size. In. Bytes); // 每一個頂點 的大小 11

基本形狀 (Primitives) public enum Primitive. Type { Point. List = 1, // 點 Line.

基本形狀 (Primitives) public enum Primitive. Type { Point. List = 1, // 點 Line. List = 2, // 兩點成一線 Line. Strip = 3, // 點串成一線 Triangle. List = 4, // 三點成一個面 Triangle. Strip = 5, // 三點成一個面 Triangle. Fan = 6, // 三點成一個面 } 12

Point. List基本形狀 13

Point. List基本形狀 13

Line. List基本形狀 14

Line. List基本形狀 14

Line. Strip基本形狀 15

Line. Strip基本形狀 15

Triangle. List 基本形狀 16

Triangle. List 基本形狀 16

Triangle. Strip 基本形狀 17

Triangle. Strip 基本形狀 17

Triangle. Fan 基本形狀 18

Triangle. Fan 基本形狀 18

Draw. Primitives()方法 19

Draw. Primitives()方法 19

Basic. Effect (基本特效) Basic. Effect effect; // 基本特效 effect = new Basic. Effect(graphics. Graphics.

Basic. Effect (基本特效) Basic. Effect effect; // 基本特效 effect = new Basic. Effect(graphics. Graphics. Device, null); effect. World = Matrix. Identity; // 世界矩陣 effect. View = Matrix. Create. Look. At(new Vector 3(2. 0 f, 2. 0 f), Vector 3. Zero, Vector 3. Up); // 視覺矩陣 effect. Projection = Matrix. Create. Perspective. Field. Of. View( Math. Helper. To. Radians(45. 0 f), 1. 333 f, 1. 0 f, 10. 0 f); // 投影矩陣 21

範例一:顯示出六個頂點 定義兩個旋轉角度的global variable. public class Game 1 : Microsoft. Xna. Framework. Game { Graphics.

範例一:顯示出六個頂點 定義兩個旋轉角度的global variable. public class Game 1 : Microsoft. Xna. Framework. Game { Graphics. Device. Manager graphics; Sprite. Batch sprite. Batch; 1. private Vertex. Buffer vertex. Buffer; // 頂點緩衝區 private Basic. Effect effect; // 基本 特效 float model. Rotation_Y = 0; // 旋轉角度 全域變數 float model. Rotation_X = 0; // 旋轉角度 全域變數 public Game 1() { ……… } 23

範例一:顯示出六個頂點 2. 在initialize()定義六個頂點 protected override void Initialize() { // TODO: Add your initialization logic

範例一:顯示出六個頂點 2. 在initialize()定義六個頂點 protected override void Initialize() { // TODO: Add your initialization logic here effect = new Basic. Effect(graphics. Graphics. Device, null); Vertex. Position. Color[] vertices = new Vertex. Position. Color[6]; // X axis at [0, 1], Y axis at[2, 3], Z axis at [4, 5] vertices[0] = new Vertex. Position. Color(new Vector 3(-1, 0. 0 f), Color. Red); vertices[1] = new Vertex. Position. Color(new Vector 3(1, 0. 0 f), Color. Red); vertices[2] = new Vertex. Position. Color(new Vector 3(0. 0 f, -1, 0. 0 f), Color. Green); vertices[3] = new Vertex. Position. Color(new Vector 3(0. 0 f, 1, 0. 0 f), Color. Green); vertices[4] = new Vertex. Position. Color(new Vector 3(0. 0 f, -1), Color. Blue); vertices[5] = new Vertex. Position. Color(new Vector 3(0. 0 f, 1), Color. Blue); vertex. Buffer = new Vertex. Buffer(this. Graphics. Device, 6*Vertex. Position. Color. Size. In. Bytes, Buffer. Usage. Write. Only); vertex. Buffer. Set. Data<Vertex. Position. Color>(vertices); 24

範例一:顯示出六個頂點 3. 在update()改變旋轉角度 protected override void Update(Game. Time game. Time) { // Allows the

範例一:顯示出六個頂點 3. 在update()改變旋轉角度 protected override void Update(Game. Time game. Time) { // Allows the game to exit if (Game. Pad. Get. State(Player. Index. One). Buttons. Back == Button. State. Pressed) this. Exit(); // TODO: Add your update logic here model. Rotation_Y += 0. 01 f; model. Rotation_X += 0. 01 f; base. Update(game. Time); } 25

範例一:顯示出六個頂點 4. 在draw()定義世界矩陣與投影矩陣,並顯示出結果 protected override void Draw(Game. Time game. Time) { graphics. Graphics. Device.

範例一:顯示出六個頂點 4. 在draw()定義世界矩陣與投影矩陣,並顯示出結果 protected override void Draw(Game. Time game. Time) { graphics. Graphics. Device. Clear(Color. Cornflower. Blue); // Set the World Matrix effect. World = Matrix. Create. Rotation. Y(model. Rotation_Y) * Matrix. Create. Rotation. X(model. Rotation_X); // 世界矩陣 effect. View = Matrix. Create. Look. At(new Vector 3(2. 0 f, 2. 0 f), Vector 3. Zero, Vector 3. Up); // 視覺矩陣 effect. Projection = Matrix. Create. Perspective. Field. Of. View( Math. Helper. To. Radians(45. 0 f), 1. 333 f, 1. 0 f, 10. 0 f); // 投影矩陣 effect. Lighting. Enabled = false; // 沒設光源 所以不作燈光運算 effect. Vertex. Color. Enabled = true; ……… } 26

範例一:顯示出六個頂點 4. 在draw()定義世界矩陣與投影矩陣,並顯示出結果 protected override void Draw(Game. Time game. Time) { ……. . //

範例一:顯示出六個頂點 4. 在draw()定義世界矩陣與投影矩陣,並顯示出結果 protected override void Draw(Game. Time game. Time) { ……. . // 頂點格式宣告 graphics. Graphics. Device. Vertex. Declaration = new Vertex. Declaration(graphics. Graphics. Device, Vertex. Position. Color. Vertex. Elements); // 設定 頂點資料流 編號 0 就是第一條 的 來源 graphics. Graphics. Device. Vertices[0]. Set. Source( vertex. Buffer, //使用 vertex. Buffer 頂點緩衝區 0, // 從頭開始 Vertex. Position. Color. Size. In. Bytes); // 每一個頂點 的大小 graphics. Graphics. Device. Render. State. Point. Size = 10; 27

範例一:顯示出六個頂點 4. 在draw()定義世界矩陣與投影矩陣,並顯示出結果 protected override void Draw(Game. Time game. Time) { ……. . //

範例一:顯示出六個頂點 4. 在draw()定義世界矩陣與投影矩陣,並顯示出結果 protected override void Draw(Game. Time game. Time) { ……. . // Draw the 3 D axis effect. Begin(); foreach (Effect. Pass Current. Pass in effect. Current. Technique. Passes) { Current. Pass. Begin(); graphics. Graphics. Device. Draw. Primitives(Primitive. Type. Point. List, 0, 6); Current. Pass. End(); } effect. End(); base. Draw(game. Time); } } 28

範例一:顯示出六個頂點 4. 在draw()定義世界矩陣與投影矩陣,並顯示出結果 protected override void Draw(Game. Time game. Time) { ……. . //

範例一:顯示出六個頂點 4. 在draw()定義世界矩陣與投影矩陣,並顯示出結果 protected override void Draw(Game. Time game. Time) { ……. . // Draw the 3 D axis effect. Begin(); foreach (Effect. Pass Current. Pass in effect. Current. Technique. Passes) { Current. Pass. Begin(); graphics. Graphics. Device. Draw. Primitives(Primitive. Type. Point. List, 0, 6); Current. Pass. End(); } effect. End(); base. Draw(game. Time); } } 30

範例二:顯示出三個軸線 4. 在draw()定義世界矩陣與投影矩陣,並顯示出結果 protected override void Draw(Game. Time game. Time) { ……. . //

範例二:顯示出三個軸線 4. 在draw()定義世界矩陣與投影矩陣,並顯示出結果 protected override void Draw(Game. Time game. Time) { ……. . // Draw the 3 D axis effect. Begin(); foreach (Effect. Pass Current. Pass in effect. Current. Technique. Passes) { Current. Pass. Begin(); graphics. Graphics. Device. Draw. Primitives(Primitive. Type. Point. List, 0, 6); Current. Pass. End(); } effect. End(); base. Draw(game. Time); } } 31

範例三:顯示出一個立方體 定義八個頂點 protected override void Initialize() { ……… Vertex. Position. Color[] vertices = new

範例三:顯示出一個立方體 定義八個頂點 protected override void Initialize() { ……… Vertex. Position. Color[] vertices = new Vertex. Position. Color[8]; // X, Y, Z axis vertices[0] = new Vertex. Position. Color(new Vector 3(-1. 0 f, -1. 0 f), Color. White); vertices[1] = new Vertex. Position. Color(new Vector 3(-1. 0 f, -1. 0 f), Color. White); vertices[2] = new Vertex. Position. Color(new Vector 3(1. 0 f, -1. 0 f), Color. White); vertices[3] = new Vertex. Position. Color(new Vector 3(1. 0 f, -1. 0 f), Color. White); vertices[4] = new Vertex. Position. Color(new Vector 3(-1. 0 f, 1. 0 f), Color. White); vertices[5] = new Vertex. Position. Color(new Vector 3(-1. 0 f, 1. 0 f), Color. White); vertices[6] = new Vertex. Position. Color(new Vector 3(1. 0 f, 1. 0 f), Color. White); vertices[7] = new Vertex. Position. Color(new Vector 3(1. 0 f, -1. 0 f, 1. 0 f), Color. White); vertex. Buffer = new Vertex. Buffer(this. Graphics. Device, 8*Vertex. Position. Color. Size. In. Bytes, Buffer. Usage. Write. Only); // 產生 頂點緩衝區 33 vertex. Buffer. Set. Data<Vertex. Position. Color>(vertices); // 複製 頂點資料

範例三:顯示出一個立方體 2. 在initialize定義六個平面,共十二個三角形 short[] vertex. Indices = new short[36]; // 36 個 頂點索引 vertex.

範例三:顯示出一個立方體 2. 在initialize定義六個平面,共十二個三角形 short[] vertex. Indices = new short[36]; // 36 個 頂點索引 vertex. Indices[0] = 3; vertex. Indices[1] = 2; vertex. Indices[2] = 1; // 後面 vertex. Indices[3] = 3; vertex. Indices[4] = 1; vertex. Indices[5] = 0; vertex. Indices[6] = 4; vertex. Indices[7] = 5; vertex. Indices[8] = 6; // 前面 vertex. Indices[9] = 4; vertex. Indices[10] = 6; vertex. Indices[11] = 7; vertex. Indices[12] = 0; vertex. Indices[13] = 1; vertex. Indices[14] = 5; // 左面 vertex. Indices[15] = 0; vertex. Indices[16] = 5; vertex. Indices[17] = 4; vertex. Indices[18] = 7; vertex. Indices[19] = 6; vertex. Indices[20] = 2; // 右面 vertex. Indices[21] = 7; vertex. Indices[22] = 2; vertex. Indices[23] = 3; vertex. Indices[24] = 5; vertex. Indices[25] = 1; vertex. Indices[26] = 2; // 上面 vertex. Indices[27] = 5; vertex. Indices[28] = 2; vertex. Indices[29] = 6; vertex. Indices[30] = 0; vertex. Indices[31] = 4; vertex. Indices[32] = 7; // 下面 vertex. Indices[33] = 0; vertex. Indices[34] = 7; vertex. Indices[35] = 3; 34

範例三:顯示出一個立方體 2. 在initialize定義六個平面,共十二個三角形 index. Buffer = new Index. Buffer(this. Graphics. Device, 36 * sizeof(short),

範例三:顯示出一個立方體 2. 在initialize定義六個平面,共十二個三角形 index. Buffer = new Index. Buffer(this. Graphics. Device, 36 * sizeof(short), Buffer. Usage. Write. Only, Index. Element. Size. Sixteen. Bits); // 產生 頂點索引緩衝區 index. Buffer. Set. Data<short>(vertex. Indices); // 複製 頂點索引資料個三角形, base. Initialize(); } 35

範例三:顯示出一個立方體 3. 繪出 protected override void Draw(Game. Time game. Time) { ………. // Set

範例三:顯示出一個立方體 3. 繪出 protected override void Draw(Game. Time game. Time) { ………. // Set the World Matrix model. Rotation += 0. 01 f; effect. World = Matrix. Create. Rotation. Y(model. Rotation); // 世界矩陣 effect. View = Matrix. Create. Look. At(new Vector 3(4. 0 f, 4. 0 f), Vector 3. Zero, Vector 3. Up); // 視覺矩陣 effect. Projection = Matrix. Create. Perspective. Field. Of. View( Math. Helper. To. Radians(45. 0 f), 1. 333 f, 1. 0 f, 10. 0 f); // 投影矩陣 effect. Lighting. Enabled = false; // 沒設光源 所以不作燈光運算 36

範例三:顯示出一個立方體 3. 繪出 protected override void Draw(Game. Time game. Time) { ………. // 頂點格式宣告

範例三:顯示出一個立方體 3. 繪出 protected override void Draw(Game. Time game. Time) { ………. // 頂點格式宣告 graphics. Graphics. Device. Vertex. Declaration = new vertex. Declaration(this. Graphics. Device, Vertex. Position. Color. Vertex. Elements); // 設定 頂點資料流 編號 0 就是第一條 的 來源 graphics. Graphics. Device. Vertices[0]. Set. Source(vertex. Buffer, //使用頂點緩衝區 0, // 從頭開始 Vertex. Position. Color. Size. In. Bytes); // 每一個頂點 的大小 graphics. Graphics. Device. Indices = index. Buffer; // 頂點索引緩衝區 graphics. Graphics. Device. Render. State. Fill. Mode = Fill. Mode. Wire. Frame; // 畫線 條 graphics. Graphics. Device. Render. State. Cull. Mode = Cull. Mode. Cull. Counter. Clockwise. Face; //內定 逆時間 裁掉 37

範例三:顯示出一個立方體 3. 繪出 protected override void Draw(Game. Time game. Time) { ………. // Draw

範例三:顯示出一個立方體 3. 繪出 protected override void Draw(Game. Time game. Time) { ………. // Draw the 3 D axis effect. Begin(); foreach (Effect. Pass Current. Pass in effect. Current. Technique. Passes) { Current. Pass. Begin(); effect. Diffuse. Color = new Vector 3(1. 0 f, 0. 0 f); effect. Commit. Changes(); 38

範例三:顯示出一個立方體 3. 繪出 protected override void Draw(Game. Time game. Time) { ………. graphics. Graphics.

範例三:顯示出一個立方體 3. 繪出 protected override void Draw(Game. Time game. Time) { ………. graphics. Graphics. Device. Draw. Indexed. Primitives( Primitive. Type. Triangle. List, // 三個點 為一個面 0, // 索引偏移値 Offset to add to each vertex index in the index buffer. 0, // 頂點緩衝區 的 頂點 偏移値 8, // 頂點 個數 0, // 開始 的 索引 Location in the index array at which to start reading vertices 12 // 畫 12 個 三角面 ); Current. Pass. End(); } effect. End(); base. Draw(game. Time); } } } 39

範例四:有文理貼圖的立方體 public class Game 1 : Microsoft. Xna. Framework. Game // 1. {…… private

範例四:有文理貼圖的立方體 public class Game 1 : Microsoft. Xna. Framework. Game // 1. {…… private Basic. Effect effect; // 基本 特效 Texture 2 D texture; ……. . } protected override void Load. Content() // 2 {……. . texture = Content. Load<Texture 2 D>("Sun"); ………} protected override void Draw(Game. Time game. Time) {……. . // Draw the 3 D axis effect. Begin(); foreach (Effect. Pass Current. Pass in effect. Current. Technique. Passes) { Current. Pass. Begin(); effect. Texture = texture; effect. Texture. Enabled = true; this. Graphics. Device. Draw. Primitives(Primitive. Type. Triangle. List, 0, 12); Current. Pass. End(); } effect. End(); ………. } 41

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game.

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game. Component { public int grid. Size = 12; // 每邊有 幾格 public float grid. Scale = 5. 0 f; // 每格 的 寬 public Color grid. Color = new Color(0 x. FF, 0 x. FF); //格線的顏色黑色 Vertex. Buffer vertex. Buffer; // 頂點緩衝區, private Vertex. Declaration vertex. Declaration; // 頂點格式 (每個頂點 包含什麼內容) Basic. Effect effect; // 產出時會用到的 效果 int vertex. Count; // 頂點 數目 Graphics. Device device; //繪圖設備 ……. } 43

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game.

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game. Component {……. . public Matrix world = Matrix. Identity; // 世界 觀測 投影 矩陣 public Matrix view = Matrix. Create. Look. At(new Vector 3(0. 0 f, 20. 0 f), Vector 3. Zero, Vector 3. Up); public Matrix projection = Matrix. Create. Perspective. Field. Of. View( Math. Helper. To. Radians(45. 0 f), 1. 333 f, 1. 0 f, 10000. 0 f); //在Game. Component_Grid的Constructor先存好繪圖設備備用 public Game. Component_Grid(Game game) : base(game) { // TODO: Construct any child components here this. device = game. Graphics. Device; ……… } 44

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game.

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game. Component {……. . // Allows the game component to perform any initialization it // needs to before starting to run. This is where it can query for any required // services and load content. public override void Initialize() { // TODO: Add your initialization code here effect = new Basic. Effect(device, null); // 效果 vertex. Count=(grid. Size + 1)*4; // 每邊的頂點數比每邊的格數多一,共有四個邊 // 每個頂點 包含 位置 和 顏色 ,先空出 vertex. Count 個頂點 Vertex. Position. Color[] vertices = new Vertex. Position. Color[vertex. Count]; float length = (float)grid. Size * grid. Scale; // 邊長 等於 格數 乘以 格寬 float half. Length = length * 0. 5 f; // 半邊長 因為是要以原點為中心左右半 45

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game.

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game. Component {…… int index = 0; // 頂點 索引 // 定義頂點位置 頂都是 躺在 X Z 平面上 for (int i = 0; i <= grid. Size; ++i) { vertices[index++] = new Vertex. Position. Color( new Vector 3(-half. Length, 0. 0 f, i*grid. Scale-half. Length), grid. Color); // 左邊的頂點 vertices[index++] = new Vertex. Position. Color( new Vector 3(half. Length, 0. 0 f, i*grid. Scale-half. Length), grid. Color); // 右邊的頂點 vertices[index++] = new Vertex. Position. Color( new Vector 3(i*grid. Scale-half. Length, 0. 0 f, -half. Length), grid. Color); // 上緣的頂點 vertices[index++] = new Vertex. Position. Color( new Vector 3(i*grid. Scale-half. Length, 0. 0 f, half. Length), grid. Color); // 下緣的頂點 } 46

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game.

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game. Component {……// 建立 頂點緩衝區 vertex. Buffer = new Vertex. Buffer(device, vertex. Count * Vertex. Position. Color. Size. In. Bytes, Buffer. Usage. Write. Only); // 將頂點資料複製入頂點緩衝區內 vertex. Buffer. Set. Data<Vertex. Position. Color>(vertices); // 頂點格式 vertex. Declaration = new Vertex. Declaration(device, Vertex. Position. Color. Vertex. Elements); base. Initialize(); } 47

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game.

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game. Component { …. . // Allows the game component to update itself. // name="game. Time">Provides a snapshot of timing values. public override void Update(Game. Time game. Time) { // TODO: Add your update code here base. Update(game. Time); } 48

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game.

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game. Component { …. . public override void Draw(Game. Time game. Time) { // 效果 三大矩陣 設定 effect. World = world; effect. View = view; effect. Projection = projection; effect. Vertex. Color. Enabled = true; // 使用 頂點顏色 效果 device. Vertex. Declaration = vertex. Declaration; // 頂點格式 device. Vertices[0]. Set. Source(vertex. Buffer, 0, Vertex. Position. Color. Size. In. Bytes); // 頂點來源 49

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game.

範例五:格線類別製作 (Game. Component_Grid. cs) public class Game. Component_Grid : Microsoft. Xna. Framework. Drawable. Game. Component { …. . public override void Draw(Game. Time game. Time) …………. . effect. Begin(); // 效果 開始 foreach (Effect. Pass Current. Pass in effect. Current. Technique. Passes) { Current. Pass. Begin(); device. Draw. Primitives(Primitive. Type. Line. List, 0, vertex. Count / 2); // 兩兩畫線 所以只有 vertex. Count/2 條線 Current. Pass. End(); } effect. End(); } } 50

範例五:格線類別製作 (Game 1. cs) protected override void Initialize() { // TODO: Add your initialization

範例五:格線類別製作 (Game 1. cs) protected override void Initialize() { // TODO: Add your initialization logic here Grid = new Game. Component_Grid(this); this. Components. Add(Grid); base. Initialize(); } 51