Friday, June 13, 2008

DirectX draw triangle

This is an example of how to draw a simple triangle using DirectX:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DirectXHelloWorld
{
public partial class DirectXHelloWorld : Form
{
//Define device to write to.
Device device = null;
// Define a vertexBuffer to store the 3d objects.
VertexBuffer vertexBuffer = null;

//Main method.
static void Main()
{
// Instantiate form, initialize graphics and show the form.
DirectXHelloWorld form = new DirectXHelloWorld();
form.InitializeGraphics();
form.Show();

// Render loop
while (form.Created)
{
form.render();
Application.DoEvents();
}
}
// Called to draw screen.
public void render()
{
//Clear the backbuffer to a blue color (ARGB = 000000ff)
device.Clear(ClearFlags.Target, System.Drawing.Color.Black, 1.0f, 0);

device.BeginScene();

device.SetStreamSource(0, vertexBuffer, 0);
device.VertexFormat = CustomVertex.TransformedColored.Format;
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

//End the scene
device.EndScene();
device.Present();
}
// Initalize the graphics system.
public void InitializeGraphics()
{
try
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;

// On my chepo laptop I don't have hardward support so I'm
// using SoftwareVertexProcessing. If you have a real graphics
// card you should change this to HardwareVertexProcessing.
device = new Device(0,
DeviceType.Hardware,
this,
CreateFlags.SoftwareVertexProcessing,
presentParams);
device.DeviceReset+=
new System.EventHandler(this.OnResetDevice);
OnResetDevice(device, null);

}
catch (DirectXException e)
{

MessageBox.Show(null, "Error intializing graphics: " + e.Message, "Error");
Close();
}
}
public void OnResetDevice(object sender, EventArgs e)
{
Device dev = (Device)sender;
vertexBuffer = new VertexBuffer(typeof(CustomVertex.TransformedColored),3, dev,
0,
CustomVertex.TransformedColored.Format,
Pool.Default);

GraphicsStream stm = vertexBuffer.Lock(0, 0, 0);
CustomVertex.TransformedColored[] verts = new CustomVertex.TransformedColored[3];

verts[0].X = 150;
verts[0].Y = 50;
verts[0].Z = 0.5f;
verts[0].Rhw = 1;
verts[0].Color = System.Drawing.Color.Red.ToArgb();
verts[1].X = 250;
verts[1].Y = 250;
verts[1].Z = 0.5f;
verts[1].Rhw = 1;
verts[1].Color = System.Drawing.Color.Green.ToArgb();
verts[2].X = 50;
verts[2].Y = 250;
verts[2].Z = 0.5f;
verts[2].Rhw = 1;
verts[2].Color = System.Drawing.Color.Blue.ToArgb();
stm.Write(verts);
vertexBuffer.Unlock();
}

}

}

For more information on DirectX with c# I recommend the following book: <a href="http://www.amazon.com/gp/product/1568812361?ie=UTF8&tag=develoresour-20&linkCode=as2&camp=1789&creative=9325&creativeASIN=1568812361">C# and Game Programming: A Beginner's Guide, Second Edition (Book & CD-ROM)</a><img src="http://www.assoc-amazon.com/e/ir?t=develoresour-20&l=as2&o=1&a=1568812361" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />

No comments: