Original Post
Hello, I wrote a simple Managed DirectX code using C#. The program basically displays a triangle in 3D space. However, when I run this program, it shows nothing but a colored background. I counldn't figure out why it is like that. By the way, the SDK I use is the latest version (August 2005). Can anyone have a quick look at the following code and point out the problem for me? Thanks
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace DirectX1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class DirectXForm : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private Device device;
private System.ComponentModel.Container components = null;
public DirectXForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
}
/// <summary>
/// Initialize the DirectX Device
/// </summary>
public void InitializeDevice()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
}
protected override void OnPaint(PaintEventArgs e)
{
// Set the Camera
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0.0f, 0.0f, 5.0f), new Vector3(), new Vector3(0.0f, 1.0f, 0.0f));
device.RenderState.Lighting = false;
device.RenderState.CullMode = Cull.None;
// Clear the Scene
device.Clear(ClearFlags.Target, Color.CornflowerBlue, 1.0f, 0);
// Vertex Buffer
CustomVertex.PositionColored[] verts= new CustomVertex.PositionColored[3];
verts[0].Position = new Vector3(0f, 1f, 1f);
verts[0].Color = Color.Red.ToArgb();
verts[1].Position = new Vector3(-1f, -1f, 1f);
verts[1].Color = Color.Green.ToArgb();
verts[2].Position = new Vector3(1f, -1f, 1f);
verts[2].Color = Color.Yellow.ToArgb();
// Draw the scene
device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts);
device.EndScene();
device.Present();
//base.OnPaint (e);
this.Invalidate();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// DirectXForm
//
this.components = new Container();
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(500, 500);
this.Text = "DirectXForm";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
using(DirectXForm dxf = new DirectXForm())
{
dxf.Show();
dxf.InitializeDevice();
Application.Run(dxf);
}
}
}
}