using StgSharp.Geometries; using StgSharp.Graphics; using StgSharp.Logic; using StgSharp.Math; using StgSharp.Math.Linear; using System; using System.Diagnostics; using System.IO; using System.Numerics; using System.Runtime.InteropServices; using System.Text; namespace StgSharp { public class StgSharpDemoForm : Form { Uniform color; ElementBuffer ebo; Uniform ourTexture; Uniform ourTexture2; Shader shader; Texture texture; VertexArray vao; VertexBuffer vbo; public static unsafe void ProcessInput(IntPtr windowPtr) { if (InternalIO.glfw.GetKey(windowPtr, (int)Operation.Key.ESCAPE) == KeyStatus.PRESS) { InternalIO.glfw.SetWindowShouldClose(windowPtr, 1); } } public override void Deinit(object[] args) { /**/ vao.Dispose(); vbo.Dispose(); ebo.Dispose(); /**/ InternalIO.glfw.Terminate(); } 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(1); SetMetadata(800, 600, "Hello, STG#!", IntPtr.Zero, null); MakeAsCurrentContext(); SetSizeLimit(800, 600, 800, 600); FrameBufferSizeCallback = (IntPtr id, uint width, uint height) => { GL.Viewport(0, 0, width, height); }; this.LoadGLapi(null); shader = CreateShader(ShaderType.Program, 1); var vertex = CreateShader(ShaderType.Vertex, 1); var fragment = CreateShader(ShaderType.Fragment, 1); vertex.Compile(File.ReadAllText("Shader\\vertex.vert")); fragment.Compile(File.ReadAllText("Shader\\fragment.frag")); shader.Link(0); 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, typeof(float), false, 4 * sizeof(float), 0); vbo.SetValueArray(1, texCoord, BufferUsage.STATIC_DRAW); gl.SetVertexAttribute(2, 2, typeof(float), false, 2 * sizeof(float), 0); /**/ gl.BindBuffer(BufferType.ARRAY_BUFFER, 0); 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.glfw.GetTime(); 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, typeof(uint), IntPtr.Zero); vao.BindNull(); InternalIO.glfw.SwapBuffers(windowID); InternalIO.glfw.PollEvents(); } } internal static class Program { internal unsafe static void Main(string[] args) { try { StgSharpDemoForm demoForm = new StgSharpDemoForm(); demoForm.Init(); demoForm.Show(); } catch (Exception ex) { InternalIO.InternalWriteLog( ex.Message + "\n" + ex.StackTrace, LogType.Error); } } [DllImport(InternalIO.SSC_libname, EntryPoint = "printShaderCode", CallingConvention = CallingConvention.Cdecl)] internal extern unsafe static void PrintShader(byte** shadercode); } }