From a603ecc4173af9d001076d3baff00b60f55be750 Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Sat, 19 Sep 2020 15:04:04 +0200 Subject: [PATCH] 18.09.2020 + Textures ~ Changed 2D coordnate system to lower right as XY+ --- SMCode/SM.Base/Drawing/DrawingBasis.cs | 28 ++++++++++ .../SM.Base/Drawing/GenericTransformation.cs | 9 ++++ SMCode/SM.Base/Drawing/IShader.cs | 12 +++++ .../{Scene => Drawing}/IShowCollection.cs | 0 .../SM.Base/{Scene => Drawing}/IShowItem.cs | 0 SMCode/SM.Base/Drawing/Material.cs | 14 +++++ SMCode/SM.Base/SM.Base.csproj | 9 +++- SMCode/SM.Base/Shader/Files/default.frag | 6 ++- SMCode/SM.Base/Shader/Files/default.vert | 5 ++ SMCode/SM.Base/Shader/InstanceShader.cs | 12 +++-- SMCode/SM.Base/Shader/Shaders.cs | 3 +- SMCode/SM.Base/StaticObjects/Plate.cs | 10 ++++ SMCode/SM.Base/Textures/Texture.cs | 51 +++++++++++++++++++ SMCode/SM.Base/Window/Contexts/DrawContext.cs | 2 +- SMCode/SM.OGL/Mesh/VBO.cs | 24 ++++++--- SMCode/SM.OGL/SM.OGL.csproj | 1 + SMCode/SM.OGL/Shaders/GenericShader.cs | 1 + SMCode/SM.OGL/Shaders/Uniform.cs | 10 ++++ SMCode/SM.OGL/Shaders/UniformCollection.cs | 2 + SMCode/SM.OGL/Texture/TextureBase.cs | 13 +++++ SMCode/SM2D/Drawing/DrawTexture.cs | 43 ++++++++++++++++ SMCode/SM2D/SM2D.csproj | 3 ++ SMCode/SM2D/Scene/Camera.cs | 2 +- SMCode/SM2D/Types/Transformation.cs | 19 +++++++ SM_TEST/Program.cs | 3 +- SM_TEST/SM_TEST.csproj | 1 + 26 files changed, 267 insertions(+), 16 deletions(-) create mode 100644 SMCode/SM.Base/Drawing/DrawingBasis.cs create mode 100644 SMCode/SM.Base/Drawing/GenericTransformation.cs create mode 100644 SMCode/SM.Base/Drawing/IShader.cs rename SMCode/SM.Base/{Scene => Drawing}/IShowCollection.cs (100%) rename SMCode/SM.Base/{Scene => Drawing}/IShowItem.cs (100%) create mode 100644 SMCode/SM.Base/Drawing/Material.cs create mode 100644 SMCode/SM.Base/Textures/Texture.cs create mode 100644 SMCode/SM.OGL/Texture/TextureBase.cs create mode 100644 SMCode/SM2D/Drawing/DrawTexture.cs create mode 100644 SMCode/SM2D/Types/Transformation.cs diff --git a/SMCode/SM.Base/Drawing/DrawingBasis.cs b/SMCode/SM.Base/Drawing/DrawingBasis.cs new file mode 100644 index 0000000..8ab8869 --- /dev/null +++ b/SMCode/SM.Base/Drawing/DrawingBasis.cs @@ -0,0 +1,28 @@ +using SM.Base.Contexts; +using SM.Base.StaticObjects; +using SM.OGL.Mesh; + +namespace SM.Base.Scene +{ + public class DrawingBasis : IShowItem + where TTransformation : GenericTransformation, new() + { + protected Material _material = new Material(); + protected Mesh _mesh = Plate.Object; + + public TTransformation Transform = new TTransformation(); + public virtual void Update(UpdateContext context) + { + + } + + public virtual void Draw(DrawContext context) + { } + + protected void ApplyContext(ref DrawContext context) + { + context.Material = _material; + context.Mesh = _mesh; + } + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Drawing/GenericTransformation.cs b/SMCode/SM.Base/Drawing/GenericTransformation.cs new file mode 100644 index 0000000..05223f6 --- /dev/null +++ b/SMCode/SM.Base/Drawing/GenericTransformation.cs @@ -0,0 +1,9 @@ +using OpenTK; + +namespace SM.Base.Scene +{ + public abstract class GenericTransformation + { + public abstract Matrix4 GetMatrix(); + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Drawing/IShader.cs b/SMCode/SM.Base/Drawing/IShader.cs new file mode 100644 index 0000000..aabd2cf --- /dev/null +++ b/SMCode/SM.Base/Drawing/IShader.cs @@ -0,0 +1,12 @@ +using System.Collections.Generic; +using OpenTK; +using SM.Base.Contexts; + +namespace SM.Base.Scene +{ + public interface IShader + { + void Draw(DrawContext context); + void DrawInstanced(DrawContext context, ICollection instanceCollection); + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Scene/IShowCollection.cs b/SMCode/SM.Base/Drawing/IShowCollection.cs similarity index 100% rename from SMCode/SM.Base/Scene/IShowCollection.cs rename to SMCode/SM.Base/Drawing/IShowCollection.cs diff --git a/SMCode/SM.Base/Scene/IShowItem.cs b/SMCode/SM.Base/Drawing/IShowItem.cs similarity index 100% rename from SMCode/SM.Base/Scene/IShowItem.cs rename to SMCode/SM.Base/Drawing/IShowItem.cs diff --git a/SMCode/SM.Base/Drawing/Material.cs b/SMCode/SM.Base/Drawing/Material.cs new file mode 100644 index 0000000..e716b05 --- /dev/null +++ b/SMCode/SM.Base/Drawing/Material.cs @@ -0,0 +1,14 @@ +using OpenTK.Graphics; +using SM.Base.Shader; +using SM.OGL.Texture; + +namespace SM.Base.Scene +{ + public class Material + { + public TextureBase Texture; + public Color4 Tint; + + public IShader Shader = Shaders.Default; + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/SM.Base.csproj b/SMCode/SM.Base/SM.Base.csproj index fffa410..e40f4f9 100644 --- a/SMCode/SM.Base/SM.Base.csproj +++ b/SMCode/SM.Base/SM.Base.csproj @@ -45,10 +45,15 @@ - - + + + + + + + diff --git a/SMCode/SM.Base/Shader/Files/default.frag b/SMCode/SM.Base/Shader/Files/default.frag index bf6a742..8fd1eff 100644 --- a/SMCode/SM.Base/Shader/Files/default.frag +++ b/SMCode/SM.Base/Shader/Files/default.frag @@ -1,8 +1,12 @@ #version 330 + +in vec2 vTexture; + uniform vec4 Tint; +uniform sampler2D Texture; layout(location = 0) out vec4 color; void main() { - color = vec4(1,1,1,1) + Tint; + color = Tint * texture(Texture, vTexture); } \ No newline at end of file diff --git a/SMCode/SM.Base/Shader/Files/default.vert b/SMCode/SM.Base/Shader/Files/default.vert index e17d5f4..2a49e46 100644 --- a/SMCode/SM.Base/Shader/Files/default.vert +++ b/SMCode/SM.Base/Shader/Files/default.vert @@ -1,9 +1,14 @@ #version 330 layout(location = 0) in vec3 aPos; +layout(location = 1) in vec2 aTex; uniform mat4 MVP; uniform mat4 ModelMatrix; +out vec2 vTexture; + void main() { + vTexture = aTex; + gl_Position = MVP * ModelMatrix * vec4(aPos, 1); } \ No newline at end of file diff --git a/SMCode/SM.Base/Shader/InstanceShader.cs b/SMCode/SM.Base/Shader/InstanceShader.cs index cccbb7e..067284f 100644 --- a/SMCode/SM.Base/Shader/InstanceShader.cs +++ b/SMCode/SM.Base/Shader/InstanceShader.cs @@ -1,20 +1,21 @@ using System; +using System.Collections.Generic; using OpenTK; using OpenTK.Graphics.OpenGL4; using SM.Base.Contexts; +using SM.Base.Scene; using SM.Base.StaticObjects; using SM.OGL.Shaders; namespace SM.Base.Shader { - public class InstanceShader : GenericShader + public class InstanceShader : GenericShader, IShader { protected override bool AutoCompile { get; } = true; public Action SetUniform; - public InstanceShader(string vertex, string fragment, Action setUniform) : base( - new ShaderFileCollection(vertex, fragment)) + public InstanceShader(string vertex, string fragment, Action setUniform) : base(new ShaderFileCollection(vertex, fragment)) { SetUniform = setUniform; } @@ -28,5 +29,10 @@ namespace SM.Base.Shader GL.UseProgram(0); } + + public void DrawInstanced(DrawContext context, ICollection instanceCollection) + { + throw new NotImplementedException(); + } } } \ No newline at end of file diff --git a/SMCode/SM.Base/Shader/Shaders.cs b/SMCode/SM.Base/Shader/Shaders.cs index 7546142..438e9bb 100644 --- a/SMCode/SM.Base/Shader/Shaders.cs +++ b/SMCode/SM.Base/Shader/Shaders.cs @@ -12,7 +12,8 @@ namespace SM.Base.Shader { u["MVP"].SetMatrix4(context.View * context.World); u["ModelMatrix"].SetMatrix4(context.ModelMatrix); - u["Tint"].SetUniform4(1,1,1,1); + u["Tint"].SetUniform4(context.Material.Tint); + u["Texture"].SetTexture(context.Material.Texture, 0); })); } } \ No newline at end of file diff --git a/SMCode/SM.Base/StaticObjects/Plate.cs b/SMCode/SM.Base/StaticObjects/Plate.cs index 72caab9..c80664b 100644 --- a/SMCode/SM.Base/StaticObjects/Plate.cs +++ b/SMCode/SM.Base/StaticObjects/Plate.cs @@ -17,6 +17,16 @@ namespace SM.Base.StaticObjects {0,0,0}, }; + public override VBO UVs { get; } = new VBO(pointerSize: 2) + { + {0, 0}, + {0, 1}, + {1, 1}, + {1, 0}, + {0, 0}, + {0, 0}, + }; + public override PrimitiveType PrimitiveType { get; } = PrimitiveType.Quads; private Plate() {} diff --git a/SMCode/SM.Base/Textures/Texture.cs b/SMCode/SM.Base/Textures/Texture.cs new file mode 100644 index 0000000..6bda402 --- /dev/null +++ b/SMCode/SM.Base/Textures/Texture.cs @@ -0,0 +1,51 @@ +using System.Drawing; +using System.Drawing.Imaging; +using OpenTK.Graphics.OpenGL4; +using SM.OGL.Texture; +using PixelFormat = System.Drawing.Imaging.PixelFormat; + +namespace SM.Base.Textures +{ + public class Texture : TextureBase + { + public Bitmap Map; + + public Texture(Bitmap map) : this(map, TextureMinFilter.Linear, TextureWrapMode.Repeat) {} + + public Texture(Bitmap map, TextureMinFilter filter, TextureWrapMode wrapMode) + { + Map = map; + Filter = filter; + WrapMode = wrapMode; + } + + protected override void Compile() + { + base.Compile(); + + _id = GL.GenTexture(); + GL.BindTexture(TextureTarget.Texture2D, _id); + + BitmapData data = Map.LockBits(new Rectangle(0, 0, Map.Width, Map.Height), ImageLockMode.ReadOnly, + Map.PixelFormat); + + bool transparenz = Map.PixelFormat == PixelFormat.Format32bppArgb; + + GL.TexImage2D(TextureTarget.Texture2D, 0, + transparenz ? PixelInternalFormat.Rgba : PixelInternalFormat.Rgb, + data.Width, data.Height, 0, + transparenz ? OpenTK.Graphics.OpenGL4.PixelFormat.Bgra : OpenTK.Graphics.OpenGL4.PixelFormat.Bgr, + PixelType.UnsignedByte, data.Scan0); + + GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)Filter); + GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)Filter); + GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) WrapMode); + GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) WrapMode); + + GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); + + GL.BindTexture(TextureTarget.Texture2D, 0); + Map.UnlockBits(data); + } + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Window/Contexts/DrawContext.cs b/SMCode/SM.Base/Window/Contexts/DrawContext.cs index ce68f87..3a59f0e 100644 --- a/SMCode/SM.Base/Window/Contexts/DrawContext.cs +++ b/SMCode/SM.Base/Window/Contexts/DrawContext.cs @@ -13,6 +13,6 @@ namespace SM.Base.Contexts public Matrix4 ModelMatrix; public Mesh Mesh; - + public Material Material; } } \ No newline at end of file diff --git a/SMCode/SM.OGL/Mesh/VBO.cs b/SMCode/SM.OGL/Mesh/VBO.cs index 3e483bc..b832bed 100644 --- a/SMCode/SM.OGL/Mesh/VBO.cs +++ b/SMCode/SM.OGL/Mesh/VBO.cs @@ -7,12 +7,24 @@ namespace SM.OGL.Mesh { public class VBO : List { - public BufferUsageHint BufferUsageHint = BufferUsageHint.StaticDraw; - public VertexAttribPointerType PointerType = VertexAttribPointerType.Float; - public int PointerSize = 3; - public bool Normalised = false; - public int PointerStride = 0; - public int PointerOffset = 0; + public BufferUsageHint BufferUsageHint; + public VertexAttribPointerType PointerType; + public int PointerSize; + public bool Normalised; + public int PointerStride; + public int PointerOffset; + + public VBO(BufferUsageHint bufferUsageHint = BufferUsageHint.StaticDraw, + VertexAttribPointerType pointerType = VertexAttribPointerType.Float, int pointerSize = 3, + int pointerStride = 0, int pointerOffset = 0, bool normalised = false) + { + BufferUsageHint = bufferUsageHint; + PointerType = pointerType; + PointerSize = pointerSize; + PointerStride = pointerStride; + PointerOffset = pointerOffset; + Normalised = normalised; + } public void Add(float x, float y) => AddRange(new[] {x,y}); public void Add(float x, float y, float z) => AddRange(new[] {x,y,z}); diff --git a/SMCode/SM.OGL/SM.OGL.csproj b/SMCode/SM.OGL/SM.OGL.csproj index 686419b..36858f0 100644 --- a/SMCode/SM.OGL/SM.OGL.csproj +++ b/SMCode/SM.OGL/SM.OGL.csproj @@ -55,6 +55,7 @@ + diff --git a/SMCode/SM.OGL/Shaders/GenericShader.cs b/SMCode/SM.OGL/Shaders/GenericShader.cs index 246d0e4..28d9ec5 100644 --- a/SMCode/SM.OGL/Shaders/GenericShader.cs +++ b/SMCode/SM.OGL/Shaders/GenericShader.cs @@ -30,6 +30,7 @@ namespace SM.OGL.Shaders throw new Exception("[Critical] No uniforms has been found."); Uniforms = new UniformCollection(); + Uniforms._parentShader = this; for (int i = 0; i < uniformCount; i++) { string key = GL.GetActiveUniform(_id, i, out _, out _); diff --git a/SMCode/SM.OGL/Shaders/Uniform.cs b/SMCode/SM.OGL/Shaders/Uniform.cs index be3e5c7..97821d2 100644 --- a/SMCode/SM.OGL/Shaders/Uniform.cs +++ b/SMCode/SM.OGL/Shaders/Uniform.cs @@ -1,6 +1,7 @@ using OpenTK; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL4; +using SM.OGL.Texture; namespace SM.OGL.Shaders { @@ -160,5 +161,14 @@ namespace SM.OGL.Shaders public void SetMatrix4(int count, float[] value, bool transpose = false) { GL.UniformMatrix4(Location, count, transpose, value); } #endregion + + public void SetTexture(TextureBase texture) => SetTexture(texture, Parent.NextTexture++); + + public void SetTexture(TextureBase texture, int texturePos) + { + GL.ActiveTexture(TextureUnit.Texture0 + texturePos); + GL.BindTexture(TextureTarget.Texture2D, texture); + SetUniform1(texturePos); + } } } \ No newline at end of file diff --git a/SMCode/SM.OGL/Shaders/UniformCollection.cs b/SMCode/SM.OGL/Shaders/UniformCollection.cs index 8bba1ca..215b059 100644 --- a/SMCode/SM.OGL/Shaders/UniformCollection.cs +++ b/SMCode/SM.OGL/Shaders/UniformCollection.cs @@ -6,6 +6,8 @@ namespace SM.OGL.Shaders { public class UniformCollection : Dictionary { + internal int NextTexture = 0; + internal GenericShader _parentShader; public new Uniform this[string key] diff --git a/SMCode/SM.OGL/Texture/TextureBase.cs b/SMCode/SM.OGL/Texture/TextureBase.cs new file mode 100644 index 0000000..9c0d7d8 --- /dev/null +++ b/SMCode/SM.OGL/Texture/TextureBase.cs @@ -0,0 +1,13 @@ +using OpenTK.Graphics.OpenGL4; + +namespace SM.OGL.Texture +{ + public class TextureBase : GLObject + { + protected override bool AutoCompile { get; } = true; + public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Texture; + + public virtual TextureMinFilter Filter { get; set; } + public virtual TextureWrapMode WrapMode { get; set; } + } +} \ No newline at end of file diff --git a/SMCode/SM2D/Drawing/DrawTexture.cs b/SMCode/SM2D/Drawing/DrawTexture.cs new file mode 100644 index 0000000..31f4d23 --- /dev/null +++ b/SMCode/SM2D/Drawing/DrawTexture.cs @@ -0,0 +1,43 @@ +using System.Drawing; +using OpenTK.Graphics; +using SM.Base.Contexts; +using SM.Base.Scene; +using SM.Base.Textures; +using SM2D.Types; + +namespace SM2D.Drawing +{ + public class DrawTexture : DrawingBasis + { + public Texture Texture + { + get => (Texture) _material.Texture; + set => _material.Texture = value; + } + + public Color4 Tint + { + get => _material.Tint; + set => _material.Tint = value; + } + + public DrawTexture(Bitmap map) : this(map, Color4.White) + { } + + public DrawTexture(Bitmap map, Color4 tint) + { + _material.Texture = new Texture(map); + _material.Tint = tint; + } + + public override void Draw(DrawContext context) + { + base.Draw(context); + ApplyContext(ref context); + + context.ModelMatrix = Transform.GetMatrix(); + + _material.Shader.Draw(context); + } + } +} \ No newline at end of file diff --git a/SMCode/SM2D/SM2D.csproj b/SMCode/SM2D/SM2D.csproj index 3b677d6..39c6cd7 100644 --- a/SMCode/SM2D/SM2D.csproj +++ b/SMCode/SM2D/SM2D.csproj @@ -36,6 +36,7 @@ + @@ -45,10 +46,12 @@ + + diff --git a/SMCode/SM2D/Scene/Camera.cs b/SMCode/SM2D/Scene/Camera.cs index 81bab30..81a9941 100644 --- a/SMCode/SM2D/Scene/Camera.cs +++ b/SMCode/SM2D/Scene/Camera.cs @@ -16,7 +16,7 @@ namespace SM2D.Scene public override void RecalculateWorld(float width, float height) { - OrthographicWorld = Matrix4.CreateOrthographic(width, height, 0.1f, 100); + OrthographicWorld = Matrix4.CreateOrthographicOffCenter(-width / 2, width / 2, height / 2, -height / 2, 0.1f, 100); } } } \ No newline at end of file diff --git a/SMCode/SM2D/Types/Transformation.cs b/SMCode/SM2D/Types/Transformation.cs new file mode 100644 index 0000000..dad78b0 --- /dev/null +++ b/SMCode/SM2D/Types/Transformation.cs @@ -0,0 +1,19 @@ +using OpenTK; +using SM.Base.Scene; + +namespace SM2D.Types +{ + public class Transformation : GenericTransformation + { + public Vector2 Position; + public Vector2 Size = new Vector2(50); + public float Rotation; + + public override Matrix4 GetMatrix() + { + return Matrix4.CreateScale(Size.X, Size.Y, 1) * + Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(Rotation)) * + Matrix4.CreateTranslation(Position.X, Position.Y, 1); + } + } +} \ No newline at end of file diff --git a/SM_TEST/Program.cs b/SM_TEST/Program.cs index 6e92ef3..b4214fa 100644 --- a/SM_TEST/Program.cs +++ b/SM_TEST/Program.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -23,7 +24,7 @@ namespace SM_TEST private static void WindowOnLoad(object sender, EventArgs e) { - scene.Objects.Add(new DrawEmpty()); + scene.Objects.Add(new DrawTexture(new Bitmap("draconier_logo.png"))); } } } diff --git a/SM_TEST/SM_TEST.csproj b/SM_TEST/SM_TEST.csproj index d93a798..ba34341 100644 --- a/SM_TEST/SM_TEST.csproj +++ b/SM_TEST/SM_TEST.csproj @@ -38,6 +38,7 @@ +