I am having with this code. Any idea?
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 RailwaySimulator
{
public partial class Form1_railwaySimulator : Form
{
private Device m_device = null;
Button btnStart = new Button();
private VertexBuffer m_vb = null;
private IndexBuffer m_ib = null;
float MIN_BOX_Z = -10.0f;
float MAX_BOX_Z = 10.0f;
float angle, speedIncrement;
private static readonly short[] indices =
{
0,1,2,1,3,2,
7,4,6,7,5,4,
5,7,3,2,5,3,
0,4,1,1,4,6,
7,6,1,7,1,3,
0,4,2,2,4,5
};
void InitGraphics()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
m_device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
m_vb = new VertexBuffer(typeof(CustomVertex.PositionColored), 8,m_device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[8];
m_vb.Created += new EventHandler(this.CreateVertexBuffer);
CreateVertexBuffer(m_vb, null);
m_ib = new IndexBuffer(typeof(short), indices.Length, m_device, Usage.WriteOnly, Pool.Default);
m_ib.Created += new EventHandler(this.OnIndexBufferCreate);
OnIndexBufferCreate(m_ib, null);
}
private void OnIndexBufferCreate(object sender, EventArgs e)
{
IndexBuffer buffer = (IndexBuffer)sender;
buffer.SetData(indices, 0, LockFlags.None);
}
protected override void OnPaint(PaintEventArgs e)
{
m_device.Clear(ClearFlags.Target,
Color.PowderBlue, 1.0f, 0);
SetupCamera();
m_device.BeginScene();
m_device.SetStreamSource(0, m_vb, 0);
m_device.VertexFormat = CustomVertex.PositionColored.Format;
drawTrain(4, 3, 8,
0.0f, 0.0f, 0.0f,
2.0f, 1.0f, 2.0f);
m_device.EndScene();
m_device.Present();
this.Invalidate();
}
protected void CreateVertexBuffer(object sender, EventArgs e)
{
VertexBuffer vb = (VertexBuffer)sender;
CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[8];
verts[0] = new CustomVertex.PositionColored(
new Vector3(-1.0f, 1.0f, 1.0f),
Color.Red.ToArgb());
verts[1] = new CustomVertex.PositionColored(
new Vector3(-1.0f, -1.0f, 1.0f),
Color.Yellow.ToArgb());
verts[2] = new CustomVertex.PositionColored(
new Vector3(1.0f, 1.0f, 1.0f),
Color.Green.ToArgb());
verts[3] = new CustomVertex.PositionColored(
new Vector3(1.0f, -1.0f, 1.0f),
Color.Orange.ToArgb());
verts[4] = new CustomVertex.PositionColored(
new Vector3(-1.0f, 1.0f, -1.0f),
Color.Black.ToArgb());
verts[5] = new CustomVertex.PositionColored(
new Vector3(1.0f, 1.0f, -1.0f),
Color.Blue.ToArgb());
verts[6] = new CustomVertex.PositionColored(
new Vector3(-1.0f, -1.0f, -1.0f),
Color.Indigo.ToArgb());
verts[7] = new CustomVertex.PositionColored(
new Vector3(1.0f, -1.0f, -1.0f),
Color.Aqua.ToArgb());
m_vb.SetData(verts, 0, LockFlags.None);
}
private void SetupCamera()
{
m_device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);
m_device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 5.0f),
new Vector3(),
new Vector3(0, 1, 0));
m_device.RenderState.PointSize = 3.0f;
m_device.RenderState.Lighting = false;
m_device.RenderState.Ambient = Color.AntiqueWhite;
}
public Form1_railwaySimulator()
{
btnStart.Location = new Point(10, 10);
btnStart.Text = "Start";
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
}
private float F(float x, float z)
{
return (float)((Math.Sin(x) + Math.Sin(z)) * 1.5);
}
private void drawTrain(float scaleX, float scaleY, float scaleZ,
float yaw, float pitch, float roll,
float x, float y, float z)
{
angle += 0.1f;
speedIncrement += 0.01f;
Vector3 direction = new Vector3(0.0f, 0.0f, 0.0f);
Vector3 vectorA, vectorB, newPosition;
vectorA = new Vector3(x, 0, z);
if (z < MIN_BOX_Z || (direction.Z <= 0 && z <= MAX_BOX_Z))
{
z -= speedIncrement;
}
else
{
z += speedIncrement;
}
vectorB = new Vector3(0, 0, z);
direction = vectorA - vectorB;
Vector3.Normalize(direction);
newPosition = vectorA + (direction * speedIncrement);
m_device.Transform.World = Matrix.Translation(F(x, z), y, z * angle * 0.1f);
m_device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8,
0,indices.Length/3);
}
static void Main()
{
using (Form1_railwaySimulator frm = new Form1_railwaySimulator())
{
frm.Show();
frm.InitGraphics();
Application.Run(frm);
}
}
}
}
When I debug, I got the error which is "Error in the application."
What's that mean actually? Any help I appreciate very much.