18.09.2020
+ Textures ~ Changed 2D coordnate system to lower right as XY+
This commit is contained in:
parent
589d131246
commit
a603ecc417
26 changed files with 267 additions and 16 deletions
28
SMCode/SM.Base/Drawing/DrawingBasis.cs
Normal file
28
SMCode/SM.Base/Drawing/DrawingBasis.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
using SM.Base.Contexts;
|
||||
using SM.Base.StaticObjects;
|
||||
using SM.OGL.Mesh;
|
||||
|
||||
namespace SM.Base.Scene
|
||||
{
|
||||
public class DrawingBasis<TTransformation> : 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
9
SMCode/SM.Base/Drawing/GenericTransformation.cs
Normal file
9
SMCode/SM.Base/Drawing/GenericTransformation.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
using OpenTK;
|
||||
|
||||
namespace SM.Base.Scene
|
||||
{
|
||||
public abstract class GenericTransformation
|
||||
{
|
||||
public abstract Matrix4 GetMatrix();
|
||||
}
|
||||
}
|
||||
12
SMCode/SM.Base/Drawing/IShader.cs
Normal file
12
SMCode/SM.Base/Drawing/IShader.cs
Normal file
|
|
@ -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<Matrix4> instanceCollection);
|
||||
}
|
||||
}
|
||||
14
SMCode/SM.Base/Drawing/Material.cs
Normal file
14
SMCode/SM.Base/Drawing/Material.cs
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -45,10 +45,15 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Scene\IShowCollection.cs" />
|
||||
<Compile Include="Scene\IShowItem.cs" />
|
||||
<Compile Include="Drawing\DrawingBasis.cs" />
|
||||
<Compile Include="Drawing\GenericTransformation.cs" />
|
||||
<Compile Include="Drawing\IShader.cs" />
|
||||
<Compile Include="Drawing\IShowCollection.cs" />
|
||||
<Compile Include="Drawing\IShowItem.cs" />
|
||||
<Compile Include="Drawing\Material.cs" />
|
||||
<Compile Include="Shader\InstanceShader.cs" />
|
||||
<Compile Include="Shader\Shaders.cs" />
|
||||
<Compile Include="Textures\Texture.cs" />
|
||||
<Compile Include="Window\Contexts\DrawContext.cs" />
|
||||
<Compile Include="Window\Contexts\UpdateContext.cs" />
|
||||
<Compile Include="Window\GenericWindow.cs" />
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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<UniformCollection, DrawContext> SetUniform;
|
||||
|
||||
public InstanceShader(string vertex, string fragment, Action<UniformCollection, DrawContext> setUniform) : base(
|
||||
new ShaderFileCollection(vertex, fragment))
|
||||
public InstanceShader(string vertex, string fragment, Action<UniformCollection, DrawContext> 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<Matrix4> instanceCollection)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
@ -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() {}
|
||||
|
|
|
|||
51
SMCode/SM.Base/Textures/Texture.cs
Normal file
51
SMCode/SM.Base/Textures/Texture.cs
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -13,6 +13,6 @@ namespace SM.Base.Contexts
|
|||
public Matrix4 ModelMatrix;
|
||||
|
||||
public Mesh Mesh;
|
||||
|
||||
public Material Material;
|
||||
}
|
||||
}
|
||||
|
|
@ -7,12 +7,24 @@ namespace SM.OGL.Mesh
|
|||
{
|
||||
public class VBO : List<float>
|
||||
{
|
||||
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});
|
||||
|
|
|
|||
|
|
@ -55,6 +55,7 @@
|
|||
<Compile Include="Shaders\ShaderFile.cs" />
|
||||
<Compile Include="Shaders\Uniform.cs" />
|
||||
<Compile Include="Shaders\UniformCollection.cs" />
|
||||
<Compile Include="Texture\TextureBase.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -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 _);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,8 @@ namespace SM.OGL.Shaders
|
|||
{
|
||||
public class UniformCollection : Dictionary<string, Uniform>
|
||||
{
|
||||
internal int NextTexture = 0;
|
||||
|
||||
internal GenericShader _parentShader;
|
||||
|
||||
public new Uniform this[string key]
|
||||
|
|
|
|||
13
SMCode/SM.OGL/Texture/TextureBase.cs
Normal file
13
SMCode/SM.OGL/Texture/TextureBase.cs
Normal file
|
|
@ -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; }
|
||||
}
|
||||
}
|
||||
43
SMCode/SM2D/Drawing/DrawTexture.cs
Normal file
43
SMCode/SM2D/Drawing/DrawTexture.cs
Normal file
|
|
@ -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<Transformation>
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -36,6 +36,7 @@
|
|||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
|
|
@ -45,10 +46,12 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Drawing\DrawEmpty.cs" />
|
||||
<Compile Include="Drawing\DrawTexture.cs" />
|
||||
<Compile Include="GLWindow2D.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Scene\Camera.cs" />
|
||||
<Compile Include="Scene\Scene.cs" />
|
||||
<Compile Include="Types\Transformation.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SM.Base\SM.Base.csproj">
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
SMCode/SM2D/Types/Transformation.cs
Normal file
19
SMCode/SM2D/Types/Transformation.cs
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@
|
|||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue