Dashboard/QuikTestApplication/Program.cs

232 lines
7.4 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>();
int i = 0;
QuikStrokeStyle strokeBorder = new QuikStrokeStyle()
{
Color = new QuikColor(0xaaaaaaff),
Width = 8
};
QuikStrokeStyle strokeNoBorder = new QuikStrokeStyle()
{
Color = new QuikColor(0xaaaaaaff),
Width = 0
};
QuikFillStyle fill = new QuikFillStyle()
{
Color = new QuikColor(0xeeeeeeff)
};
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.Draw.Commands.Enqueue(
new QuikCommandRectangle(new QuikRectangle(120, 60, 20, 20))
{
CornerRadius = 0,
FillStyle = fill,
StrokeStyle = strokeNoBorder
});
context.Draw.Commands.Enqueue(
new QuikCommandRectangle(new QuikRectangle(240, 60, 140, 20))
{
CornerRadius = 4,
FillStyle = fill,
StrokeStyle = strokeNoBorder
});
context.Draw.Commands.Enqueue(
new QuikCommandRectangle(new QuikRectangle(360, 60, 260, 20))
{
CornerRadius = 15,
FillStyle = fill,
StrokeStyle = strokeNoBorder
});
context.Draw.Commands.Enqueue(
new QuikCommandRectangle(new QuikRectangle(120, 120, 20, 80))
{
CornerRadius = 0,
FillStyle = fill,
StrokeStyle = strokeBorder
});
context.Draw.Commands.Enqueue(
new QuikCommandRectangle(new QuikRectangle(240, 120, 140, 80))
{
CornerRadius = 4,
FillStyle = fill,
StrokeStyle = strokeBorder
});
context.Draw.Commands.Enqueue(
new QuikCommandRectangle(new QuikRectangle(360, 120, 260, 80))
{
CornerRadius = 15,
FillStyle = fill,
StrokeStyle = strokeBorder
});
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();
if (++i % 60 == 0)
{
Console.WriteLine("Vertex Usage: {0} ; Element Usage: {1}", gen.VertexUsage, gen.ElementUsage);
}
window.Context.SwapBuffers();
}
}
}
}