edges working but intermittent

This commit is contained in:
2018-04-06 20:42:00 -04:00
parent 4a25ae992f
commit f254facb85
4 changed files with 150 additions and 79 deletions

View File

@@ -1,8 +1,10 @@
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;
@@ -20,7 +22,8 @@ namespace Tetrahedrons
private int _bufPentVerts;
private int _bufPentInds;
private int _bufHullInds;
private int _bufHullEdgeInds;
private int _bufHullFaceInds;
private int _bufHullVerts;
private int _bufTransView;
@@ -35,12 +38,17 @@ namespace Tetrahedrons
private int _shComp;
private int _pgmComp;
private int _hullIndsCount;
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);
@@ -80,20 +88,43 @@ namespace Tetrahedrons
#endregion
_datPentVerts = new[]
_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++)
{
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(+1, +1, +1, +1) / 2,
new Vector4(-1, -1, -1, -1) / 2,
};
_datPentInds = new uint[]
{
0, 1, 2, 3, 4,
// 0, 1, 2, 3, 5,
};
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;
@@ -104,8 +135,8 @@ namespace Tetrahedrons
_invocationCount = _penIndsCount / 5;
_hullVertsCount = _invocationCount * 6; // 6 hull verts per pent
_hullIndsCount = _invocationCount * 24; // 24 hull inds per pent
Console.Out.WriteLine("_hullIndsCount = {0}", _hullIndsCount);
_hullFaceIndsCount = _invocationCount * 24; // 24 hull inds per pent
_hullEdgeIndsCount = _invocationCount * 18;
_bufPentVerts = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, _bufPentVerts);
@@ -122,9 +153,14 @@ namespace Tetrahedrons
GL.BufferData(BufferTarget.ShaderStorageBuffer, (IntPtr) (_hullVertsCount * Vector4.SizeInBytes), IntPtr.Zero, BufferUsageHint.DynamicDraw);
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, 0);
_bufHullInds = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ShaderStorageBuffer, _bufHullInds);
GL.BufferData(BufferTarget.ShaderStorageBuffer, (IntPtr) (_hullIndsCount * sizeof(int)), IntPtr.Zero, BufferUsageHint.DynamicDraw);
_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();
@@ -152,25 +188,32 @@ namespace Tetrahedrons
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 1, _bufPentVerts);
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 2, _bufPentInds);
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 3, _bufHullVerts);
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 4, _bufHullInds);
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 5, _bufTransPent);
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.BindBuffer(BufferTarget.ElementArrayBuffer, _bufHullInds);
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.DrawElements(BeginMode.Triangles, _hullIndsCount, DrawElementsType.UnsignedInt, 0);
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.DrawArrays(PrimitiveType.Points, 0, _hullVertsCount);
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();
@@ -182,11 +225,9 @@ namespace Tetrahedrons
base.OnUpdateFrame(e);
_t += (float) e.Time;
float w = 3;
_transView.Projection = Matrix4.CreateOrthographic(w, w * Height / Width, -w, w);
_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 / 3);
_transView.Model = Matrix4.CreateRotationZ(_t / 10);
_bufTransView = GL.GenBuffer();
GL.BindBuffer(BufferTarget.UniformBuffer, _bufTransView);
@@ -194,15 +235,15 @@ namespace Tetrahedrons
GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, _bufTransView);
GL.BindBuffer(BufferTarget.UniformBuffer, 0);
_transPent.Pivot.W = .3f;
// _transPent.Pivot.W = (float) (Math.Sin(_t) * .6);
var a = 3f;
// _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.Sin(a / 5), (float) -Math.Cos(a / 5),
0, 0, (float) Math.Cos(a / 5), (float) Math.Sin(a / 5));
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);

View File

@@ -15,12 +15,17 @@ layout(std430, binding=3) buffer HullVerts
vec4 hullVerts[];
};
layout(std430, binding=4) buffer HullInds
layout(std430, binding=4) buffer HullFaceInds
{
int hullInds[];
int hullFaceInds[];
};
layout(std430, binding=5) buffer Transform
layout(std430, binding=5) buffer HullEgdeInds
{
int hullEdgeInds[];
};
layout(std430, binding=6) buffer Transform
{
mat4 rotate;
vec4 pivot;
@@ -34,15 +39,17 @@ void main() {
int pi0 = iid * 5;
int hv0 = iid * 6;
int hi0 = iid * 24;
// use range [19:] as lengths for sub arrays
// re-use hullInds as multidim array for faces
int he0 = iid * 18;
for(int i = 0; i < 24; i++)
hullInds[hi0 + i] = 0;
hullFaceInds[hi0 + i] = 0;
for(int i = 0; i < 18; i++)
hullEdgeInds[he0 + i] = 0;
int vi = 0; // vert index
int ii = 0; // inds 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++)
@@ -57,64 +64,85 @@ void main() {
hullVerts[hv0 + vi] = p + pivot;
hullInds[hi0 + 23 - 2 * vi] = i;
hullInds[hi0 + 22 - 2 * vi] = j;
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++)
{
hullInds[hi0 + ii++] = i;
hullInds[hi0 + ii++] = j;
hullInds[hi0 + ii++] = 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 < 6; i++)
for(int j = i + 1; j < 6; j++)
for(int i = 0; i < 4; i++)
for(int j = i + 1; j < 5; j++)
for(int k = j + 1; k < 6; k++)
if (
(hullInds[hi0 + 23 - 2 * i] == f ||
hullInds[hi0 + 22 - 2 * i] == f) &&
(hullInds[hi0 + 23 - 2 * j] == f ||
hullInds[hi0 + 22 - 2 * j] == f) &&
(hullInds[hi0 + 23 - 2 * k] == f ||
hullInds[hi0 + 22 - 2 * k] == f))
(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))
{
hullInds[hi0 + ii++] = hv0 + i;
hullInds[hi0 + ii++] = hv0 + j;
hullInds[hi0 + ii++] = hv0 + k;
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 = hullInds[hi0 + i];
int b = hullInds[hi0 + (1 + i) % 3];
int c = hullInds[hi0 + 3 + i];
int d = hullInds[hi0 + 3 + (1 + i) % 3];
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];
hullInds[hi0 + ii++] = c;
hullInds[hi0 + ii++] = a;
hullInds[hi0 + ii++] = b;
hullInds[hi0 + ii++] = b;
hullInds[hi0 + ii++] = d;
hullInds[hi0 + ii++] = c;
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 (ii < 24)
hullInds[hi0 + ii++] = 0;
while (fi < 24)
hullFaceInds[hi0 + fi++] = 0;
while (ei < 18)
hullEdgeInds[he0 + ei++] = 0;
}

View File

@@ -1,8 +1,10 @@
#version 430 compatibility
in vec4 color;
uniform vec4 color;
in vec4 scol;
void main()
{
gl_FragColor = color;
gl_FragColor = color * scol;
}

View File

@@ -9,10 +9,10 @@ layout(row_major) uniform Matrices
in vec4 pos;
out vec4 color;
out vec4 scol;
void main()
{
gl_Position = vec4(pos.xyz, 1) * model * view * proj;
color = vec4(gl_Position.xyz + vec3(.5), 1);
scol = vec4(pos.xyz / 5 + .5, 1);
}