diff --git a/CNC_Machine.sln b/CNC_Machine.sln
new file mode 100644
index 0000000..d69383d
--- /dev/null
+++ b/CNC_Machine.sln
@@ -0,0 +1,62 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Express 2012 for Windows Desktop
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Robot", "Robot\Robot.csproj", "{C7BBABF0-0698-4C5F-A510-7E160C8771A5}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serial", "Serial\Serial.csproj", "{9477FD2C-FE8F-4653-AB25-BFA14338A932}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GUI", "GUI\GUI.csproj", "{7BA42A6E-9E73-43E4-9461-B98C8D50AED1}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Triangle", "ThirdParty\Triangle\Triangle.csproj", "{F7907A0A-B75F-400B-9E78-BFAD00DB4D6B}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Geometry", "Geometry\Geometry.csproj", "{29611CC3-E54F-45E4-9681-8DF653459CA2}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "clipper_library", "ThirdParty\clipper_library\clipper_library.csproj", "{9B062971-A88E-4A3D-B3C9-12B78D15FA66}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Router", "Router\Router.csproj", "{6A2ED2BC-1FEA-46E3-8403-3C484A7BCCA5}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Commands", "Commands\Commands.csproj", "{0BE94AA8-662B-45EC-B95A-545180F54618}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {C7BBABF0-0698-4C5F-A510-7E160C8771A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {C7BBABF0-0698-4C5F-A510-7E160C8771A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {C7BBABF0-0698-4C5F-A510-7E160C8771A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {C7BBABF0-0698-4C5F-A510-7E160C8771A5}.Release|Any CPU.Build.0 = Release|Any CPU
+ {9477FD2C-FE8F-4653-AB25-BFA14338A932}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {9477FD2C-FE8F-4653-AB25-BFA14338A932}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {9477FD2C-FE8F-4653-AB25-BFA14338A932}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {9477FD2C-FE8F-4653-AB25-BFA14338A932}.Release|Any CPU.Build.0 = Release|Any CPU
+ {7BA42A6E-9E73-43E4-9461-B98C8D50AED1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {7BA42A6E-9E73-43E4-9461-B98C8D50AED1}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {7BA42A6E-9E73-43E4-9461-B98C8D50AED1}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {7BA42A6E-9E73-43E4-9461-B98C8D50AED1}.Release|Any CPU.Build.0 = Release|Any CPU
+ {F7907A0A-B75F-400B-9E78-BFAD00DB4D6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {F7907A0A-B75F-400B-9E78-BFAD00DB4D6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {F7907A0A-B75F-400B-9E78-BFAD00DB4D6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {F7907A0A-B75F-400B-9E78-BFAD00DB4D6B}.Release|Any CPU.Build.0 = Release|Any CPU
+ {29611CC3-E54F-45E4-9681-8DF653459CA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {29611CC3-E54F-45E4-9681-8DF653459CA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {29611CC3-E54F-45E4-9681-8DF653459CA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {29611CC3-E54F-45E4-9681-8DF653459CA2}.Release|Any CPU.Build.0 = Release|Any CPU
+ {9B062971-A88E-4A3D-B3C9-12B78D15FA66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {9B062971-A88E-4A3D-B3C9-12B78D15FA66}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {9B062971-A88E-4A3D-B3C9-12B78D15FA66}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {9B062971-A88E-4A3D-B3C9-12B78D15FA66}.Release|Any CPU.Build.0 = Release|Any CPU
+ {6A2ED2BC-1FEA-46E3-8403-3C484A7BCCA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {6A2ED2BC-1FEA-46E3-8403-3C484A7BCCA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {6A2ED2BC-1FEA-46E3-8403-3C484A7BCCA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {6A2ED2BC-1FEA-46E3-8403-3C484A7BCCA5}.Release|Any CPU.Build.0 = Release|Any CPU
+ {0BE94AA8-662B-45EC-B95A-545180F54618}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {0BE94AA8-662B-45EC-B95A-545180F54618}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {0BE94AA8-662B-45EC-B95A-545180F54618}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {0BE94AA8-662B-45EC-B95A-545180F54618}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+EndGlobal
diff --git a/Commands/Commands.csproj b/Commands/Commands.csproj
new file mode 100644
index 0000000..430d5b0
--- /dev/null
+++ b/Commands/Commands.csproj
@@ -0,0 +1,60 @@
+
+
+
+
+ Debug
+ AnyCPU
+ {0BE94AA8-662B-45EC-B95A-545180F54618}
+ Library
+ Properties
+ Commands
+ Commands
+ v4.5
+ 512
+
+
+ true
+ full
+ false
+ bin\Debug\
+ DEBUG;TRACE
+ prompt
+ 4
+
+
+ pdbonly
+ true
+ bin\Release\
+ TRACE
+ prompt
+ 4
+
+
+
+ ..\packages\OpenTK.1.1.1456.5398\lib\NET40\OpenTK.dll
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Commands/ICommand.cs b/Commands/ICommand.cs
new file mode 100644
index 0000000..36d151b
--- /dev/null
+++ b/Commands/ICommand.cs
@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Commands
+{
+ public abstract class ICommand
+ {
+ }
+}
diff --git a/Commands/MoveTool.cs b/Commands/MoveTool.cs
new file mode 100644
index 0000000..42f0ce3
--- /dev/null
+++ b/Commands/MoveTool.cs
@@ -0,0 +1,31 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using OpenTK;
+
+namespace Commands
+{
+ public class MoveTool : ICommand
+ {
+ private Vector3 target; // Target location in inches
+ private float speed; // Moving speed in inches per minute
+
+ public MoveTool(Vector3 target, float speed)
+ {
+ this.target = target;
+ this.speed = speed;
+ }
+
+ public Vector3 Target
+ {
+ get { return target; }
+ }
+
+ public float Speed
+ {
+ get { return speed; }
+ }
+ }
+}
diff --git a/Commands/Properties/AssemblyInfo.cs b/Commands/Properties/AssemblyInfo.cs
new file mode 100644
index 0000000..99cf9d0
--- /dev/null
+++ b/Commands/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("Commands")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("Commands")]
+[assembly: AssemblyCopyright("Copyright © 2013")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("8aea738d-0d61-414f-a11c-e73b7f1169c9")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
diff --git a/Commands/packages.config b/Commands/packages.config
new file mode 100644
index 0000000..d0ea9c3
--- /dev/null
+++ b/Commands/packages.config
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/GUI/App.config b/GUI/App.config
new file mode 100644
index 0000000..8e15646
--- /dev/null
+++ b/GUI/App.config
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/GUI/COMPortForm.Designer.cs b/GUI/COMPortForm.Designer.cs
new file mode 100644
index 0000000..330c1a6
--- /dev/null
+++ b/GUI/COMPortForm.Designer.cs
@@ -0,0 +1,108 @@
+namespace GUI
+{
+ partial class COMPortForm
+ {
+ ///
+ /// Required designer variable.
+ ///
+ private System.ComponentModel.IContainer components = null;
+
+ ///
+ /// Clean up any resources being used.
+ ///
+ /// true if managed resources should be disposed; otherwise, false.
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing && (components != null))
+ {
+ components.Dispose();
+ }
+ base.Dispose(disposing);
+ }
+
+ #region Windows Form Designer generated code
+
+ ///
+ /// Required method for Designer support - do not modify
+ /// the contents of this method with the code editor.
+ ///
+ private void InitializeComponent()
+ {
+ this.label2 = new System.Windows.Forms.Label();
+ this.label1 = new System.Windows.Forms.Label();
+ this.baudBox = new System.Windows.Forms.TextBox();
+ this.connect = new System.Windows.Forms.Button();
+ this.listBox1 = new System.Windows.Forms.ListBox();
+ this.SuspendLayout();
+ //
+ // label2
+ //
+ this.label2.AutoSize = true;
+ this.label2.Location = new System.Drawing.Point(138, 9);
+ this.label2.Name = "label2";
+ this.label2.Size = new System.Drawing.Size(58, 13);
+ this.label2.TabIndex = 12;
+ this.label2.Text = "Baud Rate";
+ //
+ // label1
+ //
+ this.label1.AutoSize = true;
+ this.label1.Location = new System.Drawing.Point(12, 9);
+ this.label1.Name = "label1";
+ this.label1.Size = new System.Drawing.Size(26, 13);
+ this.label1.TabIndex = 11;
+ this.label1.Text = "Port";
+ //
+ // baudBox
+ //
+ this.baudBox.Location = new System.Drawing.Point(138, 25);
+ this.baudBox.Name = "baudBox";
+ this.baudBox.Size = new System.Drawing.Size(120, 20);
+ this.baudBox.TabIndex = 10;
+ this.baudBox.Text = "9600";
+ //
+ // connect
+ //
+ this.connect.Location = new System.Drawing.Point(138, 51);
+ this.connect.Name = "connect";
+ this.connect.Size = new System.Drawing.Size(120, 23);
+ this.connect.TabIndex = 8;
+ this.connect.Text = "Connect";
+ this.connect.UseVisualStyleBackColor = true;
+ this.connect.Click += new System.EventHandler(this.connect_Click);
+ //
+ // listBox1
+ //
+ this.listBox1.FormattingEnabled = true;
+ this.listBox1.Location = new System.Drawing.Point(12, 25);
+ this.listBox1.Name = "listBox1";
+ this.listBox1.Size = new System.Drawing.Size(120, 69);
+ this.listBox1.TabIndex = 14;
+ this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
+ //
+ // COMPortForm
+ //
+ this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
+ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+ this.ClientSize = new System.Drawing.Size(270, 102);
+ this.Controls.Add(this.listBox1);
+ this.Controls.Add(this.label2);
+ this.Controls.Add(this.label1);
+ this.Controls.Add(this.baudBox);
+ this.Controls.Add(this.connect);
+ this.Name = "COMPortForm";
+ this.Text = "COMPort";
+ this.ResumeLayout(false);
+ this.PerformLayout();
+
+ }
+
+ #endregion
+
+ private System.Windows.Forms.Label label2;
+ private System.Windows.Forms.Label label1;
+ private System.Windows.Forms.TextBox baudBox;
+ private System.Windows.Forms.Button connect;
+ private System.Windows.Forms.ListBox listBox1;
+ }
+}
\ No newline at end of file
diff --git a/GUI/COMPortForm.cs b/GUI/COMPortForm.cs
new file mode 100644
index 0000000..f163985
--- /dev/null
+++ b/GUI/COMPortForm.cs
@@ -0,0 +1,140 @@
+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 System.IO.Ports;
+using Serial;
+using Robot;
+using Router;
+
+namespace GUI
+{
+ public partial class COMPortForm : Form
+ {
+ SerialPortWrapper port;
+ Timer t;
+ String[] portNames = new String [0];
+
+
+ public COMPortForm(SerialPortWrapper p)
+ {
+ if (p == null)
+ {
+ throw new NullReferenceException("Serial Port Wrapper object cannot be null");
+ }
+
+ InitializeComponent();
+ port = p;
+
+ t_Tick(null, EventArgs.Empty);
+ baudBox.Text = port.BaudRate.ToString();
+ SelectPortName(port.PortName);
+ if (port.IsOpen)
+ {
+ connect.Text = "Disconnect";
+ }
+ else
+ {
+ if (listBox1.SelectedItem == null)
+ {
+ connect.Enabled = false;
+ }
+ }
+
+ t = new Timer();
+ t.Interval = 100;
+ t.Tick += new EventHandler(t_Tick);
+ t.Start();
+ }
+
+ void t_Tick(object sender, EventArgs e)
+ {
+ string [] s = port.PortNames;
+ bool matched = true;
+ if (s.Length != portNames.Length)
+ {
+ matched = false;
+ }
+
+ if (!matched)
+ {
+ object selectedObject = listBox1.SelectedItem;
+
+ portNames = s;
+ listBox1.Items.Clear();
+ listBox1.Items.AddRange(portNames);
+
+ if (selectedObject != null)
+ {
+ string str = selectedObject.ToString();
+ SelectPortName(str);
+ }
+ if (listBox1.SelectedItem == null && !port.IsOpen)
+ {
+ connect.Enabled = false;
+ }
+ }
+ }
+
+ private void SelectPortName(string name)
+ {
+ for (int i = 0; i < listBox1.Items.Count; i++)
+ {
+ if (listBox1.Items[i].ToString() == name)
+ {
+ listBox1.SelectedIndex = i;
+ }
+ }
+ }
+
+ private void connect_Click(object sender, EventArgs e)
+ {
+ if (port.IsOpen)
+ {
+ port.Close();
+ connect.Text = "Connect";
+ }
+ else
+ {
+ try
+ {
+ int baudRate = Convert.ToInt32(this.baudBox.Text);
+ string portName = listBox1.SelectedItem.ToString();
+ port.Open(portName, baudRate);
+ connect.Text = "Disconnect";
+ }
+ catch (NullReferenceException ex)
+ {
+ MessageBox.Show("Error Opening Com Port: No Port Name Selected!");
+ }
+ catch (FormatException ex)
+ {
+ MessageBox.Show("Error Opening Com Port: Invalid Baud Rate!");
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Error Opening Com Port: " + ex.Message);
+ }
+ }
+ }
+
+ private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
+ {
+ if (!port.IsOpen)
+ {
+ if (listBox1.SelectedItem != null)
+ {
+ this.connect.Enabled = true;
+ }
+ else
+ {
+ this.connect.Enabled = false;
+ }
+ }
+ }
+ }
+}
diff --git a/GUI/COMPortForm.resx b/GUI/COMPortForm.resx
new file mode 100644
index 0000000..d58980a
--- /dev/null
+++ b/GUI/COMPortForm.resx
@@ -0,0 +1,120 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ text/microsoft-resx
+
+
+ 2.0
+
+
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
\ No newline at end of file
diff --git a/GUI/Drawing3D.Designer.cs b/GUI/Drawing3D.Designer.cs
new file mode 100644
index 0000000..856e6af
--- /dev/null
+++ b/GUI/Drawing3D.Designer.cs
@@ -0,0 +1,45 @@
+namespace GUI
+{
+ partial class Drawing3D
+ {
+ ///
+ /// Required designer variable.
+ ///
+ private System.ComponentModel.IContainer components = null;
+
+ ///
+ /// Clean up any resources being used.
+ ///
+ /// true if managed resources should be disposed; otherwise, false.
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing && (components != null))
+ {
+ components.Dispose();
+ }
+ base.Dispose(disposing);
+ }
+
+ #region Component Designer generated code
+
+ ///
+ /// Required method for Designer support - do not modify
+ /// the contents of this method with the code editor.
+ ///
+ private void InitializeComponent()
+ {
+ this.SuspendLayout();
+ //
+ // UserControl1
+ //
+ this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
+ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+ this.Name = "UserControl1";
+ this.Load += new System.EventHandler(this.UserControl1_Load);
+ this.ResumeLayout(false);
+
+ }
+
+ #endregion
+ }
+}
diff --git a/GUI/Drawing3D.cs b/GUI/Drawing3D.cs
new file mode 100644
index 0000000..9d04691
--- /dev/null
+++ b/GUI/Drawing3D.cs
@@ -0,0 +1,348 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Drawing;
+using System.Data;
+using System.Linq;
+using System.Text;
+using System.Windows.Forms;
+using OpenTK;
+using OpenTK.Graphics.OpenGL;
+using Robot;
+using Serial;
+using Router;
+using Geometry;
+
+namespace GUI
+{
+ public partial class Drawing3D : GLControl
+ {
+ Color clearColor;
+ Viewport3d viewport;
+ Vector2 lastMouseLocation = new Vector2(0, 0);
+ IClickable3D clickedObject = null;
+ private Router.Router router;
+
+ Timer drawTimer;
+
+ public Drawing3D()
+ {
+ this.InitializeComponent();
+
+ this.viewport = new Viewport3d(this);
+ this.Resize += new EventHandler(HandleControlResize);
+ this.MouseMove += new MouseEventHandler(HandleMouseMove);
+ this.MouseEnter += new EventHandler(HandleMouseEnter);
+ this.MouseLeave += new EventHandler(HandleMouseLeave);
+ this.MouseUp += new MouseEventHandler(HandleMouseUp);
+ this.MouseDown += new MouseEventHandler(HandleMouseDown);
+ this.MouseWheel += new MouseEventHandler(HandleMouseWheel);
+
+ drawTimer = new Timer();
+ drawTimer.Tick += new EventHandler(drawTimer_Tick);
+ drawTimer.Interval = 15;
+ drawTimer.Start();
+ }
+
+ void drawTimer_Tick(object sender, EventArgs e)
+ {
+ this.Invalidate();
+ }
+
+ #region Mouse Control
+
+ void HandleMouseWheel(object sender, MouseEventArgs e)
+ {
+ Ray pointer = viewport.GetPointerRay(e.Location);
+ viewport.Zoom(pointer, -e.Delta / 120);
+ this.Invalidate();
+ }
+
+ private Point mouseDownLocation;
+ private bool mouseRDown = false;
+ void HandleMouseDown(object sender, MouseEventArgs e)
+ {
+ if (clickedObject != null)
+ {
+ // If something's already clicked, don't allow selecting other items.
+ // This will force a mouse up in the client area.
+ return;
+ }
+
+ mouseDownLocation = e.Location;
+
+ Ray pointer = viewport.GetPointerRay(e.Location);
+ clickedObject = GetClosestObject(pointer);
+
+ if (e.Button == System.Windows.Forms.MouseButtons.Right)
+ {
+ mouseRDown = true;
+ viewport.BeginRotate();
+ return;
+ }
+ else if (e.Button != System.Windows.Forms.MouseButtons.Left)
+ {
+ return;
+ }
+
+
+
+ clickedObject.MouseDown(pointer);
+
+ this.Invalidate();
+ }
+
+ public Ray GetPointerRay(Point mouseLocation)
+ {
+ return viewport.GetPointerRay(mouseLocation);
+ }
+
+ private IClickable3D GetClosestObject(Ray pointer)
+ {
+ IClickable3D closestClickable = null;
+ float closest = float.PositiveInfinity;
+ foreach (var o in objects)
+ {
+ if (o is IClickable3D)
+ {
+ var clickable = o as IClickable3D;
+ float distance = clickable.DistanceToObject(pointer);
+ if (distance < closest)
+ {
+ closestClickable = clickable;
+ closest = distance;
+ }
+ }
+ }
+
+ if (FilterSelectable(closestClickable))
+ {
+ closestClickable.MouseHover(); // TODO: find a better way to handle hover indication...
+ return closestClickable;
+ }
+ return viewport as IClickable3D;
+ }
+
+ private bool FilterSelectable(Object selecting)
+ {
+ if (selecting == null)
+ {
+ return false;
+ }
+ //if (selecting is TabsGUI)
+ //{
+ // return true;
+ //}
+ //return false;
+ return true;
+ }
+
+ void HandleMouseUp(object sender, MouseEventArgs e)
+ {
+ // Display a context menu?
+ if (mouseRDown)
+ {
+ if (mouseDownLocation == e.Location)
+ {
+ if (clickedObject != null && clickedObject is TriangleMeshGUI)
+ {
+ System.Windows.Forms.ContextMenu menu = new ContextMenu();
+ var item = new MenuItem("Delete", new EventHandler(objectDeleteClicked));
+ item.Tag = clickedObject;
+ menu.MenuItems.Add(item);
+ menu.Show(this, e.Location);
+ }
+ }
+ }
+
+ mouseRDown = false;
+ if (clickedObject != null)
+ {
+ Ray pointer = viewport.GetPointerRay(e.Location);
+ clickedObject.MouseUp(pointer);
+ clickedObject = null;
+ }
+ }
+
+ void objectDeleteClicked(object sender, EventArgs e)
+ {
+ var item = sender as MenuItem;
+ if (item != null)
+ {
+ IClickable3D toRemove = item.Tag as IClickable3D;
+ if (toRemove != null)
+ {
+ if (toRemove is TriangleMeshGUI)
+ {
+ foreach (var tab in (toRemove as TriangleMeshGUI).Tabs)
+ {
+ objects.RemoveAll(o => o == tab);
+ }
+ }
+ objects.RemoveAll(o => o == toRemove);
+ }
+ }
+ }
+
+ void HandleMouseLeave(object sender, EventArgs e)
+ {
+ }
+
+ void HandleMouseEnter(object sender, EventArgs e)
+ {
+ this.Focus();
+ }
+
+ IClickable3D hoveredObject = null;
+ void HandleMouseMove(object sender, MouseEventArgs e)
+ {
+ hoveredObject = null;
+ if (mouseRDown)
+ {
+ Point mouseDelta = new Point(e.Location.X - mouseDownLocation.X, e.Location.Y - mouseDownLocation.Y);
+ viewport.ViewportRotate(mouseDelta.X, mouseDelta.Y);
+ }
+ else
+ {
+ Ray pointer = viewport.GetPointerRay(e.Location);
+ if (clickedObject != null)
+ {
+ clickedObject.MouseMove(pointer);
+ }
+ else
+ {
+ IClickable3D hovered = GetClosestObject(pointer);
+ if (hovered != null)
+ {
+ hoveredObject = hovered;
+ }
+ }
+ }
+ this.Invalidate();
+ }
+
+ #endregion
+
+ #region Drawing
+
+ void HandleControlResize(object sender, EventArgs e)
+ {
+ // TODO: move viewport setup code here, only call when changing the viewport size.
+ }
+
+ public Color ClearColor
+ {
+ get { return clearColor; }
+ set
+ {
+ clearColor = value;
+
+ if (!this.DesignMode)
+ {
+ MakeCurrent();
+ GL.ClearColor(clearColor);
+ }
+ }
+ }
+
+ protected override void OnPaint(PaintEventArgs e)
+ {
+ base.OnPaint(e);
+
+ if (!this.DesignMode)
+ {
+ MakeCurrent();
+ GL.Viewport(ClientRectangle);
+ //float aspect = this.ClientSize.Width / (float)this.ClientSize.Height;
+
+ GL.MatrixMode(MatrixMode.Projection);
+ GL.LoadIdentity();
+
+ // 3D Setup
+ Matrix4 projection = viewport.ProjectionMatrix;
+ GL.LoadMatrix(ref projection);
+ GL.MatrixMode(MatrixMode.Modelview);
+
+ GL.Enable(EnableCap.Blend); // glEnable(GL_BLEND);
+ GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+
+ GL.ClearColor(Color.White);
+ GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
+ GL.LoadIdentity();
+
+ GL.Enable(EnableCap.Lighting);
+ GL.Enable(EnableCap.ColorMaterial);
+ GL.Light(LightName.Light0, LightParameter.Position, new Vector4(0, 0, 1, 0));
+ GL.Light(LightName.Light0, LightParameter.Diffuse, new Vector4 (.8f, .8f, .8f, 1));
+ GL.Light(LightName.Light0, LightParameter.Ambient, new Vector4(.3f, .3f, .3f, 1));
+ //GL.Light(LightName.Light0, LightParameter.Specular, new Vector4(1.0f, 1.0f, 1.0f, 1));
+ GL.Enable(EnableCap.Light0);
+ GL.ColorMaterial(MaterialFace.FrontAndBack, ColorMaterialParameter.AmbientAndDiffuse);
+ //GL.ColorMaterial(MaterialFace.Front, ColorMaterialParameter.AmbientAndDiffuse);
+ GL.Enable(EnableCap.ColorMaterial);
+
+ GL.LoadMatrix(ref viewport.viewMatrix);
+ GL.Normal3(new Vector3(0, 0, 1));
+ GL.Enable(EnableCap.Normalize);
+
+ GL.DepthMask(true);
+ GL.Enable(EnableCap.DepthTest);
+ GL.DepthFunc(DepthFunction.Lequal);
+ //GL.Enable(EnableCap.CullFace);
+ //GL.CullFace(CullFaceMode.Back);
+ GL.PolygonOffset(1.0f, 2.0f);
+ GL.Enable(EnableCap.PolygonOffsetFill);
+
+ viewport.Draw();
+
+ //GL.PushMatrix();
+ foreach (object o in objects)
+ {
+ if (o is IOpenGLDrawable)
+ {
+ var drawable = o as IOpenGLDrawable;
+ drawable.Draw();
+ }
+ }
+ //GL.PopMatrix();
+
+ SwapBuffers();
+ }
+ }
+
+ private void UserControl1_Load(object sender, EventArgs e)
+ {
+ this.HandleControlResize(sender, e);
+ }
+
+ #endregion
+
+ List