using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Device device = null;
private float angle = 5;
public void InitializeGraphics()
{
//Set the presentation parameters
PresentParameters presentParameters = new PresentParameters();
presentParameters.Windowed = true;//true=windowed, false=full screened
presentParameters.SwapEffect = SwapEffect.Discard;//Flip and Copy are other possibilities
//Create the device
device = new Device(0,//the adapter
DeviceType.Hardware, //"Reference" is another possibility
this, //which control to bind this device to
CreateFlags.SoftwareVertexProcessing, //processing done by the CPU
presentParameters); //how data is present to the screen
device.DeviceResizing += new CancelEventHandler(this.CancelResize);
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
}
//protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
//{
////clears the screen with a blue color
//device.Clear(ClearFlags.Target, System.Drawing.Color.CornflowerBlue, 1.0f, 0);
///*
// create an array that will store the vertices of a triangle
// Transformed means the position are actual screen coordinates
// Colored means that this point has a color associated with it
//*/
//CustomVertex.TransformedColored[] verts = new CustomVertex.TransformedColored[3];
//verts[0].Position = new Vector4(this.Width / 2.0f, 50.0f, 0.5f, 1.0f);
//verts[0].Color = System.Drawing.Color.Aqua.ToArgb();
//verts[1].Position = new Vector4(this.Width - (this.Width / 5.0f), this.Height - (this.Height / 5.0f), 0.5f, 1.0f);
//verts[1].Color = System.Drawing.Color.Black.ToArgb();
//verts[2].Position = new Vector4(this.Width / 5.0f, this.Height - (this.Height / 5.0f), 0.5f, 1.0f);
//verts[2].Color = System.Drawing.Color.Purple.ToArgb();
////start the rendering
//device.BeginScene();
//device.VertexFormat = CustomVertex.TransformedColored.Format;
//device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts);
//device.EndScene();
////rendering is complete so display it
//device.Present();
////this.Invalidate();
//}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
//clears the screen with a blue color
device.Clear(ClearFlags.Target, System.Drawing.Color.CornflowerBlue, 1.0f, 0);
SetupCamera();
/*
create an array that will store the vertices of a triangle
Position means the coordinate are world space and has to be transformed
Colored means that this point has a color associated with it
*/
CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3];
verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
verts[0].Color = System.Drawing.Color.Aqua.ToArgb();
verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
verts[1].Color = System.Drawing.Color.Black.ToArgb();
verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
verts[2].Color = System.Drawing.Color.Purple.ToArgb();
//start the rendering
device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts);
device.EndScene();
//rendering is complete so display it
device.Present();
this.Invalidate();
}
private void SetupCamera()
{
//disables culling
device.RenderState.CullMode = Cull.None;
////Rotation about the z axis
//device.Transform.World = Matrix.RotationZ(angle / (float)Math.PI);
//angle += 0.1f;
//Rotation about an arbitrary axis
device.Transform.World = Matrix.RotationAxis(
new Vector3(
angle / ((float)Math.PI * 2.0f),
angle / ((float)Math.PI * 4.0f),
angle / ((float)Math.PI * 6.0f)),
angle / (float)Math.PI);
angle += 0.1f;
device.Transform.Projection = Matrix.PerspectiveFovLH(
(float)Math.PI / 4,//angle of the Field Of View
this.Width / this.Height, //similar to the aspect of a tv
1.0f, //the near plane
100.0f); //the far plane
device.Transform.View = Matrix.LookAtLH(
new Vector3(0, 0, 5.0f),//camera position
new Vector3(0,1,1), //what direction to look
new Vector3(0, 1, 0)); //what direction is up
device.RenderState.Lighting = false;//there are no lights in this scene, let each object emit its own light
}
private void CancelResize(object sender, CancelEventArgs e)
{
e.Cancel = true;
}
public Form1()
{
InitializeComponent();
}
}
}