Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 49 additions & 21 deletions src/OpenPuppet/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Numerics;
using System.Runtime.InteropServices;
using ImGuiNET;
using OpenPuppet.rendering.VertexTypes;
using OpenPuppet.vector;
using Silk.NET.Input;
using Silk.NET.OpenGL;
using Silk.NET.OpenGL.Extensions.ImGui;
using Silk.NET.Windowing;
using System;
using System.Numerics;
using System.Runtime.InteropServices;

namespace OpenPuppet
{
Expand All @@ -18,6 +20,7 @@ static void Main(string[] args)
Title = "OpenPuppet",
PreferredDepthBufferBits = 24,
PreferredStencilBufferBits = 8,
VSync = false,
API = new GraphicsAPI(ContextAPI.OpenGL, new APIVersion(3, 3))
};

Expand All @@ -27,6 +30,7 @@ static void Main(string[] args)

uint vbo = 0;
uint vao = 0;
uint ebo = 0;
uint shader = 0;

const string VertexShaderCode = @"
Expand All @@ -53,33 +57,59 @@ void main() {

ImGuiController cont = null;

int idxcount = 0;

window.Load += () =>
{
gl = window.CreateOpenGL();
inputContext = window.CreateInput();
cont = new ImGuiController(gl, window, inputContext);

Span<Vertex> vertexData =
[
new Vertex(new Vector3(-0.5f, -0.5f, 0), 255, 0, 0, 255),
new Vertex(new Vector3(0, 0.5f, 0), 0, 255, 0, 255),
new Vertex(new Vector3(0.5f, -0.5f, 0), 0, 0, 255, 255)
];
var path = new VectorPathComponent([
new ArcCommand(
origin: new Vector2(0.5f, 0.0f),
rx: 0.5,
ry: 0.5,
xRot: 0,
largeArc: true,
sweep: true,
destination: new Vector2(0.5f, 1.0f)
),

new ArcCommand(
origin: new Vector2(0.5f, 1.0f),
rx: 0.5,
ry: 0.5,
xRot: 0,
largeArc: true,
sweep: true,
destination: new Vector2(0.5f, 0.0f)
)
]);

var mesh = VectorMesher.GenerateMesh<ColorVertex>(path);
idxcount = mesh.Indices.Length;

Span<ColorVertex> vertexData = mesh.Verticies;

vbo = gl.GenBuffer();
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo);
gl.BufferData<Vertex>(BufferTargetARB.ArrayBuffer, vertexData, BufferUsageARB.StaticDraw);
gl.BufferData<ColorVertex>(BufferTargetARB.ArrayBuffer, vertexData, BufferUsageARB.StaticDraw);

vao = gl.GenVertexArray();
gl.BindVertexArray(vao);
unsafe
{
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vertex.Size, (void*)0);
gl.VertexAttribPointer(1, 4, VertexAttribPointerType.UnsignedByte, true, Vertex.Size, (void*)12);
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, ColorVertex.Size, (void*)0);
gl.VertexAttribPointer(1, 4, VertexAttribPointerType.Float, true, ColorVertex.Size, (void*)12);
}
gl.EnableVertexAttribArray(0);
gl.EnableVertexAttribArray(1);

ebo = gl.GenBuffer();
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo);
gl.BufferData<int>(BufferTargetARB.ElementArrayBuffer, mesh.Indices, BufferUsageARB.StaticDraw);

uint vs = gl.CreateShader(ShaderType.VertexShader);
gl.ShaderSource(vs, VertexShaderCode);
gl.CompileShader(vs);
Expand Down Expand Up @@ -113,11 +143,15 @@ void main() {

gl.BindVertexArray(vao);
gl.UseProgram(shader);
gl.DrawArrays(PrimitiveType.Triangles, 0, 3);
unsafe
{
gl.DrawElements(GLEnum.Triangles, (uint)idxcount, GLEnum.UnsignedInt, (void*)0);
}


ImGui.DockSpaceOverViewport();
//ImGui.DockSpaceOverViewport();

ImGui.ShowDemoWindow();
//ImGui.ShowDemoWindow();

cont.Render(window);
};
Expand All @@ -137,10 +171,4 @@ void main() {
window.Run();
}
}

[StructLayout(LayoutKind.Sequential)]
internal record struct Vertex(Vector3 Position, byte R, byte G, byte B, byte A)
{
public static uint Size = 3 * 4 + 4;
}
}
15 changes: 15 additions & 0 deletions src/OpenPuppet/rendering/IVertex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace OpenPuppet.rendering
{
public interface IVertex<TSelf> where TSelf : IVertex<TSelf>
{
public static abstract uint Size { get; set; }
public static abstract List<TSelf> FromVec3(List<Vector3> vecs);
}
}
14 changes: 14 additions & 0 deletions src/OpenPuppet/rendering/VertexMesh.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenPuppet.rendering
{
public class VertexMesh<T>(List<T> verticies,List<int> indicies) where T : IVertex<T>
{
public T[] Verticies { get; } = verticies.ToArray();
public int[] Indices { get; } = indicies.ToArray();
}
}
25 changes: 25 additions & 0 deletions src/OpenPuppet/rendering/VertexTypes/ColorVertex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace OpenPuppet.rendering.VertexTypes
{
[StructLayout(LayoutKind.Sequential)]
internal unsafe record struct ColorVertex(Vector3 Position, Vector4 color) : IVertex<ColorVertex>
{
public static uint Size { get; set; } = (uint)sizeof(Vector3) + (uint)sizeof(Vector4);

public static List<ColorVertex> FromVec3(List<Vector3> vecs)
{
List<ColorVertex> vtx = new();

foreach (var item in vecs) vtx.Add(new(item,Vector4.One));

return vtx;
}
}
}
Loading
Loading