From d4c3e9f5fe107a62fe85ead347671f69ef457f3d Mon Sep 17 00:00:00 2001 From: ikpil Date: Tue, 21 Mar 2023 01:09:57 +0900 Subject: [PATCH] add sample silk --- src/DotRecast.Silk/DotRecast.Silk.csproj | 1 + src/DotRecast.Silk/SilkDemo.cs | 137 +++++++++++++++++++++-- 2 files changed, 126 insertions(+), 12 deletions(-) diff --git a/src/DotRecast.Silk/DotRecast.Silk.csproj b/src/DotRecast.Silk/DotRecast.Silk.csproj index 4735394..331d88b 100644 --- a/src/DotRecast.Silk/DotRecast.Silk.csproj +++ b/src/DotRecast.Silk/DotRecast.Silk.csproj @@ -3,6 +3,7 @@ Exe net7.0 + true diff --git a/src/DotRecast.Silk/SilkDemo.cs b/src/DotRecast.Silk/SilkDemo.cs index 8ed99bc..9b736a5 100644 --- a/src/DotRecast.Silk/SilkDemo.cs +++ b/src/DotRecast.Silk/SilkDemo.cs @@ -1,5 +1,8 @@ -using Serilog; +using System; +using Serilog; +using Silk.NET.Input; using Silk.NET.Maths; +using Silk.NET.OpenGL; using Silk.NET.Windowing; namespace DotRecast.Silk; @@ -9,16 +12,22 @@ public class SilkDemo private static readonly ILogger Logger = Log.ForContext(); private IWindow _win; - + private IInputContext _input; + private GL _gl; + private uint _vao; // vertex array object + private uint _vbo; // vertex buffer object + private uint _ebo; // + private uint _program; + public void Run() { Log.Logger.Information("running"); - + var options = WindowOptions.Default; options.Title = "silk demo"; options.Size = new Vector2D(1024, 768); options.VSync = false; - //options.ShouldSwapAutomatically = false; + options.ShouldSwapAutomatically = false; _win = Window.Create(options); _win.Closing += OnWindowClosing; @@ -27,37 +36,141 @@ public class SilkDemo _win.FramebufferResize += OnWindowFramebufferResize; _win.Update += OnWindowUpdate; _win.Render += OnWindowRender; - + _win.Run(); } private void OnWindowClosing() { - } - private void OnWindowLoad() + private unsafe void OnWindowLoad() { - + _input = _win.CreateInput(); + _gl = _win.CreateOpenGL(); + + Logger.Information($"{_win.API.Profile}"); + Logger.Information($"{_win.API.Version.MajorVersion} {_win.API.Version.MinorVersion}"); + + //_gl.ClearColor(Color.CornflowerBlue); + + // Create the VAO. + _vao = _gl.GenVertexArray(); + _gl.BindVertexArray(_vao); + + // The quad vertices data. + float[] vertices = + { + 0.5f, 0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + -0.5f, -0.5f, 0.0f, + -0.5f, 0.5f, 0.0f + }; + + // Create the VBO. + _vbo = _gl.GenBuffer(); + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, _vbo); + + // Upload the vertices data to the VBO. + fixed (float* buf = vertices) + _gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint) (vertices.Length * sizeof(float)), buf, BufferUsageARB.StaticDraw); + + // The quad indices data. + uint[] indices = + { + 0u, 1u, 3u, + 1u, 2u, 3u + }; + + // Create the EBO. + _ebo = _gl.GenBuffer(); + _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, _ebo); + + // Upload the indices data to the EBO. + fixed (uint* buf = indices) + _gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint) (indices.Length * sizeof(uint)), buf, BufferUsageARB.StaticDraw); + + const string vertexCode = @" +#version 330 core +layout (location = 0) in vec3 aPosition; +void main() +{ + gl_Position = vec4(aPosition, 1.0); +}"; + + const string fragmentCode = @" +#version 330 core +out vec4 out_color; +void main() +{ + out_color = vec4(1.0, 0.5, 0.2, 1.0); +}"; + + uint vertexShader = _gl.CreateShader(ShaderType.VertexShader); + _gl.ShaderSource(vertexShader, vertexCode); + + _gl.CompileShader(vertexShader); + + _gl.GetShader(vertexShader, ShaderParameterName.CompileStatus, out int vStatus); + if (vStatus != (int) GLEnum.True) + throw new Exception("Vertex shader failed to compile: " + _gl.GetShaderInfoLog(vertexShader)); + + uint fragmentShader = _gl.CreateShader(ShaderType.FragmentShader); + _gl.ShaderSource(fragmentShader, fragmentCode); + + _gl.CompileShader(fragmentShader); + + _gl.GetShader(fragmentShader, ShaderParameterName.CompileStatus, out int fStatus); + if (fStatus != (int) GLEnum.True) + throw new Exception("Fragment shader failed to compile: " + _gl.GetShaderInfoLog(fragmentShader)); + + _program = _gl.CreateProgram(); + + _gl.AttachShader(_program, vertexShader); + _gl.AttachShader(_program, fragmentShader); + + _gl.LinkProgram(_program); + + _gl.GetProgram(_program, ProgramPropertyARB.LinkStatus, out int lStatus); + if (lStatus != (int) GLEnum.True) + throw new Exception("Program failed to link: " + _gl.GetProgramInfoLog(_program)); + + _gl.DetachShader(_program, vertexShader); + _gl.DetachShader(_program, fragmentShader); + _gl.DeleteShader(vertexShader); + _gl.DeleteShader(fragmentShader); + + const uint positionLoc = 0; + _gl.EnableVertexAttribArray(positionLoc); + _gl.VertexAttribPointer(positionLoc, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), (void*) 0); + + _gl.BindVertexArray(0); + _gl.BindBuffer(BufferTargetARB.ArrayBuffer, 0); + _gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, 0); } + private void OnWindowResize(Vector2D size) { - } private void OnWindowFramebufferResize(Vector2D size) { - } private void OnWindowUpdate(double dt) { - } - private void OnWindowRender(double dt) + private unsafe void OnWindowRender(double dt) { + _gl.ClearColor(0.3f, 0.3f, 0.32f, 1.0f); + _gl.Clear((uint)GLEnum.ColorBufferBit | (uint)GLEnum.DepthBufferBit); + + _gl.BindVertexArray(_vao); + _gl.UseProgram(_program); + _gl.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, (void*) 0); + _win.SwapBuffers(); } } \ No newline at end of file