27.09.2020

~ Moved Default-Shader to 2D to provied 2D-specific feature
~ Fixed UVs in Polygon
This commit is contained in:
Michel Fedde 2020-09-27 11:58:14 +02:00
parent 617a7ef044
commit 2aa12f8d25
19 changed files with 166 additions and 83 deletions

View file

@ -1,8 +1,6 @@
using SM.Base.Objects;
using SM.Base.Objects.Static;
using SM.Base.Scene;
using SM.Base.Shader;
using SM.OGL.Mesh;
namespace SM.Base
{

View file

@ -7,7 +7,7 @@ namespace SM.Base.Scene
public abstract class DrawingBasis : IShowItem
{
protected Material _material = new Material();
protected GenericMesh _mesh = Plate.Object;
protected GenericMesh _mesh = Defaults.DefaultMesh;
public virtual void Update(UpdateContext context)
{

View file

@ -1,5 +1,4 @@
using OpenTK.Graphics;
using SM.Base.Shader;
using SM.OGL.Texture;
namespace SM.Base.Scene
@ -9,6 +8,6 @@ namespace SM.Base.Scene
public TextureBase Texture;
public Color4 Tint = Color4.White;
public IShader Shader = Shaders.Default;
public IShader Shader = Defaults.DefaultShader;
}
}

View file

@ -58,8 +58,6 @@
<Compile Include="Drawing\Material.cs" />
<Compile Include="Scene\IBackgroundItem.cs" />
<Compile Include="Scene\GenericItemCollection.cs" />
<Compile Include="Shader\InstanceShader.cs" />
<Compile Include="Shader\Shaders.cs" />
<Compile Include="Textures\Texture.cs" />
<Compile Include="Text\CharParameter.cs" />
<Compile Include="Text\Font.cs" />
@ -69,6 +67,10 @@
<Compile Include="Types\Vector2.cs" />
<Compile Include="Types\Vector3.cs" />
<Compile Include="Types\Vector4.cs" />
<Compile Include="Utility\Assembly.cs" />
<Compile Include="Utility\BezierCurve.cs" />
<Compile Include="Utility\Deltatime.cs" />
<Compile Include="Utility\Randomize.cs" />
<Compile Include="Window\Contexts\DrawContext.cs" />
<Compile Include="Window\Contexts\UpdateContext.cs" />
<Compile Include="Window\GenericWindow.cs" />
@ -80,8 +82,6 @@
<ItemGroup>
<None Include="OpenTK.dll.config" />
<None Include="packages.config" />
<EmbeddedResource Include="Shader\Files\default.frag" />
<EmbeddedResource Include="Shader\Files\default.vert" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SM.OGL\SM.OGL.csproj">

View file

@ -1,39 +0,0 @@
using System;
using System.Collections.Generic;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.OGL.Shaders;
namespace SM.Base.Shader
{
public class InstanceShader : GenericShader, IShader
{
protected override bool AutoCompile { get; } = true;
public Action<UniformCollection, DrawContext, int> SetUniformVertex;
public Action<UniformCollection, DrawContext> SetUniformFragment;
public InstanceShader(string vertex, string fragment) : base(new ShaderFileCollection(vertex, fragment))
{
}
public void Draw(DrawContext context)
{
GL.UseProgram(this);
Uniforms["MVP"].SetMatrix4(context.View * context.World);
Uniforms["HasVColor"].SetUniform1(context.Mesh.AttribDataIndex.ContainsKey(3) && context.Mesh.AttribDataIndex[3] != null);
for (int i = 0; i < context.Instances.Length; i++) SetUniformVertex?.Invoke(Uniforms, context, i);
SetUniformFragment?.Invoke(Uniforms, context);
DrawObject(context.Mesh, context.Instances.Length, true);
CleanUp();
GL.UseProgram(0);
}
}
}

View file

@ -1,27 +0,0 @@
using System.IO;
using System.Reflection;
using OpenTK.Graphics.OpenGL4;
using SM.OGL.Shaders;
namespace SM.Base.Shader
{
public class Shaders
{
public static InstanceShader Default = new InstanceShader(new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("SM.Base.Shader.Files.default.vert")).ReadToEnd(),
new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("SM.Base.Shader.Files.default.frag")).ReadToEnd())
{
SetUniformFragment = (u, context) =>
{
u["Tint"].SetUniform4(context.Material.Tint);
u["Texture"].SetTexture(context.Material.Texture, 0, u["UseTexture"]);
},
SetUniformVertex = (u, context, i) =>
{
GL.UniformMatrix4(u["ModelMatrix"] + i, false, ref context.Instances[i].ModelMatrix);
GL.Uniform2(u["TextureOffset"] + i, context.Instances[i].TexturePosition);
GL.Uniform2(u["TextureScale"] + i, context.Instances[i].TextureScale);
}
};
}
}

View file

@ -0,0 +1,29 @@
using System;
using System.IO;
using System.Reflection;
namespace SM.Utility
{
public class AssemblyUtility
{
/// <summary>
/// Read a file that is saved in a assembly
/// </summary>
/// <param name="ass">The assembly that contains the file</param>
/// <param name="path">The path to the file inside the assembly</param>
/// <returns></returns>
public static string ReadAssemblyFile(Assembly ass, string path) { return new StreamReader(GetAssemblyStream(ass, path)).ReadToEnd(); }
/// <summary>
/// Read a file that is saved in the calling assembly
/// </summary>
/// <param name="path">The path to the file inside the assembly</param>
/// <returns></returns>
public static string ReadAssemblyFile(string path) { return ReadAssemblyFile(Assembly.GetCallingAssembly(), path); }
public static Stream GetAssemblyStream(Assembly ass, string path) { return ass.GetManifestResourceStream(ass.GetName().Name + "." + path) ?? throw new InvalidOperationException("Assembly couldn't find resource at path '" + path + "'."); }
public static Stream GetAssemblyStream(string path) { return GetAssemblyStream(Assembly.GetCallingAssembly(), path); }
}
}

View file

@ -0,0 +1,23 @@
using System;
namespace SM.Utility
{
public class BezierCurve
{
public static float Calculate(float t, params float[] points)
{
int pointAmount = points.Length;
int itterations = pointAmount - 1;
double x = Math.Pow(1 - t, itterations) * points[0];
for (int i = 1; i < itterations; i++)
{
if (i % 2 == 0) x += itterations * (1 - t) * Math.Pow(t, itterations - 1) * points[i];
else x += itterations * Math.Pow(1 - t, itterations - 1) * t * points[i];
}
x += Math.Pow(t, itterations) * points[pointAmount - 1];
return (float)x;
}
}
}

View file

@ -0,0 +1,21 @@
namespace SM.Utility
{
public class Deltatime
{
public static float UpdateDelta { get; internal set; }
public static float RenderDelta { get; internal set; }
public bool UseRender;
public float Scale;
public float DeltaTime => (UseRender ? RenderDelta : UpdateDelta) * Scale;
public Deltatime(float scale = 1, bool useRender = false)
{
UseRender = useRender;
Scale = scale;
}
}
}

View file

@ -0,0 +1,22 @@
using System;
namespace SM.Utility
{
public class Randomize
{
public static Random Randomizer = new Random();
public static void SetSeed(int seed) { Randomizer = new Random(seed); }
public static bool GetBool(float tolerance) { return Randomizer.NextDouble() > tolerance; }
public static int GetInt() { return Randomizer.Next(); }
public static int GetInt(int max) { return Randomizer.Next(max); }
public static int GetInt(int min, int max) { return Randomizer.Next(min, max); }
public static float GetFloat() { return (float)Randomizer.NextDouble(); }
public static float GetFloat(float max) { return (float)Randomizer.NextDouble() * max; }
public static float GetFloat(float min, float max) { return (float)Randomizer.NextDouble() * max + min; }
}
}

View file

@ -24,7 +24,7 @@ namespace SM.OGL.Mesh
for (int i = 0; i < 2; i++)
{
Min[i] = Math.Min(Min[i], vector[i]);
Max[i] = Math.Max(Min[i], vector[i]);
Max[i] = Math.Max(Max[i], vector[i]);
}
}

View file

@ -1,11 +1,13 @@
using System.Drawing;
using OpenTK;
using OpenTK.Graphics;
using SM.Base;
using SM.Base.Contexts;
using SM.Base.Objects.Static;
using SM.Base.Scene;
using SM.Base.Textures;
using SM.OGL.Texture;
using SM2D.Shader;
using SM2D.Types;
namespace SM2D.Drawing
@ -48,6 +50,8 @@ namespace SM2D.Drawing
public void Draw(DrawContext context)
{
if (_material.Shader == null) _material.Shader = Defaults.DefaultShader;
context.Material = _material;
context.Mesh = Plate.Object;

View file

@ -8,7 +8,7 @@ namespace SM2D.Drawing
{
public class DrawColor : DrawingBasis<Transformation>, I2DShowItem
{
public Color4 Tint
public Color4 Color
{
get => _material.Tint;
set => _material.Tint = value;

View file

@ -3,6 +3,7 @@ using OpenTK;
using OpenTK.Graphics.OpenGL4;
using SM.Base;
using SM2D.Scene;
using SM2D.Shader;
namespace SM2D
{
@ -12,6 +13,8 @@ namespace SM2D
protected override void OnLoad(EventArgs e)
{
Defaults.DefaultShader = new Default2DShader();
base.OnLoad(e);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);

View file

@ -59,6 +59,7 @@
<Compile Include="Scene\I2DShowItem.cs" />
<Compile Include="Scene\ItemCollection.cs" />
<Compile Include="Scene\Scene.cs" />
<Compile Include="Shader\Default2DShader.cs" />
<Compile Include="Types\Transformation.cs" />
</ItemGroup>
<ItemGroup>
@ -74,6 +75,8 @@
<ItemGroup>
<None Include="OpenTK.dll.config" />
<None Include="packages.config" />
<EmbeddedResource Include="Shader\ShaderFiles\default.frag" />
<EmbeddedResource Include="Shader\ShaderFiles\default.vert" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

View file

@ -0,0 +1,47 @@
using OpenTK.Graphics.OpenGL4;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.OGL.Shaders;
using SM.Utility;
namespace SM2D.Shader
{
public class Default2DShader : GenericShader, IShader
{
protected override bool AutoCompile { get; } = true;
public Default2DShader() : base(new ShaderFileCollection(
AssemblyUtility.ReadAssemblyFile("Shader.ShaderFiles.default.vert"),
AssemblyUtility.ReadAssemblyFile("Shader.ShaderFiles.default.frag")))
{
}
public void Draw(DrawContext context)
{
GL.UseProgram(this);
GL.BindVertexArray(context.Mesh);
// Vertex Uniforms
Uniforms["MVP"].SetMatrix4(context.View * context.World);
Uniforms["HasVColor"].SetUniform1(context.Mesh.AttribDataIndex.ContainsKey(3) && context.Mesh.AttribDataIndex[3] != null);
for (int i = 0; i < context.Instances.Length; i++)
{
GL.UniformMatrix4(Uniforms["ModelMatrix"] + i, false, ref context.Instances[i].ModelMatrix);
GL.Uniform2(Uniforms["TextureOffset"] + i, context.Instances[i].TexturePosition);
GL.Uniform2(Uniforms["TextureScale"] + i, context.Instances[i].TextureScale);
}
// Fragment Uniforms
Uniforms["Tint"].SetUniform4(context.Material.Tint);
Uniforms["Texture"].SetTexture(context.Material.Texture, Uniforms["UseTexture"]);
DrawObject(context.Mesh, context.Instances.Length);
CleanUp();
GL.UseProgram(0);
}
}
}

View file

@ -24,7 +24,7 @@ namespace SM_TEST
FontSize = 32
};
GLWindow2D window = new GLWindow2D {Scaling = new Vector2(0, 500)};
GLWindow2D window = new GLWindow2D {Scaling = new Vector2(0, 1000)};
window.SetScene(scene = new Scene());
window.Load += WindowOnLoad;
window.UpdateFrame += WindowOnUpdateFrame;
@ -50,7 +50,7 @@ namespace SM_TEST
{
ZIndex = 1
});
col.Objects.Add(new DrawColor(Color4.Aqua)
col.Objects.Add(new DrawColor(Color4.Black)
{
Transform = { Rotation = 45, Position = new SM.Base.Types.Vector2(0, 25) },
});
@ -82,8 +82,8 @@ namespace SM_TEST
new PolygonVertex(new Vector2(1, .75f), Color4.White),
new PolygonVertex(new Vector2(.75f, 1), Color4.White),
new PolygonVertex(new Vector2(.25f, 1), Color4.White),
new PolygonVertex(new Vector2(0, .75f), Color4.Gray),
new PolygonVertex(new Vector2(0, .25f), Color4.Gray)
new PolygonVertex(new Vector2(0, .75f), new Color4(10,10,10,255)),
new PolygonVertex(new Vector2(0, .25f), new Color4(10,10,10,255))
}), Color4.LawnGreen)
{
Transform = {Position = new SM.Base.Types.Vector2(50,0)}
@ -96,8 +96,8 @@ namespace SM_TEST
new PolygonVertex(new Vector2(1, .75f), Color4.White),
new PolygonVertex(new Vector2(.75f, 1), Color4.White),
new PolygonVertex(new Vector2(.25f, 1), Color4.White),
new PolygonVertex(new Vector2(0, .75f), Color4.Gray),
new PolygonVertex(new Vector2(0, .25f), Color4.Gray)
new PolygonVertex(new Vector2(0, .75f), new Color4(10,10,10,255)),
new PolygonVertex(new Vector2(0, .25f), new Color4(10,10,10,255))
}), new Bitmap("soldier_logo.png"))
{
Transform = {Position = new SM.Base.Types.Vector2(-50,0)}