From 9cb8b1ab090be9ced56d7431066b23c687d7f863 Mon Sep 17 00:00:00 2001 From: David Allemang Date: Sun, 1 Apr 2018 03:56:47 -0400 Subject: [PATCH] ITS WORKING! simple 3d slicey movement --- Tetrahedrons/MVec3d.cs | 3 + Tetrahedrons/TetrahedronWindow.cs | 106 ++++++++++++++++++++++-------- 2 files changed, 80 insertions(+), 29 deletions(-) diff --git a/Tetrahedrons/MVec3d.cs b/Tetrahedrons/MVec3d.cs index 7e60e19..97bf346 100644 --- a/Tetrahedrons/MVec3d.cs +++ b/Tetrahedrons/MVec3d.cs @@ -6,6 +6,9 @@ namespace Tetrahedrons // ReSharper disable once InconsistentNaming public struct MVec3d { + public static readonly MVec3d Zero = new MVec3d(0, 0, 0, 0, 0, 0, 0, 0); + public static readonly MVec3d One = new MVec3d(1, 1, 1, 1, 1, 1, 1, 1); + public static readonly MVec3d Unit = new MVec3d(1, 0, 0, 0, 0, 0, 0, 0); public static readonly MVec3d Unit1 = new MVec3d(0, 1, 0, 0, 0, 0, 0, 0); public static readonly MVec3d Unit2 = new MVec3d(0, 0, 1, 0, 0, 0, 0, 0); diff --git a/Tetrahedrons/TetrahedronWindow.cs b/Tetrahedrons/TetrahedronWindow.cs index 8f02e7d..1ea9e16 100644 --- a/Tetrahedrons/TetrahedronWindow.cs +++ b/Tetrahedrons/TetrahedronWindow.cs @@ -4,6 +4,7 @@ using System.Drawing; using System.Net.Mail; using System.Security; using OpenTK; +using OpenTK.Audio.OpenAL; using OpenTK.Graphics.OpenGL; using OpenTK.Input; @@ -164,7 +165,8 @@ namespace Tetrahedrons public class TetrahedronWindow : GameWindow { - private Matrix4 _proj; + private Matrix4 _proj3d; + private Matrix4 _proj2d; private Matrix4 _view; private double _t; @@ -173,6 +175,10 @@ namespace Tetrahedrons private Tetrahedron _tetra; private Polygon _polyg; + private MVec3d _b1 = MVec3d.Unit1; + private MVec3d _b2 = MVec3d.Unit3; + private MVec3d _pivot = MVec3d.Zero; + private Polygon _square = new Polygon(new[] {new Vector3d(-1, -1, 0), new Vector3d(-1, 1, 0), new Vector3d(1, -1, 0), new Vector3d(1, 1, 0),}, Color.Red); protected override void OnLoad(EventArgs e) @@ -197,31 +203,41 @@ namespace Tetrahedrons GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.PointSize(10f); -// _view = Matrix4.LookAt(Vector3.Zero, new Vector3((float) Math.Cos(_t / 5), (float) Math.Sin(_t / 5), -1), Vector3.UnitZ); - GL.PushMatrix(); - GL.MatrixMode(MatrixMode.Projection); - GL.LoadMatrix(ref _proj); - GL.MatrixMode(MatrixMode.Modelview); - GL.LoadMatrix(ref _view); - GL.Enable(EnableCap.DepthTest); - _tetra.Draw(wire: false); - GL.Disable(EnableCap.DepthTest); if (_div) { - GL.PushMatrix(); - GL.MatrixMode(MatrixMode.Modelview); - GL.LoadMatrix(ref Matrix4d.Identity); - _square.Draw(); + GL.MatrixMode(MatrixMode.Projection); + GL.LoadMatrix(ref _proj2d); + _polyg.Draw(); - GL.PopMatrix(); } else + { + GL.MatrixMode(MatrixMode.Projection); + GL.LoadMatrix(ref _proj3d); + GL.MatrixMode(MatrixMode.Modelview); + GL.LoadMatrix(ref _view); + + + GL.Enable(EnableCap.DepthTest); + _tetra.Draw(wire: false); + GL.Disable(EnableCap.DepthTest); _polyg.Draw(); - GL.Enable(EnableCap.DepthTest); - _tetra.Draw(wire: true); + GL.Begin(PrimitiveType.Points); + GL.Color3(Color.DarkBlue); + GL.Vertex3(_pivot.VectorPart); + GL.End(); + + GL.Begin(PrimitiveType.Lines); + GL.Vertex3(_pivot.VectorPart); + GL.Vertex3((_pivot + (_b2 ^ _b1) * MVec3d.Unit123 * .5).VectorPart); + GL.End(); + + GL.Enable(EnableCap.DepthTest); + _tetra.Draw(wire: true); + } GL.PopMatrix(); @@ -231,9 +247,36 @@ namespace Tetrahedrons protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); - _proj = Matrix4.CreateOrthographic(6, 6f * Height / Width, -2, 2); + + _proj3d = Matrix4.CreateOrthographic(6, 6f * Height / Width, -2, 2); + _proj2d = Matrix4.CreateOrthographic(4, 4f * Height / Width, -1, 1); + _t += e.Time; + if (Keyboard[Key.Left]) + { + var rot = MVec3d.Complex(-e.Time, MVec3d.Unit12); + _b1 = rot * _b1 * ~rot; // todo make blade classes + _b2 = rot * _b2 * ~rot; + } + + if (Keyboard[Key.Right]) + { + var rot = MVec3d.Complex(e.Time, MVec3d.Unit12); + _b1 = rot * _b1 * ~rot; + _b2 = rot * _b2 * ~rot; + } + + if (Keyboard[Key.Up]) + { + _pivot -= (_b1 ^ _b2) * MVec3d.Unit123 * e.Time; + } + + if (Keyboard[Key.Down]) + { + _pivot += (_b1 ^ _b2) * MVec3d.Unit123 * e.Time; + } + if (_pause) return; @@ -241,15 +284,15 @@ namespace Tetrahedrons // var b2 = MVec3d.Unit3; // var pivot = new Vector3d(Math.Cos(_t), 0, 0); - var b1 = MVec3d.Vector(0, Math.Cos(_t * .2), Math.Sin(_t * .5)); - var b2 = MVec3d.Vector(Math.Cos(_t * .6), 0, Math.Sin(_t * .4)); - var pivot = Vector3d.Zero; +// var b1 = MVec3d.Vector(0, Math.Cos(_t * .2), Math.Sin(_t * .5)); +// var b2 = MVec3d.Vector(Math.Cos(_t * .6), 0, Math.Sin(_t * .4)); +// var pivot = Vector3d.Zero; // var b1 = MVec3d.Vector(Math.Cos(_t/2), Math.Sin(_t/2), 0); // var b2 = MVec3d.Unit3; // var pivot = Vector3d.Zero; - var blade = MVec3d.Outer(b1, b2); +// var blade = MVec3d.Outer(b1, b2); // var rot = MVec3d.Complex(_t / 2, MVec3d.Unit12); // var b1 = rot * MVec3d.Unit2 * ~rot; @@ -258,19 +301,19 @@ namespace Tetrahedrons // var blade = pln; // var pivot = Vector3d.Zero; - Console.Out.WriteLine("b1 = {0}", b1); - Console.Out.WriteLine("b2 = {0}", b2); - - _polyg = _tetra.Intersection(blade, pivot); + _polyg = _tetra.Intersection(_b1 ^ _b2, _pivot.VectorPart); if (_div) { + var px = ((_pivot & _b1) * ~_b1) & _b1; + var py = ((_pivot ^ _b1) * ~_b1) & ((_b2 ^ _b1) * ~_b1); + for (var i = 0; i < _polyg.Points.Length; i++) { MVec3d p = _polyg.Points[i]; - var x = ((p & b1) * ~b1) & b1; - var y = ((p ^ b1) * ~b1) & ((b2 ^ b1) * ~b1); - _polyg.Points[i] = new Vector3d(x.E, y.E, 0); + var x = ((p & _b1) * ~_b1) & _b1; + var y = ((p ^ _b1) * ~_b1) & ((_b2 ^ _b1) * ~_b1); + _polyg.Points[i] = new Vector3d(x.E - px.E, y.E - py.E, 0); } } } @@ -287,6 +330,11 @@ namespace Tetrahedrons case Key.A: _div = !_div; break; + case Key.R: + _b1 = MVec3d.Unit1; + _b2 = MVec3d.Unit3; + _pivot = MVec3d.Zero; + break; } } }