From d17d6096714ac0c43ad9bed44935c85f71ce76ee Mon Sep 17 00:00:00 2001 From: mono Date: Tue, 19 Feb 2019 02:30:48 +0100 Subject: [PATCH] =?UTF-8?q?Shader=20l=C3=A4uft=20irgendwie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/JuicyGraphics.csproj | 3 ++ .../graphicalObjects/gridBackground.cs | 38 +++++++++++++++---- .../renderer/shaders/background.frag | 19 +++++++--- .../renderer/shaders/default.vert | 10 +++++ src/JuicyGraphics/ui/canvas.cs | 7 +++- src/SharpGL/Shaders/Shader.cs | 16 ++++++-- 6 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 src/JuicyGraphics/renderer/shaders/default.vert diff --git a/src/JuicyGraphics.csproj b/src/JuicyGraphics.csproj index 537ed06..3b59ab8 100644 --- a/src/JuicyGraphics.csproj +++ b/src/JuicyGraphics.csproj @@ -108,6 +108,9 @@ PreserveNewest + + PreserveNewest + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/src/JuicyGraphics/renderer/graphicalObjects/gridBackground.cs b/src/JuicyGraphics/renderer/graphicalObjects/gridBackground.cs index ebd1ee4..c4237e4 100644 --- a/src/JuicyGraphics/renderer/graphicalObjects/gridBackground.cs +++ b/src/JuicyGraphics/renderer/graphicalObjects/gridBackground.cs @@ -5,20 +5,42 @@ using SharpGL; using SharpGL.Enumerations; using System.Reflection; using System.IO; +using System.Collections.Generic; +using System; +using SharpGL.VertexBuffers; namespace Graphics.Objects { class gridBackground : iGraphicalObject { + + VertexBuffer vbo = new VertexBuffer(); + VertexBufferArray vao = new VertexBufferArray(); + ShaderProgram gridRender = new ShaderProgram(); + + public gridBackground(renderCam context) { + vao.Create(context.GL); + vao.Bind(context.GL); + gridRender.Create(context.GL, Shader.FromFile("default.vert"), + Shader.FromFile("background.frag"), new Dictionary() + { { 0, "position" } }); + vbo.Create(context.GL); + vbo.Bind(context.GL); + vbo.SetData(context.GL, 0, new float[] { -1.0f, -1.0f, + 1.0f, -1.0f, + 1.0f, 1.0f, + -1.0f, 1.0f }, false, 2); + vao.Unbind(context.GL); + } + const double realTileSize = 24.0f; public void render(renderCam context) { - Shader renderGrid = new Shader(); - renderGrid.Create(context.GL, OpenGL.GL_FRAGMENT_SHADER, "background.frag"); - context.GL.Begin(BeginMode.Quads); - context.GL.Vertex(-1.0, -1.0); - context.GL.Vertex(1.0, -1.0); - context.GL.Vertex(1.0, 1.0); - context.GL.Vertex(-1.0, 1.0); - context.GL.End(); + gridRender.Bind(context.GL); + + vao.Bind(context.GL); + context.GL.DrawArrays(OpenGL.GL_QUADS, 0, 8); + + vao.Unbind(context.GL); + gridRender.Unbind(context.GL); } } } diff --git a/src/JuicyGraphics/renderer/shaders/background.frag b/src/JuicyGraphics/renderer/shaders/background.frag index 93e0a7f..bc6a679 100644 --- a/src/JuicyGraphics/renderer/shaders/background.frag +++ b/src/JuicyGraphics/renderer/shaders/background.frag @@ -1,8 +1,15 @@ -#version 330 core -out vec4 FragColor; - -in vec4 vertexColor; +#version 330 + +in vec2 texCoord; + +out vec4 fragColor; void main() { - FragColor = vec4(1.0, 0.0, 0.0, 1.0); -} \ No newline at end of file + if (floor(mod(texCoord.x * 48.0, 2)) != floor(mod(texCoord.y * 27.0, 2))) { + fragColor = vec4(0.9, 0.9, 0.9, 1.0); + } + else { + fragColor = vec4(0.8, 0.8, 0.8, 1.0); + } + + } \ No newline at end of file diff --git a/src/JuicyGraphics/renderer/shaders/default.vert b/src/JuicyGraphics/renderer/shaders/default.vert new file mode 100644 index 0000000..15105ad --- /dev/null +++ b/src/JuicyGraphics/renderer/shaders/default.vert @@ -0,0 +1,10 @@ +#version 330 + +in vec2 position; + +out vec2 texCoord; + +void main() { + texCoord = position * 0.5 + vec2(0.5, 0.5); + gl_Position = vec4(position, 0.0, 1.0); + } \ No newline at end of file diff --git a/src/JuicyGraphics/ui/canvas.cs b/src/JuicyGraphics/ui/canvas.cs index b9af5bc..c904685 100644 --- a/src/JuicyGraphics/ui/canvas.cs +++ b/src/JuicyGraphics/ui/canvas.cs @@ -11,6 +11,7 @@ using System.Numerics; using System.Drawing; namespace JuicyGraphics { + class canvas : Control, ISupportInitialize { OpenGL GL = new OpenGL(); @@ -52,7 +53,7 @@ namespace JuicyGraphics { return; } - gridBackground gBg = new gridBackground(); + gridBackground gBg; protected override void OnPaint(PaintEventArgs e) { if (renderingForDesigner()) { @@ -61,13 +62,15 @@ namespace JuicyGraphics { InitializeOpenGL(); } GL.MakeCurrent(); - GL.ClearColor(0.9f, 0.9f, 0.9f, 1f); + GL.ClearColor(0.1f, 0.9f, 0.9f, 1f); GL.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); GL.LoadIdentity(); if (rc != null) { GL.Color(0.8, 0.8, 0.8); + if (gBg == null) + gBg = new gridBackground(rc); gBg.render(rc); } diff --git a/src/SharpGL/Shaders/Shader.cs b/src/SharpGL/Shaders/Shader.cs index 23fefc7..3c80a3e 100644 --- a/src/SharpGL/Shaders/Shader.cs +++ b/src/SharpGL/Shaders/Shader.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Reflection; using System.Text; +using System.Windows.Forms; namespace SharpGL.Shaders { @@ -13,15 +14,14 @@ namespace SharpGL.Shaders /// public class Shader { - public void Create(OpenGL gl, uint shaderType, string resourceFile) + public void Create(OpenGL gl, uint shaderType, string source) { // Create the OpenGL shader object. shaderObject = gl.CreateShader(shaderType); // Set the shader source. - using (var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format("JuicyGraphics.JuicyGraphics.renderer.shaders.{0}", resourceFile)))) { - gl.ShaderSource(shaderObject, reader.ReadToEnd()); - } + + gl.ShaderSource(shaderObject, source); // Compile the shader object. @@ -31,6 +31,8 @@ namespace SharpGL.Shaders // going to throw an exception. if (GetCompileStatus(gl) == false) { + MessageBox.Show(GetInfoLog(gl)); + System.Environment.Exit(1); throw new ShaderCompilationException(string.Format("Failed to compile shader with ID {0}.", shaderObject), GetInfoLog(gl)); } } @@ -41,6 +43,12 @@ namespace SharpGL.Shaders shaderObject = 0; } + public static string FromFile(string resourceFile) { + using (var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format("JuicyGraphics.JuicyGraphics.renderer.shaders.{0}", resourceFile)))) { + return reader.ReadToEnd(); + } + } + public bool GetCompileStatus(OpenGL gl) { int[] parameters = new int[] { 0 };