| 1: |
using System; |
| 2: |
using System.Drawing; |
| 3: |
using System.Windows.Forms; |
| 4: |
using Microsoft.DirectX; |
| 5: |
using Microsoft.DirectX.Direct3D; |
| 6: |
|
| 7: |
public class VertexBufferForm : Form |
| 8: |
{ |
| 9: |
|
| 10: |
Device device = null; |
| 11: |
VertexBuffer vertexBuffer = null; |
| 12: |
|
| 13: |
public VertexBufferForm() |
| 14: |
{ |
| 15: |
|
| 16: |
this.ClientSize = new System.Drawing.Size(300,300); |
| 17: |
|
| 18: |
this.Text = "DirectX Tutorial - VertexBuffer"; |
| 19: |
} |
| 20: |
|
| 21: |
public bool InitializeGraphics() |
| 22: |
{ |
| 23: |
try |
| 24: |
{ |
| 25: |
|
| 26: |
PresentParameters presentParams = new PresentParameters(); |
| 27: |
presentParams.Windowed=true; |
| 28: |
presentParams.SwapEffect = SwapEffect.Discard; |
| 29: |
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); |
| 30: |
vertexBuffer = new VertexBuffer(typeof(CustomVertex.TransformedColored), 6, device, 0, CustomVertex.TransformedColored.Format, Pool.Default); |
| 31: |
vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer); |
| 32: |
this.OnCreateVertexBuffer(vertexBuffer, null); |
| 33: |
return true; |
| 34: |
} |
| 35: |
catch (DirectXException) |
| 36: |
{ |
| 37: |
return false; |
| 38: |
} |
| 39: |
} |
| 40: |
|
| 41: |
public void OnCreateVertexBuffer(object sender, EventArgs e) |
| 42: |
{ |
| 43: |
VertexBuffer vertexBuffer = (VertexBuffer)sender; |
| 44: |
GraphicsStream stm = vertexBuffer.Lock(0, 0, 0); |
| 45: |
CustomVertex.TransformedColored[] verts = new CustomVertex.TransformedColored[6]; |
| 46: |
|
| 47: |
verts[0].X = 50; |
| 48: |
verts[0].Y = 50; |
| 49: |
verts[0].Z = 0.5f; |
| 50: |
verts[0].Rhw = 1; |
| 51: |
verts[0].Color = System.Drawing.Color.Blue.ToArgb(); |
| 52: |
verts[1].X = 250; |
| 53: |
verts[1].Y = 50; |
| 54: |
verts[1].Z = 0.5f; |
| 55: |
verts[1].Rhw = 1; |
| 56: |
verts[1].Color = System.Drawing.Color.Red.ToArgb(); |
| 57: |
verts[2].X = 50; |
| 58: |
verts[2].Y = 250; |
| 59: |
verts[2].Z = 0.5f; |
| 60: |
verts[2].Rhw = 1; |
| 61: |
verts[2].Color = System.Drawing.Color.Lime.ToArgb(); |
| 62: |
verts[3].X = 250; |
| 63: |
verts[3].Y = 50; |
| 64: |
verts[3].Z = 0.5f; |
| 65: |
verts[3].Rhw = 1; |
| 66: |
verts[3].Color = System.Drawing.Color.Red.ToArgb(); |
| 67: |
verts[4].X = 250; |
| 68: |
verts[4].Y = 250; |
| 69: |
verts[4].Z = 0.5f; |
| 70: |
verts[4].Rhw = 1; |
| 71: |
verts[4].Color = System.Drawing.Color.Yellow.ToArgb(); |
| 72: |
verts[5].X = 50; |
| 73: |
verts[5].Y = 250; |
| 74: |
verts[5].Z = 0.5f; |
| 75: |
verts[5].Rhw = 1; |
| 76: |
verts[5].Color = System.Drawing.Color.Lime.ToArgb(); |
| 77: |
stm.Write(verts); |
| 78: |
vertexBuffer.Unlock(); |
| 79: |
} |
| 80: |
|
| 81: |
private void Render() |
| 82: |
{ |
| 83: |
if (device == null) |
| 84: |
return; |
| 85: |
|
| 86: |
|
| 87: |
device.Clear(ClearFlags.Target, System.Drawing.Color.Black, 1.0f, 0); |
| 88: |
|
| 89: |
device.BeginScene(); |
| 90: |
|
| 91: |
device.SetStreamSource( 0, vertexBuffer, 0); |
| 92: |
device.VertexFormat = CustomVertex.TransformedColored.Format; |
| 93: |
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 2); |
| 94: |
|
| 95: |
|
| 96: |
device.EndScene(); |
| 97: |
device.Present(); |
| 98: |
} |
| 99: |
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) |
| 100: |
{ |
| 101: |
this.Render(); |
| 102: |
} |
| 103: |
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e) |
| 104: |
{ |
| 105: |
if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape) |
| 106: |
this.Close(); |
| 107: |
} |
| 108: |
protected void DisposeGraphics() |
| 109: |
{ |
| 110: |
} |
| 111: |
|
| 112: |
|
| 113: |
static void Main() |
| 114: |
{ |
| 115: |
|
| 116: |
using (VertexBufferForm frm = new VertexBufferForm()) |
| 117: |
{ |
| 118: |
if (!frm.InitializeGraphics()) |
| 119: |
{ |
| 120: |
MessageBox.Show("Konnte Hardware nicht initialisieren."); |
| 121: |
return; |
| 122: |
} |
| 123: |
frm.Show(); |
| 124: |
|
| 125: |
|
| 126: |
while(frm.Created) |
| 127: |
{ |
| 128: |
frm.Render(); |
| 129: |
Application.DoEvents(); |
| 130: |
} |
| 131: |
frm.DisposeGraphics(); |
| 132: |
} |
| 133: |
} |
| 134: |
} |