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,15 +0,0 @@
#version 330
in vec2 vTexture;
in vec4 vColor;
uniform vec4 Tint;
uniform bool UseTexture;
uniform sampler2D Texture;
layout(location = 0) out vec4 color;
void main() {
color = vColor * Tint;
if (UseTexture) color *= texture(Texture, vTexture);
}

View file

@ -1,23 +0,0 @@
#version 330
#define maxInstances 32
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec2 aTex;
layout(location = 3) in vec4 aColor;
uniform mat4 MVP;
uniform bool HasVColor;
uniform mat4 ModelMatrix[maxInstances];
uniform vec2 TextureOffset[maxInstances];
uniform vec2 TextureScale[maxInstances];
out vec2 vTexture;
out vec4 vColor;
void main() {
vTexture = aTex * TextureScale[gl_InstanceID] + TextureOffset[gl_InstanceID];
if (HasVColor) vColor = aColor;
else vColor = vec4(1);
gl_Position = MVP * ModelMatrix[gl_InstanceID] * vec4(aPos, 1);
}

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