//----------------------------------------------------------------------- //----------------------------------------------------------------------- // file="MainRenderStream.cs" // Project: StgSharp // AuthorGroup: Nitload Space // Copyright (c) Nitload Space. All rights reserved. // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the “Software”), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, // and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and // this permission notice shall be included in all copies // or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED “AS IS”, // WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH // THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // //----------------------------------------------------------------------- //----------------------------------------------------------------------- using StgSharp; using StgSharp.Geometries; using StgSharp.Graphics; using StgSharp.Logic; using StgSharp.Math; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StgSharp { public class MainRenderStream : glRenderStream { private ElementBuffer ebo; private ShaderProgram shader; private Texture texture; private Uniform color; private Uniform ourTexture; private Uniform ourTexture2; private VertexArray vao; private VertexBuffer vbo; public MainRenderStream(RenderStreamConstructArgs args) : base(args) { } protected override string[] InputPortsName => ["input_1"]; protected override string[] OutputPortsName => ["output_1"]; protected override void Deinit() { /**/ vao.Dispose(); vbo.Dispose(); ebo.Dispose(); /**/ } protected override sealed void Init() { SetSizeLimit(800, 800, 800, 800); FrameSizeCallback = (IntPtr id, uint width, uint height) => GL.Viewport(0, 0, width, height); shader = CreateShaderProgram(); Shader vertex = CreateShaderSegment(ShaderType.Vertex, 1); Shader fragment = CreateShaderSegment(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.3f, 0.3f, 130.0f, 0.3f, 0.3f, 130.0f, 0.3f, -0.3f, 130.0f, -0.3f, -0.3f, 130.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 }; vao.Bind(0); ebo.SetValue(0, rec.Indices, BufferUsage.STATIC_DRAW); vbo.WriteMatrixData(0, rec.VertexBuffer, BufferUsage.STATIC_DRAW); vao.SetVertexAttribute(0, 3, TypeCode.Single, false, 4 * sizeof(float), 0); vbo.WriteScalerData(1, texCoord, BufferUsage.STATIC_DRAW); vao.SetVertexAttribute(2, 2, TypeCode.Single, false, 2 * sizeof(float), 0); /**/ vbo.Unbind(); 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); shader.Use(); /**/ color = shader.GetUniform("color"); ourTexture = shader.GetUniform("ourTexture"); GL.SetValue(ourTexture, 0); ourTexture2 = shader.GetUniform("ourTexture2"); GL.SetValue(ourTexture2, 1); /**/ NativeCameraViewTarget( new vec3d(0, 0, 200), new vec3d(0, 0, 100), new vec3d(0, 1, 0) ); NativeCameraViewRange(new Degree(10f), (0, 0), (50f, 50f)); NativeCameraUniform(shader, "camera"); } protected override sealed void Main( in Dictionary input, in Dictionary output ) { GL.ClearColor(0.333f, 0.555f, 0.555f, 1.0f); //Console.WriteLine(1); GL.Clear(MaskBufferBit.COLOR /**/| MaskBufferBit.DEPTH /**/ ); /**/ vao.Bind(0); shader.Use(); /**/ /**/ texture.Activate(TextureUnit.Unit0); texture.Bind2D(0); texture.Activate(TextureUnit.Unit1); texture.Bind2D(1); /**/ /**/ float time = /* CurrentFrameCount, / /**/ 60.0f; GL.SetValue(color, 0.5f + MathF.Cos(time), 0.5f + MathF.Sin((time * 0.66667f) + ((2 * Scaler.Pi) / 3)), 0.5f + MathF.Sin((time * 0.33333f) + (Scaler.Pi / 3)), time ); NativeCamera.SetAllUniforms(); /**/ //Console.WriteLine(NativeCamera.Projection); GL.DrawElements(GeometryType.TRIANGLES, 6, TypeCode.UInt32, IntPtr.Zero); vao.BindNull(); /**/ BlueprintPipeline.SkipAll(output); PollEvents(); SwapBuffers(); } protected override void ProcessInput() { float movement = 0.005f; if (InternalIO.glfwGetKey(CanvasHandle, (int)KeyboardKey.ESCAPE) == KeyStatus.PRESS) { InternalIO.glfwSetWindowShouldClose(CanvasHandle, 1); return; } if (InternalIO.glfwGetKey(CanvasHandle, (int)KeyboardKey.LeftShift) == KeyStatus.PRESS) { movement = 0.002f; } if (InternalIO.glfwGetKey(this.CanvasHandle, (int)KeyboardKey.UP) == KeyStatus.PRESS) { //Console.WriteLine(8); NativeCamera.Pitch(new Radius(movement)); } else if (InternalIO.glfwGetKey(this.CanvasHandle, (int)KeyboardKey.DOWN) == KeyStatus.PRESS) { //Console.WriteLine(2); NativeCamera.Pitch(new Radius(-movement)); } if (InternalIO.glfwGetKey(CanvasHandle, GLconst.GLFW_KEY_LEFT) == KeyStatus.PRESS) { //Console.WriteLine(4); NativeCamera.Yaw(new Radius(-movement)); } else if (InternalIO.glfwGetKey(CanvasHandle, GLconst.GLFW_KEY_RIGHT) == KeyStatus.PRESS) { //Console.WriteLine(6); NativeCamera.Yaw(new Radius(movement)); } } } }