//using StgSharp.Envitronment.Win32; using StgSharp.Geometries; using StgSharp.Graphics; using StgSharp.Logic; using StgSharp.Math; using StgSharp.Math.Linear; using System; using System.Collections.Concurrent; using System.Diagnostics; using System.IO; using System.Numerics; using System.Runtime.InteropServices; using System.Text; namespace StgSharp { public class StgSharpDemoForm : Form { private Uniform color; private ElementBuffer ebo; private Uniform ourTexture; private Uniform ourTexture2; private ShaderProgram shader; private Texture texture; private VertexArray vao; private VertexBuffer vbo; public static unsafe void ProcessInput(IntPtr windowPtr) { if (InternalIO.glfwGetKey(windowPtr, (int)Operation.Key.ESCAPE) == KeyStatus.PRESS) { InternalIO.glfwSetWindowShouldClose(windowPtr, 1); } } public override void Deinit(object[] args) { /**/ vao.Dispose(); vbo.Dispose(); ebo.Dispose(); /**/ InternalIO.glfwTerminate(); } public unsafe void FormSizeCallback(IntPtr window, uint width, uint height) { gl.Viewport(0, 0, width, height); } public override void Init(object[] args) { InternalIO.InternalWriteLog("\n\n\n", "Program start", LogType.Info); Platform.InitGL(4, 6); SetRenderThreads(2); SetMetadata(800, 600, "Hello, STG#!", IntPtr.Zero, null); MakeAsCurrentContext(); SetSizeLimit(800, 600, 800, 600); this.LoadGLapi(null); FrameBufferSizeCallback = (IntPtr id, uint width, uint height) => { gl.Viewport(0, 0, width, height); }; shader = CreateShaderProgram(); var vertex = CreateShader(ShaderType.Vertex, 1); var fragment = CreateShader(ShaderType.Fragment, 1); vertex.Compile(0, File.ReadAllText("Shader\\vertex.vert")); vertex.AttachTo(0, shader); fragment.Compile(0, File.ReadAllText("Shader\\fragment.frag")); fragment.AttachTo(0, shader); shader.Link(); Triangle tri = new Triangle( -0.5f, -0.3f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.0f ); Rectangle rec = new Rectangle( -0.5f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f ); Polygon5 polygon = new Polygon5( -0.4f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 0.6f, -0.5f, 0.0f, 0, -1, 0, -0.7f, -0.5f, 0.0f ); vao = CreateVertexArray(1); vbo = CreateVertexBuffer(2); ebo = CreateElementBuffer(1); texture = CreateTexture(2); float[] texCoord = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f }; gl.BindVertexArray(vao[0]); ebo.SetValue(0, rec.Indices, BufferUsage.STATIC_DRAW); vbo.SetValue(0, rec.VertexBuffer, BufferUsage.STATIC_DRAW); gl.SetVertexAttribute(0, 3, TypeCode.Single, false, 4 * sizeof(float), 0); vbo.SetValueArray(1, texCoord, BufferUsage.STATIC_DRAW); gl.SetVertexAttribute(2, 2, TypeCode.Single, false, 2 * sizeof(float), 0); /**/ gl.BindBuffer(BufferType.ARRAY_BUFFER, glHandle.Zero); vao.BindNull(); /**/ texture.Bind2D(0); texture.Set2dWrapProperty(Wrap.Repeat, Wrap.Repeat); texture.Set2dFilterProperty(Filter.Nearest, Filter.Nearest); texture.LoadTexture(0, "Shader\\Image1.png", null); texture.Bind2D(1); texture.Set2dWrapProperty(Wrap.Repeat, Wrap.Repeat); texture.Set2dFilterProperty(Filter.Nearest, Filter.Nearest); texture.LoadTexture(1, "Shader\\awesomeface.png", null); //color = GetUniform(shader, "color"); shader.Use(); /**/ color = shader.GetUniform("color"); ourTexture = shader.GetUniform("ourTexture"); gl.SetValue(ourTexture, 0); ourTexture2 = shader.GetUniform("ourTexture2"); gl.SetValue(ourTexture2, 1); /**/ } public override void Main(params object[] args) { ProcessInput(windowID); gl.ClearColor(0.333f, 0.555f, 0.555f, 1.0f); //GL.api.Clear(GLconst.COLOR_BUFFER_BIT); gl.Clear(MaskBufferBit.COLOR); vao.Bind(0); shader.Use(); /**/ texture.Activate(TextureUnit.Unit0); texture.Bind2D(0); texture.Activate(TextureUnit.Unit1); texture.Bind2D(1); /**/ float time = (float)InternalIO.glfwGetTime(); gl.SetValue(color, 0.5f + MathF.Sin(time), 0.5f + MathF.Sin(time/* + 0.667f * 3.14f/**/), 0.5f + MathF.Sin(time/* - 0.667f * 3.14f/**/) ); vao.Bind(0); gl.DrawElements(GepmetryType.TRIANGLES, 6, TypeCode.UInt32, IntPtr.Zero); vao.BindNull(); InternalIO.glfwSwapBuffers(windowID); InternalIO.glfwPollEvents(); } } internal static class Program { internal static unsafe void Main(string[] args) { try { //Platform.TryCreatWindow(); /**/ StgSharpDemoForm demoForm = new StgSharpDemoForm(); demoForm.Init(); demoForm.Show(); /**/ /* GLparamQueue queue = new GLparamQueue(); Thread t = default; for (int i = 0; i < 100; i++) { M128 m = new M128(); m.Write(0, 1 + i); m.Write(1, 10 - i); m.Write(1, 511L * i); queue.Enqueue(m); if (i == 10) { t = new Thread(new ThreadStart( () => { for (int i = 0; i < 100; i++) { M128 m = default; queue.TryDequeue(out m); Console.WriteLine(m.ToString(10)); } } )); t.Start(); } } t.Join(); /**/ } catch (Exception ex) { InternalIO.InternalWriteLog( ex.Message + "\n" + ex.StackTrace, LogType.Error); } } [DllImport(InternalIO.SSC_libname, EntryPoint = "printShaderCode", CallingConvention = CallingConvention.Cdecl)] internal static extern unsafe void PrintShader(byte** shadercode); } }