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,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

@ -0,0 +1,15 @@
#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

@ -0,0 +1,23 @@
#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);
}