Dashboard/QuikTestApplication/Program.cs

181 lines
5.7 KiB
C#

using System;
using System.Collections.Generic;
using Quik;
using Quik.VertexGenerator;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework;
namespace QuikTestApplication
{
public class Program
{
public const string vertex =
@"#version 140
uniform mat4 matrix;
in vec2 position;
in vec2 texcoord;
in vec4 color;
out vec2 ftexcoord;
out vec4 fcolor;
void main()
{
fcolor = color;
ftexcoord = texcoord;
gl_Position = matrix * vec4(position.xy, 0, 1);
}
";
public const string fragment =
@"#version 140
in vec2 ftexcoord;
in vec4 fcolor;
out vec4 outcolor;
void main()
{
outcolor = fcolor;
}
";
public static void Main(string[] args)
{
NativeWindowSettings windowSettings = NativeWindowSettings.Default;
windowSettings.NumberOfSamples = 4;
NativeWindow window = new NativeWindow(windowSettings);
window.Context.MakeCurrent();
GL.LoadBindings(new GLFWBindingsContext());
QuikContext context = new QuikContext();
QuikVertexGenerator gen = new QuikVertexGenerator(context);
GL.Enable(EnableCap.Multisample);
int sp;
{
int vs, fs;
sp = GL.CreateProgram();
vs = GL.CreateShader(ShaderType.VertexShader);
fs = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(vs, vertex);
GL.CompileShader(vs);
GL.ShaderSource(fs, fragment);
GL.CompileShader(fs);
GL.AttachShader(sp, vs);
GL.AttachShader(sp, fs);
GL.LinkProgram(sp);
GL.UseProgram(sp);
}
int vbo, ebo, vao;
vbo = GL.GenBuffer();
ebo = GL.GenBuffer();
vao = GL.GenVertexArray();
GL.BindVertexArray(vao);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, ebo);
int loc;
GL.VertexAttribPointer(
loc = GL.GetAttribLocation(sp, "position"),
2,
VertexAttribPointerType.Float,
false,
QuikVertex.Stride,
QuikVertex.PositionOffset);
GL.EnableVertexAttribArray(loc);
GL.VertexAttribPointer(
loc = GL.GetAttribLocation(sp, "texcoords"),
2,
VertexAttribPointerType.Float,
false,
QuikVertex.Stride,
QuikVertex.TextureCoordinatesOffset);
GL.EnableVertexAttribArray(loc);
GL.VertexAttribPointer(
loc = GL.GetAttribLocation(sp, "color"),
4,
VertexAttribPointerType.UnsignedByte,
true,
QuikVertex.Stride,
QuikVertex.ColorOffset);
GL.EnableVertexAttribArray(loc);
loc = GL.GetUniformLocation(sp, "matrix");
List<QuikDrawCall> calls = new List<QuikDrawCall>();
for (;!window.IsExiting;)
{
NativeWindow.ProcessWindowEvents(false);
GL.Viewport(0, 0, window.Size.X, window.Size.Y);
GL.ClearColor(1,1,1,1);
GL.Clear(ClearBufferMask.ColorBufferBit);
Matrix4 matrix = Matrix4.CreateOrthographicOffCenter(
0,
(float)window.Size.X / 2,
0,
(float)window.Size.Y / 2,
1,
-1);
GL.UniformMatrix4(loc, false, ref matrix);
context.DefaultStroke.Color = new QuikColor(40, 128, 60, 255);
context.Draw.Line(
// >
new QuikLine(10,10, 20, 20),
new QuikLine(20, 20, 10, 30),
// s
new QuikLine(28, 10, 38, 10),
new QuikLine(38, 10, 38, 20),
new QuikLine(38, 20, 28, 20),
new QuikLine(28, 20, 28, 30),
new QuikLine(28, 30, 38, 30),
// e
new QuikLine(64, 20, 74, 20),
new QuikLine(74, 20, 74, 30),
new QuikLine(74, 30, 64, 30),
new QuikLine(64, 30, 64, 10),
new QuikLine(64, 10, 74, 10)
);
QuikCommand command;
while (context.Draw.Commands.TryDequeue(out command))
{
QuikDrawCall? call = gen.ConsumeCommand(command);
if (call.HasValue) calls.Add(call.Value);
}
GL.BufferData(BufferTarget.ArrayBuffer, gen.VertexCount * QuikVertex.Stride, ref gen.VertexBuffer[0], BufferUsageHint.StreamDraw);
GL.BufferData(BufferTarget.ElementArrayBuffer, gen.ElementCount * 2, ref gen.ElementBuffer[0], BufferUsageHint.StreamDraw);
foreach (QuikDrawCall call in calls)
{
GL.DrawElements(BeginMode.Triangles, call.Count, DrawElementsType.UnsignedShort, call.Offset);
}
gen.Clear();
calls.Clear();
window.Context.SwapBuffers();
}
}
}
}