diff --git a/SMCode/SM.Base/Drawing/GenericTransformation.cs b/SMCode/SM.Base/Drawing/GenericTransformation.cs index 970b81d..949c4e7 100644 --- a/SMCode/SM.Base/Drawing/GenericTransformation.cs +++ b/SMCode/SM.Base/Drawing/GenericTransformation.cs @@ -11,6 +11,8 @@ namespace SM.Base.Drawing /// public abstract class GenericTransformation { + public bool Ignore = false; + /// /// Contains the current model matrix. /// @@ -27,6 +29,8 @@ namespace SM.Base.Drawing /// public Matrix4 GetMatrix() { + if (Ignore) return Matrix4.Identity; + if (_lastFrame != SMRenderer.CurrentFrame) { _lastFrame = SMRenderer.CurrentFrame; diff --git a/SMCode/SM.Base/Objects/Mesh.cs b/SMCode/SM.Base/Objects/Mesh.cs index e362251..f01ef79 100644 --- a/SMCode/SM.Base/Objects/Mesh.cs +++ b/SMCode/SM.Base/Objects/Mesh.cs @@ -14,7 +14,7 @@ namespace SM.Base.Objects /// protected Mesh() { - AttribDataIndex.Add(3, Color); + Attributes.Add(3, "color", Color); } /// diff --git a/SMCode/SM.Base/Objects/Static/AxisHelper.cs b/SMCode/SM.Base/Objects/Static/AxisHelper.cs new file mode 100644 index 0000000..be22208 --- /dev/null +++ b/SMCode/SM.Base/Objects/Static/AxisHelper.cs @@ -0,0 +1,35 @@ +using OpenTK.Graphics; +using OpenTK.Graphics.OpenGL4; +using SM.OGL.Mesh; + +namespace SM.Base.Objects.Static +{ + public class AxisHelper : Mesh + { + public static AxisHelper Object = new AxisHelper(); + + private AxisHelper() {} + + public override VBO Vertex { get; } = new VBO() + { + {0, 0, 0}, + {.5f, 0, 0}, + {0, 0, 0}, + {0, .5f, 0}, + {0, 0, -.5f}, + {0, 0, .5f}, + }; + + public override VBO Color { get; } = new VBO(pointerSize:4) + { + {Color4.White}, + {Color4.Red}, + {Color4.White}, + {Color4.Green}, + {Color4.White}, + {Color4.DarkBlue}, + }; + + public override PrimitiveType PrimitiveType { get; } = PrimitiveType.Lines; + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/SM.Base.csproj b/SMCode/SM.Base/SM.Base.csproj index 2fe8e21..b2c5c4c 100644 --- a/SMCode/SM.Base/SM.Base.csproj +++ b/SMCode/SM.Base/SM.Base.csproj @@ -56,6 +56,7 @@ + diff --git a/SMCode/SM.Base/Scene/GenericScene.cs b/SMCode/SM.Base/Scene/GenericScene.cs index d206b9d..6377ffc 100644 --- a/SMCode/SM.Base/Scene/GenericScene.cs +++ b/SMCode/SM.Base/Scene/GenericScene.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Dynamic; using SM.Base.Contexts; +using SM.Base.Drawing; #endregion @@ -13,8 +14,6 @@ namespace SM.Base.Scene /// public abstract class GenericScene { - - private IBackgroundItem _background; /// @@ -34,6 +33,7 @@ namespace SM.Base.Scene /// public bool IsInitialized { get; private set; } + /// /// Updates this scene. /// @@ -91,6 +91,8 @@ namespace SM.Base.Scene private TCollection _hud = new TCollection(); private TCollection _objectCollection = new TCollection(); + public bool ShowAxisHelper { get; set; } = false; + /// /// A collection for cameras to switch easier to different cameras. /// @@ -154,6 +156,7 @@ namespace SM.Base.Scene DrawMainObjects(context); DrawHUD(context); + DrawDebug(context); } public void DrawBackground(DrawContext context) @@ -174,5 +177,10 @@ namespace SM.Base.Scene context.View = HUDCamera.CalculateViewMatrix(); _hud.Draw(context); } + + public virtual void DrawDebug(DrawContext context) + { + + } } } \ No newline at end of file diff --git a/SMCode/SM.Base/ShaderExtension/vertex/basic.vert b/SMCode/SM.Base/ShaderExtension/vertex/basic.vert index a5b7caa..0007f9f 100644 --- a/SMCode/SM.Base/ShaderExtension/vertex/basic.vert +++ b/SMCode/SM.Base/ShaderExtension/vertex/basic.vert @@ -1,11 +1,6 @@ #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; @@ -13,14 +8,13 @@ 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; + vTexture = aTex; } void CheckVertexColor() { @@ -29,7 +23,7 @@ void CheckVertexColor() { } void ApplyModelTransformation() { - gl_Position = MVP * Instances[gl_InstanceID].ModelMatrix * vec4(aPos, 1); + gl_Position = MVP * vec4(aPos, 1); - FragPos = vec3(Instances[gl_InstanceID].ModelMatrix * vec4(aPos, 1)); + FragPos = vec3(vec4(aPos, 1)); } \ No newline at end of file diff --git a/SMCode/SM.OGL/Mesh/GenericMesh.cs b/SMCode/SM.OGL/Mesh/GenericMesh.cs index 925e378..888f58c 100644 --- a/SMCode/SM.OGL/Mesh/GenericMesh.cs +++ b/SMCode/SM.OGL/Mesh/GenericMesh.cs @@ -13,16 +13,17 @@ namespace SM.OGL.Mesh /// public abstract class GenericMesh : GLObject { + /// /// Generates the AttribDataIndex /// protected GenericMesh() { - AttribDataIndex = new Dictionary + Attributes = new MeshAttributeList() { - {0, Vertex}, - {1, UVs}, - {2, Normals} + {0, "vertex", Vertex}, + {1, "uv", UVs}, + {2, "normal", Normals} }; } @@ -61,7 +62,7 @@ namespace SM.OGL.Mesh /// /// Connects the different buffer objects with ids. /// - public Dictionary AttribDataIndex { get; } + public MeshAttributeList Attributes { get; } /// /// Stores indices for a more performance friendly method to draw objects. @@ -74,9 +75,9 @@ namespace SM.OGL.Mesh _id = GL.GenVertexArray(); GL.BindVertexArray(_id); - if (AttribDataIndex == null) throw new Exception("[Critical] The model requires a attribute data index."); + if (Attributes == null) throw new Exception("[Critical] The model requires attributes."); - foreach (var kvp in AttribDataIndex) kvp.Value?.BindBuffer(kvp.Key); + foreach (var kvp in Attributes) kvp.ConnectedVBO?.BindBuffer(kvp.Index); GL.BindVertexArray(0); } diff --git a/SMCode/SM.OGL/Mesh/MeshAttribute.cs b/SMCode/SM.OGL/Mesh/MeshAttribute.cs new file mode 100644 index 0000000..f7833f0 --- /dev/null +++ b/SMCode/SM.OGL/Mesh/MeshAttribute.cs @@ -0,0 +1,16 @@ +namespace SM.OGL.Mesh +{ + public struct MeshAttribute + { + public int Index; + public string Name; + public VBO ConnectedVBO; + + public MeshAttribute(int index, string name, VBO buffer) + { + Index = index; + Name = name; + ConnectedVBO = buffer; + } + } +} \ No newline at end of file diff --git a/SMCode/SM.OGL/Mesh/MeshAttributeList.cs b/SMCode/SM.OGL/Mesh/MeshAttributeList.cs new file mode 100644 index 0000000..d5c2364 --- /dev/null +++ b/SMCode/SM.OGL/Mesh/MeshAttributeList.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; + +namespace SM.OGL.Mesh +{ + public class MeshAttributeList : List + { + public VBO this[string name] + { + get + { + for (int i = 0; i < Count; i++) + { + if (this[i].Name == name) + { + return this[i].ConnectedVBO; + } + } + + return null; + } + } + + public void Add(int id, string name, VBO vbo) + { + if (vbo == null) return; + Add(new MeshAttribute(id, name, vbo)); + } + } +} \ No newline at end of file diff --git a/SMCode/SM.OGL/SM.OGL.csproj b/SMCode/SM.OGL/SM.OGL.csproj index 960bba9..c65abb9 100644 --- a/SMCode/SM.OGL/SM.OGL.csproj +++ b/SMCode/SM.OGL/SM.OGL.csproj @@ -51,6 +51,8 @@ + + diff --git a/SMCode/SM2D/Scene/Scene.cs b/SMCode/SM2D/Scene/Scene.cs index e195669..64b6953 100644 --- a/SMCode/SM2D/Scene/Scene.cs +++ b/SMCode/SM2D/Scene/Scene.cs @@ -1,6 +1,9 @@ #region usings +using OpenTK; using OpenTK.Graphics; +using SM.Base.Contexts; +using SM.Base.Objects.Static; using SM.Base.Scene; using SM2D.Drawing; @@ -10,11 +13,30 @@ namespace SM2D.Scene { public class Scene : GenericScene { + private static DrawObject2D _axisHelper; + + public float AxisHelperSize = 100; + static Scene() + { + _axisHelper = new DrawObject2D(); + _axisHelper.ApplyMesh(AxisHelper.Object); + } + public Scene() { _Background = new DrawBackground(Color4.Black); } + public DrawBackground Background => (DrawBackground) _Background; + + public override void DrawDebug(DrawContext context) + { + if (ShowAxisHelper) + { + _axisHelper.Transform.Size.Set(AxisHelperSize, AxisHelperSize); + _axisHelper.Draw(context); + } + } } } \ No newline at end of file diff --git a/SMCode/SM2D/Shader/Default2DShader.cs b/SMCode/SM2D/Shader/Default2DShader.cs index d3692f7..62e8411 100644 --- a/SMCode/SM2D/Shader/Default2DShader.cs +++ b/SMCode/SM2D/Shader/Default2DShader.cs @@ -26,8 +26,8 @@ namespace SM2D.Shader // Vertex Uniforms Uniforms["MVP"].SetMatrix4(context.ModelMaster * context.View * context.World); Uniforms["HasVColor"] - .SetUniform1(context.Mesh.AttribDataIndex.ContainsKey(3) && context.Mesh.AttribDataIndex[3] != null); - + .SetUniform1(context.Mesh.Attributes["color"] != null); + /* Uniforms.GetArray("Instances").Set((i, uniforms) => { if (i >= context.Instances.Count) return false; @@ -38,7 +38,7 @@ namespace SM2D.Shader uniforms["TextureScale"].SetUniform2(instance.TextureScale); return true; - }); + });*/ // Fragment Uniforms Uniforms["Tint"].SetUniform4(context.Material.Tint); diff --git a/SMCode/SM2D/Shader/ShaderFiles/default.glsl b/SMCode/SM2D/Shader/ShaderFiles/default.glsl index efab3a6..3b7796a 100644 --- a/SMCode/SM2D/Shader/ShaderFiles/default.glsl +++ b/SMCode/SM2D/Shader/ShaderFiles/default.glsl @@ -24,6 +24,6 @@ uniform sampler2D Texture; layout(location = 0) out vec4 color; void fmain() { - color = vColor * Tint; - if (UseTexture) color *= texture(Texture, vTexture); + color = vColor; + //if (UseTexture) color *= texture(Texture, vTexture); } \ No newline at end of file diff --git a/SM_TEST/Program.cs b/SM_TEST/Program.cs index 4b02b87..3d7a8be 100644 --- a/SM_TEST/Program.cs +++ b/SM_TEST/Program.cs @@ -8,6 +8,7 @@ using OpenTK.Graphics; using OpenTK.Input; using SM.Base; using SM.Base.Scene; +using SM.Base.Textures; using SM.Base.Time; using SM.Utility; using SM2D; @@ -46,16 +47,17 @@ namespace SM_TEST { if (Keyboard.GetState()[Key.R]) particles.Trigger(); - particles.Paused = Keyboard.GetState()[Key.P]; + //particles.Paused = Keyboard.GetState()[Key.P]; } private static void WindowOnLoad(object sender, EventArgs e) { - particles = new DrawParticles(TimeSpan.FromSeconds(5)) - { - MaxSpeed = 10 - }; - window.CurrentScene.Objects.Add(particles); + scene.ShowAxisHelper = true; + + DrawObject2D kasten = new DrawObject2D(); + kasten.Transform.Size.Set(50,50); + kasten.Texture = new Texture(new Bitmap("herosword.png")); + scene.Objects.Add(kasten); //particles.Trigger(); }