+ AxisHelper

~ Transformation can now set to be ignored. (Sending a Identity, when requested)
~ Changed how Meshes store Attributes
This commit is contained in:
Michel Fedde 2020-12-13 13:03:57 +01:00
parent 0895c600cf
commit 2e7051d800
14 changed files with 146 additions and 30 deletions

View file

@ -11,6 +11,8 @@ namespace SM.Base.Drawing
/// </summary>
public abstract class GenericTransformation
{
public bool Ignore = false;
/// <summary>
/// Contains the current model matrix.
/// </summary>
@ -27,6 +29,8 @@ namespace SM.Base.Drawing
/// <returns></returns>
public Matrix4 GetMatrix()
{
if (Ignore) return Matrix4.Identity;
if (_lastFrame != SMRenderer.CurrentFrame)
{
_lastFrame = SMRenderer.CurrentFrame;

View file

@ -14,7 +14,7 @@ namespace SM.Base.Objects
/// </summary>
protected Mesh()
{
AttribDataIndex.Add(3, Color);
Attributes.Add(3, "color", Color);
}
/// <summary>

View file

@ -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;
}
}

View file

@ -56,6 +56,7 @@
<Compile Include="Drawing\Particles\ParticleDrawingBasis.cs" />
<Compile Include="Log.cs" />
<Compile Include="Objects\Mesh.cs" />
<Compile Include="Objects\Static\AxisHelper.cs" />
<Compile Include="PostProcess\PostProcessEffect.cs" />
<Compile Include="PostProcess\PostProcessShader.cs" />
<Compile Include="Scene\IScriptable.cs" />

View file

@ -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
/// </summary>
public abstract class GenericScene
{
private IBackgroundItem _background;
/// <summary>
@ -34,6 +33,7 @@ namespace SM.Base.Scene
/// </summary>
public bool IsInitialized { get; private set; }
/// <summary>
/// Updates this scene.
/// </summary>
@ -91,6 +91,8 @@ namespace SM.Base.Scene
private TCollection _hud = new TCollection();
private TCollection _objectCollection = new TCollection();
public bool ShowAxisHelper { get; set; } = false;
/// <summary>
/// A collection for cameras to switch easier to different cameras.
/// </summary>
@ -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)
{
}
}
}

View file

@ -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));
}

View file

@ -13,16 +13,17 @@ namespace SM.OGL.Mesh
/// </summary>
public abstract class GenericMesh : GLObject
{
/// <summary>
/// Generates the AttribDataIndex
/// </summary>
protected GenericMesh()
{
AttribDataIndex = new Dictionary<int, VBO>
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
/// <summary>
/// Connects the different buffer objects with ids.
/// </summary>
public Dictionary<int, VBO> AttribDataIndex { get; }
public MeshAttributeList Attributes { get; }
/// <summary>
/// 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);
}

View file

@ -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;
}
}
}

View file

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace SM.OGL.Mesh
{
public class MeshAttributeList : List<MeshAttribute>
{
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));
}
}
}

View file

@ -51,6 +51,8 @@
<Compile Include="GLSystem.cs" />
<Compile Include="Mesh\BoundingBox.cs" />
<Compile Include="Mesh\GenericMesh.cs" />
<Compile Include="Mesh\MeshAttribute.cs" />
<Compile Include="Mesh\MeshAttributeList.cs" />
<Compile Include="Mesh\VBO.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Shaders\GenericShader.cs" />

View file

@ -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<Camera, ItemCollection, I2DShowItem>
{
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);
}
}
}
}

View file

@ -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);

View file

@ -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);
}