Sander

Sander

  • NA
  • 1
  • 0

[C#] Problem using DirectX

Jun 16 2006 5:27 PM
Hey, im learning DirectX http://www.riemers.net/Tutorials/DirectX/Csharp/tut3.php, but im a little stuck. Im using Visual Studio 2005.

At http://www.riemers.net/Tutorials/DirectX/Csharp/tut3.php u have to draw a triangle, but i only get a blue background, and no colored triangle nor text...

My code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using D3D = Microsoft.DirectX.Direct3D;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private Device device;
private D3D.Font text;


public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
}


static void Main()
{
using (Form1 Form1 = new Form1())
{
Form1.InitializeDevice();
Form1.InitializeFont();
Application.Run(Form1);
}
}


public void InitializeDevice()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;


device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
}


private void InitializeFont()
{
System.Drawing.Font systemfont = new System.Drawing.Font("Arial", 12f, FontStyle.Regular);
text = new D3D.Font(device, systemfont);
}


protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{


device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 50f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, -30), new Vector3(0, 0, 0), new Vector3(0, 1, 0));


device.RenderState.Lighting = false;


CustomVertex.PositionColored[] vertices = new CustomVertex.PositionColored[3];
vertices[2].Position = new Vector3(0f, 0f, 0f);
vertices[2].Color = Color.Red.ToArgb();
vertices[0].Position = new Vector3(5f, 10f, 0f);
vertices[0].Color = Color.Yellow.ToArgb();
vertices[1].Position = new Vector3(10f, 0f, 0f);
vertices[1].Color = Color.Green.ToArgb();


device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0);
device.Present();


device.BeginScene();
text.DrawText(null, string.Format("Wee"), new Point(10, 20), Color.White);
device.VertexFormat = CustomVertex.PositionColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);
device.EndScene();


this.Invalidate();


}


private void Form1_Load(object sender, EventArgs e)
{


}
}
}


Im sorry for the code, the forum doesnt seem to format my copy and paste correctly.. Ive tried to put as much < br> to make it look as good as possible