diff --git a/Platformer/PlatformWindow.cs b/Platformer/PlatformWindow.cs index a998de1..46c344a 100644 --- a/Platformer/PlatformWindow.cs +++ b/Platformer/PlatformWindow.cs @@ -1,4 +1,5 @@ using System; +using System.Data.Common; using System.Drawing; using OpenTK; using OpenTK.Graphics.OpenGL; @@ -22,6 +23,13 @@ namespace Platformer public static ViewMats Identity => new ViewMats {Model = Matrix4.Identity, View = Matrix4.Identity}; } + public struct PlaneMats + { + public Matrix4 Tform; + + public static PlaneMats Identity => new PlaneMats {Tform = Matrix4.Identity}; + } + public class PlatformWindow : GameWindow { private Program _render; @@ -29,16 +37,22 @@ namespace Platformer private Buffer _tVerts; private Buffer _tFaces; private Buffer _tEdges; - private Buffer _ubo; + private Buffer _viewBuf; + private Buffer _tformBuf; private VertexArray _tetraVao; private ViewMats _view; + private PlaneMats _tform; + private Buffer _tInds; private Buffer _pVerts; + private Program _compute; + private Buffer _pEdges; private Buffer _pAreas; + private VertexArray _polyVao; protected override void OnLoad(EventArgs e) @@ -75,12 +89,18 @@ namespace Platformer _pEdges = new Buffer(_tInds.Count / 4 * 8); _pAreas = new Buffer(_tInds.Count / 4 * 6); - _ubo = new Buffer(); + _viewBuf = new Buffer(); _view = ViewMats.Identity; - GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, _ubo); + _tformBuf = new Buffer(); + _tform = PlaneMats.Identity; + + GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, _viewBuf); GL.UniformBlockBinding(_render, _render.UnifBlockInd("ViewMats"), 0); + GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 1, _tformBuf); + GL.UniformBlockBinding(_compute, _compute.UnifBlockInd("PlaneMats"), 1); + _tetraVao = new VertexArray(); _tetraVao.VertexPointer(_tVerts, index: _render.AttrLoc("pos"), size: 4); @@ -126,10 +146,10 @@ namespace Platformer _view.Proj = Matrix4.CreateOrthographic(5, 5f * Height / Width, -2.5f, 2.5f); _view.View = Matrix4.LookAt(Vector3.Zero, -Vector3.One, Vector3.UnitZ); + _tform.Tform = _view.Model *= Matrix4.CreateRotationZ((float) e.Time); - _view.Model *= Matrix4.CreateRotationZ((float) e.Time); - - _ubo.SetData(ref _view); + _viewBuf.SetData(ref _view); + _tformBuf.SetData(ref _tform); GL.UseProgram(_compute); GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 0, _tVerts); diff --git a/Platformer/shaders/intersect.comp b/Platformer/shaders/intersect.comp index 3b97030..6d9f4f6 100644 --- a/Platformer/shaders/intersect.comp +++ b/Platformer/shaders/intersect.comp @@ -28,28 +28,35 @@ uniform PlaneMats layout(local_size_x = 128, local_size_y = 1, local_size_z = 1) in; void main () { - uint ti0 = gl_GlobalInvocationID.x * 4; - uint pv0 = gl_GlobalInvocationID.x * 4; - uint pe0 = gl_GlobalInvocationID.x * 8; + int ti0 = int(gl_GlobalInvocationID.x * 4); + int pv0 = int(gl_GlobalInvocationID.x * 4); + int pe0 = int(gl_GlobalInvocationID.x * 8); - uint ii = 0; - uint vv = 0; + int ii = 0; + int vv = 0; -// polyVerts[pv0 + vv++] = vec4(-1,0,0,1); -// polyVerts[pv0 + vv++] = vec4(1,0,0,1); -// -// polyEdges[pe0 + ii++] = pv0 + 0; -// polyEdges[pe0 + ii++] = pv0 + 1; - - for(uint i = 1; i < 4; i++) + for (int i = 0; i < 4; i++) + for (int j = i + 1; j < 4; j++) { - polyVerts[pv0 + vv++] = (tetraVerts[ti0] + tetraVerts[ti0 + i]) / 2; - } + vec4 a = tetraVerts[tetraInds[ti0 + i]]; + vec4 a_ = tform * a; + + vec4 b = tetraVerts[tetraInds[ti0 + j]]; + vec4 b_ = tform * b; - polyEdges[pe0 + ii++] = pv0 + 0; - polyEdges[pe0 + ii++] = pv0 + 1; - polyEdges[pe0 + ii++] = pv0 + 1; - polyEdges[pe0 + ii++] = pv0 + 2; - polyEdges[pe0 + ii++] = pv0 + 2; - polyEdges[pe0 + ii++] = pv0 + 0; + if (a_.x * b_.x < 0) + { + vec4 p = (b-a) / (a_.x - b_.x) * b_.x + b; + polyVerts[pv0 + vv++] = p; + } + } + + for (int i = 0; i < vv; i++) + { + polyEdges[pe0 + ii++] = pv0 + i; + polyEdges[pe0 + ii++] = pv0 + (i + 1) % vv; + } + + while (vv < 4) polyVerts[pv0 + vv++] = vec4(0); + while (ii < 8) polyEdges[pe0 + ii++] = 0; } \ No newline at end of file