Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f254facb85 | |||
| 4a25ae992f | |||
| 94775b73cf | |||
| 93cd1c767f | |||
| edb2985a98 | |||
| dd1fc7e12e | |||
| 8d2177ef4a | |||
| d5feb8facd |
24
Tetrahedrons/BlockStructs.cs
Normal file
24
Tetrahedrons/BlockStructs.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using OpenTK;
|
||||
|
||||
namespace Tetrahedrons
|
||||
{
|
||||
public struct PenTransform
|
||||
{
|
||||
public Matrix4 Rotate; // want this to be a bivector somehow
|
||||
public Vector4 Pivot;
|
||||
|
||||
public static PenTransform Identity => new PenTransform() {Rotate = Matrix4.Identity, Pivot = Vector4.Zero};
|
||||
public static readonly int SizeInBytes = Marshal.SizeOf(typeof(PenTransform));
|
||||
}
|
||||
|
||||
public struct ViewTransform
|
||||
{
|
||||
public Matrix4 Model;
|
||||
public Matrix4 View;
|
||||
public Matrix4 Projection;
|
||||
|
||||
public static ViewTransform Identity => new ViewTransform() {Model = Matrix4.Identity, View = Matrix4.Identity, Projection = Matrix4.Identity};
|
||||
public static readonly int SizeInBytes = Marshal.SizeOf(typeof(ViewTransform));
|
||||
}
|
||||
}
|
||||
254
Tetrahedrons/Gl4Window.cs
Normal file
254
Tetrahedrons/Gl4Window.cs
Normal file
@@ -0,0 +1,254 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics;
|
||||
using OpenTK.Graphics.OpenGL;
|
||||
using OpenTK.Platform.Windows;
|
||||
|
||||
namespace Tetrahedrons
|
||||
{
|
||||
internal class Gl4Window : GameWindow
|
||||
{
|
||||
private float _t;
|
||||
|
||||
private int _pgmRend;
|
||||
|
||||
private int _shVert;
|
||||
private int _shFrag;
|
||||
|
||||
private int _bufPentVerts;
|
||||
private int _bufPentInds;
|
||||
|
||||
private int _bufHullEdgeInds;
|
||||
private int _bufHullFaceInds;
|
||||
private int _bufHullVerts;
|
||||
|
||||
private int _bufTransView;
|
||||
private int _bufTransPent;
|
||||
|
||||
private Vector4[] _datPentVerts;
|
||||
private uint[] _datPentInds;
|
||||
|
||||
private ViewTransform _transView;
|
||||
private PenTransform _transPent;
|
||||
|
||||
private int _shComp;
|
||||
private int _pgmComp;
|
||||
|
||||
private int _hullFaceIndsCount;
|
||||
private int _hullEdgeIndsCount;
|
||||
private int _hullVertsCount;
|
||||
|
||||
private int _penIndsCount;
|
||||
private int _penVertsCount;
|
||||
|
||||
private int _invocationCount;
|
||||
|
||||
private float _w = 6;
|
||||
|
||||
protected override void OnLoad(EventArgs e)
|
||||
{
|
||||
base.OnLoad(e);
|
||||
|
||||
X = (DisplayDevice.Default.Width - Width) / 2;
|
||||
Y = (DisplayDevice.Default.Height - Height) / 2;
|
||||
|
||||
#region Shaders
|
||||
|
||||
_pgmRend = GL.CreateProgram();
|
||||
|
||||
_shVert = GL.CreateShader(ShaderType.VertexShader);
|
||||
GL.ShaderSource(_shVert, File.ReadAllText("Shaders/simple.vert"));
|
||||
GL.CompileShader(_shVert);
|
||||
GL.AttachShader(_pgmRend, _shVert);
|
||||
Console.WriteLine(GL.GetShaderInfoLog(_shVert));
|
||||
|
||||
_shFrag = GL.CreateShader(ShaderType.FragmentShader);
|
||||
GL.ShaderSource(_shFrag, File.ReadAllText("Shaders/simple.frag"));
|
||||
GL.CompileShader(_shFrag);
|
||||
GL.AttachShader(_pgmRend, _shFrag);
|
||||
Console.WriteLine(GL.GetShaderInfoLog(_shFrag));
|
||||
|
||||
GL.LinkProgram(_pgmRend);
|
||||
Console.WriteLine(GL.GetProgramInfoLog(_pgmRend));
|
||||
|
||||
_pgmComp = GL.CreateProgram();
|
||||
|
||||
_shComp = GL.CreateShader(ShaderType.ComputeShader);
|
||||
GL.ShaderSource(_shComp, File.ReadAllText("Shaders/intersect.comp"));
|
||||
GL.CompileShader(_shComp);
|
||||
GL.AttachShader(_pgmComp, _shComp);
|
||||
Console.Out.WriteLine(GL.GetShaderInfoLog(_shComp));
|
||||
|
||||
GL.LinkProgram(_pgmComp);
|
||||
Console.Out.WriteLine(GL.GetProgramInfoLog(_pgmComp));
|
||||
|
||||
#endregion
|
||||
|
||||
_w = 10;
|
||||
var n = 5;
|
||||
var vlst = new List<Vector4>();
|
||||
var ilst = new List<uint>();
|
||||
for (var x = 0; x < n; x++)
|
||||
for (var y = 0; y < n; y++)
|
||||
for (var z = 0; z < n; z++)
|
||||
for (var w = 0; w < n; w++)
|
||||
{
|
||||
vlst.Add(new Vector4(x + 1, y - 0, z - 0, w - 0) - new Vector4(n) / 2);
|
||||
vlst.Add(new Vector4(x - 0, y + 1, z - 0, w - 0) - new Vector4(n) / 2);
|
||||
vlst.Add(new Vector4(x - 0, y - 0, z + 1, w - 0) - new Vector4(n) / 2);
|
||||
vlst.Add(new Vector4(x - 0, y - 0, z - 0, w + 1) - new Vector4(n) / 2);
|
||||
vlst.Add(new Vector4(x + 1, y + 1, z + 1, w + 1) - new Vector4(n) / 2);
|
||||
|
||||
ilst.Add((uint) ilst.Count);
|
||||
ilst.Add((uint) ilst.Count);
|
||||
ilst.Add((uint) ilst.Count);
|
||||
ilst.Add((uint) ilst.Count);
|
||||
ilst.Add((uint) ilst.Count);
|
||||
}
|
||||
|
||||
_datPentVerts = vlst.ToArray();
|
||||
_datPentInds = ilst.ToArray();
|
||||
|
||||
// _datPentVerts = new[]
|
||||
// {
|
||||
// new Vector4(-1, -1, -1, -1) / 2,
|
||||
// new Vector4(+1, +1, -1, -1) / 2,
|
||||
// new Vector4(-1, +1, +1, -1) / 2,
|
||||
// new Vector4(+1, -1, +1, -1) / 2,
|
||||
// new Vector4(+0, +0, +0, +1) / 2,
|
||||
// };
|
||||
// _datPentInds = new uint[]
|
||||
// {
|
||||
// 0, 1, 2, 3, 4
|
||||
// };
|
||||
|
||||
_transView = ViewTransform.Identity;
|
||||
_transPent = PenTransform.Identity;
|
||||
|
||||
_penVertsCount = _datPentVerts.Length;
|
||||
_penIndsCount = _datPentInds.Length;
|
||||
|
||||
_invocationCount = _penIndsCount / 5;
|
||||
|
||||
_hullVertsCount = _invocationCount * 6; // 6 hull verts per pent
|
||||
_hullFaceIndsCount = _invocationCount * 24; // 24 hull inds per pent
|
||||
_hullEdgeIndsCount = _invocationCount * 18;
|
||||
|
||||
_bufPentVerts = GL.GenBuffer();
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, _bufPentVerts);
|
||||
GL.BufferData(BufferTarget.ShaderStorageBuffer, (IntPtr) (_penVertsCount * Vector4.SizeInBytes), _datPentVerts, BufferUsageHint.DynamicDraw);
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, 0);
|
||||
|
||||
_bufPentInds = GL.GenBuffer();
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, _bufPentInds);
|
||||
GL.BufferData(BufferTarget.ShaderStorageBuffer, (IntPtr) (_penIndsCount * sizeof(int)), _datPentInds, BufferUsageHint.DynamicDraw);
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, 0);
|
||||
|
||||
_bufHullVerts = GL.GenBuffer();
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, _bufHullVerts);
|
||||
GL.BufferData(BufferTarget.ShaderStorageBuffer, (IntPtr) (_hullVertsCount * Vector4.SizeInBytes), IntPtr.Zero, BufferUsageHint.DynamicDraw);
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, 0);
|
||||
|
||||
_bufHullFaceInds = GL.GenBuffer();
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, _bufHullFaceInds);
|
||||
GL.BufferData(BufferTarget.ShaderStorageBuffer, (IntPtr) (_hullFaceIndsCount * sizeof(int)), IntPtr.Zero, BufferUsageHint.DynamicDraw);
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, 0);
|
||||
|
||||
_bufHullEdgeInds = GL.GenBuffer();
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, _bufHullEdgeInds);
|
||||
GL.BufferData(BufferTarget.ShaderStorageBuffer, (IntPtr) (_hullEdgeIndsCount * sizeof(int)), IntPtr.Zero, BufferUsageHint.DynamicDraw);
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, 0);
|
||||
|
||||
_bufTransView = GL.GenBuffer();
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, _bufTransView);
|
||||
GL.BufferData(BufferTarget.ShaderStorageBuffer, (IntPtr) ViewTransform.SizeInBytes, ref _transView, BufferUsageHint.DynamicDraw);
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, 0);
|
||||
|
||||
_bufTransPent = GL.GenBuffer();
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, _bufTransPent);
|
||||
GL.BufferData(BufferTarget.ShaderStorageBuffer, (IntPtr) ViewTransform.SizeInBytes, ref _transPent, BufferUsageHint.DynamicDraw);
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, 0);
|
||||
|
||||
GL.UniformBlockBinding(_pgmRend, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
protected override void OnRenderFrame(FrameEventArgs e)
|
||||
{
|
||||
base.OnRenderFrame(e);
|
||||
GL.Viewport(0, 0, Width, Height);
|
||||
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
||||
GL.Enable(EnableCap.DepthTest);
|
||||
|
||||
GL.UseProgram(_pgmComp);
|
||||
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 1, _bufPentVerts);
|
||||
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 2, _bufPentInds);
|
||||
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 3, _bufHullVerts);
|
||||
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 4, _bufHullFaceInds);
|
||||
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 5, _bufHullEdgeInds);
|
||||
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 6, _bufTransPent);
|
||||
GL.DispatchCompute(_invocationCount, 1, 1);
|
||||
GL.MemoryBarrier(MemoryBarrierFlags.ShaderStorageBarrierBit);
|
||||
|
||||
GL.UseProgram(_pgmRend);
|
||||
GL.PointSize(10f);
|
||||
GL.LineWidth(4f);
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, _bufHullVerts);
|
||||
GL.VertexAttribPointer(0, 4, VertexAttribPointerType.Float, false, 0, 0);
|
||||
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
|
||||
GL.EnableVertexAttribArray(0);
|
||||
|
||||
|
||||
GL.Enable(EnableCap.DepthTest);
|
||||
GL.Uniform4(3, new Vector4(1, 1, 1, 1));
|
||||
GL.BindBuffer(BufferTarget.ElementArrayBuffer, _bufHullFaceInds);
|
||||
GL.DrawElements(BeginMode.Triangles, _hullFaceIndsCount, DrawElementsType.UnsignedInt, 0);
|
||||
GL.Disable(EnableCap.DepthTest);
|
||||
|
||||
GL.Uniform4(3, new Vector4(0, 0, 0, 1));
|
||||
GL.BindBuffer(BufferTarget.ElementArrayBuffer, _bufHullEdgeInds);
|
||||
GL.DrawElements(BeginMode.Lines, _hullEdgeIndsCount, DrawElementsType.UnsignedInt, 0);
|
||||
|
||||
|
||||
GL.DisableVertexAttribArray(0);
|
||||
|
||||
GL.Flush();
|
||||
SwapBuffers();
|
||||
}
|
||||
|
||||
protected override void OnUpdateFrame(FrameEventArgs e)
|
||||
{
|
||||
base.OnUpdateFrame(e);
|
||||
_t += (float) e.Time;
|
||||
|
||||
_transView.Projection = Matrix4.CreateOrthographic(_w, _w * Height / Width, -_w, _w);
|
||||
_transView.View = Matrix4.LookAt(Vector3.Zero, -new Vector3(1, -1, 1), Vector3.UnitZ);
|
||||
_transView.Model = Matrix4.CreateRotationZ(_t / 10);
|
||||
|
||||
_bufTransView = GL.GenBuffer();
|
||||
GL.BindBuffer(BufferTarget.UniformBuffer, _bufTransView);
|
||||
GL.BufferData(BufferTarget.UniformBuffer, (IntPtr) ViewTransform.SizeInBytes, ref _transView, BufferUsageHint.StaticDraw);
|
||||
GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, _bufTransView);
|
||||
GL.BindBuffer(BufferTarget.UniformBuffer, 0);
|
||||
|
||||
// _transPent.Pivot.W = .3f;
|
||||
// _transPent.Pivot.W = (float) (Math.Sin(_t*.5) * .6);
|
||||
var a = 1f;
|
||||
a = _t;
|
||||
_transPent.Rotate = new Matrix4(
|
||||
1, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, (float) Math.Cos(a / 5), (float) Math.Sin(a / 5),
|
||||
0, 0, (float) -Math.Sin(a / 5), (float) Math.Cos(a / 5));
|
||||
|
||||
_bufTransPent = GL.GenBuffer();
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, _bufTransPent);
|
||||
GL.BufferData(BufferTarget.ShaderStorageBuffer, (IntPtr) ViewTransform.SizeInBytes, ref _transPent, BufferUsageHint.DynamicDraw);
|
||||
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,11 @@ using OpenTK;
|
||||
|
||||
namespace Tetrahedrons
|
||||
{
|
||||
public static class Program
|
||||
internal class Program : GameWindow
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
using (var p = new TetrahedronWindow())
|
||||
using (var p = new Gl4Window())
|
||||
p.Run();
|
||||
}
|
||||
}
|
||||
|
||||
148
Tetrahedrons/Shaders/intersect.comp
Normal file
148
Tetrahedrons/Shaders/intersect.comp
Normal file
@@ -0,0 +1,148 @@
|
||||
#version 430
|
||||
|
||||
layout(std430, binding=1) buffer PentVerts
|
||||
{
|
||||
vec4 pentVerts[];
|
||||
};
|
||||
|
||||
layout(std430, binding=2) buffer PentInds
|
||||
{
|
||||
int pentInds[];
|
||||
};
|
||||
|
||||
layout(std430, binding=3) buffer HullVerts
|
||||
{
|
||||
vec4 hullVerts[];
|
||||
};
|
||||
|
||||
layout(std430, binding=4) buffer HullFaceInds
|
||||
{
|
||||
int hullFaceInds[];
|
||||
};
|
||||
|
||||
layout(std430, binding=5) buffer HullEgdeInds
|
||||
{
|
||||
int hullEdgeInds[];
|
||||
};
|
||||
|
||||
layout(std430, binding=6) buffer Transform
|
||||
{
|
||||
mat4 rotate;
|
||||
vec4 pivot;
|
||||
};
|
||||
|
||||
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
void main() {
|
||||
int iid = int(gl_GlobalInvocationID.x);
|
||||
|
||||
int pi0 = iid * 5;
|
||||
int hv0 = iid * 6;
|
||||
int hi0 = iid * 24;
|
||||
int he0 = iid * 18;
|
||||
|
||||
for(int i = 0; i < 24; i++)
|
||||
hullFaceInds[hi0 + i] = 0;
|
||||
|
||||
for(int i = 0; i < 18; i++)
|
||||
hullEdgeInds[he0 + i] = 0;
|
||||
|
||||
int vi = 0; // vert index
|
||||
int fi = 0; // face inds index
|
||||
int ei = 0; // edge inds index
|
||||
|
||||
for(int i = 0; i < 5; i++)
|
||||
for(int j = i + 1; j < 5; j++)
|
||||
{
|
||||
vec4 a = (pentVerts[pentInds[pi0 + i]] - pivot) * rotate;
|
||||
vec4 b = (pentVerts[pentInds[pi0 + j]] - pivot) * rotate;
|
||||
|
||||
if (a.w * b.w < 0) // different sides
|
||||
{
|
||||
float alph = a.w / (a.w - b.w);
|
||||
vec4 p = a + alph * (b - a);
|
||||
|
||||
hullVerts[hv0 + vi] = p + pivot;
|
||||
|
||||
hullFaceInds[hi0 + 23 - 2 * vi] = i;
|
||||
hullFaceInds[hi0 + 22 - 2 * vi] = j;
|
||||
|
||||
vi++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (vi == 4) // points form a tetrahedron
|
||||
{
|
||||
for(int i = 0; i < 4; i++)
|
||||
for(int j = i + 1; j < 4; j++)
|
||||
for(int k = j + 1; k < 4; k++)
|
||||
{
|
||||
hullFaceInds[hi0 + fi++] = hv0 + i;
|
||||
hullFaceInds[hi0 + fi++] = hv0 + j;
|
||||
hullFaceInds[hi0 + fi++] = hv0 + k;
|
||||
}
|
||||
|
||||
for(int i = 0; i < 4; i++)
|
||||
for(int j = i + 1; j < 4; j++)
|
||||
{
|
||||
hullEdgeInds[hi0 + ei++] = hv0 + i;
|
||||
hullEdgeInds[hi0 + ei++] = hv0 + j;
|
||||
}
|
||||
}
|
||||
else if (vi == 6) { // points form a triangular prism
|
||||
for(int f = 0; f < 5; f++)
|
||||
{
|
||||
for(int i = 0; i < 4; i++)
|
||||
for(int j = i + 1; j < 5; j++)
|
||||
for(int k = j + 1; k < 6; k++)
|
||||
if (
|
||||
(hullFaceInds[hi0 + 23 - 2 * i] == f ||
|
||||
hullFaceInds[hi0 + 22 - 2 * i] == f) &&
|
||||
(hullFaceInds[hi0 + 23 - 2 * j] == f ||
|
||||
hullFaceInds[hi0 + 22 - 2 * j] == f) &&
|
||||
(hullFaceInds[hi0 + 23 - 2 * k] == f ||
|
||||
hullFaceInds[hi0 + 22 - 2 * k] == f))
|
||||
{
|
||||
hullFaceInds[hi0 + fi++] = hv0 + i;
|
||||
hullFaceInds[hi0 + fi++] = hv0 + j;
|
||||
hullFaceInds[hi0 + fi++] = hv0 + k;
|
||||
|
||||
hullEdgeInds[he0 + ei++] = hv0 + i;
|
||||
hullEdgeInds[he0 + ei++] = hv0 + j;
|
||||
hullEdgeInds[he0 + ei++] = hv0 + j;
|
||||
hullEdgeInds[he0 + ei++] = hv0 + k;
|
||||
hullEdgeInds[he0 + ei++] = hv0 + k;
|
||||
hullEdgeInds[he0 + ei++] = hv0 + i;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
int a = hullFaceInds[hi0 + i];
|
||||
int b = hullFaceInds[hi0 + (1 + i) % 3];
|
||||
int c = hullFaceInds[hi0 + 3 + i];
|
||||
int d = hullFaceInds[hi0 + 3 + (1 + i) % 3];
|
||||
|
||||
hullFaceInds[hi0 + fi++] = c;
|
||||
hullFaceInds[hi0 + fi++] = a;
|
||||
hullFaceInds[hi0 + fi++] = b;
|
||||
hullFaceInds[hi0 + fi++] = b;
|
||||
hullFaceInds[hi0 + fi++] = d;
|
||||
hullFaceInds[hi0 + fi++] = c;
|
||||
|
||||
hullEdgeInds[he0 + ei++] = a;
|
||||
hullEdgeInds[he0 + ei++] = c;
|
||||
}
|
||||
}
|
||||
|
||||
while (vi < 6)
|
||||
hullVerts[hv0 + vi++] = vec4(0);
|
||||
|
||||
while (fi < 24)
|
||||
hullFaceInds[hi0 + fi++] = 0;
|
||||
|
||||
while (ei < 18)
|
||||
hullEdgeInds[he0 + ei++] = 0;
|
||||
|
||||
}
|
||||
10
Tetrahedrons/Shaders/simple.frag
Normal file
10
Tetrahedrons/Shaders/simple.frag
Normal file
@@ -0,0 +1,10 @@
|
||||
#version 430 compatibility
|
||||
|
||||
uniform vec4 color;
|
||||
|
||||
in vec4 scol;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = color * scol;
|
||||
}
|
||||
18
Tetrahedrons/Shaders/simple.vert
Normal file
18
Tetrahedrons/Shaders/simple.vert
Normal file
@@ -0,0 +1,18 @@
|
||||
#version 430 compatibility
|
||||
|
||||
layout(row_major) uniform Matrices
|
||||
{
|
||||
mat4 model;
|
||||
mat4 view;
|
||||
mat4 proj;
|
||||
};
|
||||
|
||||
in vec4 pos;
|
||||
|
||||
out vec4 scol;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(pos.xyz, 1) * model * view * proj;
|
||||
scol = vec4(pos.xyz / 5 + .5, 1);
|
||||
}
|
||||
@@ -1,17 +1,30 @@
|
||||
using System;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics.OpenGL;
|
||||
using OpenTK.Input;
|
||||
using System;
|
||||
using System.Diagnostics.PerformanceData;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Printing;
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using System.Security;
|
||||
using System.Xml.XPath;
|
||||
using OpenTK;
|
||||
using OpenTK.Audio.OpenAL;
|
||||
using OpenTK.Graphics.OpenGL;
|
||||
using OpenTK.Input;
|
||||
using OpenTK.Platform.Windows;
|
||||
|
||||
namespace Tetrahedrons
|
||||
{
|
||||
public class TetrahedronWindow : GameWindow
|
||||
{
|
||||
private Matrix4 _proj3D;
|
||||
private Matrix4 _proj3d;
|
||||
private Matrix4 _view;
|
||||
|
||||
private Simplex[] _simplexes;
|
||||
private Simplex[] _intersections;
|
||||
private MVec4D _a = MVec4D.UnitX;
|
||||
private MVec4D _b = MVec4D.UnitY;
|
||||
|
||||
private Simplex _sim;
|
||||
private Simplex _int;
|
||||
|
||||
private double _t;
|
||||
private bool _pause;
|
||||
@@ -26,31 +39,13 @@ namespace Tetrahedrons
|
||||
|
||||
_view = Matrix4.LookAt(Vector3.Zero, -new Vector3(.86f, .5f, 1), Vector3.UnitZ);
|
||||
|
||||
|
||||
// build the pentatope lattice
|
||||
var n = 5;
|
||||
|
||||
_simplexes = new Simplex[n * n * n * n];
|
||||
_intersections = new Simplex[_simplexes.Length];
|
||||
|
||||
var off = (new Vector4d(n - 1, n - 1, n - 1, n - 1)) / 2;
|
||||
|
||||
var i = 0;
|
||||
for (var x = 0; x < n; x++)
|
||||
for (var y = 0; y < n; y++)
|
||||
for (var z = 0; z < n; z++)
|
||||
for (var w = 0; w < n; w++)
|
||||
{
|
||||
var d = new Vector4d(x, y, z, w);
|
||||
_simplexes[i] = new Simplex( // not-quite-regular pentatope
|
||||
new Vector4d(+.5, -.5, -.5, -.5) + d - off,
|
||||
new Vector4d(-.5, +.5, -.5, -.5) + d - off,
|
||||
new Vector4d(-.5, -.5, +.5, -.5) + d - off,
|
||||
new Vector4d(-.5, -.5, -.5, +.5) + d - off,
|
||||
new Vector4d(+.5, +.5, +.5, +.5) + d - off);
|
||||
_intersections[i] = new Simplex();
|
||||
i++;
|
||||
}
|
||||
_sim = new Simplex( // not-quite-regular pentatope
|
||||
new Vector4d(+.5, -.5, -.5, -.5),
|
||||
new Vector4d(-.5, +.5, -.5, -.5),
|
||||
new Vector4d(-.5, -.5, +.5, -.5),
|
||||
new Vector4d(-.5, -.5, -.5, +.5),
|
||||
new Vector4d(+.5, +.5, +.5, +.5));
|
||||
_int = new Simplex();
|
||||
}
|
||||
|
||||
protected override void OnRenderFrame(FrameEventArgs e)
|
||||
@@ -66,44 +61,33 @@ namespace Tetrahedrons
|
||||
GL.PushMatrix();
|
||||
|
||||
GL.MatrixMode(MatrixMode.Projection);
|
||||
GL.LoadMatrix(ref _proj3D);
|
||||
GL.LoadMatrix(ref _proj3d);
|
||||
GL.MatrixMode(MatrixMode.Modelview);
|
||||
GL.LoadMatrix(ref _view);
|
||||
|
||||
GL.Enable(EnableCap.DepthTest);
|
||||
|
||||
GL.Begin(PrimitiveType.Lines);
|
||||
// render intersection wireframes
|
||||
|
||||
GL.Color3(0f, 0, 0);
|
||||
foreach (var intr in _intersections)
|
||||
foreach (var f in intr.Edjes)
|
||||
Util.Vertex3(intr.Verts[f]);
|
||||
|
||||
foreach (var f in _int.Edjes)
|
||||
Util.Vertex3(_int.Verts[f]);
|
||||
GL.End();
|
||||
|
||||
GL.Begin(PrimitiveType.Triangles);
|
||||
// render intersection faces
|
||||
|
||||
foreach (var intr in _intersections)
|
||||
foreach (var f in intr.Faces)
|
||||
foreach (var f in _int.Faces)
|
||||
{
|
||||
Util.Color3(intr.Verts[f]);
|
||||
Util.Vertex3(intr.Verts[f]);
|
||||
Util.Color3(_int.Verts[f]);
|
||||
Util.Vertex3(_int.Verts[f]);
|
||||
}
|
||||
|
||||
|
||||
GL.End();
|
||||
|
||||
GL.Disable(EnableCap.DepthTest);
|
||||
|
||||
if (_wire)
|
||||
{
|
||||
GL.Begin(PrimitiveType.Lines);
|
||||
// render pentatope wireframes
|
||||
|
||||
foreach (var sim in _simplexes)
|
||||
foreach (var f in sim.Edjes)
|
||||
Util.Vertex4(sim.Verts[f]);
|
||||
foreach (var f in _sim.Edjes)
|
||||
Util.Vertex4(_sim.Verts[f]);
|
||||
|
||||
GL.End();
|
||||
}
|
||||
@@ -115,27 +99,23 @@ namespace Tetrahedrons
|
||||
{
|
||||
base.OnUpdateFrame(e);
|
||||
|
||||
var w = 10f;
|
||||
var w = 2f;
|
||||
var d = w;
|
||||
_proj3D = Matrix4.CreateOrthographic(w, w * Height / Width, -d / 2, d / 2);
|
||||
_proj3d = Matrix4.CreateOrthographic(w, w * Height / Width, -d / 2, d / 2);
|
||||
|
||||
if (_pause) return;
|
||||
|
||||
_t += e.Time;
|
||||
|
||||
var pln = MVec4D.UnitXy + MVec4D.UnitXz + MVec4D.UnitYw;
|
||||
var r = MVec4D.Rotor(e.Time / 10, pln.Normalized);
|
||||
foreach (var pent in _simplexes)
|
||||
for (var i = 0; i < pent.Verts.Length; i++)
|
||||
pent.Verts[i] |= r;
|
||||
|
||||
var plane = MVec4D.UnitXw-2*MVec4D.UnitXy;
|
||||
var blade = MVec4D.UnitXyz;
|
||||
var pivot = MVec4D.Zero;
|
||||
var pivot = .1 * MVec4D.UnitW;
|
||||
|
||||
for (var i = 0; i < _simplexes.Length; i++)
|
||||
{
|
||||
_intersections[i] = _simplexes[i].Intersect(blade, pivot);
|
||||
}
|
||||
var r = MVec4D.Rotor(e.Time / 10, plane.Normalized);
|
||||
for (var i = 0; i < _sim.Verts.Length; i++)
|
||||
_sim.Verts[i] |= r;
|
||||
|
||||
_int = _sim.Intersect(blade, pivot);
|
||||
}
|
||||
|
||||
protected override void OnKeyDown(KeyboardKeyEventArgs e)
|
||||
|
||||
@@ -42,6 +42,8 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Gl4Window.cs" />
|
||||
<Compile Include="BlockStructs.cs" />
|
||||
<Compile Include="MVec4D.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
@@ -53,6 +55,17 @@
|
||||
<None Include="OpenTK.dll.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Shaders\intersect.comp">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="Shaders\simple.frag">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="Shaders\simple.vert">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
||||
Reference in New Issue
Block a user