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 calls = new List(); 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, window.Size.X, 0, window.Size.Y, 1, -1); GL.UniformMatrix4(loc, false, ref matrix); context.Draw.Line( new QuikLine() { Start = new QuikVec2() { X = 20, Y = 40 }, End = new QuikVec2() { X=100, Y=100 } }); 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(); } } } }