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 DX17Interaction
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private Device GraphicDevice;
//My objects
private myTeapot leftTeapot;
private myTeapot centreTeapot;
private myTeapot rightTeapot;
private float angle = 0;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
//
// Form1
//
this.ClientSize = new System.Drawing.Size(640, 480);
this.Name = "Form1";
this.Text = "Form1";
}
#endregion
[STAThread]
static void Main()
{
Form1 frm = new Form1();
frm.InitializeGraphics();
frm.SetupScene();
frm.Show();
while (frm.Created)
{
frm.Render();
Application.DoEvents();
}
frm.DisposeGraphics();
}
public void InitializeGraphics()
{
PresentParameters pp = new PresentParameters();
pp.Windowed = true;
pp.BackBufferFormat = Format.X8R8G8B8;
pp.SwapEffect = SwapEffect.Discard;
pp.AutoDepthStencilFormat = DepthFormat.D16;
pp.EnableAutoDepthStencil = true;
GraphicDevice = new Device(0,DeviceType.Hardware,this,CreateFlags.SoftwareVertexProcessing,pp);
GraphicDevice.RenderState.Lighting = true;
GraphicDevice.RenderState.Ambient = Color.Gray;
}
public void DisposeGraphics()
{
GraphicDevice.Dispose();
}
public void SetupScene()
{
GraphicDevice.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height,1.0f,500.0f);
GraphicDevice.Transform.View = Matrix.LookAtLH(new Vector3(0,0,-20),new Vector3(0,0,0),new Vector3(0,1,0));
GraphicDevice.Lights[0].Type = LightType.Point ;
GraphicDevice.Lights[0].Position = new Vector3(-10,0,-5);
GraphicDevice.Lights[0].Attenuation1 = 0.1f;
GraphicDevice.Lights[0].Range = 20.0f;
GraphicDevice.Lights[0].Enabled = true;
leftTeapot = new myTeapot(ref GraphicDevice);
centreTeapot = new myTeapot(ref GraphicDevice);
rightTeapot = new myTeapot(ref GraphicDevice);
}
public void Render()
{
GraphicDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer,Color.CornflowerBlue ,1,0);
GraphicDevice.BeginScene();
//Get Cursor Position
Point cursor = Point.Empty;
cursor = this.PointToClient(Cursor.Position);
if(cursor.X < 0) cursor.X = this.Width;
if(cursor.Y < 0) cursor.Y = this.Height;
//Left Teapot
GraphicDevice.Transform.World = Matrix.Identity;
GraphicDevice.Transform.World = Matrix.Multiply(Matrix.RotationY(Geometry.DegreeToRadian(angle)),Matrix.RotationX(Geometry.DegreeToRadian(angle)));
GraphicDevice.Transform.World = Matrix.Multiply(GraphicDevice.Transform.World,Matrix.RotationZ(Geometry.DegreeToRadian(-angle)));
GraphicDevice.Transform.World = Matrix.Multiply(GraphicDevice.Transform.World,Matrix.Translation(-5,5,0));
leftTeapot.Intersect(cursor.X, cursor.Y,
GraphicDevice.Viewport,
GraphicDevice.Transform.Projection,
GraphicDevice.Transform.View,
GraphicDevice.Transform.World);
leftTeapot.Render(ref GraphicDevice);
//Centre Teapot
GraphicDevice.Transform.World = Matrix.Identity;
GraphicDevice.Transform.World = Matrix.Multiply(GraphicDevice.Transform.World,Matrix.Translation(0,-5,0));
centreTeapot.Intersect(cursor.X, cursor.Y,
GraphicDevice.Viewport,
GraphicDevice.Transform.Projection,
GraphicDevice.Transform.View,
GraphicDevice.Transform.World);
centreTeapot.Render(ref GraphicDevice);
//Right Teapot
GraphicDevice.Transform.World = Matrix.Identity;
GraphicDevice.Transform.World = Matrix.Multiply(Matrix.RotationY(Geometry.DegreeToRadian(angle)),Matrix.RotationX(Geometry.DegreeToRadian(angle)));
GraphicDevice.Transform.World = Matrix.Multiply(GraphicDevice.Transform.World,Matrix.RotationZ(Geometry.DegreeToRadian(-angle)));
GraphicDevice.Transform.World = Matrix.Multiply(GraphicDevice.Transform.World,Matrix.Translation(5,0,0));
rightTeapot.Intersect(cursor.X, cursor.Y,
GraphicDevice.Viewport,
GraphicDevice.Transform.Projection,
GraphicDevice.Transform.View,
GraphicDevice.Transform.World);
rightTeapot.Render(ref GraphicDevice);
angle += 1f;
if (angle == 360.0f)
{
angle = 0;
}
GraphicDevice.EndScene();
GraphicDevice.Present();
}
}
class myTeapot
{
Mesh mesh;
Material material;
public myTeapot(ref Device dev)
{
mesh = Mesh.Teapot(dev);
material = new Material();
material.Diffuse = Color.SeaGreen;
material.Ambient = Color.SeaGreen;
material.Specular = Color.SeaGreen;
}
public void Intersect(int X, int Y, Viewport viewport, Matrix projection, Matrix view, Matrix world)
{
Vector3 Nearcor;
Vector3 Farcor;
Nearcor = new Vector3(X, Y, 0);
Nearcor = Vector3.Unproject(Nearcor, viewport, projection, view, world);
Farcor = new Vector3(X, Y, 1);
Farcor = Vector3.Unproject(Farcor, viewport, projection, view, world);
Farcor.Normalize();
if (mesh.Intersect(Nearcor,Farcor))
{
material.Diffuse = Color.Gold;
material.Ambient = Color.Gold;
material.Specular = Color.Gold;
}
else
{
material.Diffuse = Color.SeaGreen;
material.Ambient = Color.SeaGreen;
material.Specular = Color.SeaGreen;
}
}
public void Render(ref Device dev)
{
dev.Material = material;
mesh.DrawSubset(0);
}
}
}