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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue