Holidays 12.10. -> 25.10.2020

~ Moved code around in files.

SM.Base:
+ PostProcessing-system
+ OnInitialization() for Scenes.
+ Shader-Extensions
+ Added option to not react while unfocused to the window.
+ Added Screenshots to the window.
+ Connected the log system to the SM.OGL-action system.

~ Replaced IShader with abstract MaterialShader.
~ When a log compression folder doesn't exist, it will create one.

SM.OGL:
+ Added support for UniformArrays
+ Added ShaderPreProcessing
+ Added Shader Extensions.
+ Added Debug actions.
+ SM.OGL settings

~ Framebuffer Size is automaticly changed, when the window and scale is set.

SM2D:
+ Added easy shader drawing.
This commit is contained in:
Michel Fedde 2020-10-24 15:10:36 +02:00
parent 2c00dbd31a
commit 03b3942732
102 changed files with 2683 additions and 1398 deletions

View file

@ -1,32 +1,26 @@
using System;
#region usings
using OpenTK;
using OpenTK.Input;
#endregion
namespace SM.Base.Controls
{
/// <summary>
/// Mouse controller
/// Mouse controller
/// </summary>
/// <typeparam name="TWindow">The type of window this controller is connected to.</typeparam>
public class Mouse<TWindow>
where TWindow : GenericWindow
where TWindow : GenericWindow
{
/// <summary>
/// The window it is connected to.
/// The window it is connected to.
/// </summary>
protected TWindow _window;
/// <summary>
/// The current position of the mouse in the screen.
/// </summary>
public Vector2 InScreen { get; private set; }
/// <summary>
/// The current position of the mouse in the screen from 0..1.
/// </summary>
public Vector2 InScreenNormalized { get; private set; }
/// <summary>
/// The constructor
/// The constructor
/// </summary>
/// <param name="window">The window, its listen to.</param>
protected internal Mouse(TWindow window)
@ -35,13 +29,23 @@ namespace SM.Base.Controls
}
/// <summary>
/// The event to update the values.
/// The current position of the mouse in the screen.
/// </summary>
public Vector2 InScreen { get; private set; }
/// <summary>
/// The current position of the mouse in the screen from 0..1.
/// </summary>
public Vector2 InScreenNormalized { get; private set; }
/// <summary>
/// The event to update the values.
/// </summary>
/// <param name="mmea">The event args.</param>
protected void MouseMoveEvent(MouseMoveEventArgs mmea)
{
InScreen = new Vector2(mmea.X, mmea.Y);
InScreenNormalized = new Vector2(mmea.X / (float)_window.Width, mmea.Y / (float)_window.Height);
InScreenNormalized = new Vector2(mmea.X / (float) _window.Width, mmea.Y / (float) _window.Height);
}
}
}

View file

@ -1,21 +1,25 @@
using System.Collections.Generic;
#region usings
using System.Collections.Generic;
using SM.Base.Contexts;
using SM.Base.Objects.Static;
using SM.OGL.Mesh;
#endregion
namespace SM.Base.Scene
{
/// <summary>
/// Contains general basis systems for drawing objects.
/// Contains general basis systems for drawing objects.
/// </summary>
public abstract class DrawingBasis : IShowItem
{
/// <summary>
/// The material it should use.
/// The material it should use.
/// </summary>
protected Material _material = new Material();
/// <summary>
/// The mesh it should use.
/// The mesh it should use.
/// </summary>
protected GenericMesh _mesh = SMRenderer.DefaultMesh;
@ -31,7 +35,6 @@ namespace SM.Base.Scene
/// <inheritdoc />
public virtual void Update(UpdateContext context)
{
}
/// <inheritdoc />
@ -46,33 +49,31 @@ namespace SM.Base.Scene
/// <inheritdoc />
public virtual void OnAdded(object sender)
{
}
/// <inheritdoc />
public virtual void OnRemoved(object sender)
{
}
/// <summary>
/// Draws the context, that was given to them.
/// Draws the context, that was given to them.
/// </summary>
/// <param name="context"></param>
protected virtual void DrawContext(ref DrawContext context)
{
}
}
/// <summary>
/// Contains general basis systems for drawing objects.
/// Contains general basis systems for drawing objects.
/// </summary>
/// <typeparam name="TTransformation">The transformation type</typeparam>
public abstract class DrawingBasis<TTransformation> : DrawingBasis
where TTransformation : GenericTransformation, new()
{
/// <summary>
/// The current transformation.
/// The current transformation.
/// </summary>
public TTransformation Transform = new TTransformation();
}

View file

@ -1,23 +1,28 @@
using OpenTK;
#region usings
using OpenTK;
#endregion
namespace SM.Base.Scene
{
/// <summary>
/// Contains methods for using transformations right.
/// Contains methods for using transformations right.
/// </summary>
public abstract class GenericTransformation
{
/// <summary>
/// Contains the current model matrix.
/// Contains the current model matrix.
/// </summary>
protected Matrix4 _modelMatrix { get; private set; }
/// <summary>
/// Contains the last frame the matrix was calculated.
/// Contains the last frame the matrix was calculated.
/// </summary>
protected ulong _lastFrame { get; private set; }
/// <summary>
/// Returns the current model matrix.
/// Returns the current model matrix.
/// </summary>
/// <returns></returns>
public Matrix4 GetMatrix()
@ -32,7 +37,7 @@ namespace SM.Base.Scene
}
/// <summary>
/// Calculates the current matrix.
/// Calculates the current matrix.
/// </summary>
/// <returns>The current matrix.</returns>
protected abstract Matrix4 RequestMatrix();

View file

@ -1,18 +0,0 @@
using System.Collections.Generic;
using OpenTK;
using SM.Base.Contexts;
namespace SM.Base.Scene
{
/// <summary>
/// A general interface to work with material shaders properly.
/// </summary>
public interface IShader
{
/// <summary>
/// Draws the context.
/// </summary>
/// <param name="context">The context</param>
void Draw(DrawContext context);
}
}

View file

@ -1,22 +1,28 @@
using OpenTK;
#region usings
using OpenTK;
#endregion
namespace SM.Base.Scene
{
/// <summary>
/// This represens a drawing instance.
/// This represens a drawing instance.
/// </summary>
public struct Instance
{
/// <summary>
/// The model matrix.
/// The model matrix.
/// </summary>
public Matrix4 ModelMatrix;
/// <summary>
/// The texture offset.
/// The texture offset.
/// </summary>
public Vector2 TexturePosition;
/// <summary>
/// The texture scale.
/// The texture scale.
/// </summary>
public Vector2 TextureScale;
}

View file

@ -1,25 +1,30 @@
using OpenTK.Graphics;
#region usings
using OpenTK.Graphics;
using SM.OGL.Texture;
#endregion
namespace SM.Base.Scene
{
/// <summary>
/// Represents a material.
/// Represents a material.
/// </summary>
public class Material
{
/// <summary>
/// The base texture. (aka. Diffuse Texture)
/// A custom shader, that is used to draw this material.
/// </summary>
public TextureBase Texture;
/// <summary>
/// The tint or color.
/// </summary>
public Color4 Tint = Color4.White;
public MaterialShader CustomShader;
/// <summary>
/// A custom shader, that is used to draw this material.
/// The base texture. (aka. Diffuse Texture)
/// </summary>
public IShader CustomShader;
public TextureBase Texture;
/// <summary>
/// The tint or color.
/// </summary>
public Color4 Tint = Color4.White;
}
}

View file

@ -0,0 +1,47 @@
#region usings
using OpenTK.Graphics.OpenGL4;
using SM.Base.Contexts;
using SM.OGL.Shaders;
#endregion
namespace SM.Base.Scene
{
/// <summary>
/// A general class to work with material shaders properly.
/// </summary>
public abstract class MaterialShader : GenericShader
{
protected MaterialShader(string vertex, string fragment) : base(vertex, fragment)
{
}
protected MaterialShader(ShaderFileCollection shaderFileFiles) : base(shaderFileFiles)
{
}
/// <summary>
/// Draws the context.
/// </summary>
/// <param name="context">The context</param>
public virtual void Draw(DrawContext context)
{
GL.UseProgram(this);
GL.BindVertexArray(context.Mesh);
DrawProcess(context);
CleanUp();
GL.UseProgram(0);
}
protected virtual void DrawProcess(DrawContext context)
{
}
}
}

View file

@ -1,98 +1,108 @@
using System;
#region usings
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Windows.Forms;
using OpenTK.Graphics.OpenGL4;
using SM.OGL;
#endregion
namespace SM.Base
{
/// <summary>
/// Specifies the target.
/// Specifies the target.
/// </summary>
[Flags]
public enum LogTarget
{
/// <summary>
/// No target, will not draw.
/// No target, will not draw.
/// </summary>
None = 0,
/// <summary>
/// Takes the <see cref="Log.DefaultTarget"/>.
/// Takes the <see cref="Log.DefaultTarget" />.
/// </summary>
Default = 1,
/// <summary>
/// Writes the log to the console.
/// Writes the log to the console.
/// </summary>
Console = 2,
/// <summary>
/// Writes the log to the debugger at <see cref="Debug"/>.
/// Writes the log to the debugger at <see cref="Debug" />.
/// </summary>
Debugger = 4,
/// <summary>
/// Writes the log to the specific file.
/// Writes the log to the specific file.
/// </summary>
File = 8,
/// <summary>
/// Writes the log to every target.
/// Writes the log to every target.
/// </summary>
All = Console | Debugger | File
}
/// <summary>
/// Preset log types.
/// Preset log types.
/// </summary>
public enum LogType
{
/// <summary>
/// Informations. Console Color: Green
/// Informations. Console Color: Green
/// </summary>
Info,
/// <summary>
/// Warnings. Console Color: Yellow
/// Warnings. Console Color: Yellow
/// </summary>
Warning,
/// <summary>
/// Error. Console Color: Red
/// Error. Console Color: Red
/// </summary>
Error
}
/// <summary>
/// Contains the system for logging.
/// Contains the system for logging.
/// </summary>
public class Log
{
private static StreamWriter _logStream;
private static bool _init = false;
private static bool _init;
/// <summary>
/// Presets for the log targets.
/// Presets for the log targets.
/// </summary>
public static Dictionary<LogTarget, string> Preset = new Dictionary<LogTarget, string>()
public static Dictionary<LogTarget, string> Preset = new Dictionary<LogTarget, string>
{
{LogTarget.Console, "[%type%] %msg%"},
{LogTarget.Debugger, "[%type%] %msg%"},
{LogTarget.File, "<%date%, %time%> [%type%] %msg%"}
};
private static readonly Dictionary<LogType, ConsoleColor> Colors = new Dictionary<LogType, ConsoleColor>()
private static readonly Dictionary<LogType, ConsoleColor> Colors = new Dictionary<LogType, ConsoleColor>
{
{LogType.Info, ConsoleColor.Green},
{LogType.Warning, ConsoleColor.Yellow},
{LogType.Error, ConsoleColor.Red},
{LogType.Error, ConsoleColor.Red}
};
/// <summary>
/// Specified the default target.
/// Specified the default target.
/// </summary>
public static LogTarget DefaultTarget = LogTarget.All;
/// <summary>
/// Sets the log file. At wish compresses the old file to a zip file.
/// Sets the log file. At wish compresses the old file to a zip file.
/// </summary>
/// <param name="path">The path to the log file.</param>
/// <param name="compressionFolder">Path for the compression, if desired.</param>
@ -106,10 +116,12 @@ namespace SM.Base
{
if (compressionFolder != "")
{
DateTime creation = File.GetLastWriteTime(path);
if (!Directory.Exists(compressionFolder)) Directory.CreateDirectory(compressionFolder);
var creation = File.GetLastWriteTime(path);
try
{
using ZipArchive archive =
using var archive =
ZipFile.Open(
$"{compressionFolder}{Path.DirectorySeparatorChar}{Path.GetFileName(path)}_{creation.Year.ToString() + creation.Month + creation.Day}_{creation.Hour.ToString() + creation.Minute + creation.Second + creation.Millisecond}.zip",
ZipArchiveMode.Create);
@ -127,11 +139,12 @@ namespace SM.Base
_logStream = new StreamWriter(path) {AutoFlush = true};
Write(LogType.Info, $"Activated new log file. ['{path}']");
}
static void Init()
internal static void Init()
{
if (_init) return;
AppDomain.CurrentDomain.UnhandledException += ExceptionHandler;
AppDomain.CurrentDomain.DomainUnload += (sender, args) =>
{
@ -139,27 +152,25 @@ namespace SM.Base
_logStream.Close();
};
GLDebugging.DebugAction = GLDebugAction;
GLDebugging.GlErrorAction = code =>
{
Write(LogType.Warning, $"A '{code}' GL error occured.");
};
GLCustomActions.AtKHRDebug = GLDebugAction;
GLCustomActions.AtError = err => Write(LogType.Error, err);
GLCustomActions.AtWarning = warning => Write(LogType.Warning, warning);
GLCustomActions.AtInfo = info => Write(LogType.Info, info);
_init = true;
}
private static void GLDebugAction(DebugSource source, DebugType type, DebugSeverity severity, string msg)
{
if (type.HasFlag(DebugType.DebugTypeError))
{
throw new Exception("[GLError] "+msg);
}
if (type.HasFlag(DebugType.DebugTypeError)) throw new Exception("[GLError] " + msg);
Write(type != DebugType.DontCare ? type.ToString().Substring(9) : "DontCare", ConsoleColor.Gray, msg);
}
[DebuggerStepThrough]
private static void ExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
Write(e.IsTerminating ? "Terminating Error" : LogType.Error.ToString(), e.IsTerminating ? ConsoleColor.DarkRed : ConsoleColor.Red, e.ExceptionObject);
Write(e.IsTerminating ? "Terminating Error" : LogType.Error.ToString(),
e.IsTerminating ? ConsoleColor.DarkRed : ConsoleColor.Red, e.ExceptionObject);
if (e.IsTerminating)
{
@ -170,30 +181,31 @@ namespace SM.Base
}
/// <summary>
/// Writes multiple lines of the same type to the log.
/// Writes multiple lines of the same type to the log.
/// </summary>
public static void Write<T>(LogType type, params T[] values) => Write<T>(type.ToString(), Colors[type], values);
/// <summary>
/// Writes multiple lines of the same type to the log.
/// </summary>
public static void Write<T>(string type, ConsoleColor color, params T[] values)
public static void Write<T>(LogType type, params T[] values)
{
for (var i = 0; i < values.Length; i++)
{
Write(type, color, values[i], DefaultTarget);
}
Write(type.ToString(), Colors[type], values);
}
/// <summary>
/// Writes one line to the log.
/// Writes multiple lines of the same type to the log.
/// </summary>
public static void Write<T>(LogType type, T value, LogTarget target = LogTarget.Default) =>
Write<T>(type.ToString(), Colors[type], value, target);
public static void Write<T>(string type, ConsoleColor color, params T[] values)
{
for (var i = 0; i < values.Length; i++) Write(type, color, values[i], DefaultTarget);
}
/// <summary>
/// Writes one line to the log.
///
/// Writes one line to the log.
/// </summary>
public static void Write<T>(LogType type, T value, LogTarget target = LogTarget.Default)
{
Write(type.ToString(), Colors[type], value, target);
}
/// <summary>
/// Writes one line to the log.
/// </summary>
public static void Write<T>(string type, ConsoleColor color, T value, LogTarget target = LogTarget.Default)
{
@ -209,23 +221,23 @@ namespace SM.Base
}
/// <summary>
/// Writes a text with a different color.
/// Writes a text with a different color.
/// </summary>
/// <param name="color"></param>
/// <param name="value"></param>
public static void ColorfulWriteLine(ConsoleColor color, string value)
{
ConsoleColor before = Console.ForegroundColor;
var before = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.WriteLine(value);
Console.ForegroundColor = before;
}
static string ProcessPreset(LogTarget target, string type, string msg)
private static string ProcessPreset(LogTarget target, string type, string msg)
{
string preset = Preset[target];
DateTime now = DateTime.Now;
var preset = Preset[target];
var now = DateTime.Now;
return preset.Replace("%date%", now.ToShortDateString())
.Replace("%time%", now.ToShortTimeString())

View file

@ -1,4 +1,8 @@
using SM.OGL.Mesh;
#region usings
using SM.OGL.Mesh;
#endregion
namespace SM.Base.Objects
{
@ -6,16 +10,16 @@ namespace SM.Base.Objects
public class Mesh : GenericMesh
{
/// <summary>
/// Contains vertex colors
/// </summary>
public virtual VBO Color { get; }
/// <summary>
/// While initializing, it will add the <see cref="Color"/> to the data index.
/// While initializing, it will add the <see cref="Color" /> to the data index.
/// </summary>
protected Mesh()
{
AttribDataIndex.Add(3, Color);
}
/// <summary>
/// Contains vertex colors
/// </summary>
public virtual VBO Color { get; }
}
}

View file

@ -1,27 +1,36 @@
using OpenTK;
using OpenTK.Graphics;
#region usings
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using SM.OGL.Mesh;
#endregion
namespace SM.Base.Objects.Static
{
/// <summary>
/// A basic plate
/// A basic plate
/// </summary>
public class Plate : GenericMesh
{
/// <summary>
/// The object.
/// The object.
/// </summary>
public static Plate Object = new Plate();
//public override int[] Indices { get; set; } = new[] {0, 1, 2, 3};
private Plate()
{
}
/// <inheritdoc />
public override VBO Vertex { get; } = new VBO()
public override VBO Vertex { get; } = new VBO
{
{-.5f, -.5f, 0},
{-.5f, .5f, 0},
{.5f, .5f, 0},
{.5f, -.5f, 0},
{.5f, -.5f, 0}
};
/// <inheritdoc />
@ -30,17 +39,14 @@ namespace SM.Base.Objects.Static
{0, 0},
{0, 1},
{1, 1},
{1, 0},
{1, 0}
};
/// <inheritdoc />
public override PrimitiveType PrimitiveType { get; } = PrimitiveType.Quads;
/// <inheritdoc />
public override BoundingBox BoundingBox { get; } = new BoundingBox(new Vector3(-.5f, -.5f, 0), new Vector3(.5f, .5f, 0));
//public override int[] Indices { get; set; } = new[] {0, 1, 2, 3};
private Plate() {}
public override BoundingBox BoundingBox { get; } =
new BoundingBox(new Vector3(-.5f, -.5f, 0), new Vector3(.5f, .5f, 0));
}
}

View file

@ -0,0 +1,9 @@
#version 330
in vec2 vTexture;
uniform sampler2D renderedTexture;
vec4 GetRenderColor() {
return texture(renderedTexture, vTexture);
}

View file

@ -0,0 +1,14 @@
#version 330
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec2 aTex;
uniform mat4 MVP;
out vec2 vTexture;
void main() {
vTexture = aTex;
gl_Position = MVP * vec4(aPos, 1);
}

View file

@ -0,0 +1,18 @@
#version 330
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec2 aTex;
uniform mat4 MVP;
out vec2 vTexture;
void vertex();
void main() {
vTexture = aTex;
gl_Position = MVP * vec4(aPos, 1);
vertex();
}

View file

@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using SM.Base.Objects.Static;
using SM.OGL.Framebuffer;
using SM.OGL.Shaders;
namespace SM.Base.PostProcess
{
public abstract class PostProcessEffect
{
internal static Matrix4 Mvp;
public virtual ICollection<Framebuffer> RequiredFramebuffers { get; }
public virtual void Init() {}
public virtual void Init(Framebuffer main)
{
Init();
}
public virtual void Draw(Framebuffer main)
{
}
}
}

View file

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using OpenTK.Graphics.OpenGL4;
using SM.Base.Objects.Static;
using SM.OGL.Framebuffer;
using SM.OGL.Shaders;
using SM.Utility;
namespace SM.Base.PostProcess
{
public class PostProcessShader : GenericShader
{
private static ShaderFile _fragExtensions = new ShaderFile(AssemblyUtility.ReadAssemblyFile("SM.Base.PostProcess.DefaultFiles.extensions.frag"));
private static ShaderFile _normalVertex = new ShaderFile(AssemblyUtility.ReadAssemblyFile("SM.Base.PostProcess.DefaultFiles.vertexFile.vert"));
private static string _normalVertexWithExt =
AssemblyUtility.ReadAssemblyFile("SM.Base.PostProcess.DefaultFiles.vertexWithExt.vert");
public PostProcessShader(string fragment) : this(_normalVertex,
new ShaderFile(fragment)) { }
public PostProcessShader(string vertexExt, string fragment) : this(new ShaderFile(_normalVertexWithExt)
{
GLSLExtensions = new List<ShaderFile>() { new ShaderFile(vertexExt) }
}, new ShaderFile(fragment)) { }
private PostProcessShader(ShaderFile vertex, ShaderFile fragment) : base(
new ShaderFileCollection(vertex, fragment))
{
fragment.GLSLExtensions.Add(_fragExtensions);
}
public void Draw(ColorAttachment color)
{
GL.UseProgram(this);
GL.BindVertexArray(Plate.Object);
Uniforms["MVP"].SetMatrix4(PostProcessEffect.Mvp);
Uniforms["renderedTexture"].SetTexture(color, 0);
GL.DrawArrays(PrimitiveType.Quads, 0, 4);
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.BindVertexArray(0);
GL.UseProgram(0);
}
public void Draw(ColorAttachment color, Action<UniformCollection> setUniformAction)
{
GL.UseProgram(this);
GL.BindVertexArray(Plate.Object);
Uniforms["MVP"].SetMatrix4(PostProcessEffect.Mvp);
Uniforms["renderedTexture"].SetTexture(color, 0);
setUniformAction(Uniforms);
GL.DrawArrays(PrimitiveType.Quads, 0, 4);
GL.BindTexture(TextureTarget.Texture2D, 0);
GL.BindVertexArray(0);
GL.UseProgram(0);
}
}
}

View file

@ -1,16 +0,0 @@
using System.Collections.Generic;
using SM.OGL.Framebuffer;
namespace SM.Base.PostProcessing
{
public abstract class PostProcessingEffect
{
public virtual Dictionary<string, int> AdditionalFramebufferOutputs { get; }
public virtual ICollection<Framebuffer> AdditionalFramebuffers { get; }
public void ApplyOutputs(Framebuffer mainbuffer)
{
mainbuffer.Append();
}
}
}

View file

@ -1,16 +1,19 @@
using System.Reflection;
using System.Runtime.CompilerServices;
#region usings
using System.Reflection;
using System.Runtime.InteropServices;
#endregion
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SM.Base")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyTitle("Basis for every SMRenderer")]
[assembly: AssemblyDescription("SMRenderer-Basis functions")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyCompany("iedSoftworks")]
[assembly: AssemblyProduct("SM.Base")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2020")]
[assembly: AssemblyCopyright("Copyright © iedSoftworks 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@ -33,4 +36,4 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View file

@ -34,8 +34,8 @@
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="OpenTK, Version=3.2.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>..\packages\OpenTK.3.2\lib\net20\OpenTK.dll</HintPath>
<Reference Include="OpenTK, Version=3.2.1.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>..\..\packages\OpenTK.3.2.1\lib\net20\OpenTK.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
@ -43,27 +43,23 @@
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Controls\Mouse.cs" />
<Compile Include="Drawing\DrawingBasis.cs" />
<Compile Include="Drawing\GenericTransformation.cs" />
<Compile Include="Drawing\Instance.cs" />
<Compile Include="Drawing\IShader.cs" />
<Compile Include="Drawing\MaterialShader.cs" />
<Compile Include="Log.cs" />
<Compile Include="Objects\Mesh.cs" />
<Compile Include="PostProcessing\PostProcessingEffect.cs" />
<Compile Include="PostProcess\PostProcessEffect.cs" />
<Compile Include="PostProcess\PostProcessShader.cs" />
<Compile Include="Scene\IShowCollection.cs" />
<Compile Include="Scene\IShowItem.cs" />
<Compile Include="Drawing\Material.cs" />
<Compile Include="Scene\IBackgroundItem.cs" />
<Compile Include="Scene\GenericItemCollection.cs" />
<Compile Include="ShaderExtension\ExtensionManager.cs" />
<Compile Include="SMRenderer.cs" />
<Compile Include="Textures\Texture.cs" />
<Compile Include="Text\CharParameter.cs" />
@ -81,6 +77,7 @@
<Compile Include="Utility\Deltatime.cs" />
<Compile Include="Utility\Randomize.cs" />
<Compile Include="Utility\RotationUtility.cs" />
<Compile Include="Utility\ShaderUtility.cs" />
<Compile Include="Window\Contexts\DrawContext.cs" />
<Compile Include="Window\Contexts\UpdateContext.cs" />
<Compile Include="Window\GenericWindow.cs" />
@ -91,8 +88,9 @@
<Compile Include="Window\RenderPipeline.cs" />
</ItemGroup>
<ItemGroup>
<None Include="OpenTK.dll.config" />
<None Include="packages.config" />
<EmbeddedResource Include="ShaderExtension\vertex\basic.vert" />
<EmbeddedResource Include="PostProcess\DefaultFiles\vertexFile.vert" />
<EmbeddedResource Include="PostProcess\DefaultFiles\extensions.frag" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SM.OGL\SM.OGL.csproj">
@ -100,6 +98,15 @@
<Name>SM.OGL</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Folder Include="ShaderExtension\fragment\" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="PostProcess\DefaultFiles\vertexWithExt.vert" />
</ItemGroup>
<ItemGroup>
<None Include="OpenTK.dll.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View file

@ -1,33 +1,54 @@
using SM.Base.Objects.Static;
#region usings
using SM.Base.Objects.Static;
using SM.Base.Scene;
using SM.Base.Text;
using SM.OGL.Mesh;
using SM.OGL.Shaders;
using SM.Utility;
#endregion
namespace SM.Base
{
/// <summary>
/// Contains different information about this renderer.
/// Contains different information about this renderer.
/// </summary>
public class SMRenderer
{
/// <summary>
/// The default mesh.
/// Defines, how many instances the 'SM_base_vertex_basic'-extension can handle.
/// </summary>
public const int MaxInstances = 32;
/// <summary>
/// The default mesh.
/// </summary>
public static GenericMesh DefaultMesh = Plate.Object;
/// <summary>
/// The default font.
/// The default font.
/// </summary>
public static Font DefaultFont;
/// <summary>
/// The default deltatime helper.
/// The default deltatime helper.
/// </summary>
public static Deltatime DefaultDeltatime = new Deltatime();
public static MaterialShader DefaultMaterialShader;
/// <summary>
/// Current Frame
/// Shows more information onto the log system.
/// </summary>
public static bool AdvancedDebugging = false;
/// <summary>
/// Current Frame
/// </summary>
public static ulong CurrentFrame { get; internal set; } = 0;
public static GenericWindow CurrentWindow;
public static GenericScene CurrentScene;
}
}

View file

@ -1,41 +1,52 @@
using OpenTK;
#region usings
using OpenTK;
#endregion
namespace SM.Base.Scene
{
/// <summary>
/// Controller for a camera
/// Controller for a camera
/// </summary>
public abstract class GenericCamera
{
/// <summary>
/// The matrix for the orthographic world.
/// The matrix for the orthographic world.
/// </summary>
public static Matrix4 OrthographicWorld { get; protected set; }
/// <summary>
/// The matrix for the perspective world.
/// The matrix for the perspective world.
/// </summary>
public static Matrix4 PerspectiveWorld { get; protected set; }
/// <summary>
/// This defines what is up. (Normalized)
/// <para>Default: <see cref="Vector3.UnitY"/></para>
/// This defines what is up. (Normalized)
/// <para>Default: <see cref="Vector3.UnitY" /></para>
/// </summary>
public static Vector3 UpVector { get; set; } = Vector3.UnitY;
/// <summary>
/// Contains the view matrix of this camera.
/// <para>Default: <see cref="Matrix4.Identity"/></para>
/// Contains the view matrix of this camera.
/// <para>Default: <see cref="Matrix4.Identity" /></para>
/// </summary>
public Matrix4 ViewMatrix { get; protected set; } = Matrix4.Identity;
/// <summary>
/// Returns the world matrix that is connected to this camera.
/// Returns the world matrix that is connected to this camera.
/// </summary>
public Matrix4 World => Orthographic ? OrthographicWorld : PerspectiveWorld;
/// <summary>
/// Calculates the view matrix.
/// Represents if the camera is orthographic.
/// </summary>
/// <returns>The calculated view matrix. Same as <see cref="ViewMatrix"/></returns>
public abstract bool Orthographic { get; }
/// <summary>
/// Calculates the view matrix.
/// </summary>
/// <returns>The calculated view matrix. Same as <see cref="ViewMatrix" /></returns>
internal Matrix4 CalculateViewMatrix()
{
ViewMatrix = ViewCalculation();
@ -43,18 +54,17 @@ namespace SM.Base.Scene
}
/// <summary>
/// This calculates the view matrix.
/// This calculates the view matrix.
/// </summary>
/// <returns>The new view matrix. This is the returns for <see cref="CalculateViewMatrix"/> and the next value for <see cref="ViewMatrix"/>. </returns>
/// <returns>
/// The new view matrix. This is the returns for <see cref="CalculateViewMatrix" /> and the next value for
/// <see cref="ViewMatrix" />.
/// </returns>
protected abstract Matrix4 ViewCalculation();
/// <summary>
/// Represents if the camera is orthographic.
/// </summary>
public abstract bool Orthographic { get; }
/// <summary>
/// This will calculate the world.
/// <para>This is called on <see cref="GenericWindow{TScene,TCamera}.ViewportCamera"/> to calculate the world.</para>
/// This will calculate the world.
/// <para>This is called on <see cref="GenericWindow{TScene,TCamera}.ViewportCamera" /> to calculate the world.</para>
/// </summary>
/// <param name="world">The world scale</param>
/// <param name="aspect">The aspect ratio from the window.</param>

View file

@ -1,10 +1,14 @@
using System.Collections.Generic;
#region usings
using System.Collections.Generic;
using SM.Base.Contexts;
#endregion
namespace SM.Base.Scene
{
/// <summary>
/// Contains a list of show items.
/// Contains a list of show items.
/// </summary>
/// <typeparam name="TItem">The type of show items.</typeparam>
public abstract class GenericItemCollection<TItem> : List<TItem>, IShowItem, IShowCollection<TItem>
@ -22,8 +26,32 @@ namespace SM.Base.Scene
/// <inheritdoc />
public ICollection<string> Flags { get; set; } = new[] {"collection"};
/// <inheritdoc />
public virtual void Update(UpdateContext context)
{
for (var i = 0; i < Objects.Count; i++)
this[i].Update(context);
}
/// <inheritdoc cref="IShowCollection{TItem}.Draw" />
public virtual void Draw(DrawContext context)
{
for (var i = 0; i < Objects.Count; i++)
this[i].Draw(context);
}
/// <inheritdoc />
public virtual void OnAdded(object sender)
{
}
/// <inheritdoc />
public virtual void OnRemoved(object sender)
{
}
/// <summary>
/// Adds a item.
/// Adds a item.
/// </summary>
public new void Add(TItem item)
{
@ -33,7 +61,7 @@ namespace SM.Base.Scene
}
/// <summary>
/// Removes a item.
/// Removes a item.
/// </summary>
/// <param name="item"></param>
public new void Remove(TItem item)
@ -43,72 +71,46 @@ namespace SM.Base.Scene
item.OnRemoved(this);
}
/// <inheritdoc />
public virtual void Update(UpdateContext context)
{
for(int i = 0; i < Objects.Count; i++)
this[i].Update(context);
}
/// <inheritdoc cref="IShowCollection{TItem}.Draw" />
public virtual void Draw(DrawContext context)
{
for (int i = 0; i < Objects.Count; i++)
this[i].Draw(context);
}
/// <inheritdoc />
public virtual void OnAdded(object sender)
{
}
/// <inheritdoc />
public virtual void OnRemoved(object sender)
{ }
/// <summary>
/// Returns a object with this name or the default, if not available.
/// <para>Not reclusive.</para>
/// Returns a object with this name or the default, if not available.
/// <para>Not reclusive.</para>
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public TItem GetItemByName(string name)
{
TItem obj = default;
for (var i = 0; i < this.Count; i++)
{
for (var i = 0; i < Count; i++)
if (this[i].Name == name)
{
obj = this[i];
break;
}
}
return obj;
}
/// <summary>
/// Returns a object with this name or the default if not available.
/// <para>Not reclusive.</para>
/// Returns a object with this name or the default if not available.
/// <para>Not reclusive.</para>
/// </summary>
/// <typeparam name="TGetItem">Type of return</typeparam>
public TGetItem GetItemByName<TGetItem>(string name)
where TGetItem : TItem
{
return (TGetItem)GetItemByName(name);
return (TGetItem) GetItemByName(name);
}
/// <summary>
/// Returns all object that have this flag.
/// <para>Only in this list.</para>
/// Returns all object that have this flag.
/// <para>Only in this list.</para>
/// </summary>
public ICollection<TItem> GetItemsWithFlag(string flag)
{
List<TItem> list = new List<TItem>();
for (var i = 0; i < this.Count; i++)
var list = new List<TItem>();
for (var i = 0; i < Count; i++)
{
TItem obj = this[i];
var obj = this[i];
if (obj.Flags.Contains(flag)) list.Add(obj);
}
@ -117,7 +119,7 @@ namespace SM.Base.Scene
}
/// <summary>
/// Contains a list of show items with transformation.
/// Contains a list of show items with transformation.
/// </summary>
/// <typeparam name="TItem">The type of show items.</typeparam>
/// <typeparam name="TTransformation">The type of transformation.</typeparam>
@ -126,7 +128,7 @@ namespace SM.Base.Scene
where TTransformation : GenericTransformation, new()
{
/// <summary>
/// Transformation of the collection
/// Transformation of the collection
/// </summary>
public TTransformation Transform = new TTransformation();
@ -134,7 +136,7 @@ namespace SM.Base.Scene
public override void Draw(DrawContext context)
{
context.ModelMaster = Transform.GetMatrix() * context.ModelMaster;
base.Draw(context);
}
}

View file

@ -1,19 +1,22 @@
using System;
#region usings
using System.Collections.Generic;
using OpenTK;
using System.Dynamic;
using SM.Base.Contexts;
#endregion
namespace SM.Base.Scene
{
/// <summary>
/// A generic scene, that imports functions for scene control.
/// A generic scene, that imports functions for scene control.
/// </summary>
public abstract class GenericScene
{
private IBackgroundItem _background;
/// <summary>
/// This contains the background.
/// This contains the background.
/// </summary>
protected IBackgroundItem _Background
{
@ -24,41 +27,53 @@ namespace SM.Base.Scene
_background = value;
}
}
public bool IsInitialized { get; private set; }
/// <summary>
/// Updates this scene.
/// Updates this scene.
/// </summary>
/// <param name="context"></param>
public virtual void Update(UpdateContext context)
{
}
/// <summary>
/// Draws this scene.
/// Draws this scene.
/// </summary>
public virtual void Draw(DrawContext context)
{
}
/// <summary>
/// Called, when the user activates the scene.
/// Called, when the user activates the scene.
/// </summary>
internal void Activate()
{
if (!IsInitialized)
{
OnInitialization();
IsInitialized = true;
}
OnActivating();
}
/// <summary>
/// Called, when the user activates the scene.
/// Called, when the user activates the scene for the first time.
/// </summary>
protected virtual void OnInitialization()
{ }
/// <summary>
/// Called, when the user activates the scene.
/// </summary>
protected virtual void OnActivating()
{ }
{
}
}
/// <summary>
/// A generic scene that imports different functions.
/// A generic scene that imports different functions.
/// </summary>
/// <typeparam name="TCamera">The type of cameras.</typeparam>
/// <typeparam name="TItem">The type of show items.</typeparam>
@ -68,25 +83,32 @@ namespace SM.Base.Scene
where TCollection : GenericItemCollection<TItem>, new()
where TItem : IShowItem
{
private TCollection _objectCollection = new TCollection();
private TCollection _hud = new TCollection();
private TCollection _objectCollection = new TCollection();
/// <summary>
/// The active camera, that is used if the context doesn't force the viewport camera.
/// <para>If none set, it automaticly uses the viewport camera.</para>
/// A collection for cameras to switch easier to different cameras.
/// </summary>
public Dictionary<string, TCamera> Cameras = new Dictionary<string, TCamera>();
/// <summary>
/// The active camera, that is used if the context doesn't force the viewport camera.
/// <para>If none set, it automaticly uses the viewport camera.</para>
/// </summary>
public TCamera Camera { get; set; }
/// <summary>
/// A camera to control the background.
/// A camera to control the background.
/// </summary>
public TCamera BackgroundCamera { get; set; } = new TCamera();
/// <summary>
/// A camera to control the HUD.
/// A camera to control the HUD.
/// </summary>
public TCamera HUDCamera { get; set; } = new TCamera();
/// <summary>
/// Objects inside the scene.
/// Objects inside the scene.
/// </summary>
public TCollection Objects
{
@ -99,7 +121,7 @@ namespace SM.Base.Scene
}
/// <summary>
/// This defines the HUD objects.
/// This defines the HUD objects.
/// </summary>
public TCollection HUD
{
@ -110,10 +132,6 @@ namespace SM.Base.Scene
_hud = value;
}
}
/// <summary>
/// A collection for cameras to switch easier to different cameras.
/// </summary>
public Dictionary<string, TCamera> Cameras = new Dictionary<string, TCamera>();
/// <inheritdoc />
@ -127,14 +145,28 @@ namespace SM.Base.Scene
/// <inheritdoc />
public override void Draw(DrawContext context)
{
if (!context.ForceViewport && Camera != null) context.View = Camera.CalculateViewMatrix();
DrawBackground(context);
DrawContext backgroundDrawContext = context;
DrawMainObjects(context);
DrawHUD(context);
}
public void DrawBackground(DrawContext context)
{
var backgroundDrawContext = context;
backgroundDrawContext.View = BackgroundCamera.CalculateViewMatrix();
_Background?.Draw(backgroundDrawContext);
}
public void DrawMainObjects(DrawContext context)
{
if (!context.ForceViewport && Camera != null) context.View = Camera.CalculateViewMatrix();
_objectCollection.Draw(context);
}
public void DrawHUD(DrawContext context)
{
context.View = HUDCamera.CalculateViewMatrix();
_hud.Draw(context);
}

View file

@ -1,10 +1,9 @@
namespace SM.Base.Scene
{
/// <summary>
/// A iteration of <see cref="IShowItem"/> to reduce clutter.
/// A iteration of <see cref="IShowItem" /> to reduce clutter.
/// </summary>
public interface IBackgroundItem : IShowItem
{
}
}

View file

@ -1,21 +1,25 @@
using System.Collections.Generic;
#region usings
using System.Collections.Generic;
using SM.Base.Contexts;
#endregion
namespace SM.Base.Scene
{
/// <summary>
/// Adds functions, that is required for a collection.
/// Adds functions, that is required for a collection.
/// </summary>
/// <typeparam name="TItem">The type of show item.</typeparam>
public interface IShowCollection<TItem> where TItem : IShowItem
{
/// <summary>
/// The object collection.
/// The object collection.
/// </summary>
List<TItem> Objects { get; }
/// <summary>
/// This draws the objects in the <see cref="Objects"/> list.
/// This draws the objects in the <see cref="Objects" /> list.
/// </summary>
/// <param name="context">The context how the objects need to be drawn.</param>
void Draw(DrawContext context);

View file

@ -1,45 +1,51 @@
using System.Collections.Generic;
#region usings
using System.Collections.Generic;
using SM.Base.Contexts;
#endregion
namespace SM.Base.Scene
{
/// <summary>
/// Adds requirements to object, to be properly used as a update and/or draw item.
/// Adds requirements to object, to be properly used as a update and/or draw item.
/// </summary>
public interface IShowItem
{
/// <summary>
/// Parent of the object.
/// Parent of the object.
/// </summary>
object Parent { get; set; }
/// <summary>
/// Contains the name for the object.
/// Contains the name for the object.
/// </summary>
string Name { get; set; }
/// <summary>
/// Contains specific flags for the object.
/// Contains specific flags for the object.
/// </summary>
ICollection<string> Flags { get; set; }
/// <summary>
/// Tells the object to update own systems.
/// Tells the object to update own systems.
/// </summary>
/// <param name="context">The update context</param>
void Update(UpdateContext context);
/// <summary>
/// Tells the object to draw its object.
/// Tells the object to draw its object.
/// </summary>
/// <param name="context"></param>
void Draw(DrawContext context);
/// <summary>
/// Action, that is called, when the object was added to a GenericItemCollection.
/// Action, that is called, when the object was added to a GenericItemCollection.
/// </summary>
void OnAdded(object sender);
/// <summary>
/// Action, that is called, when the object was removed from a GenericItemCollection.
/// Action, that is called, when the object was removed from a GenericItemCollection.
/// </summary>
void OnRemoved(object sender);
}

View file

@ -0,0 +1,19 @@
#region usings
using SM.OGL.Shaders;
#endregion
namespace SM.Base.ShaderExtension
{
internal class ExtensionManager
{
internal static void InitExtensions()
{
ShaderExtensions.AddAssemblyExtensions("SM_base", "SM.Base.ShaderExtension");
ShaderExtensions.Extensions["SM_base_vertex_basic"].StringOverrides["instanceMax"] =
SMRenderer.MaxInstances.ToString();
}
}
}

View file

@ -0,0 +1,35 @@
#version 330
#define maxInstances //!instanceMax
struct Instance {
mat4 ModelMatrix;
vec2 TextureOffset;
vec2 TextureScale;
};
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 Instance[maxInstances] Instances;
out vec2 vTexture;
out vec4 vColor;
out vec3 FragPos;
void ApplyTexModifier() {
vTexture = aTex * Instances[gl_InstanceID].TextureScale + Instances[gl_InstanceID].TextureOffset;
}
void CheckVertexColor() {
if (HasVColor) vColor = aColor;
else vColor = vec4(1);
}
void ApplyModelTransformation() {
gl_Position = MVP * Instances[gl_InstanceID].ModelMatrix * vec4(aPos, 1);
FragPos = vec3(Instances[gl_InstanceID].ModelMatrix * vec4(aPos, 1));
}

View file

@ -1,28 +1,34 @@
using System;
#region usings
using System;
#endregion
namespace SM.Base.Text
{
/// <summary>
/// Contains information for a font character.
/// Contains information for a font character.
/// </summary>
[Serializable]
public struct CharParameter
{
/// <summary>
/// The position on the X-axis.
/// The position on the X-axis.
/// </summary>
public int X;
/// <summary>
/// The width of the character.
/// The width of the character.
/// </summary>
public float Width;
/// <summary>
/// The normalized position inside the texture.
/// The normalized position inside the texture.
/// </summary>
public float NormalizedX;
/// <summary>
/// The normalized width inside the texture.
/// The normalized width inside the texture.
/// </summary>
public float NormalizedWidth;
}

View file

@ -1,71 +1,74 @@
using System.Collections.Generic;
#region usings
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Security.Policy;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Input;
using SM.Base.Textures;
using SM.Data.Fonts;
using SM.OGL.Texture;
#endregion
namespace SM.Base.Text
{
/// <summary>
/// Represents a font.
/// Represents a font.
/// </summary>
public class Font : Texture
{
/// <inheritdoc />
public override TextureWrapMode WrapMode { get; set; } = TextureWrapMode.ClampToEdge;
/// <summary>
/// The font family, that is used to find the right font.
/// </summary>
public FontFamily FontFamily;
/// <summary>
/// The font style.
/// <para>Default: <see cref="System.Drawing.FontStyle.Regular"/></para>
/// </summary>
public FontStyle FontStyle = FontStyle.Regular;
/// <summary>
/// The font size.
/// <para>Default: 12</para>
/// </summary>
public float FontSize = 12;
/// <summary>
/// The char set for the font.
/// <para>Default: <see cref="FontCharStorage.SimpleUTF8"/></para>
/// The char set for the font.
/// <para>Default: <see cref="FontCharStorage.SimpleUTF8" /></para>
/// </summary>
public ICollection<char> CharSet = FontCharStorage.SimpleUTF8;
/// <summary>
/// This contains all information for the different font character.
/// The font family, that is used to find the right font.
/// </summary>
public FontFamily FontFamily;
/// <summary>
/// The font size.
/// <para>Default: 12</para>
/// </summary>
public float FontSize = 12;
/// <summary>
/// The font style.
/// <para>Default: <see cref="System.Drawing.FontStyle.Regular" /></para>
/// </summary>
public FontStyle FontStyle = FontStyle.Regular;
/// <summary>
/// This contains all information for the different font character.
/// </summary>
public Dictionary<char, CharParameter> Positions = new Dictionary<char, CharParameter>();
/// <summary>
/// Generates a font from a font family from the specified path.
/// Generates a font from a font family from the specified path.
/// </summary>
/// <param name="path">The specified path</param>
public Font(string path)
{
PrivateFontCollection pfc = new PrivateFontCollection();
var pfc = new PrivateFontCollection();
pfc.AddFontFile(path);
FontFamily = pfc.Families[0];
}
/// <summary>
/// Generates a font from a specified font family.
/// Generates a font from a specified font family.
/// </summary>
/// <param name="font">Font-Family</param>
public Font(FontFamily font)
{
FontFamily = font;
}
/// <inheritdoc />
public override TextureWrapMode WrapMode { get; set; } = TextureWrapMode.ClampToEdge;
/// <summary>
/// Regenerates the texture.
/// Regenerates the texture.
/// </summary>
public void RegenerateTexture()
{
@ -74,47 +77,47 @@ namespace SM.Base.Text
Positions = new Dictionary<char, CharParameter>();
Bitmap map = new Bitmap(1000, 20);
Dictionary<char, float[]> charParams = new Dictionary<char, float[]>();
using (System.Drawing.Font f = new System.Drawing.Font(FontFamily, FontSize, FontStyle))
var map = new Bitmap(1000, 20);
var charParams = new Dictionary<char, float[]>();
using (var f = new System.Drawing.Font(FontFamily, FontSize, FontStyle))
{
using (Graphics g = Graphics.FromImage(map))
using (var g = Graphics.FromImage(map))
{
g.Clear(Color.Transparent);
foreach (char c in CharSet)
foreach (var c in CharSet)
{
string s = c.ToString();
SizeF size = g.MeasureString(s, f);
var s = c.ToString();
var size = g.MeasureString(s, f);
try
{
charParams.Add(c, new[] {size.Width, Width });
charParams.Add(c, new[] {size.Width, Width});
}
catch
{
// ignored
}
if (Height < size.Height) Height = (int)size.Height;
Width += (int)size.Width;
if (Height < size.Height) Height = (int) size.Height;
Width += (int) size.Width;
}
}
map = new Bitmap(Width, Height);
using (Graphics g = Graphics.FromImage(map))
using (var g = Graphics.FromImage(map))
{
foreach (KeyValuePair<char, float[]> keyValuePair in charParams)
foreach (var keyValuePair in charParams)
{
float normalizedX = (keyValuePair.Value[1] + 0.00001f) / Width;
float normalizedWidth = (keyValuePair.Value[0]) / Width;
var normalizedX = (keyValuePair.Value[1] + 0.00001f) / Width;
var normalizedWidth = keyValuePair.Value[0] / Width;
CharParameter parameter;
Positions.Add(keyValuePair.Key, parameter = new CharParameter()
Positions.Add(keyValuePair.Key, parameter = new CharParameter
{
NormalizedWidth = normalizedWidth,
NormalizedX = normalizedX,
Width = keyValuePair.Value[0],
X = (int)keyValuePair.Value[1]
X = (int) keyValuePair.Value[1]
});
g.DrawString(keyValuePair.Key.ToString(), f, Brushes.White, parameter.X, 0);

View file

@ -1,28 +1,29 @@
namespace SM.Data.Fonts
{
/// <summary>
/// Contains default char sets.
/// Contains default char sets.
/// </summary>
public class FontCharStorage
{
/// <summary>
/// Contains the english alphabet and the common special character.
/// Contains the english alphabet and the common special character.
/// </summary>
public static readonly char[] SimpleUTF8 = new char[]
public static readonly char[] SimpleUTF8 =
{
'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6',
'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5',
'6',
'7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'
};
/// <summary>
/// Contains only numbers.
/// Contains only numbers.
/// </summary>
public static readonly char[] Numbers = new[]
public static readonly char[] Numbers =
{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};
}
}

View file

@ -1,30 +1,49 @@
using System;
#region usings
using System;
using OpenTK;
using OpenTK.Graphics;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.Data.Fonts;
#endregion
namespace SM.Base.Text
{
/// <summary>
/// Defines a basis for text drawing.
/// Defines a basis for text drawing.
/// </summary>
/// <typeparam name="TTransform">Transformation type</typeparam>
public abstract class TextDrawingBasis<TTransform> : DrawingBasis<TTransform>
where TTransform : GenericTransformation, new()
{
/// <summary>
/// The different instances for drawing.
/// The different instances for drawing.
/// </summary>
protected Instance[] _instances;
/// <summary>
/// The text, that is drawn.
/// The text, that is drawn.
/// </summary>
protected string _text;
/// <summary>
/// The font.
/// The spacing between numbers.
/// <para>Default: 1</para>
/// </summary>
public float Spacing = 1;
/// <summary>
/// Creates a text object with a font.
/// </summary>
/// <param name="font">The font.</param>
protected TextDrawingBasis(Font font)
{
_material.Texture = font;
}
/// <summary>
/// The font.
/// </summary>
public Font Font
{
@ -37,7 +56,7 @@ namespace SM.Base.Text
}
/// <summary>
/// The text, that is drawn.
/// The text, that is drawn.
/// </summary>
public string Text
{
@ -50,7 +69,7 @@ namespace SM.Base.Text
}
/// <summary>
/// The font color.
/// The font color.
/// </summary>
public Color4 Color
{
@ -58,21 +77,6 @@ namespace SM.Base.Text
set => _material.Tint = value;
}
/// <summary>
/// The spacing between numbers.
/// <para>Default: 1</para>
/// </summary>
public float Spacing = 1;
/// <summary>
/// Creates a text object with a font.
/// </summary>
/// <param name="font">The font.</param>
protected TextDrawingBasis(Font font)
{
_material.Texture = font;
}
/// <inheritdoc />
protected override void DrawContext(ref DrawContext context)
@ -81,7 +85,7 @@ namespace SM.Base.Text
}
/// <summary>
/// This generates the instances.
/// This generates the instances.
/// </summary>
/// <exception cref="Exception">The font doesn't contain a character that is needed for the text.</exception>
public void GenerateMatrixes()
@ -91,7 +95,7 @@ namespace SM.Base.Text
_instances = new Instance[_text.Length];
float x = 0;
CharParameter _last = new CharParameter();
var _last = new CharParameter();
for (var i = 0; i < _text.Length; i++)
{
if (_text[i] == 32)
@ -99,7 +103,7 @@ namespace SM.Base.Text
x += _last.Width * Spacing;
continue;
}
CharParameter parameter;
try
{
@ -110,15 +114,15 @@ namespace SM.Base.Text
throw new Exception("Font doesn't contain '" + _text[i] + "'");
}
Matrix4 matrix = Matrix4.CreateScale(parameter.Width, Font.Height, 1) *
Matrix4.CreateTranslation(x, 0, 0);
var matrix = Matrix4.CreateScale(parameter.Width, Font.Height, 1) *
Matrix4.CreateTranslation(x, 0, 0);
_instances[i] = new Instance
{
ModelMatrix = matrix,
TexturePosition = new Vector2(parameter.NormalizedX, 0),
TextureScale = new Vector2(parameter.NormalizedWidth, 1)
};
x += parameter.Width * Spacing;
_last = parameter;
}

View file

@ -1,38 +1,48 @@
using System.Drawing;
#region usings
using System.Drawing;
using System.Drawing.Imaging;
using OpenTK.Graphics.OpenGL4;
using SM.OGL.Texture;
using PixelFormat = System.Drawing.Imaging.PixelFormat;
#endregion
namespace SM.Base.Textures
{
/// <summary>
/// Texture that can be drawn to an object.
/// Texture that can be drawn to an object.
/// </summary>
public class Texture : TextureBase
{
/// <summary>
/// The texture as bitmap.
/// </summary>
public Bitmap Map;
/// <summary>
/// Decides if the bitmap will automatically dispose itself.
/// Decides if the bitmap will automatically dispose itself.
/// </summary>
public bool AutoDispose = false;
/// <summary>
/// Empty constructor
/// The texture as bitmap.
/// </summary>
protected Texture() {}
public Bitmap Map;
/// <summary>
/// Creates a texture with only the map.
/// <para>Sets the filter to Linear and WrapMode to Repeat.</para>
/// Empty constructor
/// </summary>
protected Texture()
{
}
/// <summary>
/// Creates a texture with only the map.
/// <para>Sets the filter to Linear and WrapMode to Repeat.</para>
/// </summary>
/// <param name="map">The map</param>
public Texture(Bitmap map) : this(map, TextureMinFilter.Linear, TextureWrapMode.Repeat) {}
public Texture(Bitmap map) : this(map, TextureMinFilter.Linear, TextureWrapMode.Repeat)
{
}
/// <summary>
/// Creates the texture.
/// Creates the texture.
/// </summary>
/// <param name="map">The texture map</param>
/// <param name="filter">The filter</param>
@ -62,22 +72,23 @@ namespace SM.Base.Textures
}
/// <summary>
/// Generates a OpenGL-texture.
/// Generates a OpenGL-texture.
/// </summary>
/// <param name="map">The texture as bitmap</param>
/// <param name="filter">The filter</param>
/// <param name="wrapMode">The wrap mode</param>
/// <param name="dispose">Auto dispose of the bitmap? Default: false</param>
/// <returns></returns>
public static int GenerateTexture(Bitmap map, TextureMinFilter filter, TextureWrapMode wrapMode, bool dispose = false)
public static int GenerateTexture(Bitmap map, TextureMinFilter filter, TextureWrapMode wrapMode,
bool dispose = false)
{
int id = GL.GenTexture();
var id = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, id);
BitmapData data = map.LockBits(new Rectangle(0, 0, map.Width, map.Height), ImageLockMode.ReadOnly,
var data = map.LockBits(new Rectangle(0, 0, map.Width, map.Height), ImageLockMode.ReadOnly,
map.PixelFormat);
bool transparenz = map.PixelFormat == PixelFormat.Format32bppArgb;
var transparenz = map.PixelFormat == PixelFormat.Format32bppArgb;
GL.TexImage2D(TextureTarget.Texture2D, 0,
transparenz ? PixelInternalFormat.Rgba : PixelInternalFormat.Rgb,
@ -85,10 +96,10 @@ namespace SM.Base.Textures
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.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);
@ -101,8 +112,11 @@ namespace SM.Base.Textures
}
/// <summary>
/// Converts a bitmap to a texture.
/// Converts a bitmap to a texture.
/// </summary>
public static implicit operator Texture(Bitmap map) => new Texture(map);
public static implicit operator Texture(Bitmap map)
{
return new Texture(map);
}
}
}

View file

@ -1,10 +1,14 @@
using System;
#region usings
using System;
using SM.Base.Contexts;
#endregion
namespace SM.Base.Time
{
/// <summary>
/// Performs intervals.
/// Performs intervals.
/// </summary>
public class Interval : Timer
{
@ -24,13 +28,14 @@ namespace SM.Base.Time
protected override void Stopping(UpdateContext context)
{
TriggerEndAction(context);
if (_stop){base.Stop();}
if (_stop)
base.Stop();
else Reset();
}
/// <summary>
/// This will tell the interval to stop after the next iteration.
/// <para>To stop immediately use <see cref="Cancel"/></para>
/// This will tell the interval to stop after the next iteration.
/// <para>To stop immediately use <see cref="Cancel" /></para>
/// </summary>
public override void Stop()
{
@ -38,7 +43,7 @@ namespace SM.Base.Time
}
/// <summary>
/// This will stop the interval immediately.
/// This will stop the interval immediately.
/// </summary>
public void Cancel()
{

View file

@ -1,22 +1,26 @@
using System.Collections.Generic;
#region usings
using System.Collections.Generic;
using SM.Base.Contexts;
#endregion
namespace SM.Base.Time
{
/// <summary>
/// Represents a stopwatch.
/// Represents a stopwatch.
/// </summary>
public class Stopwatch
{
private static List<Stopwatch> _activeStopwatches = new List<Stopwatch>();
/// <summary>
/// Contains how much time already has passed. (in seconds)
/// Contains how much time already has passed. (in seconds)
/// </summary>
public float Elapsed { get; private set; }
/// <summary>
/// Starts the stopwatch.
/// Starts the stopwatch.
/// </summary>
public virtual void Start()
{
@ -24,7 +28,7 @@ namespace SM.Base.Time
}
/// <summary>
/// Performs a tick.
/// Performs a tick.
/// </summary>
/// <param name="context"></param>
private protected virtual void Tick(UpdateContext context)
@ -33,7 +37,7 @@ namespace SM.Base.Time
}
/// <summary>
/// Stops the stopwatch.
/// Stops the stopwatch.
/// </summary>
public virtual void Stop()
{
@ -41,7 +45,7 @@ namespace SM.Base.Time
}
/// <summary>
/// Resets the stopwatch.
/// Resets the stopwatch.
/// </summary>
public void Reset()
{
@ -50,10 +54,7 @@ namespace SM.Base.Time
internal static void PerformTicks(UpdateContext context)
{
for (var i = 0; i < _activeStopwatches.Count; i++)
{
_activeStopwatches[i].Tick(context);
}
for (var i = 0; i < _activeStopwatches.Count; i++) _activeStopwatches[i].Tick(context);
}
}
}

View file

@ -1,31 +1,19 @@
using System;
using System.Collections.Generic;
#region usings
using System;
using SM.Base.Contexts;
#endregion
namespace SM.Base.Time
{
/// <summary>
/// Timer-System
/// Timer-System
/// </summary>
public class Timer : Stopwatch
{
/// <summary>
/// The target time in seconds.
/// </summary>
public float Target { get; private set; }
/// <summary>
/// The already elapsed time but normalized to the target.
/// </summary>
public float ElapsedNormalized { get; private set; }
/// <summary>
/// The event, that is triggered when the timer stops.
/// </summary>
public event Action<Timer, UpdateContext> EndAction;
/// <summary>
/// Creates a timer with specified seconds.
/// Creates a timer with specified seconds.
/// </summary>
/// <param name="seconds"></param>
public Timer(float seconds)
@ -34,14 +22,29 @@ namespace SM.Base.Time
}
/// <summary>
/// Creates a timer with a time span.
/// Creates a timer with a time span.
/// </summary>
/// <param name="timeSpan"></param>
public Timer(TimeSpan timeSpan)
{
Target = (float)timeSpan.TotalSeconds;
Target = (float) timeSpan.TotalSeconds;
}
/// <summary>
/// The target time in seconds.
/// </summary>
public float Target { get; private set; }
/// <summary>
/// The already elapsed time but normalized to the target.
/// </summary>
public float ElapsedNormalized { get; private set; }
/// <summary>
/// The event, that is triggered when the timer stops.
/// </summary>
public event Action<Timer, UpdateContext> EndAction;
/// <inheritdoc />
public override void Start()
{
@ -58,15 +61,16 @@ namespace SM.Base.Time
}
/// <summary>
/// Occurs, when the timer tries to stop.
/// Occurs, when the timer tries to stop.
/// </summary>
protected virtual void Stopping(UpdateContext context)
{
EndAction?.Invoke(this, context);
Stop();
}
/// <summary>
/// This will trigger <see cref="EndAction"/>
/// This will trigger <see cref="EndAction" />
/// </summary>
/// <param name="context"></param>
protected void TriggerEndAction(UpdateContext context)

View file

@ -1,146 +1,145 @@
using System;
using System.Configuration.Assemblies;
#region usings
using System.Diagnostics;
using OpenTK;
#endregion
namespace SM.Base.Types
{
/// <summary>
/// Represents a base vector-class
/// Represents a base vector-class
/// </summary>
public abstract class CVector
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private float _x = default;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private float _y = default;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private float _z = default;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private float _w = default;
/// <summary>
/// The X-component.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
protected float _X
{
get => _x;
set => _x = value;
}
/// <summary>
/// The Y-component
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
protected float _Y
{
get => _y;
set => _y = value;
}
/// <summary>
/// The Z-component.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
protected float _Z
{
get => _z;
set => _z = value;
}
/// <summary>
/// The W-component.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
protected float _W
{
get => _w;
set => _w = value;
}
/// <summary>
/// Creates a vector by setting every component to the same value.
/// Creates a vector by setting every component to the same value.
/// </summary>
protected CVector(float uniform) : this(uniform, uniform, uniform, uniform)
{ }
{
}
/// <summary>
/// Creates a vector by setting values for each component.
/// Creates a vector by setting values for each component.
/// </summary>
protected CVector(float x, float y, float z, float w)
{
_x = x;
_y = y;
_z = z;
_w = w;
_X = x;
_Y = y;
_Z = z;
_W = w;
}
/// <summary>
/// The X-component.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
protected float _X { get; set; }
/// <summary>
/// The Y-component
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
protected float _Y { get; set; }
/// <summary>
/// The Z-component.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
protected float _Z { get; set; }
/// <summary>
/// The W-component.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
protected float _W { get; set; }
/// <summary>
/// Transforms a <see cref="CVector" /> to a <see cref="OpenTK.Vector2" />
/// </summary>
public static implicit operator Vector2(CVector v) => new Vector2(v._X, v._Y);
/// <summary>
/// Transforms a <see cref="CVector" /> to a <see cref="OpenTK.Vector3" />
/// </summary>
public static implicit operator Vector3(CVector v) => new Vector3(v._X, v._Y, v._Z);
/// <summary>
/// Transforms a <see cref="CVector" /> to a <see cref="OpenTK.Vector4" />
/// </summary>
public static implicit operator Vector4(CVector v) => new Vector4(v._X, v._Y, v._Z, v._W);
#region Set
/// <summary>
/// Sets the X and Y-component.
/// Sets the X and Y-component.
/// </summary>
protected void Set(float x, float y)
{
_X = x;
_Y = y;
}
/// <summary>
/// Sets the X and Y-component from a <see cref="OpenTK.Vector2"/>
/// Sets the X and Y-component from a <see cref="OpenTK.Vector2" />
/// </summary>
protected void Set(OpenTK.Vector2 vector)
protected void Set(Vector2 vector)
{
Set(vector.X, vector.Y);
}
/// <summary>
/// Sets the X, Y and Z-component.
/// Sets the X, Y and Z-component.
/// </summary>
protected void Set(float x, float y, float z)
{
Set(x,y);
Set(x, y);
_Z = z;
}
/// <summary>
/// Sets the X, Y and Z-component from a <see cref="OpenTK.Vector3"/>.
/// Sets the X, Y and Z-component from a <see cref="OpenTK.Vector3" />.
/// </summary>
/// <param name="vector"></param>
protected void Set(OpenTK.Vector3 vector)
protected void Set(Vector3 vector)
{
Set(vector.X, vector.Y, vector.Z);
}
/// <summary>
/// Sets the X, Y, Z and W-component.
/// Sets the X, Y, Z and W-component.
/// </summary>
protected void Set(float x, float y, float z, float w)
{
Set(x,y,z);
Set(x, y, z);
_W = w;
}
/// <summary>
/// Sets the X, Y, Z and W-component from a <see cref="OpenTK.Vector4"/>.
/// Sets the X, Y, Z and W-component from a <see cref="OpenTK.Vector4" />.
/// </summary>
protected void Set(OpenTK.Vector4 vector)
protected void Set(Vector4 vector)
{
Set(vector.X, vector.Y, vector.Z, vector.W);
}
#endregion
#region Add
/// <summary>
/// Adds a <see cref="OpenTK.Vector2"/> to the X and Y-component.
/// Adds a <see cref="OpenTK.Vector2" /> to the X and Y-component.
/// </summary>
protected void Add(OpenTK.Vector2 vector)
protected void Add(Vector2 vector)
{
_X += vector.X;
_Y += vector.Y;
}
/// <summary>
/// Adds a <see cref="OpenTK.Vector3"/> to the X, Y and Z-component.
/// Adds a <see cref="OpenTK.Vector3" /> to the X, Y and Z-component.
/// </summary>
protected void Add(OpenTK.Vector3 vector)
protected void Add(Vector3 vector)
{
_X += vector.X;
_Y += vector.Y;
@ -148,9 +147,9 @@ namespace SM.Base.Types
}
/// <summary>
/// Adds a <see cref="OpenTK.Vector4"/> to the X, Y, Z and W-component.
/// Adds a <see cref="OpenTK.Vector4" /> to the X, Y, Z and W-component.
/// </summary>
protected void Add(OpenTK.Vector4 vector)
protected void Add(Vector4 vector)
{
_X += vector.X;
_Y += vector.Y;
@ -159,18 +158,5 @@ namespace SM.Base.Types
}
#endregion
/// <summary>
/// Transforms a <see cref="CVector"/> to a <see cref="OpenTK.Vector2"/>
/// </summary>
public static implicit operator OpenTK.Vector2(CVector v) => new OpenTK.Vector2(v._x, v._y);
/// <summary>
/// Transforms a <see cref="CVector"/> to a <see cref="OpenTK.Vector3"/>
/// </summary>
public static implicit operator OpenTK.Vector3(CVector v) => new OpenTK.Vector3(v._x, v._y, v._z);
/// <summary>
/// Transforms a <see cref="CVector"/> to a <see cref="OpenTK.Vector4"/>
/// </summary>
public static implicit operator OpenTK.Vector4(CVector v) => new OpenTK.Vector4(v._x, v._y, v._z, v._w);
}
}

View file

@ -1,20 +1,47 @@
namespace SM.Base.Types
#region usings
using OpenTK;
#endregion
namespace SM.Base.Types
{
/// <summary>
/// Represents a vector of two floats.
/// Represents a vector of two floats.
/// </summary>
public class CVector2 : CVector
{
/// <inheritdoc />
public CVector2() : this(0)
{
}
/// <inheritdoc />
public CVector2(float uniform) : base(uniform)
{
}
/// <inheritdoc />
public CVector2(float x, float y) : base(x, y, default, default)
{
}
/// <inheritdoc />
protected CVector2(float x, float y, float z, float w) : base(x, y, z, w)
{
}
/// <summary>
/// X-component
/// X-component
/// </summary>
public float X
{
get => _X;
set => _X = value;
}
/// <summary>
/// Y-component
/// Y-component
/// </summary>
public float Y
{
@ -22,35 +49,30 @@
set => _Y = value;
}
/// <inheritdoc />
public CVector2() : this(0)
{}
/// <inheritdoc />
public CVector2(float uniform) : base(uniform)
/// <summary>
/// Sets the X and Y-component.
/// </summary>
public new void Set(float x, float y)
{
base.Set(x, y);
}
/// <inheritdoc />
public CVector2(float x, float y) : base(x,y, default, default) {}
/// <inheritdoc />
protected CVector2(float x, float y, float z, float w) : base(x, y, z, w) {}
/// <summary>
/// Sets the X and Y-component from a <see cref="OpenTK.Vector2" />
/// </summary>
public new void Set(Vector2 vector)
{
base.Set(vector);
}
/// <summary>
/// Sets the X and Y-component.
/// Adds a <see cref="OpenTK.Vector2" /> to the X and Y-component.
/// </summary>
public new void Set(float x, float y) => base.Set(x, y);
public new void Add(Vector2 vector)
{
base.Add(vector);
}
/// <summary>
/// Sets the X and Y-component from a <see cref="OpenTK.Vector2"/>
/// </summary>
public new void Set(OpenTK.Vector2 vector) => base.Set(vector);
/// <summary>
/// Adds a <see cref="OpenTK.Vector2"/> to the X and Y-component.
/// </summary>
public new void Add(OpenTK.Vector2 vector) => base.Add(vector);
public static implicit operator CVector2(Vector2 v) => new CVector2(v.X,v.Y);
}
}

View file

@ -1,16 +1,33 @@
using System.Drawing.Design;
using System.Runtime.InteropServices;
#region usings
using OpenTK;
#endregion
namespace SM.Base.Types
{
/// <summary>
/// Represents a Vector of three floats.
/// Represents a Vector of three floats.
/// </summary>
public class CVector3 : CVector2
{
/// <inheritdoc />
public CVector3(float uniform) : base(uniform)
{
}
/// <inheritdoc />
public CVector3(float x, float y, float z) : base(x, y, z, default)
{
}
/// <inheritdoc />
protected CVector3(float x, float y, float z, float w) : base(x, y, z, w)
{
}
/// <summary>
/// Z-component
/// Z-component
/// </summary>
public float Z
{
@ -18,29 +35,28 @@ namespace SM.Base.Types
set => _Z = value;
}
/// <inheritdoc />
public CVector3(float uniform) : base(uniform)
{ }
/// <inheritdoc />
public CVector3(float x, float y, float z) : base(x, y, z, default)
{ }
/// <inheritdoc />
protected CVector3(float x, float y, float z, float w) : base(x, y, z, w) { }
/// <summary>
/// Sets the X, Y and Z-component.
/// Sets the X, Y and Z-component.
/// </summary>
public new void Set(float x, float y, float z) => base.Set(x, y, z);
/// <summary>
/// Sets the X, Y and Z-component from a <see cref="OpenTK.Vector3"/>.
/// </summary>
public new void Set(Vector3 vector) => base.Set(vector);
public new void Set(float x, float y, float z)
{
base.Set(x, y, z);
}
/// <summary>
/// Adds a <see cref="OpenTK.Vector3"/> to the X, Y and Z-component.
/// Sets the X, Y and Z-component from a <see cref="OpenTK.Vector3" />.
/// </summary>
public new void Add(Vector3 vector) => base.Add(vector);
public new void Set(Vector3 vector)
{
base.Set(vector);
}
/// <summary>
/// Adds a <see cref="OpenTK.Vector3" /> to the X, Y and Z-component.
/// </summary>
public new void Add(Vector3 vector)
{
base.Add(vector);
}
}
}

View file

@ -1,21 +1,16 @@
using OpenTK;
#region usings
using OpenTK;
#endregion
namespace SM.Base.Types
{
/// <summary>
/// Represents a vector of four floats.
/// Represents a vector of four floats.
/// </summary>
public class CVector4 : CVector3
{
/// <summary>
/// W-component
/// </summary>
public float W
{
get => _W;
set => _W = value;
}
/// <inheritdoc />
public CVector4(float uniform) : base(uniform)
{
@ -27,16 +22,36 @@ namespace SM.Base.Types
}
/// <summary>
/// Sets the X, Y, Z and W-component.
/// W-component
/// </summary>
public new void Set(float x, float y, float z, float w) => base.Set(x, y, z, w);
public float W
{
get => _W;
set => _W = value;
}
/// <summary>
/// Sets the X, Y, Z and W-component from a <see cref="OpenTK.Vector4"/>.
/// Sets the X, Y, Z and W-component.
/// </summary>
public new void Set(Vector4 vector) => base.Set(vector);
public new void Set(float x, float y, float z, float w)
{
base.Set(x, y, z, w);
}
/// <summary>
/// Adds a <see cref="OpenTK.Vector4"/> to the X, Y, Z and W-component.
/// Sets the X, Y, Z and W-component from a <see cref="OpenTK.Vector4" />.
/// </summary>
public new void Add(Vector4 vector) => base.Add(vector);
public new void Set(Vector4 vector)
{
base.Set(vector);
}
/// <summary>
/// Adds a <see cref="OpenTK.Vector4" /> to the X, Y, Z and W-component.
/// </summary>
public new void Add(Vector4 vector)
{
base.Add(vector);
}
}
}

View file

@ -1,43 +1,60 @@
using System;
#region usings
using System;
using System.IO;
using System.Reflection;
#endregion
namespace SM.Utility
{
/// <summary>
/// Contains utility functions for handling with assemblies.
/// Contains utility functions for handling with assemblies.
/// </summary>
public class AssemblyUtility
{
/// <summary>
/// Read a file that is saved in a assembly
/// Read a file that is saved in a assembly
/// </summary>
/// <param name="ass">The assembly that contains the file</param>
/// <param name="path">The path to the file inside the assembly</param>
/// <returns></returns>
public static string ReadAssemblyFile(Assembly ass, string path) { return new StreamReader(GetAssemblyStream(ass, path)).ReadToEnd(); }
public static string ReadAssemblyFile(Assembly ass, string path)
{
return new StreamReader(GetAssemblyStream(ass, path)).ReadToEnd();
}
/// <summary>
/// Read a file that is saved in the calling assembly
/// Read a file that is saved in the calling assembly
/// </summary>
/// <param name="path">The path to the file inside the assembly</param>
/// <returns></returns>
public static string ReadAssemblyFile(string path) { return ReadAssemblyFile(Assembly.GetCallingAssembly(), path); }
public static string ReadAssemblyFile(string path)
{
return ReadAssemblyFile(Assembly.GetCallingAssembly(), path);
}
/// <summary>
/// Returns a stream of a requested resource.
/// Returns a stream of a requested resource.
/// </summary>
/// <param name="ass">The assembly the resource is in.</param>
/// <param name="path">The path to the resource.</param>
/// <returns>The stream</returns>
public static Stream GetAssemblyStream(Assembly ass, string path) { return ass.GetManifestResourceStream(path) ?? throw new InvalidOperationException("Assembly couldn't find resource at path '" + path + "'."); }
public static Stream GetAssemblyStream(Assembly ass, string path)
{
return ass.GetManifestResourceStream(path) ??
throw new InvalidOperationException("Assembly couldn't find resource at path '" + path + "'.");
}
/// <summary>
/// Returns a stream of a requested resource in the calling assembly.
/// Returns a stream of a requested resource in the calling assembly.
/// </summary>
/// <param name="path">The path to the resource</param>
/// <returns>The stream</returns>
public static Stream GetAssemblyStream(string path) { return GetAssemblyStream(Assembly.GetCallingAssembly(), path); }
public static Stream GetAssemblyStream(string path)
{
return GetAssemblyStream(Assembly.GetCallingAssembly(), path);
}
}
}

View file

@ -1,46 +1,49 @@
namespace SM.Utility
{
/// <summary>
/// A assistant to control the delta time.
/// A assistant to control the delta time.
/// </summary>
public class Deltatime
{
/// <summary>
/// The current update delta time.
/// </summary>
public static float UpdateDelta { get; internal set; }
/// <summary>
/// The current render delta time.
/// </summary>
public static float RenderDelta { get; internal set; }
/// <summary>
/// If true, it uses <see cref="RenderDelta"/>, otherwise <see cref="UpdateDelta"/>.
/// <para>Default: false</para>
/// </summary>
public bool UseRender;
/// <summary>
/// Scaling of the delta time.
/// <para>Default: 1</para>
/// Scaling of the delta time.
/// <para>Default: 1</para>
/// </summary>
public float Scale;
/// <summary>
/// The calculated delta time.
/// If true, it uses <see cref="RenderDelta" />, otherwise <see cref="UpdateDelta" />.
/// <para>Default: false</para>
/// </summary>
public float DeltaTime => (UseRender ? RenderDelta : UpdateDelta) * Scale;
public bool UseRender;
/// <summary>
/// Creates a delta time assistant.
/// Creates a delta time assistant.
/// </summary>
/// <param name="scale">The start scale for the delta time. Default: 1</param>
/// <param name="useRender">If true, it uses <see cref="RenderDelta"/>, otherwise <see cref="UpdateDelta"/>. Default: false</param>
public Deltatime(float scale = 1, bool useRender = false)
/// <param name="useRender">
/// If true, it uses <see cref="RenderDelta" />, otherwise <see cref="UpdateDelta" />. Default:
/// false
/// </param>
public Deltatime(float scale = 1, bool useRender = false)
{
UseRender = useRender;
Scale = scale;
}
/// <summary>
/// The current update delta time.
/// </summary>
public static float UpdateDelta { get; internal set; }
/// <summary>
/// The current render delta time.
/// </summary>
public static float RenderDelta { get; internal set; }
/// <summary>
/// The calculated delta time.
/// </summary>
public float DeltaTime => (UseRender ? RenderDelta : UpdateDelta) * Scale;
}
}

View file

@ -1,53 +1,84 @@
using System;
#region usings
using System;
#endregion
namespace SM.Utility
{
/// <summary>
/// A global helper class for randomization.
/// A global helper class for randomization.
/// </summary>
public class Randomize
{
/// <summary>
/// The randomizer.
/// The randomizer.
/// </summary>
public static Random Randomizer = new Random();
/// <summary>
/// Sets the seed for the randomizer.
/// Sets the seed for the randomizer.
/// </summary>
/// <param name="seed">The specified seed.</param>
public static void SetSeed(int seed) { Randomizer = new Random(seed); }
public static void SetSeed(int seed)
{
Randomizer = new Random(seed);
}
/// <summary>
/// Generates a double and checks if its under the tolerance.
/// Generates a double and checks if its under the tolerance.
/// </summary>
public static bool GetBool(float tolerance) { return Randomizer.NextDouble() < tolerance; }
public static bool GetBool(float tolerance)
{
return Randomizer.NextDouble() < tolerance;
}
/// <summary>
/// Generates a integer.
/// Generates a integer.
/// </summary>
public static int GetInt() { return Randomizer.Next(); }
/// <summary>
/// Generates a integer with a maximum.
/// </summary>
public static int GetInt(int max) { return Randomizer.Next(max); }
/// <summary>
/// Generates a integer with a minimum and maximum
/// </summary>
public static int GetInt(int min, int max) { return Randomizer.Next(min, max); }
public static int GetInt()
{
return Randomizer.Next();
}
/// <summary>
/// Generates a float between 0 and 1.
/// Generates a integer with a maximum.
/// </summary>
public static float GetFloat() { return (float)Randomizer.NextDouble(); }
/// <summary>
/// Generates a float between 0 and the specified maximum.
/// </summary>
public static float GetFloat(float max) { return (float)Randomizer.NextDouble() * max; }
/// <summary>
/// Generates a float between the specified minimum and the specified maximum.
/// </summary>
public static float GetFloat(float min, float max) { return (float)Randomizer.NextDouble() * max + min; }
public static int GetInt(int max)
{
return Randomizer.Next(max);
}
/// <summary>
/// Generates a integer with a minimum and maximum
/// </summary>
public static int GetInt(int min, int max)
{
return Randomizer.Next(min, max);
}
/// <summary>
/// Generates a float between 0 and 1.
/// </summary>
public static float GetFloat()
{
return (float) Randomizer.NextDouble();
}
/// <summary>
/// Generates a float between 0 and the specified maximum.
/// </summary>
public static float GetFloat(float max)
{
return (float) Randomizer.NextDouble() * max;
}
/// <summary>
/// Generates a float between the specified minimum and the specified maximum.
/// </summary>
public static float GetFloat(float min, float max)
{
return (float) Randomizer.NextDouble() * max + min;
}
}
}

View file

@ -1,20 +1,24 @@
using System;
#region usings
using System;
using OpenTK;
#endregion
namespace SM.Utility
{
/// <summary>
/// Utilitys for rotations
/// Utilitys for rotations
/// </summary>
public class RotationUtility
{
/// <summary>
/// Angle towards an coordinate.
/// Angle towards an coordinate.
/// </summary>
/// <returns></returns>
public static float TurnTowards(Vector2 origin, Vector2 target)
{
return MathHelper.RadiansToDegrees((float)Math.Atan2(target.Y - origin.Y, target.X - origin.X));
return MathHelper.RadiansToDegrees((float) Math.Atan2(target.Y - origin.Y, target.X - origin.X));
}
}
}

View file

@ -0,0 +1,7 @@
namespace SM.Utility
{
public class ShaderUtility
{
}
}

View file

@ -1,61 +1,71 @@
using System.Collections.Generic;
#region usings
using OpenTK;
using SM.Base.Scene;
using SM.OGL.Mesh;
#endregion
namespace SM.Base.Contexts
{
/// <summary>
/// Contains important information for drawing.
/// Contains important information for drawing.
/// </summary>
public struct DrawContext
{
/// <summary>
/// This says if it was forced to use the viewport camera.
/// This says if it was forced to use the viewport camera.
/// </summary>
public bool ForceViewport;
/// <summary>
/// The current world matrix.
/// The current world matrix.
/// </summary>
public Matrix4 World;
/// <summary>
/// The current view matrix.
/// The current view matrix.
/// </summary>
public Matrix4 View;
/// <summary>
/// The master model matrix.
/// The master model matrix.
/// </summary>
public Matrix4 ModelMaster;
/// <summary>
/// The drawing instances.
/// <para>If there is only one, it's index 0</para>
/// The drawing instances.
/// <para>If there is only one, it's index 0</para>
/// </summary>
public Instance[] Instances;
/// <summary>
/// The mesh.
/// The mesh.
/// </summary>
public GenericMesh Mesh;
/// <summary>
/// The material.
/// The material.
/// </summary>
public Material Material;
/// <summary>
/// Contains the currently used render pipeline.
/// Contains the currently used render pipeline.
/// </summary>
public RenderPipeline ActivePipeline;
/// <summary>
/// The current world scale.
/// The current world scale.
/// </summary>
public Vector2 WorldScale;
/// <summary>
/// Returns the appropriate shader.
/// <para>Returns the material shader, if available, otherwise it will take the default shader from the render pipeline.</para>
/// Returns the appropriate shader.
/// <para>
/// Returns the material shader, if available, otherwise it will take the default shader from the render
/// pipeline.
/// </para>
/// </summary>
public IShader Shader => Material.CustomShader ?? ActivePipeline._defaultShader;
public MaterialShader Shader => Material.CustomShader ?? ActivePipeline._defaultShader;
}
}

View file

@ -1,25 +1,28 @@
using OpenTK.Input;
using SM.Utility;
#region usings
using OpenTK.Input;
#endregion
namespace SM.Base.Contexts
{
/// <summary>
/// The update context.
/// The update context.
/// </summary>
public struct UpdateContext
{
/// <summary>
/// The delta time.
/// The delta time.
/// </summary>
public float Deltatime => SMRenderer.DefaultDeltatime.DeltaTime;
/// <summary>
/// The current keyboard state.
/// The current keyboard state.
/// </summary>
public KeyboardState KeyboardState;
/// <summary>
/// The current mouse state.
/// The current mouse state.
/// </summary>
public MouseState MouseState;
}

View file

@ -1,53 +1,86 @@
using System;
using System.IO;
#region usings
using System;
using System.Drawing;
using System.Linq;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Input;
using SM.Base.Contexts;
using SM.Base.Objects.Static;
using SM.Base.PostProcess;
using SM.Base.Scene;
using SM.Base.ShaderExtension;
using SM.Base.Time;
using SM.OGL;
using SM.OGL.Shaders;
using SM.OGL.Framebuffer;
using SM.Utility;
#endregion
namespace SM.Base
{
/// <summary>
/// The base window.
/// The base window.
/// </summary>
public abstract class GenericWindow : GameWindow
{
private bool _loading = false;
private bool _loading;
/// <summary>
/// This tells you the current world scale.
/// This tells you the current world scale.
/// </summary>
protected Vector2 _worldScale = Vector2.Zero;
/// <summary>
/// This tells you the current aspect ratio of this window.
/// This tells you the current aspect ratio of this window.
/// </summary>
public float Aspect { get; private set; } = 0f;
public float Aspect { get; private set; }
public bool ReactWhileUnfocused = false;
/// <inheritdoc />
protected GenericWindow() : base(1280, 720, GraphicsMode.Default, "Generic OGL Title", GameWindowFlags.Default,
DisplayDevice.Default, 0, 0, GraphicsContextFlags.Default, null, true)
{ }
protected GenericWindow() : this(1280, 720, "Generic OGL Title", GameWindowFlags.Default)
{
}
protected GenericWindow(int width, int height, string title, GameWindowFlags flags, bool vSync = true) : base(width, height,
GraphicsMode.Default, title, flags, DisplayDevice.Default, GLSettings.ForcedVersion.MajorVersion,
GLSettings.ForcedVersion.MinorVersion, GraphicsContextFlags.Default)
{
VSync = vSync ? VSyncMode.On : VSyncMode.Off;
}
/// <inheritdoc />
protected override void OnLoad(EventArgs e)
{
GLSystem.INIT_SYSTEM();
GLSettings.ShaderPreProcessing = true;
var args = Environment.GetCommandLineArgs();
if (args.Contains("--advDebugging"))
{
SMRenderer.AdvancedDebugging = true;
GLSettings.InfoEveryUniform = true;
}
Log.Init();
Log.Write("#", ConsoleColor.Cyan, "----------------------",
"--- OpenGL Loading ---",
"----------------------------------",
$"--- {"DeviceVersion",14}: {GLSystem.DeviceVersion,-10} ---",
$"--- {"ForcedVersion",14}: {GLSystem.ForcedVersion,-10} ---",
$"--- {"ForcedVersion",14}: {GLSettings.ForcedVersion,-10} ---",
$"--- {"ShadingVersion",14}: {GLSystem.ShadingVersion,-10} ---",
$"--- {"Debugging",14}: {GLSystem.Debugging,-10} ---",
$"----------------------------------");
$"--- {"AdvDebugging",14}: {SMRenderer.AdvancedDebugging,-10} ---",
"----------------------------------");
if (SMRenderer.AdvancedDebugging) Log.Write("Extension", ConsoleColor.DarkCyan, GLSystem.Extensions);
ExtensionManager.InitExtensions();
base.OnLoad(e);
_loading = true;
@ -58,7 +91,7 @@ namespace SM.Base
{
base.OnResize(e);
Aspect = (float)Width / Height;
Aspect = (float) Width / Height;
_worldScale = new Vector2(Width, Height);
SetWorldScale();
GL.Viewport(ClientRectangle);
@ -71,25 +104,28 @@ namespace SM.Base
}
/// <summary>
/// This is triggered after all the window-loading has been done.
/// This is triggered after all the window-loading has been done.
/// </summary>
protected virtual void OnLoaded()
{
}
/// <summary>
/// Sets the world scale.
/// Sets the world scale.
/// </summary>
protected virtual void SetWorldScale() { }
protected virtual void SetWorldScale()
{
}
/// <inheritdoc />
protected override void OnUpdateFrame(FrameEventArgs e)
{
if (!ReactWhileUnfocused && !Focused) return;
base.OnUpdateFrame(e);
Deltatime.UpdateDelta = (float)e.Time;
UpdateContext context = new UpdateContext()
Deltatime.UpdateDelta = (float) e.Time;
var context = new UpdateContext
{
KeyboardState = Keyboard.GetState(),
MouseState = Mouse.GetState()
@ -99,7 +135,7 @@ namespace SM.Base
}
/// <summary>
/// Updates the system.
/// Updates the system.
/// </summary>
/// <param name="e"></param>
/// <param name="context"></param>
@ -109,7 +145,7 @@ namespace SM.Base
}
/// <summary>
/// Grabs the cursor and make sure it doesn't leave the window.
/// Grabs the cursor and make sure it doesn't leave the window.
/// </summary>
/// <param name="makeItInvisible">If true, it makes the cursor invisible.</param>
public void GrabCursor(bool makeItInvisible = true)
@ -119,17 +155,42 @@ namespace SM.Base
}
/// <summary>
/// Ungrabs the cursor.
/// Ungrabs the cursor.
/// </summary>
public void UngrabCursor()
{
CursorGrabbed = false;
if (!CursorVisible) CursorVisible = true;
}
public Bitmap TakeScreenshot(Framebuffer framebuffer, ReadBufferMode readBuffer, int x, int y, int width, int height)
{
GL.GetInteger(GetPName.FramebufferBinding, out int prevFBId);
GL.GetInteger(GetPName.DrawFramebufferBinding, out int prevFBDrawId);
GL.GetInteger(GetPName.ReadFramebufferBinding, out int prevFBReadId);
Bitmap b = new Bitmap(width, height);
System.Drawing.Imaging.BitmapData bits = b.LockBits(new Rectangle(0, 0, width, height),
System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, framebuffer);
GL.ReadBuffer(readBuffer);
GL.ReadPixels(x, y, width, height, OpenTK.Graphics.OpenGL4.PixelFormat.Bgra, PixelType.UnsignedByte,
bits.Scan0);
b.UnlockBits(bits);
b.RotateFlip(RotateFlipType.RotateNoneFlipY);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, prevFBId);
GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, prevFBDrawId);
GL.BindFramebuffer(FramebufferTarget.ReadFramebuffer, prevFBReadId);
return b;
}
}
/// <summary>
/// The base window.
/// The base window.
/// </summary>
/// <typeparam name="TScene">The scene type</typeparam>
/// <typeparam name="TCamera">The camera type</typeparam>
@ -137,31 +198,32 @@ namespace SM.Base
where TScene : GenericScene, new()
where TCamera : GenericCamera, new()
{
/// <summary>
/// The viewport camera.
/// </summary>
public TCamera ViewportCamera { get; }
/// <summary>
/// This forces the render to use the viewport camera.
/// </summary>
public bool ForceViewportCamera { get; set; } = false;
/// <summary>
/// The current scene.
/// </summary>
public TScene CurrentScene { get; private set; }
/// <summary>
/// Controls how a scene is rendered.
/// </summary>
public RenderPipeline<TScene> RenderPipeline { get; private set; }
/// <inheritdoc />
protected GenericWindow()
{
ViewportCamera = new TCamera();
}
/// <summary>
/// The viewport camera.
/// </summary>
public TCamera ViewportCamera { get; }
/// <summary>
/// This forces the render to use the viewport camera.
/// </summary>
public bool ForceViewportCamera { get; set; } = false;
/// <summary>
/// The current scene.
/// </summary>
public TScene CurrentScene { get; private set; }
/// <summary>
/// Controls how a scene is rendered.
/// </summary>
public RenderPipeline<TScene> RenderPipeline { get; private set; }
/// <inheritdoc />
protected override void Update(FrameEventArgs e, ref UpdateContext context)
{
@ -172,15 +234,21 @@ namespace SM.Base
/// <inheritdoc />
protected override void OnRenderFrame(FrameEventArgs e)
{
if (!ReactWhileUnfocused && !Focused) return;
SMRenderer.CurrentFrame++;
Deltatime.RenderDelta = (float)e.Time;
DrawContext drawContext = new DrawContext()
Deltatime.RenderDelta = (float) e.Time;
var drawContext = new DrawContext
{
World = ViewportCamera.World,
View = ViewportCamera.CalculateViewMatrix(),
ModelMaster = Matrix4.Identity,
Instances = new[] { new Instance {ModelMatrix = Matrix4.Identity, TexturePosition = Vector2.Zero, TextureScale = Vector2.One } },
Instances = new[]
{
new Instance
{ModelMatrix = Matrix4.Identity, TexturePosition = Vector2.Zero, TextureScale = Vector2.One}
},
Mesh = Plate.Object,
ForceViewport = ForceViewportCamera,
WorldScale = _worldScale
@ -202,10 +270,14 @@ namespace SM.Base
ViewportCamera.RecalculateWorld(_worldScale, Aspect);
RenderPipeline.Resize();
PostProcessEffect.Mvp = Matrix4.CreateScale(_worldScale.X, -_worldScale.Y, 1) *
Matrix4.LookAt(0, 0, 1, 0, 0, 0, 0, 1, 0) *
GenericCamera.OrthographicWorld;
}
/// <summary>
/// Sets the scene.
/// Sets the scene.
/// </summary>
/// <param name="scene"></param>
public virtual void SetScene(TScene scene)
@ -215,7 +287,7 @@ namespace SM.Base
}
/// <summary>
/// Defines the render pipeline.
/// Defines the render pipeline.
/// </summary>
/// <param name="pipeline"></param>
public void SetRenderPipeline(RenderPipeline<TScene> pipeline)

View file

@ -1,70 +1,79 @@
using System.Collections.Generic;
#region usings
using System.Collections.Generic;
using System.Threading;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.OGL.Framebuffer;
#endregion
namespace SM.Base
{
/// <summary>
/// Definition of specific render options.
/// Definition of specific render options.
/// </summary>
public abstract class RenderPipeline
{
/// <summary>
/// The framebuffers, that are used in this Pipeline.
/// The framebuffers, that are used in this Pipeline.
/// </summary>
protected virtual List<Framebuffer> _framebuffers { get; }
/// <summary>
/// The default shader for the pipeline.
/// </summary>
protected internal virtual IShader _defaultShader { get; }
/// <summary>
/// Occurs, when the window is loading.
/// The default shader for the pipeline.
/// </summary>
protected internal virtual MaterialShader _defaultShader { get; } = SMRenderer.DefaultMaterialShader;
/// <summary>
/// Occurs, when the window is loading.
/// </summary>
protected internal virtual void Load()
{
foreach (Framebuffer framebuffer in _framebuffers)
framebuffer.Compile();
}
/// <summary>
/// Occurs, when the window is resizing.
/// Occurs, when the window is resizing.
/// </summary>
protected internal virtual void Resize()
{ }
{
if (_framebuffers == null) return;
foreach (var framebuffer in _framebuffers)
framebuffer.Dispose();
Thread.Sleep(50);
foreach (Framebuffer framebuffer in _framebuffers)
{
framebuffer.Compile();
}
}
/// <summary>
/// Occurs, when the pipeline was connected to a window.
/// Occurs, when the pipeline was connected to a window.
/// </summary>
protected internal virtual void Activate(GenericWindow window)
{
}
/// <summary>
/// Occurs, when the window is unloading.
/// Occurs, when the window is unloading.
/// </summary>
protected internal virtual void Unload()
{
foreach (Framebuffer framebuffer in _framebuffers)
{
framebuffer.Dispose();
}
}
}
/// <summary>
/// Represents a render pipeline.
/// Represents a render pipeline.
/// </summary>
/// <typeparam name="TScene">The scene type</typeparam>
public abstract class RenderPipeline<TScene> : RenderPipeline
where TScene : GenericScene
{
/// <summary>
/// The system to render stuff.
/// The system to render stuff.
/// </summary>
protected internal virtual void Render(ref DrawContext context, TScene scene)
{

View file

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="OpenTK" version="3.2" targetFramework="net452" />
<package id="OpenTK" version="3.2.1" targetFramework="net452" />
</packages>

View file

@ -1,11 +1,20 @@
using System;
#region usings
using System;
using OpenTK.Graphics.OpenGL4;
using SM.OGL.Texture;
#endregion
namespace SM.OGL.Framebuffer
{
public class ColorAttachment : TextureBase
{
public ColorAttachment(int attachmentId)
{
AttachmentID = attachmentId;
}
public int AttachmentID { get; }
public FramebufferAttachment FramebufferAttachment => FramebufferAttachment.ColorAttachment0 + AttachmentID;
@ -13,23 +22,22 @@ namespace SM.OGL.Framebuffer
public ReadBufferMode ReadBufferMode => ReadBufferMode.ColorAttachment0 + AttachmentID;
public DrawBuffersEnum DrawBuffersEnum => DrawBuffersEnum.ColorAttachment0 + AttachmentID;
public ColorAttachment(int attachmentId)
{
AttachmentID = attachmentId;
}
public void Generate(Framebuffer f)
{
_id = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, _id);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8,
(int) f.Size.X, (int) f.Size.Y,
(int) f.Size.X, (int) f.Size.Y,
0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureParameterName.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureParameterName.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,
(int) TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,
(int) TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS,
(int) TextureParameterName.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT,
(int) TextureParameterName.ClampToEdge);
}
}
}

View file

@ -1,15 +1,18 @@
using System;
#region usings
using System;
using System.Collections.Generic;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Platform;
#endregion
namespace SM.OGL.Framebuffer
{
// TODO: Write summeries for framebuffer-system.
public class Framebuffer : GLObject
{
public static readonly Framebuffer Screen = new Framebuffer()
public static readonly Framebuffer Screen = new Framebuffer
{
_id = 0,
_canBeCompiled = false
@ -17,34 +20,45 @@ namespace SM.OGL.Framebuffer
private bool _canBeCompiled = true;
public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Framebuffer;
public Vector2 Size { get; private set; }
public Dictionary<string, ColorAttachment> ColorAttachments { get; private set; } = new Dictionary<string, ColorAttachment>();
private INativeWindow _window;
private float _windowScale;
public Framebuffer()
{ }
{
}
public Framebuffer(INativeWindow window, float scale = 1) : this(new Vector2(window.Width * scale, window.Height * scale))
{ }
public Framebuffer(INativeWindow window, float scale = 1) : this(new Vector2(window.Width * scale,
window.Height * scale))
{
_window = window;
_windowScale = scale;
}
public Framebuffer(Vector2 size)
{
Size = size;
}
public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Framebuffer;
public Vector2 Size { get; private set; }
public Dictionary<string, ColorAttachment> ColorAttachments { get; private set; } =
new Dictionary<string, ColorAttachment>();
public override void Compile()
{
if (!_canBeCompiled) return;
if (_window != null) Size = new Vector2(_window.Width * _windowScale, _window.Height * _windowScale);
base.Compile();
_id = GL.GenFramebuffer();
GL.BindFramebuffer(FramebufferTarget.Framebuffer, _id);
DrawBuffersEnum[] enums = new DrawBuffersEnum[ColorAttachments.Count];
int c = 0;
foreach (KeyValuePair<string, ColorAttachment> pair in ColorAttachments)
var enums = new DrawBuffersEnum[ColorAttachments.Count];
var c = 0;
foreach (var pair in ColorAttachments)
{
pair.Value.Generate(this);
@ -53,11 +67,12 @@ namespace SM.OGL.Framebuffer
GL.DrawBuffers(enums.Length, enums);
foreach (var pair in ColorAttachments)
GL.FramebufferTexture(FramebufferTarget.Framebuffer, pair.Value.FramebufferAttachment, pair.Value.ID, 0);
GL.FramebufferTexture(FramebufferTarget.Framebuffer, pair.Value.FramebufferAttachment, pair.Value.ID,
0);
FramebufferErrorCode err = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
var err = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
if (err != FramebufferErrorCode.FramebufferComplete)
throw new Exception("Failed loading framebuffer.\nProblem: "+err);
throw new Exception("Failed loading framebuffer.\nProblem: " + err);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
GL.BindTexture(TextureTarget.Texture2D, 0);
@ -66,23 +81,36 @@ namespace SM.OGL.Framebuffer
public override void Dispose()
{
base.Dispose();
foreach (ColorAttachment attachment in ColorAttachments.Values) attachment.Dispose();
foreach (var attachment in ColorAttachments.Values) attachment.Dispose();
GL.DeleteFramebuffer(this);
}
public void Append(string key, int pos) => Append(key, new ColorAttachment(pos));
public void Append(string key, ColorAttachment value)
{
ColorAttachments.Add(key, value);
}
public void Activate() => Activate(FramebufferTarget.Framebuffer, ClearBufferMask.None);
public void Activate(FramebufferTarget target) => Activate(target, ClearBufferMask.None);
public void Activate(ClearBufferMask clearMask) => Activate(FramebufferTarget.Framebuffer, clearMask);
public void Activate()
{
Activate(FramebufferTarget.Framebuffer, ClearBufferMask.None);
}
public void Activate(FramebufferTarget target)
{
Activate(target, ClearBufferMask.None);
}
public void Activate(ClearBufferMask clearMask)
{
Activate(FramebufferTarget.Framebuffer, clearMask);
}
public void Activate(FramebufferTarget target, ClearBufferMask clear)
{
GL.Clear(clear);
GL.BindFramebuffer(target, this);
GL.Clear(clear);
}
}
}

View file

@ -0,0 +1,47 @@
#region usings
using System;
using OpenTK.Graphics.OpenGL4;
#endregion
namespace SM.OGL
{
public class GLCustomActions
{
/// <summary>
/// A action that is performed, when a OpenGL-error occurs.
/// </summary>
public static Action<DebugSource, DebugType, DebugSeverity, string> AtKHRDebug = DefaultDebugAction;
/// <summary>
/// A action, that is performed, when a GLError occurred.
/// <para>Doesn't account for "KHR_debugging"</para>
/// </summary>
public static Action<string> AtError;
/// <summary>
/// A action, that is performed, when a warning want to be shown.
/// </summary>
public static Action<string> AtWarning;
/// <summary>
/// A action, that is performed, when a information needs to be shown.
/// </summary>
public static Action<string> AtInfo;
/// <summary>
/// Default action for 'AtKHRDebug'.
/// </summary>
/// <param name="source"></param>
/// <param name="type"></param>
/// <param name="severity"></param>
/// <param name="msg"></param>
private static void DefaultDebugAction(DebugSource source, DebugType type, DebugSeverity severity, string msg)
{
Console.WriteLine($"{severity}, {type}, {source} -> {msg}");
if (type == DebugType.DebugTypeError) throw new Exception(msg);
}
}
}

View file

@ -1,36 +1,33 @@
using System;
#region usings
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Runtime.InteropServices;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Platform.Egl;
using ErrorCode = OpenTK.Graphics.OpenGL4.ErrorCode;
#endregion
namespace SM.OGL
{
/// <summary>
/// Contains everything that is needed to debug OpenGL
/// Contains everything that is needed to debug OpenGL
/// </summary>
public static class GLDebugging
{
private static DebugProc _debugProc = DebugCallback;
private static GCHandle _debugGcHandle;
/// <summary>
/// A action that is performed, when a OpenGL-error occurs.
/// </summary>
public static Action<DebugSource, DebugType, DebugSeverity, string> DebugAction = DefaultDebugAction;
[DebuggerStepThrough]
private static void DebugCallback(DebugSource source, DebugType type, int id, DebugSeverity severity,
int length, IntPtr message, IntPtr userparam)
{
string msg = Marshal.PtrToStringAnsi(message, length);
DebugAction?.Invoke(source, type, severity, msg);
var msg = Marshal.PtrToStringAnsi(message, length);
GLCustomActions.AtKHRDebug?.Invoke(source, type, severity, msg);
}
/// <summary>
/// Enables the debugging.
/// Enables the debugging.
/// </summary>
public static void EnableDebugging()
{
@ -47,40 +44,33 @@ namespace SM.OGL
Console.WriteLine("Enableing proper GLDebugging failed. \n" +
"Often it fails, because your hardware doesn't provide proper OpenGL 4 \n" +
" or KHR_debug extension support.");
}
}
/// <summary>
/// Default action for 'DebugAction'.
/// </summary>
/// <param name="source"></param>
/// <param name="type"></param>
/// <param name="severity"></param>
/// <param name="msg"></param>
public static void DefaultDebugAction(DebugSource source, DebugType type, DebugSeverity severity, string msg)
{
Console.WriteLine($"{severity}, {type}, {source} -> {msg}");
if (type == DebugType.DebugTypeError) throw new Exception(msg);
}
/// <summary>
/// A action, that is performed, when <see cref="GLDebugging.CheckGLErrors"/> find an error.
/// </summary>
public static Action<ErrorCode> GlErrorAction;
/// <summary>
/// Checks for OpenGL errors.
/// Checks for OpenGL errors.
/// </summary>
public static bool CheckGLErrors()
{
bool hasError = false;
var hasError = false;
ErrorCode c;
while ((c = GL.GetError()) != ErrorCode.NoError)
{
hasError = true;
GlErrorAction?.Invoke(c);
GLCustomActions.AtError?.Invoke("A GLError occurred: " + c);
}
return hasError;
}
public static bool CheckGLErrors(string formating)
{
var hasError = false;
ErrorCode c;
while ((c = GL.GetError()) != ErrorCode.NoError)
{
hasError = true;
GLCustomActions.AtError?.Invoke(formating.Replace("%code%", c.ToString()));
}
return hasError;

View file

@ -1,30 +1,35 @@
using System.Diagnostics;
#region usings
using System.Diagnostics;
using OpenTK.Graphics.OpenGL4;
#endregion
namespace SM.OGL
{
/// <summary>
/// Specifies default object behaviour.
/// Specifies default object behaviour.
/// </summary>
public abstract class GLObject
{
/// <summary>
/// Contains the OpenGL ID
/// Contains the OpenGL ID
/// </summary>
protected int _id = -1;
/// <summary>
/// If true, the system will call "Compile()", when "ID" is tried to get, but the id is still -1.
/// If true, the system will call "Compile()", when "ID" is tried to get, but the id is still -1.
/// </summary>
protected virtual bool AutoCompile { get; } = false;
/// <summary>
/// Checks if the object was compiled.
/// Checks if the object was compiled.
/// </summary>
public bool WasCompiled => _id > 0;
/// <summary>
/// Returns the id for this object.
/// <para>It will auto compile, if needed and allowed.</para>
/// Returns the id for this object.
/// <para>It will auto compile, if needed and allowed.</para>
/// </summary>
public virtual int ID
{
@ -36,7 +41,7 @@ namespace SM.OGL
}
/// <summary>
/// Identifies the object.
/// Identifies the object.
/// </summary>
public abstract ObjectLabelIdentifier TypeIdentifier { get; }
@ -47,21 +52,21 @@ namespace SM.OGL
}
/// <summary>
/// The action, that is called, when "ID" tries to compile something.
/// The action, that is called, when "ID" tries to compile something.
/// </summary>
public virtual void Compile()
{
}
/// <summary>
/// Is triggered, when something want to dispose this object.
/// Is triggered, when something want to dispose this object.
/// </summary>
public virtual void Dispose() {}
public virtual void Dispose()
{
}
/// <summary>
/// Re-compiles the object.
/// Re-compiles the object.
/// </summary>
public void Recompile()
{
@ -72,7 +77,7 @@ namespace SM.OGL
}
/// <summary>
/// Names the object for debugging.
/// Names the object for debugging.
/// </summary>
/// <param name="name"></param>
public void Name(string name)
@ -81,9 +86,12 @@ namespace SM.OGL
}
/// <summary>
/// Returns the ID for the object.
/// Returns the ID for the object.
/// </summary>
/// <param name="glo"></param>
public static implicit operator int(GLObject glo) => glo.ID;
public static implicit operator int(GLObject glo)
{
return glo.ID;
}
}
}

View file

@ -0,0 +1,15 @@
namespace SM.OGL
{
public class GLSettings
{
public static bool InfoEveryUniform = false;
/// <summary>
/// Get/Sets the forced version for OpenGL.
/// <para>Needs to be set before init a window.</para>
/// </summary>
public static Version ForcedVersion { get; set; } = new Version();
public static bool ShaderPreProcessing { get; set; } = false;
}
}

View file

@ -1,43 +1,44 @@
using System.Linq;
#region usings
using System.Linq;
using OpenTK.Graphics.OpenGL4;
#endregion
namespace SM.OGL
{
/// <summary>
/// Contains data about the current OpenGL system.
/// Contains data and settings about the current OpenGL system.
/// </summary>
public class GLSystem
{
private static bool _init = false;
private static bool _init;
/// <summary>
/// Contains the device version of OpenGL.
/// Contains the device version of OpenGL.
/// </summary>
public static Version DeviceVersion { get; private set; }
/// <summary>
/// Get/Sets the forced version for OpenGL.
/// <para>Needs to be set before init a window.</para>
/// </summary>
public static Version ForcedVersion { get; set; } = new Version();
/// <summary>
/// Contains the shader version for GLSL.
/// Contains the shader version for GLSL.
/// </summary>
public static Version ShadingVersion { get; private set; }
/// <summary>
/// Contains the extensions for OpenGL.
/// Contains the extensions for OpenGL.
/// </summary>
public static string[] Extensions { get; private set; }
/// <summary>
/// Checks if proper Debugging is for this system available.
/// <para>Determent, if the system has the "KHR_debug"-extension. </para>
/// Checks if proper Debugging is for this system available.
/// <para>Determent, if the system has the "KHR_debug"-extension. </para>
/// </summary>
public static bool Debugging { get; private set; }
/// <summary>
/// Initialize the system data.
/// <para>Does nothing after the data was already collected.</para>
/// Initialize the system data.
/// <para>Does nothing after the data was already collected.</para>
/// </summary>
public static void INIT_SYSTEM()
{

View file

@ -1,39 +1,36 @@
using System;
using System.Runtime.CompilerServices;
#region usings
using System;
using OpenTK;
#endregion
namespace SM.OGL.Mesh
{
/// <summary>
/// Contains information about bounding boxes of meshes
/// Contains information about bounding boxes of meshes
/// </summary>
public class BoundingBox
{
/// <summary>
/// The minimum corner.
/// </summary>
public Vector3 Min = Vector3.Zero;
/// <summary>
/// The maximum corner.
/// The maximum corner.
/// </summary>
public Vector3 Max = Vector3.Zero;
/// <summary>
/// Returns specific configurations of corners
/// The minimum corner.
/// </summary>
/// <param name="x">If true, it takes the X-value of maximum, otherwise the minimum.</param>
/// <param name="y">If true, it takes the Y-value of maximum, otherwise the minimum.</param>
/// <param name="z">If true, it takes the Z-value of maximum, otherwise the minimum.</param>
/// <returns></returns>
public Vector3 this[bool x, bool y, bool z] => new Vector3(x ? Max.X : Min.X, y ? Max.Y : Min.Y, z ? Max.Z : Min.Z);
public Vector3 Min = Vector3.Zero;
/// <summary>
/// Empty constructor
/// Empty constructor
/// </summary>
public BoundingBox() {}
public BoundingBox()
{
}
/// <summary>
/// Creates the bounding box with predefined min and max values
/// Creates the bounding box with predefined min and max values
/// </summary>
/// <param name="min"></param>
/// <param name="max"></param>
@ -44,12 +41,22 @@ namespace SM.OGL.Mesh
}
/// <summary>
/// Updates the bounding box.
/// Returns specific configurations of corners
/// </summary>
/// <param name="x">If true, it takes the X-value of maximum, otherwise the minimum.</param>
/// <param name="y">If true, it takes the Y-value of maximum, otherwise the minimum.</param>
/// <param name="z">If true, it takes the Z-value of maximum, otherwise the minimum.</param>
/// <returns></returns>
public Vector3 this[bool x, bool y, bool z] =>
new Vector3(x ? Max.X : Min.X, y ? Max.Y : Min.Y, z ? Max.Z : Min.Z);
/// <summary>
/// Updates the bounding box.
/// </summary>
/// <param name="vector"></param>
public void Update(Vector2 vector)
{
for (int i = 0; i < 2; i++)
for (var i = 0; i < 2; i++)
{
Min[i] = Math.Min(Min[i], vector[i]);
Max[i] = Math.Max(Max[i], vector[i]);
@ -57,12 +64,12 @@ namespace SM.OGL.Mesh
}
/// <summary>
/// Updates the bounding box.
/// Updates the bounding box.
/// </summary>
/// <param name="vector"></param>
public void Update(Vector3 vector)
{
for (int i = 0; i < 3; i++)
for (var i = 0; i < 3; i++)
{
Min[i] = Math.Min(Min[i], vector[i]);
Max[i] = Math.Max(Min[i], vector[i]);

View file

@ -1,15 +1,31 @@
using System;
#region usings
using System;
using System.Collections.Generic;
using OpenTK.Graphics.OpenGL4;
using Buffer = OpenTK.Graphics.OpenGL4.Buffer;
#endregion
namespace SM.OGL.Mesh
{
/// <summary>
/// Contains information for meshes
/// Contains information for meshes
/// </summary>
public abstract class GenericMesh : GLObject
{
/// <summary>
/// Generates the AttribDataIndex
/// </summary>
protected GenericMesh()
{
AttribDataIndex = new Dictionary<int, VBO>
{
{0, Vertex},
{1, UVs},
{2, Normals}
};
}
/// <inheritdoc />
protected override bool AutoCompile { get; } = true;
@ -17,61 +33,50 @@ namespace SM.OGL.Mesh
public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.VertexArray;
/// <summary>
/// The primitive type, that determinants how the mesh is drawn.
/// <para>Default: Triangles</para>
/// The primitive type, that determinants how the mesh is drawn.
/// <para>Default: Triangles</para>
/// </summary>
public virtual PrimitiveType PrimitiveType { get; } = PrimitiveType.Triangles;
/// <summary>
/// Contains the vertices for the mesh.
/// Contains the vertices for the mesh.
/// </summary>
public virtual VBO Vertex { get; }
/// <summary>
/// Contains the texture coords for the mesh.
/// Contains the texture coords for the mesh.
/// </summary>
public virtual VBO UVs { get; }
/// <summary>
/// Contains the normals for the mesh.
/// Contains the normals for the mesh.
/// </summary>
public virtual VBO Normals { get; }
/// <summary>
/// Represents the bounding box.
/// Represents the bounding box.
/// </summary>
public virtual BoundingBox BoundingBox { get; } = new BoundingBox();
/// <summary>
/// Connects the different buffer objects with ids.
/// Connects the different buffer objects with ids.
/// </summary>
public Dictionary<int, VBO> AttribDataIndex { get; }
/// <summary>
/// Stores indices for a more performance friendly method to draw objects.
/// Stores indices for a more performance friendly method to draw objects.
/// </summary>
public virtual int[] Indices { get; set; }
/// <summary>
/// Generates the AttribDataIndex
/// </summary>
protected GenericMesh()
{
AttribDataIndex = new Dictionary<int, VBO>()
{
{0, Vertex},
{1, UVs},
{2, Normals},
};
}
/// <inheritdoc />
public override void Compile()
{
_id = GL.GenVertexArray();
GL.BindVertexArray(_id);
if (AttribDataIndex == null) throw new Exception("[Critical] The model requires a attribute data index.");
foreach (KeyValuePair<int, VBO> kvp in AttribDataIndex) kvp.Value?.BindBuffer(kvp.Key);
foreach (var kvp in AttribDataIndex) kvp.Value?.BindBuffer(kvp.Key);
GL.BindVertexArray(0);
}

View file

@ -1,51 +1,78 @@
using System;
#region usings
using System.Collections.Generic;
using System.Linq;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
#endregion
namespace SM.OGL.Mesh
{
/// <summary>
/// Represents a Vertex Buffer Object used for meshes.
/// Represents a Vertex Buffer Object used for meshes.
/// </summary>
public class VBO : List<float>
{
/// <summary>
/// Specifies the expected usage pattern of the data store.
/// Specifies the expected usage pattern of the data store.
/// </summary>
public BufferUsageHint BufferUsageHint;
/// <summary>
/// Specifies the data type of each component in the array.
/// </summary>
public VertexAttribPointerType PointerType;
/// <summary>
/// Specifies the number of components per generic vertex attribute. Must be 1, 2, 3, 4.
/// </summary>
public int PointerSize;
/// <summary>
/// Normalise floats?
/// Normalise floats?
/// </summary>
public bool Normalised;
/// <summary>
/// Specifies the byte offset between consecutive generic vertex attributes.
/// </summary>
public int PointerStride;
/// <summary>
/// Specifies a offset of the first component of the first generic vertex attribute in the array in the data store of the buffer currently bound to the GL_ARRAY_BUFFER target.
/// Specifies a offset of the first component of the first generic vertex attribute in the array in the data store of
/// the buffer currently bound to the GL_ARRAY_BUFFER target.
/// </summary>
public int PointerOffset;
/// <summary>
/// Generates a VBO for inserting mesh data.
/// Specifies the number of components per generic vertex attribute. Must be 1, 2, 3, 4.
/// </summary>
/// <param name="bufferUsageHint">Specifies the expected usage pattern of the data store. <para>Default: StaticDraw</para></param>
/// <param name="pointerType">Specifies the data type of each component in the array. <para>Default: Float</para></param>
/// <param name="pointerSize">Specifies the number of components per generic vertex attribute. Must be 1, 2, 3, 4. <para>Default: 3</para></param>
/// <param name="pointerStride">Specifies the byte offset between consecutive generic vertex attributes. <para>Default: 0</para></param>
/// <param name="pointerOffset">Specifies a offset of the first component of the first generic vertex attribute in the array in the data store of the buffer currently bound to the GL_ARRAY_BUFFER target. <para>Default: 0</para></param>
/// <param name="normalised">Normalise floats? <para>Default: false</para></param>
public int PointerSize;
/// <summary>
/// Specifies the byte offset between consecutive generic vertex attributes.
/// </summary>
public int PointerStride;
/// <summary>
/// Specifies the data type of each component in the array.
/// </summary>
public VertexAttribPointerType PointerType;
/// <summary>
/// Generates a VBO for inserting mesh data.
/// </summary>
/// <param name="bufferUsageHint">
/// Specifies the expected usage pattern of the data store.
/// <para>Default: StaticDraw</para>
/// </param>
/// <param name="pointerType">
/// Specifies the data type of each component in the array.
/// <para>Default: Float</para>
/// </param>
/// <param name="pointerSize">
/// Specifies the number of components per generic vertex attribute. Must be 1, 2, 3, 4.
/// <para>Default: 3</para>
/// </param>
/// <param name="pointerStride">
/// Specifies the byte offset between consecutive generic vertex attributes.
/// <para>Default: 0</para>
/// </param>
/// <param name="pointerOffset">
/// Specifies a offset of the first component of the first generic vertex attribute in the
/// array in the data store of the buffer currently bound to the GL_ARRAY_BUFFER target.
/// <para>Default: 0</para>
/// </param>
/// <param name="normalised">
/// Normalise floats?
/// <para>Default: false</para>
/// </param>
public VBO(BufferUsageHint bufferUsageHint = BufferUsageHint.StaticDraw,
VertexAttribPointerType pointerType = VertexAttribPointerType.Float, int pointerSize = 3,
int pointerStride = 0, int pointerOffset = 0, bool normalised = false)
@ -59,56 +86,95 @@ namespace SM.OGL.Mesh
}
/// <summary>
/// Adds two values to the VBO.
/// Adds two values to the VBO.
/// </summary>
public void Add(float x, float y) => AddRange(new[] {x,y});
/// <summary>
/// Adds three values to the VBO.
/// </summary>
public void Add(float x, float y, float z) => AddRange(new[] {x,y,z});
/// <summary>
/// Adds four values to the VBO.
/// </summary>
public void Add(float x, float y, float z, float w) => AddRange(new[] {x,y,z,w});
/// <summary>
/// Adds a Vector2.
/// </summary>
public void Add(Vector2 vector) => Add(vector.X, vector.Y);
/// <summary>
/// Adds a Vector2 and a value.
/// </summary>
public void Add(Vector2 vector, float z) => Add(vector.X, vector.Y, z);
/// <summary>
/// Adds a Vector2 and two values.
/// </summary>
public void Add(Vector2 vector, float z, float w) => Add(vector.X, vector.Y, z, w);
/// <summary>
/// Adds a Vector3.
/// </summary>
public void Add(Vector3 vector) => Add(vector.X, vector.Y, vector.Z);
/// <summary>
/// Adds a Vector3 and a value.
/// </summary>
public void Add(Vector3 vector, float w) => Add(vector.X, vector.Y, vector.Z, w);
/// <summary>
/// Adds a vector4.
/// </summary>
/// <param name="vector"></param>
public void Add(Vector4 vector) => Add(vector.X, vector.Y, vector.Z, vector.W);
/// <summary>
/// Adds a color.
/// </summary>
public void Add(Color4 color) => Add(color.R, color.G, color.B, color.A);
public void Add(float x, float y)
{
AddRange(new[] {x, y});
}
/// <summary>
/// Binds the buffer to the active VAO.
/// Adds three values to the VBO.
/// </summary>
public void Add(float x, float y, float z)
{
AddRange(new[] {x, y, z});
}
/// <summary>
/// Adds four values to the VBO.
/// </summary>
public void Add(float x, float y, float z, float w)
{
AddRange(new[] {x, y, z, w});
}
/// <summary>
/// Adds a Vector2.
/// </summary>
public void Add(Vector2 vector)
{
Add(vector.X, vector.Y);
}
/// <summary>
/// Adds a Vector2 and a value.
/// </summary>
public void Add(Vector2 vector, float z)
{
Add(vector.X, vector.Y, z);
}
/// <summary>
/// Adds a Vector2 and two values.
/// </summary>
public void Add(Vector2 vector, float z, float w)
{
Add(vector.X, vector.Y, z, w);
}
/// <summary>
/// Adds a Vector3.
/// </summary>
public void Add(Vector3 vector)
{
Add(vector.X, vector.Y, vector.Z);
}
/// <summary>
/// Adds a Vector3 and a value.
/// </summary>
public void Add(Vector3 vector, float w)
{
Add(vector.X, vector.Y, vector.Z, w);
}
/// <summary>
/// Adds a vector4.
/// </summary>
/// <param name="vector"></param>
public void Add(Vector4 vector)
{
Add(vector.X, vector.Y, vector.Z, vector.W);
}
/// <summary>
/// Adds a color.
/// </summary>
public void Add(Color4 color)
{
Add(color.R, color.G, color.B, color.A);
}
/// <summary>
/// Binds the buffer to the active VAO.
/// </summary>
/// <param name="attribID">The id for the attribute.</param>
internal void BindBuffer(int attribID)
{
float[] data = ToArray();
var data = ToArray();
int buffer = GL.GenBuffer();
var buffer = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, buffer);
GL.BufferData(BufferTarget.ArrayBuffer, data.Length * sizeof(float), data, BufferUsageHint);

View file

@ -1,16 +1,19 @@
using System.Reflection;
using System.Runtime.CompilerServices;
#region usings
using System.Reflection;
using System.Runtime.InteropServices;
#endregion
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SM.Core")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyTitle("OpenGL Bindings for SMRederer")]
[assembly: AssemblyDescription("Defines classes to work with OpenTK/OpenGL")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyCompany("iedSoftworks")]
[assembly: AssemblyProduct("SM.Core")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2020")]
[assembly: AssemblyCopyright("Copyright © iedSoftworks 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@ -33,4 +36,4 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View file

@ -23,6 +23,7 @@
<WarningLevel>4</WarningLevel>
<DocumentationFile>
</DocumentationFile>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -31,39 +32,39 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="OpenTK, Version=3.2.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>..\packages\OpenTK.3.2\lib\net20\OpenTK.dll</HintPath>
<Reference Include="OpenTK, Version=3.2.1.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>..\..\packages\OpenTK.3.2.1\lib\net20\OpenTK.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Framebuffer\ColorAttachment.cs" />
<Compile Include="Framebuffer\Framebuffer.cs" />
<Compile Include="GLCustomActions.cs" />
<Compile Include="GLDebugging.cs" />
<Compile Include="GLObject.cs" />
<Compile Include="GLSettings.cs" />
<Compile Include="GLSystem.cs" />
<Compile Include="Mesh\BoundingBox.cs" />
<Compile Include="Mesh\GenericMesh.cs" />
<Compile Include="Mesh\VBO.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Shaders\GenericShader.cs" />
<Compile Include="Shaders\IUniform.cs" />
<Compile Include="Shaders\ShaderExtensions.cs" />
<Compile Include="Shaders\ShaderFileCollection.cs" />
<Compile Include="Shaders\ShaderFile.cs" />
<Compile Include="Shaders\ShaderPreProcess.cs" />
<Compile Include="Shaders\Uniform.cs" />
<Compile Include="Shaders\UniformArray.cs" />
<Compile Include="Shaders\UniformCollection.cs" />
<Compile Include="Texture\TextureBase.cs" />
<Compile Include="Version.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<None Include="OpenTK.dll.config" />
<None Include="packages.config" />

View file

@ -1,25 +1,30 @@
using System;
#region usings
using OpenTK.Graphics.OpenGL4;
using SM.OGL.Mesh;
#endregion
namespace SM.OGL.Shaders
{
/// <summary>
/// Abstract class, that is used to create graphic shader.
/// Abstract class, that is used to create graphic shader.
/// </summary>
public abstract class GenericShader : GLObject
{
protected override bool AutoCompile { get; } = true;
/// <summary>
/// Contains the different files for the shader.
/// Contains the different files for the shader.
/// </summary>
protected ShaderFileCollection ShaderFileFiles;
/// <summary>
/// Contains and manage the uniforms from the shader.
/// Contains and manage the uniforms from the shader.
/// </summary>
protected UniformCollection Uniforms;
/// <inheritdoc />
public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Program;
protected GenericShader(string vertex, string fragment) : this(new ShaderFileCollection(vertex, fragment)){}
/// <inheritdoc />
protected GenericShader(ShaderFileCollection shaderFileFiles)
@ -27,35 +32,26 @@ namespace SM.OGL.Shaders
ShaderFileFiles = shaderFileFiles;
}
/// <inheritdoc />
public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Program;
/// <summary>
/// Loads the shader to the GPU.
/// Loads the shader to the GPU.
/// </summary>
public void Load()
{
_id = GL.CreateProgram();
ShaderFileFiles.Append(this);
GL.LinkProgram(_id);
this.Name(GetType().Name);
Name(GetType().Name);
ShaderFileFiles.Detach(this);
GL.GetProgram(_id, GetProgramParameterName.ActiveUniforms, out int uniformCount);
if (uniformCount < 1)
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 _);
int loc = GL.GetUniformLocation(_id, key);
if (key.EndsWith("]"))
key = key.Split('[', ']')[0];
Uniforms.Add(key, loc);
}
Uniforms.ParentShader = this;
Uniforms.Import(this);
GLDebugging.CheckGLErrors($"A error occured at shader creation for '{GetType()}': %code%");
}
/// <inheritdoc />
@ -65,22 +61,21 @@ namespace SM.OGL.Shaders
}
/// <summary>
/// Draws the mesh.
/// Draws the mesh.
/// </summary>
/// <param name="mesh">The mesh.</param>
/// <param name="amount">The amounts for instancing.</param>
/// <param name="bindVAO">Binds the vertex array for the mesh.</param>
protected void DrawObject(Mesh.GenericMesh mesh, int amount = 1, bool bindVAO = false)
protected void DrawObject(GenericMesh mesh, int amount = 1)
{
if (bindVAO) GL.BindVertexArray(mesh);
if (mesh.Indices != null)
GL.DrawElementsInstanced(mesh.PrimitiveType, 0, DrawElementsType.UnsignedInt, mesh.Indices, amount);
else
else
GL.DrawArraysInstanced(mesh.PrimitiveType, 0, mesh.Vertex.Count, amount);
}
/// <summary>
/// Resets the shader specific settings to ensure proper workings.
/// Resets the shader specific settings to ensure proper workings.
/// </summary>
protected void CleanUp()
{

View file

@ -0,0 +1,7 @@
namespace SM.OGL.Shaders
{
public interface IUniform
{
int Location { get; }
}
}

View file

@ -0,0 +1,38 @@
#region usings
using System.Collections.Generic;
using System.IO;
using System.Reflection;
#endregion
namespace SM.OGL.Shaders
{
public class ShaderExtensions
{
public static Dictionary<string, ShaderFile> Extensions { get; private set; } =
new Dictionary<string, ShaderFile>();
public static void AddAssemblyExtensions(string prefix, string path)
{
AddAssemblyExtensions(prefix, Assembly.GetCallingAssembly(), path);
}
public static void AddAssemblyExtensions(string prefix, Assembly assembly, string path)
{
var paths = assembly.GetManifestResourceNames();
for (var i = 0; i < paths.Length; i++)
{
var filePath = paths[i];
if (!filePath.StartsWith(path)) continue;
using (var reader = new StreamReader(assembly.GetManifestResourceStream(filePath)))
{
var name =
$"{prefix}{Path.GetFileNameWithoutExtension(filePath.Substring(path.Length)).Replace('.', '_')}";
Extensions.Add(name, new ShaderFile(reader.ReadToEnd()));
}
}
}
}
}

View file

@ -1,30 +1,37 @@
using System;
#region usings
using System;
using System.Collections.Generic;
using OpenTK.Graphics.OpenGL4;
#endregion
namespace SM.OGL.Shaders
{
/// <summary>
/// Contains/Represents a file used in shaders.
/// Contains/Represents a file used in shaders.
/// </summary>
public class ShaderFile : GLObject
{
private string _data;
/// <inheritdoc />
public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Shader;
/// <summary>
/// Contains overrides, that can be used to import values from the CPU to the shader before it is been send to the GPU.
/// </summary>
public Dictionary<string, string> StringOverrides = new Dictionary<string, string>();
/// <summary>
/// Contains other shader files to allow access to their functions.
/// Contains other shader files to allow access to their functions.
/// </summary>
public List<ShaderFile> GLSLExtensions = new List<ShaderFile>();
/// <summary>
/// Creates a file.
/// Gets/Sets the name for this shader file.
/// </summary>
public new string Name;
/// <summary>
/// Contains overrides, that can be used to import values from the CPU to the shader before it is been send to the GPU.
/// </summary>
public Dictionary<string, string> StringOverrides = new Dictionary<string, string>();
/// <summary>
/// Creates a file.
/// </summary>
/// <param name="data">The source file.</param>
public ShaderFile(string data)
@ -32,11 +39,28 @@ namespace SM.OGL.Shaders
_data = data;
}
/// <inheritdoc />
public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Shader;
private void GenerateSource()
{
foreach (KeyValuePair<string, string> kvp in StringOverrides)
_data = _data.Replace("//! " + kvp.Key, kvp.Value);
if (!GLSettings.ShaderPreProcessing) return;
if (_data.Contains("//#"))
{
var commandSplits = _data.Split(new[] {"//#"}, StringSplitOptions.RemoveEmptyEntries);
for (var i = 1; i < commandSplits.Length; i++)
{
var split = commandSplits[i].Split('\r', '\n')[0].Trim();
var cmdArgs = split.Split(new[] {' '}, 2);
ShaderPreProcess.Actions[cmdArgs[0]]?.Invoke(this, cmdArgs[1]);
}
}
foreach (var kvp in StringOverrides)
_data = _data.Replace("//!" + kvp.Key, kvp.Value);
}
internal void Compile(GenericShader shader, ShaderType type)
@ -49,7 +73,11 @@ namespace SM.OGL.Shaders
GL.ShaderSource(_id, _data);
GL.CompileShader(_id);
}
GL.AttachShader(shader, _id);
GLDebugging.CheckGLErrors($"Error at loading shader file: '{shader.GetType()}', '{type}', %code%");
for (var i = 0; i < GLSLExtensions.Count; i++) GLSLExtensions[i].Compile(shader, type);
}
}
}

View file

@ -1,36 +1,43 @@
using System;
using System.Collections.Generic;
#region usings
using OpenTK.Graphics.OpenGL4;
#endregion
namespace SM.OGL.Shaders
{
/// <summary>
/// Collects all files that are needed for a shader.
/// Collects all files that are needed for a shader.
/// </summary>
public struct ShaderFileCollection
{
/// <summary>
/// Contains the vertex file.
/// Contains the vertex file.
/// </summary>
public ShaderFile Vertex;
/// <summary>
/// Contains the geometry file.
/// Contains the geometry file.
/// </summary>
public ShaderFile Geometry;
/// <summary>
/// Contains the fragment file.
/// Contains the fragment file.
/// </summary>
public ShaderFile Fragment;
/// <summary>
/// Creating the collection with vertex and fragment files.
/// Creating the collection with vertex and fragment files.
/// </summary>
/// <param name="vertex">The vertex source file.</param>
/// <param name="fragment">The fragment source file.</param>
public ShaderFileCollection(string vertex, string fragment) : this(new ShaderFile(vertex), new ShaderFile(fragment)) {}
public ShaderFileCollection(string vertex, string fragment) : this(new ShaderFile(vertex),
new ShaderFile(fragment))
{
}
/// <summary>
/// Creating the collection with shader files.
/// Creating the collection with shader files.
/// </summary>
/// <param name="vertex"></param>
/// <param name="fragment"></param>
@ -43,7 +50,7 @@ namespace SM.OGL.Shaders
}
/// <summary>
/// Appends the files to the shader.
/// Appends the files to the shader.
/// </summary>
/// <param name="shader"></param>
internal void Append(GenericShader shader)
@ -54,14 +61,16 @@ namespace SM.OGL.Shaders
}
/// <summary>
/// Removes the files form the shader.
/// Removes the files form the shader.
/// </summary>
/// <param name="shader"></param>
internal void Detach(GenericShader shader)
{
GL.DetachShader(Vertex, shader);
if (Geometry != null) GL.DetachShader(Geometry, shader);
GL.DetachShader(Fragment, shader);
GL.DetachShader(shader, Vertex);
if (Geometry != null) GL.DetachShader(shader, Geometry);
GL.DetachShader(shader, Fragment);
GLDebugging.CheckGLErrors($"Error at detaching '{shader.GetType()}'");
}
}
}

View file

@ -0,0 +1,24 @@
#region usings
using System;
using System.Collections.Generic;
#endregion
namespace SM.OGL.Shaders
{
public class ShaderPreProcess
{
public static Dictionary<string, Action<ShaderFile, string>> Actions =
new Dictionary<string, Action<ShaderFile, string>>
{
{"import", Import}
};
private static void Import(ShaderFile file, string param)
{
foreach (var extension in param.Split(' '))
file.GLSLExtensions.Add(ShaderExtensions.Extensions[extension]);
}
}
}

View file

@ -1,27 +1,35 @@
using OpenTK;
#region usings
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using SM.OGL.Texture;
#endregion
namespace SM.OGL.Shaders
{
/// <summary>
/// Manages the uniforms.
/// Manages the uniforms.
/// </summary>
public struct Uniform
public struct Uniform : IUniform
{
/// <summary>
/// This contains the location for the uniform.
/// This contains the location for the uniform.
/// </summary>
private int Location;
/// <summary>
/// This contains the Parent collection of this uniform.
/// </summary>
internal UniformCollection Parent;
public int Location { get; internal set; }
/// <summary>
/// This create a new uniform manager
/// This contains the Parent collection of this uniform.
/// </summary>
public UniformCollection Parent { get; }
public Uniform(int location) : this(location, null)
{
}
/// <summary>
/// This create a new uniform manager
/// </summary>
/// <param name="location">Location id</param>
/// <param name="parent">Parent collection</param>
@ -38,114 +46,343 @@ namespace SM.OGL.Shaders
GL.Uniform1(Location, value ? 1 : 0);
}
public void SetUniform1(int value) { GL.Uniform1(Location, value); }
public void SetUniform1(int count, params int[] values) { GL.Uniform1(Location, count, values); }
public void SetUniform1(int count, ref int values) { GL.Uniform1(Location, count, ref values); }
public void SetUniform1(int value)
{
GL.Uniform1(Location, value);
}
public void SetUniform1(int count, params int[] values)
{
GL.Uniform1(Location, count, values);
}
public void SetUniform1(int count, ref int values)
{
GL.Uniform1(Location, count, ref values);
}
public void SetUniform1(uint value) { GL.Uniform1(Location, value); }
public void SetUniform1(int count, params uint[] values) { GL.Uniform1(Location, count, values); }
public void SetUniform1(int count, ref uint values) { GL.Uniform1(Location, count, ref values); }
public void SetUniform1(uint value)
{
GL.Uniform1(Location, value);
}
public void SetUniform1(int count, params uint[] values)
{
GL.Uniform1(Location, count, values);
}
public void SetUniform1(int count, ref uint values)
{
GL.Uniform1(Location, count, ref values);
}
public void SetUniform1(float value) { GL.Uniform1(Location, value); }
public void SetUniform1(int count, params float[] values) { GL.Uniform1(Location, count, values); }
public void SetUniform1(int count, ref float value) { GL.Uniform1(Location, count, ref value); }
public void SetUniform1(float value)
{
GL.Uniform1(Location, value);
}
public void SetUniform1(int count, params float[] values)
{
GL.Uniform1(Location, count, values);
}
public void SetUniform1(int count, ref float value)
{
GL.Uniform1(Location, count, ref value);
}
public void SetUniform1(double value) { GL.Uniform1(Location, value); }
public void SetUniform1(int count, params double[] values) { GL.Uniform1(Location, count, values); }
public void SetUniform1(int count, ref double value) { GL.Uniform1(Location, count, ref value); }
public void SetUniform1(double value)
{
GL.Uniform1(Location, value);
}
public void SetUniform1(int count, params double[] values)
{
GL.Uniform1(Location, count, values);
}
public void SetUniform1(int count, ref double value)
{
GL.Uniform1(Location, count, ref value);
}
#endregion
#region Uniform2
public void SetUniform2(float x, float y) { GL.Uniform2(Location, x, y); }
public void SetUniform2(double x, double y) { GL.Uniform2(Location, x, y); }
public void SetUniform2(uint x, uint y) { GL.Uniform2(Location, x, y); }
public void SetUniform2(int x, int y) { GL.Uniform2(Location, x, y); }
public void SetUniform2(float x, float y)
{
GL.Uniform2(Location, x, y);
}
public void SetUniform2(int count, params float[] values) { GL.Uniform2(Location, count, values); }
public void SetUniform2(int count, params double[] values) { GL.Uniform2(Location, count, values); }
public void SetUniform2(int count, params int[] values) { GL.Uniform2(Location, count, values); }
public void SetUniform2(int count, params uint[] values) { GL.Uniform2(Location, count, values); }
public void SetUniform2(double x, double y)
{
GL.Uniform2(Location, x, y);
}
public void SetUniform2(int count, ref float values) { GL.Uniform2(Location, count, ref values); }
public void SetUniform2(int count, ref double values) { GL.Uniform2(Location, count, ref values); }
public void SetUniform2(int count, ref uint values) { GL.Uniform2(Location, count, ref values); }
public void SetUniform2(uint x, uint y)
{
GL.Uniform2(Location, x, y);
}
public void SetUniform2(Vector2 vector2) { GL.Uniform2(Location, vector2); }
public void SetUniform2(ref Vector2 vector2) { GL.Uniform2(Location, ref vector2); }
public void SetUniform2(int x, int y)
{
GL.Uniform2(Location, x, y);
}
public void SetUniform2(int count, params float[] values)
{
GL.Uniform2(Location, count, values);
}
public void SetUniform2(int count, params double[] values)
{
GL.Uniform2(Location, count, values);
}
public void SetUniform2(int count, params int[] values)
{
GL.Uniform2(Location, count, values);
}
public void SetUniform2(int count, params uint[] values)
{
GL.Uniform2(Location, count, values);
}
public void SetUniform2(int count, ref float values)
{
GL.Uniform2(Location, count, ref values);
}
public void SetUniform2(int count, ref double values)
{
GL.Uniform2(Location, count, ref values);
}
public void SetUniform2(int count, ref uint values)
{
GL.Uniform2(Location, count, ref values);
}
public void SetUniform2(Vector2 vector2)
{
GL.Uniform2(Location, vector2);
}
public void SetUniform2(ref Vector2 vector2)
{
GL.Uniform2(Location, ref vector2);
}
#endregion
#region Uniform3
public void SetUniform3(float x, float y, float z) { GL.Uniform3(Location, x, y, z); }
public void SetUniform3(double x, double y, double z) { GL.Uniform3(Location, x, y, z); }
public void SetUniform3(uint x, uint y, uint z) { GL.Uniform3(Location, x, y, z); }
public void SetUniform3(int x, int y, int z) { GL.Uniform3(Location, x, y, z); }
public void SetUniform3(float x, float y, float z)
{
GL.Uniform3(Location, x, y, z);
}
public void SetUniform3(int count, params float[] values) { GL.Uniform3(Location, count, values); }
public void SetUniform3(int count, params double[] values) { GL.Uniform3(Location, count, values); }
public void SetUniform3(int count, params int[] values) { GL.Uniform3(Location, count, values); }
public void SetUniform3(int count, params uint[] values) { GL.Uniform3(Location, count, values); }
public void SetUniform3(double x, double y, double z)
{
GL.Uniform3(Location, x, y, z);
}
public void SetUniform3(int count, ref float values) { GL.Uniform3(Location, count, ref values); }
public void SetUniform3(int count, ref double values) { GL.Uniform3(Location, count, ref values); }
public void SetUniform3(int count, ref uint values) { GL.Uniform3(Location, count, ref values); }
public void SetUniform3(uint x, uint y, uint z)
{
GL.Uniform3(Location, x, y, z);
}
public void SetUniform3(Vector3 vector) { GL.Uniform3(Location, vector); }
public void SetUniform3(ref Vector3 vector) { GL.Uniform3(Location, ref vector); }
public void SetUniform3(int x, int y, int z)
{
GL.Uniform3(Location, x, y, z);
}
public void SetUniform3(int count, params float[] values)
{
GL.Uniform3(Location, count, values);
}
public void SetUniform3(int count, params double[] values)
{
GL.Uniform3(Location, count, values);
}
public void SetUniform3(int count, params int[] values)
{
GL.Uniform3(Location, count, values);
}
public void SetUniform3(int count, params uint[] values)
{
GL.Uniform3(Location, count, values);
}
public void SetUniform3(int count, ref float values)
{
GL.Uniform3(Location, count, ref values);
}
public void SetUniform3(int count, ref double values)
{
GL.Uniform3(Location, count, ref values);
}
public void SetUniform3(int count, ref uint values)
{
GL.Uniform3(Location, count, ref values);
}
public void SetUniform3(Vector3 vector)
{
GL.Uniform3(Location, vector);
}
public void SetUniform3(ref Vector3 vector)
{
GL.Uniform3(Location, ref vector);
}
#endregion
#region Uniform4
public void SetUniform4(float x, float y, float z, float w) { GL.Uniform4(Location, x, y, z, w); }
public void SetUniform4(double x, double y, double z, double w) { GL.Uniform4(Location, x, y, z, w); }
public void SetUniform4(uint x, uint y, uint z, uint w) { GL.Uniform4(Location, x, y, z, w); }
public void SetUniform4(int x, int y, int z, int w) { GL.Uniform4(Location, x, y, z, w); }
public void SetUniform4(float x, float y, float z, float w)
{
GL.Uniform4(Location, x, y, z, w);
}
public void SetUniform4(int count, params float[] values) { GL.Uniform4(Location, count, values); }
public void SetUniform4(int count, params double[] values) { GL.Uniform4(Location, count, values); }
public void SetUniform4(int count, params int[] values) { GL.Uniform4(Location, count, values); }
public void SetUniform4(int count, params uint[] values) { GL.Uniform4(Location, count, values); }
public void SetUniform4(double x, double y, double z, double w)
{
GL.Uniform4(Location, x, y, z, w);
}
public void SetUniform4(int count, ref float values) { GL.Uniform4(Location, count, ref values); }
public void SetUniform4(int count, ref double values) { GL.Uniform4(Location, count, ref values); }
public void SetUniform4(int count, ref uint values) { GL.Uniform4(Location, count, ref values); }
public void SetUniform4(uint x, uint y, uint z, uint w)
{
GL.Uniform4(Location, x, y, z, w);
}
public void SetUniform4(Vector4 vector) { GL.Uniform4(Location, vector); }
public void SetUniform4(ref Vector4 vector) { GL.Uniform4(Location, ref vector); }
public void SetUniform4(int x, int y, int z, int w)
{
GL.Uniform4(Location, x, y, z, w);
}
public void SetUniform4(Color4 color) { GL.Uniform4(Location, color); }
public void SetUniform4(Quaternion quaternion) { GL.Uniform4(Location, quaternion); }
public void SetUniform4(int count, params float[] values)
{
GL.Uniform4(Location, count, values);
}
public void SetUniform4(int count, params double[] values)
{
GL.Uniform4(Location, count, values);
}
public void SetUniform4(int count, params int[] values)
{
GL.Uniform4(Location, count, values);
}
public void SetUniform4(int count, params uint[] values)
{
GL.Uniform4(Location, count, values);
}
public void SetUniform4(int count, ref float values)
{
GL.Uniform4(Location, count, ref values);
}
public void SetUniform4(int count, ref double values)
{
GL.Uniform4(Location, count, ref values);
}
public void SetUniform4(int count, ref uint values)
{
GL.Uniform4(Location, count, ref values);
}
public void SetUniform4(Vector4 vector)
{
GL.Uniform4(Location, vector);
}
public void SetUniform4(ref Vector4 vector)
{
GL.Uniform4(Location, ref vector);
}
public void SetUniform4(Color4 color)
{
GL.Uniform4(Location, color);
}
public void SetUniform4(Quaternion quaternion)
{
GL.Uniform4(Location, quaternion);
}
#endregion
#region Matrix2
public void SetMatrix2(ref Matrix2 matrix, bool transpose = false) { GL.UniformMatrix2(Location, transpose, ref matrix); }
public void SetMatrix2(ref Matrix2 matrix, bool transpose = false)
{
GL.UniformMatrix2(Location, transpose, ref matrix);
}
public void SetMatrix2(int count, ref double value, bool transpose = false) { GL.UniformMatrix2(Location, count, transpose, ref value); }
public void SetMatrix2(int count, ref float value, bool transpose = false) { GL.UniformMatrix2(Location, count, transpose, ref value); }
public void SetMatrix2(int count, ref double value, bool transpose = false)
{
GL.UniformMatrix2(Location, count, transpose, ref value);
}
public void SetMatrix2(int count, double[] value, bool transpose = false) { GL.UniformMatrix2(Location, count, transpose, value); }
public void SetMatrix2(int count, float[] value, bool transpose = false) { GL.UniformMatrix2(Location, count, transpose, value); }
public void SetMatrix2(int count, ref float value, bool transpose = false)
{
GL.UniformMatrix2(Location, count, transpose, ref value);
}
public void SetMatrix2(int count, double[] value, bool transpose = false)
{
GL.UniformMatrix2(Location, count, transpose, value);
}
public void SetMatrix2(int count, float[] value, bool transpose = false)
{
GL.UniformMatrix2(Location, count, transpose, value);
}
#endregion
#region Matrix3
public void SetMatrix3(ref Matrix3 matrix, bool transpose = false) { GL.UniformMatrix3(Location, transpose, ref matrix); }
public void SetMatrix3(ref Matrix3 matrix, bool transpose = false)
{
GL.UniformMatrix3(Location, transpose, ref matrix);
}
public void SetMatrix3(int count, ref double value, bool transpose = false) { GL.UniformMatrix3(Location, count, transpose, ref value); }
public void SetMatrix3(int count, ref float value, bool transpose = false) { GL.UniformMatrix3(Location, count, transpose, ref value); }
public void SetMatrix3(int count, ref double value, bool transpose = false)
{
GL.UniformMatrix3(Location, count, transpose, ref value);
}
public void SetMatrix3(int count, double[] value, bool transpose = false) { GL.UniformMatrix3(Location, count, transpose, value); }
public void SetMatrix3(int count, float[] value, bool transpose = false) { GL.UniformMatrix3(Location, count, transpose, value); }
public void SetMatrix3(int count, ref float value, bool transpose = false)
{
GL.UniformMatrix3(Location, count, transpose, ref value);
}
public void SetMatrix3(int count, double[] value, bool transpose = false)
{
GL.UniformMatrix3(Location, count, transpose, value);
}
public void SetMatrix3(int count, float[] value, bool transpose = false)
{
GL.UniformMatrix3(Location, count, transpose, value);
}
#endregion
@ -155,18 +392,36 @@ namespace SM.OGL.Shaders
{
GL.UniformMatrix4(Location, transpose, ref matrix);
}
public void SetMatrix4(ref Matrix4 matrix, bool transpose = false) { GL.UniformMatrix4(Location, transpose, ref matrix); }
public void SetMatrix4(int count, ref double value, bool transpose = false) { GL.UniformMatrix4(Location, count, transpose, ref value); }
public void SetMatrix4(int count, ref float value, bool transpose = false) { GL.UniformMatrix4(Location, count, transpose, ref value); }
public void SetMatrix4(ref Matrix4 matrix, bool transpose = false)
{
GL.UniformMatrix4(Location, transpose, ref matrix);
}
public void SetMatrix4(int count, double[] value, bool transpose = false) { GL.UniformMatrix4(Location, count, transpose, value); }
public void SetMatrix4(int count, float[] value, bool transpose = false) { GL.UniformMatrix4(Location, count, transpose, value); }
public void SetMatrix4(int count, ref double value, bool transpose = false)
{
GL.UniformMatrix4(Location, count, transpose, ref value);
}
public void SetMatrix4(int count, ref float value, bool transpose = false)
{
GL.UniformMatrix4(Location, count, transpose, ref value);
}
public void SetMatrix4(int count, double[] value, bool transpose = false)
{
GL.UniformMatrix4(Location, count, transpose, value);
}
public void SetMatrix4(int count, float[] value, bool transpose = false)
{
GL.UniformMatrix4(Location, count, transpose, value);
}
#endregion
/// <summary>
/// Try to sets the texture at the next possible position and tells the checkUniform, if worked or not.
/// Try to sets the texture at the next possible position and tells the checkUniform, if worked or not.
/// </summary>
/// <param name="texture">The texture you want to add</param>
/// <param name="checkUniform">The check uniform.</param>
@ -177,7 +432,7 @@ namespace SM.OGL.Shaders
}
/// <summary>
/// Try to sets the texture at the specified position and tells the checkUniform, if worked or not.
/// Try to sets the texture at the specified position and tells the checkUniform, if worked or not.
/// </summary>
/// <param name="texture">The texture you want to add</param>
/// <param name="pos">The position</param>
@ -187,14 +442,18 @@ namespace SM.OGL.Shaders
checkUniform.SetUniform1(texture != null);
if (texture != null) SetTexture(texture);
}
/// <summary>
/// Sets the texture to the next possible position.
/// </summary>
/// <param name="texture"></param>
public void SetTexture(TextureBase texture) => SetTexture(texture, Parent.NextTexture++);
/// <summary>
/// Sets the texture to the specified position.
/// Sets the texture to the next possible position.
/// </summary>
/// <param name="texture"></param>
public void SetTexture(TextureBase texture)
{
if (Parent != null) SetTexture(texture, Parent.NextTexture++);
}
/// <summary>
/// Sets the texture to the specified position.
/// </summary>
/// <param name="texture"></param>
/// <param name="texturePos"></param>
@ -206,9 +465,12 @@ namespace SM.OGL.Shaders
}
/// <summary>
/// Returns the location from the uniform
/// Returns the location from the uniform
/// </summary>
/// <param name="u"></param>
public static implicit operator int(Uniform u) => u.Location;
public static implicit operator int(Uniform u)
{
return u.Location;
}
}
}

View file

@ -0,0 +1,50 @@
#region usings
using System;
using System.Collections.Generic;
#endregion
namespace SM.OGL.Shaders
{
public class UniformArray : IUniform
{
internal UniformCollection collection;
internal Dictionary<string, int> Offsets = new Dictionary<string, int>();
internal int Size;
internal bool Struct = false;
public int Location { get; internal set; }
public GenericShader Parent { get; internal set; }
public string Name { get; internal set; }
public UniformArray()
{
collection = new UniformCollection()
{
ParentShader = Parent
};
}
public void Set(Action<int, Uniform> setAction)
{
for (var i = 0; i < Size; i++) setAction(i, new Uniform(Location + i));
}
public void Set(Func<int, UniformCollection, bool> setAction)
{
collection.ParentShader ??= Parent;
for (var i = 0; i < Size; i++)
{
collection.KeyString = $"{Name}[{i}]";
foreach (var pair in Offsets)
collection.Set(pair.Key, new Uniform(Location + pair.Value + i));
if (!setAction(i, collection)) break;
}
}
}
}

View file

@ -1,56 +1,109 @@
using System;
#region usings
using System;
using System.Collections.Generic;
using OpenTK.Graphics.OpenGL4;
#endregion
namespace SM.OGL.Shaders
{
/// <summary>
/// Contains and manages the uniform of the parent shader.
/// </summary>
public class UniformCollection : Dictionary<string, Uniform>
public class UniformCollection : Dictionary<string, IUniform>
{
/// <summary>
/// The next texture id for the uniform.
/// </summary>
internal int NextTexture = 0;
internal string KeyString = "";
public GenericShader ParentShader { get; internal set; }
/// <summary>
/// The parent shader.
/// </summary>
internal GenericShader _parentShader;
public new Uniform this[string key] => Get(key);
/// <summary>
/// Get you the uniform under the variable name.
/// <para>If it don't find the uniform, it tries to recreate it.</para>
/// <para>If the variable doesn't exist in the first place, it will after the recreation send everything to -1, what is the void.</para>
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public new Uniform this[string key]
public Uniform Get(string key)
{
get
try
{
try
{
return base[key];
}
catch (KeyNotFoundException)
{
Console.WriteLine("[Error] Uniform '"+key+"' was not found. Tried to recreate it.");
Uniform u = new Uniform(GL.GetUniformLocation(_parentShader, key), this);
Add(key, u);
return u;
}
return (Uniform) base[key];
}
catch (KeyNotFoundException)
{
GLCustomActions.AtWarning?.Invoke("Uniform '" + KeyString + key + "' was not found. Tried to recreate it.");
var u = new Uniform(GL.GetUniformLocation(ParentShader, KeyString + key), this);
Add(key, u);
return u;
}
}
/// <summary>
/// Adds a uniform with a location.
/// </summary>
/// <param name="key"></param>
/// <param name="location"></param>
public UniformArray GetArray(string key)
{
try
{
return (UniformArray) base[key];
}
catch (KeyNotFoundException)
{
throw new Exception("UniformArray '"+key+"' wasn't found");
}
}
public void Add(string key, int location)
{
base.Add(key, new Uniform(location, this));
}
internal void Set(string key, IUniform value)
{
base[key] = value;
}
internal void Import(GenericShader shader)
{
GL.GetProgram(shader, GetProgramParameterName.ActiveUniforms, out var uniformCount);
if (uniformCount < 1)
GLCustomActions.AtError("No uniforms has been found.");
var lastArrayKey = "";
var array = new UniformArray();
var arrayFilled = false;
if (GLSettings.InfoEveryUniform) GLCustomActions.AtInfo?.Invoke("Uniforms for: " + shader.GetType());
for (var i = 0; i < uniformCount; i++)
{
var key = GL.GetActiveUniform(shader, i, out _, out _);
var loc = GL.GetUniformLocation(shader, key);
if (GLSettings.InfoEveryUniform) GLCustomActions.AtInfo?.Invoke($"{key} - {loc}");
if (key.Contains("["))
{
var keySplits = key.Split('[', ']');
if (keySplits[0] != lastArrayKey)
{
if (arrayFilled) Add(lastArrayKey, array);
array = new UniformArray
{
Location = loc,
Name = keySplits[0],
Parent = ParentShader,
Struct = keySplits.Length > 2
};
arrayFilled = true;
lastArrayKey = keySplits[0];
}
var curIndex = int.Parse(keySplits[1]);
if (array.Size < curIndex) array.Size = curIndex;
if (array.Struct)
if (!array.Offsets.ContainsKey(keySplits[2].Trim('.')))
array.Offsets.Add(keySplits[2].Trim('.'), loc - array.Location);
}
else
{
Add(key, loc);
}
}
if (arrayFilled) Add(lastArrayKey, array);
}
}
}

View file

@ -1,9 +1,13 @@
using OpenTK.Graphics.OpenGL4;
#region usings
using OpenTK.Graphics.OpenGL4;
#endregion
namespace SM.OGL.Texture
{
/// <summary>
/// Works as a basis for textures.
/// Works as a basis for textures.
/// </summary>
public abstract class TextureBase : GLObject
{
@ -14,23 +18,24 @@ namespace SM.OGL.Texture
public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Texture;
/// <summary>
/// The texture filter.
/// <para>Default: <see cref="TextureMinFilter.Linear"/></para>
/// The texture filter.
/// <para>Default: <see cref="TextureMinFilter.Linear" /></para>
/// </summary>
public virtual TextureMinFilter Filter { get; set; } = TextureMinFilter.Linear;
/// <summary>
/// The wrap mode.
/// <para>Default: <see cref="TextureWrapMode.Repeat"/></para>
/// The wrap mode.
/// <para>Default: <see cref="TextureWrapMode.Repeat" /></para>
/// </summary>
public virtual TextureWrapMode WrapMode { get; set; } = TextureWrapMode.Repeat;
/// <summary>
/// The Width of the texture
/// The Width of the texture
/// </summary>
public int Width { get; protected set; }
/// <summary>
/// The height of the texture
/// The height of the texture
/// </summary>
public int Height { get; protected set; }

View file

@ -1,21 +1,22 @@
namespace SM.OGL
{
/// <summary>
/// Helper struct to manage versions.
/// Helper struct to manage versions.
/// </summary>
public struct Version
{
/// <summary>
/// The major version.
/// The major version.
/// </summary>
public int MajorVersion;
/// <summary>
/// The minor version.
/// The minor version.
/// </summary>
public int MinorVersion;
/// <summary>
/// Creates the struct with specific major and minor versions.
/// Creates the struct with specific major and minor versions.
/// </summary>
/// <param name="majorVersion"></param>
/// <param name="minorVersion"></param>
@ -26,12 +27,12 @@
}
/// <summary>
/// Creates the struct by reading it out of a string.
/// Creates the struct by reading it out of a string.
/// </summary>
/// <param name="version"></param>
public Version(string version)
{
string[] splits = version.Trim().Split(new []{'.'}, 2);
var splits = version.Trim().Split(new[] {'.'}, 2);
MajorVersion = int.Parse(splits[0]);
MinorVersion = int.Parse(splits[1]);
}
@ -43,7 +44,7 @@
}
/// <summary>
/// Create a version struct, with a OpenGL Version string.
/// Create a version struct, with a OpenGL Version string.
/// </summary>
/// <param name="version"></param>
/// <returns></returns>

View file

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="OpenTK" version="3.2" targetFramework="net452" />
<package id="OpenTK" version="3.2.1" targetFramework="net452" />
</packages>

View file

@ -1,21 +1,28 @@
using OpenTK;
#region usings
using OpenTK;
using OpenTK.Input;
using SM.Base;
using SM.Base.Controls;
using SM2D.Scene;
#endregion
namespace SM2D.Controls
{
public class Mouse2D : Mouse<GLWindow2D>
{
protected internal Mouse2D(GLWindow2D window) : base(window)
{ }
{
}
internal new void MouseMoveEvent(MouseMoveEventArgs mmea) => base.MouseMoveEvent(mmea);
internal new void MouseMoveEvent(MouseMoveEventArgs mmea)
{
base.MouseMoveEvent(mmea);
}
public Vector2 InWorld()
{
Vector2 res = _window.WorldScale;
var res = _window.WorldScale;
return InScreenNormalized * res - res / 2;
}

View file

@ -1,15 +1,16 @@
using System.Collections.Generic;
#region usings
using System.Collections.Generic;
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;
#endregion
namespace SM2D.Drawing
{
@ -17,6 +18,22 @@ namespace SM2D.Drawing
{
private Material _material = new Material();
public DrawBackground(Color4 color)
{
Color = color;
}
public DrawBackground(Bitmap texture)
{
Texture = (Texture) texture;
}
public DrawBackground(Bitmap texture, Color4 tint)
{
Color = tint;
Texture = (Texture) texture;
}
public Color4 Color
{
get => _material.Tint;
@ -28,28 +45,14 @@ namespace SM2D.Drawing
get => _material.Texture;
set => _material.Texture = value;
}
public DrawBackground(Color4 color)
{
Color = color;
}
public DrawBackground(Bitmap texture)
{
Texture = (Texture)texture;
}
public DrawBackground(Bitmap texture, Color4 tint)
{
Color = tint;
Texture = (Texture) texture;
}
public object Parent { get; set; }
public string Name { get; set; } = "Background";
public ICollection<string> Flags { get; set; } = new string[0];
public void Update(UpdateContext context)
{ }
{
}
public void Draw(DrawContext context)
{

View file

@ -0,0 +1,19 @@
using SM.Base.Contexts;
using SM.Base.Scene;
using SM2D.Scene;
namespace SM2D.Drawing
{
public class DrawBackgroundShader : DrawShader, IBackgroundItem
{
public DrawBackgroundShader(MaterialShader shader) : base(shader)
{ }
protected override void DrawContext(ref DrawContext context)
{
Transform.Size.Set(context.WorldScale);
base.DrawContext(ref context);
}
}
}

View file

@ -1,14 +1,26 @@
using OpenTK.Graphics;
using SM.Base;
#region usings
using OpenTK.Graphics;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM2D.Scene;
using SM2D.Types;
#endregion
namespace SM2D.Drawing
{
public class DrawColor : DrawingBasis<Transformation>, I2DShowItem
{
public DrawColor()
{
}
public DrawColor(Color4 color)
{
_material.Tint = color;
}
public Color4 Color
{
get => _material.Tint;
@ -17,13 +29,6 @@ namespace SM2D.Drawing
public int ZIndex { get; set; }
public DrawColor() {}
public DrawColor(Color4 color)
{
_material.Tint = color;
}
protected override void DrawContext(ref DrawContext context)
{
context.Instances[0].ModelMatrix = Transform.GetMatrix();

View file

@ -1,15 +1,17 @@
using SM.Base.Contexts;
#region usings
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.OGL.Mesh;
using SM2D.Scene;
using SM2D.Types;
#endregion
namespace SM2D.Drawing
{
public class DrawComplex: DrawingBasis<Transformation>, I2DShowItem
public class DrawComplex : DrawingBasis<Transformation>, I2DShowItem
{
public int ZIndex { get; set; }
public Material Material
{
get => _material;
@ -22,6 +24,8 @@ namespace SM2D.Drawing
set => _mesh = value;
}
public int ZIndex { get; set; }
protected override void DrawContext(ref DrawContext context)
{
context.Instances[0].ModelMatrix = Transform.GetMatrix();

View file

@ -1,38 +1,46 @@
using System.Drawing;
#region usings
using System.Drawing;
using OpenTK.Graphics;
using SM.Base.Textures;
using SM.OGL.Texture;
using SM2D.Object;
#endregion
namespace SM2D.Drawing
{
public class DrawPolygon : DrawColor
{
public Polygon Polygon
public DrawPolygon(Polygon polygon) : this(polygon, Color4.White)
{
get => (Polygon)_mesh;
set => _mesh = value;
}
public Texture Texture
public DrawPolygon(Polygon polygon, Bitmap map) : this(polygon, map, Color4.White)
{
get => (Texture)_material.Texture;
set => _material.Texture = value;
}
public DrawPolygon(Polygon polygon) {}
public DrawPolygon(Polygon polygon, Bitmap map) : this(polygon, map, Color4.White) {}
public DrawPolygon(Polygon polygon, Color4 color) : base(color)
{
_mesh = polygon;
}
public DrawPolygon(Polygon polygon, Bitmap map, Color4 tint) : base(tint)
{
_mesh = polygon;
_material.Texture = new Texture(map);
}
public Polygon Polygon
{
get => (Polygon) _mesh;
set => _mesh = value;
}
public Texture Texture
{
get => (Texture) _material.Texture;
set => _material.Texture = value;
}
}
}

View file

@ -0,0 +1,24 @@
using SM.Base.Contexts;
using SM.Base.Scene;
using SM2D.Scene;
using SM2D.Types;
namespace SM2D.Drawing
{
public class DrawShader : DrawingBasis<Transformation>, I2DShowItem
{
public int ZIndex { get; set; }
public DrawShader(MaterialShader shader)
{
_material.CustomShader = shader;
}
protected override void DrawContext(ref DrawContext context)
{
context.Instances[0].ModelMatrix = Transform.GetMatrix();
_material.CustomShader.Draw(context);
}
}
}

View file

@ -1,10 +1,13 @@
using SM.Base;
#region usings
using SM.Base.Contexts;
using SM.Base.Text;
using SM.Base.Types;
using SM2D.Scene;
using SM2D.Types;
#endregion
namespace SM2D.Drawing
{
public class DrawText : TextDrawingBasis<Transformation>, I2DShowItem
@ -15,6 +18,8 @@ namespace SM2D.Drawing
Transform.Size = new CVector2(1);
}
public int ZIndex { get; set; }
protected override void DrawContext(ref DrawContext context)
{
base.DrawContext(ref context);
@ -24,7 +29,5 @@ namespace SM2D.Drawing
context.Shader.Draw(context);
}
public int ZIndex { get; set; }
}
}

View file

@ -1,36 +1,37 @@
using System.Drawing;
using OpenTK;
#region usings
using System.Drawing;
using OpenTK.Graphics;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.Base.Textures;
using SM.Base.Types;
using SM2D.Scene;
using SM2D.Types;
#endregion
namespace SM2D.Drawing
{
public class DrawTexture : DrawColor
{
public static float MasterScale = .25f;
public bool ManualSize = false;
public float Scale = 1;
public bool ManualSize = false;
public Texture Texture
public DrawTexture()
{
get => (Texture) _material.Texture;
set => _material.Texture = value;
}
public DrawTexture() {}
protected DrawTexture(Color4 color) : base(color) { }
protected DrawTexture(Color4 color) : base(color)
{
}
public DrawTexture(Bitmap map) : this(map, Color4.White)
{ }
{
}
public DrawTexture(Bitmap map, Color4 color) : this((Texture)map, color)
{ }
public DrawTexture(Bitmap map, Color4 color) : this((Texture) map, color)
{
}
public DrawTexture(Texture texture, Color4 color)
{
@ -38,9 +39,17 @@ namespace SM2D.Drawing
_material.Tint = color;
}
public Texture Texture
{
get => (Texture) _material.Texture;
set => _material.Texture = value;
}
protected override void DrawContext(ref DrawContext context)
{
if (!ManualSize) Transform.Size = new CVector2(Texture.Map.Width * MasterScale * Scale, Texture.Map.Height * MasterScale * Scale);
if (!ManualSize)
Transform.Size = new CVector2(Texture.Map.Width * MasterScale * Scale,
Texture.Map.Height * MasterScale * Scale);
base.DrawContext(ref context);
}
}

View file

@ -1,29 +1,31 @@
using System;
#region usings
using System;
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Input;
using SM.Base;
using SM.Base.Controls;
using SM2D.Controls;
using SM2D.Pipelines;
using SM2D.Scene;
using SM2D.Shader;
using Vector2 = OpenTK.Vector2;
#endregion
namespace SM2D
{
public class GLWindow2D : GenericWindow<Scene.Scene, Camera>
{
public Vector2? Scaling { get; set; }
public Vector2 WorldScale => _worldScale;
public Mouse2D Mouse { get; }
public GLWindow2D()
{
Mouse = new Mouse2D(this);
}
public Vector2? Scaling { get; set; }
public Vector2 WorldScale => _worldScale;
public Mouse2D Mouse { get; }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
@ -34,6 +36,8 @@ namespace SM2D
protected override void OnLoaded()
{
base.OnLoaded();
SMRenderer.DefaultMaterialShader = Default2DShader.MaterialShader;
SetRenderPipeline(new Basic2DPipeline());
}

View file

@ -1,5 +1,6 @@
using System;
using System.Collections;
#region usings
using System;
using System.Collections.Generic;
using OpenTK;
using OpenTK.Graphics;
@ -7,46 +8,40 @@ using OpenTK.Graphics.OpenGL4;
using SM.Base.Objects;
using SM.OGL.Mesh;
#endregion
namespace SM2D.Object
{
public class Polygon : Mesh
{
public override VBO Vertex { get; } = new VBO();
public override VBO UVs { get; } = new VBO(pointerSize:2);
public override VBO Color { get; } = new VBO(pointerSize: 4);
public override PrimitiveType PrimitiveType { get; } = PrimitiveType.TriangleFan;
public Polygon(ICollection<Vector2> vertices)
{
foreach (Vector2 vertex in vertices)
foreach (var vertex in vertices)
{
Color.Add(Color4.White);
AddVertex(vertex);
}
foreach (Vector2 vertex in vertices)
{
AddUV(vertex);
}
foreach (var vertex in vertices) AddUV(vertex);
}
public Polygon(ICollection<PolygonVertex> vertices)
{
foreach (PolygonVertex polygonVertex in vertices)
foreach (var polygonVertex in vertices)
{
Color.Add(polygonVertex.Color);
AddVertex(polygonVertex.Vertex);
}
foreach (PolygonVertex vertex in vertices)
{
AddUV(vertex.Vertex);
}
foreach (var vertex in vertices) AddUV(vertex.Vertex);
}
public override VBO Vertex { get; } = new VBO();
public override VBO UVs { get; } = new VBO(pointerSize: 2);
public override VBO Color { get; } = new VBO(pointerSize: 4);
public override PrimitiveType PrimitiveType { get; } = PrimitiveType.TriangleFan;
private void AddVertex(Vector2 vertex)
{
BoundingBox.Update(vertex);
@ -55,18 +50,19 @@ namespace SM2D.Object
private void AddUV(Vector2 vertex)
{
Vector2 uv = Vector2.Divide(vertex, BoundingBox.Max.Xy) + BoundingBox.Min.Xy;
var uv = Vector2.Divide(vertex, BoundingBox.Max.Xy) + BoundingBox.Min.Xy;
UVs.Add(uv);
}
public static Polygon GenerateCircle(int secments = 32)
{
List<Vector2> vertices = new List<Vector2>() {Vector2.Zero};
var vertices = new List<Vector2> {Vector2.Zero};
float step = 360f / secments;
for (int i = 0; i < secments + 1; i++)
var step = 360f / secments;
for (var i = 0; i < secments + 1; i++)
{
Vector2 vertex = new Vector2( 0.5f * (float)Math.Cos(step * i * Math.PI / 180f), 0.5f * (float)Math.Sin(step * i * Math.PI / 180f));
var vertex = new Vector2(0.5f * (float) Math.Cos(step * i * Math.PI / 180f),
0.5f * (float) Math.Sin(step * i * Math.PI / 180f));
vertices.Add(vertex);
}

View file

@ -1,6 +1,10 @@
using OpenTK;
#region usings
using OpenTK;
using OpenTK.Graphics;
#endregion
namespace SM2D.Object
{
public struct PolygonVertex

View file

@ -2,6 +2,5 @@
{
public class Adv2DPipeline
{
}
}

View file

@ -1,15 +1,18 @@
using OpenTK.Graphics.OpenGL4;
#region usings
using OpenTK.Graphics.OpenGL4;
using SM.Base;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.OGL.Framebuffer;
using SM2D.Shader;
#endregion
namespace SM2D.Pipelines
{
public class Basic2DPipeline : RenderPipeline<Scene.Scene>
{
protected override IShader _defaultShader { get; } = Default2DShader.Shader;
protected override void Render(ref DrawContext context, Scene.Scene scene)
{

View file

@ -1,16 +1,19 @@
using System.Reflection;
using System.Runtime.CompilerServices;
#region usings
using System.Reflection;
using System.Runtime.InteropServices;
#endregion
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SM2D")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyTitle("SMRenderer for 2D-projects")]
[assembly: AssemblyDescription("Scene and Render system for 2D-projects")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyCompany("iedSoftworks")]
[assembly: AssemblyProduct("SM2D")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2020")]
[assembly: AssemblyCopyright("Copyright © iedSoftworks 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@ -33,4 +36,4 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View file

@ -31,25 +31,21 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="OpenTK, Version=3.2.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>..\..\packages\OpenTK.3.2\lib\net20\OpenTK.dll</HintPath>
<Reference Include="OpenTK, Version=3.2.1.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>..\..\packages\OpenTK.3.2.1\lib\net20\OpenTK.dll</HintPath>
</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" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Controls\Mouse2D.cs" />
<Compile Include="Drawing\DrawBackground.cs" />
<Compile Include="Drawing\DrawBackgroundShader.cs" />
<Compile Include="Drawing\DrawColor.cs" />
<Compile Include="Drawing\DrawComplex.cs" />
<Compile Include="Drawing\DrawPolygon.cs" />
<Compile Include="Drawing\DrawShader.cs" />
<Compile Include="Drawing\DrawText.cs" />
<Compile Include="Drawing\DrawTexture.cs" />
<Compile Include="GLWindow2D.cs" />
@ -76,11 +72,12 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="OpenTK.dll.config" />
<None Include="packages.config" />
<EmbeddedResource Include="Shader\ShaderFiles\default.frag" />
<EmbeddedResource Include="Shader\ShaderFiles\default.vert" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<None Include="OpenTK.dll.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View file

@ -1,23 +1,27 @@
using OpenTK;
#region usings
using OpenTK;
using SM.Base.Scene;
using SM.Base.Types;
#endregion
namespace SM2D.Scene
{
public class Camera : GenericCamera
{
public override bool Orthographic { get; } = true;
public CVector2 Position = new CVector2(0);
public override bool Orthographic { get; } = true;
protected override Matrix4 ViewCalculation()
{
return Matrix4.LookAt(Position.X, Position.Y, 2, Position.X, Position.Y, 0, 0, 1, 0);
}
public override void RecalculateWorld(OpenTK.Vector2 world, float aspect)
public override void RecalculateWorld(Vector2 world, float aspect)
{
OrthographicWorld = Matrix4.CreateOrthographicOffCenter(-world.X / 2, world.X / 2, world.Y / 2, -world.Y / 2, 0.1f, 4f);
OrthographicWorld =
Matrix4.CreateOrthographicOffCenter(-world.X / 2, world.X / 2, world.Y / 2, -world.Y / 2, 0.1f, 4f);
}
}
}

View file

@ -1,10 +1,13 @@
using SM.Base.Scene;
#region usings
using SM.Base.Scene;
#endregion
namespace SM2D.Scene
{
public interface I2DShowItem : IShowItem
{
int ZIndex { get; set; }
}
}

View file

@ -1,8 +1,12 @@
using SM.Base.Contexts;
#region usings
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.Base.Types;
using SM2D.Types;
#endregion
namespace SM2D.Scene
{
public class ItemCollection : GenericItemCollection<I2DShowItem, Transformation>, I2DShowItem

View file

@ -1,17 +1,20 @@
using OpenTK.Graphics;
using SM.Base.Contexts;
#region usings
using OpenTK.Graphics;
using SM.Base.Scene;
using SM2D.Drawing;
#endregion
namespace SM2D.Scene
{
public class Scene : GenericScene<Camera, ItemCollection, I2DShowItem>
{
public DrawBackground Background => (DrawBackground)_Background;
public Scene()
{
_Background = new DrawBackground(Color4.Black);
}
public DrawBackground Background => (DrawBackground) _Background;
}
}

View file

@ -1,14 +1,18 @@
using OpenTK.Graphics.OpenGL4;
#region usings
using OpenTK.Graphics.OpenGL4;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.OGL.Shaders;
using SM.Utility;
#endregion
namespace SM2D.Shader
{
public class Default2DShader : GenericShader, IShader
public class Default2DShader : MaterialShader
{
public static Default2DShader Shader = new Default2DShader();
public static Default2DShader MaterialShader = new Default2DShader();
//protected override bool AutoCompile { get; } = true;
@ -18,32 +22,31 @@ namespace SM2D.Shader
{
Load();
}
public void Draw(DrawContext context)
protected override void DrawProcess(DrawContext context)
{
GL.UseProgram(this);
GL.BindVertexArray(context.Mesh);
// Vertex Uniforms
Uniforms["MVP"].SetMatrix4(context.ModelMaster * context.View * context.World);
Uniforms["HasVColor"].SetUniform1(context.Mesh.AttribDataIndex.ContainsKey(3) && context.Mesh.AttribDataIndex[3] != null);
Uniforms["HasVColor"]
.SetUniform1(context.Mesh.AttribDataIndex.ContainsKey(3) && context.Mesh.AttribDataIndex[3] != null);
for (int i = 0; i < context.Instances.Length; i++)
Uniforms.GetArray("Instances").Set((i, uniforms) =>
{
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);
}
if (i >= context.Instances.Length) return false;
var instance = context.Instances[i];
uniforms["ModelMatrix"].SetMatrix4(instance.ModelMatrix);
uniforms["TextureOffset"].SetUniform2(instance.TexturePosition);
uniforms["TextureScale"].SetUniform2(instance.TextureScale);
return true;
});
// 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

@ -1,23 +1,12 @@
#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;
//# import SM_base_vertex_basic
void ApplyTexModifier();
void CheckVertexColor();
void ApplyModelTransformation();
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);
ApplyTexModifier();
CheckVertexColor();
ApplyModelTransformation();
}

View file

@ -1,52 +1,39 @@
using System;
using System.Configuration.Assemblies;
#region usings
using OpenTK;
using SM.Base.Scene;
using SM.Base.Types;
using SM.Utility;
#endregion
namespace SM2D.Types
{
public class Transformation : GenericTransformation
{
private float _eulerRotation = 0;
private CVector2 _position = new CVector2(0);
private CVector2 _scale = new CVector2(50);
public CVector2 Position { get; set; } = new CVector2(0);
public CVector2 Position
{
get => _position;
set => _position = value;
}
public CVector2 Size { get; set; } = new CVector2(50);
public CVector2 Size
{
get => _scale;
set => _scale = value;
}
public float Rotation
{
get => _eulerRotation;
set => _eulerRotation = value;
}
public float Rotation { get; set; }
protected override Matrix4 RequestMatrix()
{
return Matrix4.CreateScale(_scale.X, _scale.Y, 1) *
Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(_eulerRotation)) *
Matrix4.CreateTranslation(_position.X, _position.Y, 0);
return Matrix4.CreateScale(Size.X, Size.Y, 1) *
Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(Rotation)) *
Matrix4.CreateTranslation(Position.X, Position.Y, 0);
}
public void TurnTo(Vector2 v)
{
_eulerRotation = RotationUtility.TurnTowards(Position, v);
Rotation = RotationUtility.TurnTowards(Position, v);
}
public Vector2 LookAtVector()
{
if (_modelMatrix.Determinant < 0.0001) return new Vector2(0);
Vector3 vec = Vector3.TransformNormal(Vector3.UnitX, _modelMatrix);
var vec = Vector3.TransformNormal(Vector3.UnitX, _modelMatrix);
vec.Normalize();
return vec.Xy;
}

View file

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="OpenTK" version="3.2" targetFramework="net452" />
<package id="OpenTK" version="3.2.1" targetFramework="net452" />
</packages>

View file

@ -1,7 +1,10 @@
using System.Reflection;
using System.Runtime.CompilerServices;
#region usings
using System.Reflection;
using System.Runtime.InteropServices;
#endregion
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
@ -33,4 +36,4 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View file

@ -33,12 +33,6 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />