20.09.2020
+ Instance Drawing + Text and Font ~ Made "DrawBackground" forced Background for 2D - DrawEmpty
This commit is contained in:
parent
acccf5f0e7
commit
c4a0847567
29 changed files with 365 additions and 85 deletions
|
|
@ -1,14 +1,17 @@
|
|||
#version 330
|
||||
#define maxInstances 32
|
||||
layout(location = 0) in vec3 aPos;
|
||||
layout(location = 1) in vec2 aTex;
|
||||
|
||||
uniform mat4 MVP;
|
||||
uniform mat4 ModelMatrix;
|
||||
uniform mat4 ModelMatrix[maxInstances];
|
||||
uniform vec2 TextureOffset[maxInstances];
|
||||
uniform vec2 TextureScale[maxInstances];
|
||||
|
||||
out vec2 vTexture;
|
||||
|
||||
void main() {
|
||||
vTexture = aTex;
|
||||
vTexture = aTex * TextureScale[gl_InstanceID] + TextureOffset[gl_InstanceID];
|
||||
|
||||
gl_Position = MVP * ModelMatrix * vec4(aPos, 1);
|
||||
gl_Position = MVP * ModelMatrix[gl_InstanceID] * vec4(aPos, 1);
|
||||
}
|
||||
|
|
@ -13,26 +13,25 @@ namespace SM.Base.Shader
|
|||
{
|
||||
protected override bool AutoCompile { get; } = true;
|
||||
|
||||
public Action<UniformCollection, DrawContext> SetUniform;
|
||||
public Action<UniformCollection, DrawContext, int> SetUniformVertex;
|
||||
public Action<UniformCollection, DrawContext> SetUniformFragment;
|
||||
|
||||
public InstanceShader(string vertex, string fragment, Action<UniformCollection, DrawContext> setUniform) : base(new ShaderFileCollection(vertex, fragment))
|
||||
public InstanceShader(string vertex, string fragment) : base(new ShaderFileCollection(vertex, fragment))
|
||||
{
|
||||
SetUniform = setUniform;
|
||||
}
|
||||
public void Draw(DrawContext context)
|
||||
{
|
||||
GL.UseProgram(this);
|
||||
|
||||
SetUniform.Invoke(Uniforms, context);
|
||||
Uniforms["MVP"].SetMatrix4(context.View * context.World);
|
||||
|
||||
DrawObject(context.Mesh, true);
|
||||
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);
|
||||
|
||||
GL.UseProgram(0);
|
||||
}
|
||||
|
||||
public void DrawInstanced(DrawContext context, ICollection<Matrix4> instanceCollection)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
using System.IO;
|
||||
using System.Reflection;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using SM.OGL.Shaders;
|
||||
|
||||
namespace SM.Base.Shader
|
||||
|
|
@ -7,13 +8,20 @@ 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(),
|
||||
((u, context) =>
|
||||
new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("SM.Base.Shader.Files.default.frag")).ReadToEnd())
|
||||
{
|
||||
|
||||
SetUniformFragment = (u, context) =>
|
||||
{
|
||||
u["MVP"].SetMatrix4(context.View * context.World);
|
||||
u["ModelMatrix"].SetMatrix4(context.ModelMatrix);
|
||||
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);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue