Shader läuft irgendwie

This commit is contained in:
mono 2019-02-19 02:30:48 +01:00
parent f22d60fa20
commit d17d609671
6 changed files with 73 additions and 20 deletions

View File

@ -108,6 +108,9 @@
<EmbeddedResource Include="JuicyGraphics\renderer\shaders\background.frag">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="JuicyGraphics\renderer\shaders\default.vert">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>

View File

@ -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<uint, string>()
{ { 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);
}
}
}

View File

@ -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);
}
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);
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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
/// </summary>
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 };