17.09.2020
+ Generic Scene + Generic Camera + Generic Window + Contexts for drawing and updateing + very basic 2D-implermention
This commit is contained in:
parent
9889366317
commit
589d131246
25 changed files with 383 additions and 78 deletions
|
|
@ -1,47 +0,0 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using SM.OGL.Shaders;
|
||||
|
||||
namespace SM.Base
|
||||
{
|
||||
public class GenericWindow : GameWindow
|
||||
{
|
||||
private TestShader shader;
|
||||
private Matrix4 _viewMatrix;
|
||||
|
||||
public GenericWindow() : base(1280, 720, GraphicsMode.Default, "Testing", GameWindowFlags.Default, DisplayDevice.Default, 0, 0, GraphicsContextFlags.Default, null, true)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnLoad(EventArgs e)
|
||||
{
|
||||
base.OnLoad(e);
|
||||
shader = new TestShader(new ShaderFileCollection(File.ReadAllText("test/test.vert"), File.ReadAllText("test/test.frag")));
|
||||
shader.Compile();
|
||||
}
|
||||
|
||||
protected override void OnRenderFrame(FrameEventArgs e)
|
||||
{
|
||||
base.OnRenderFrame(e);
|
||||
|
||||
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
||||
|
||||
shader.Draw(_viewMatrix);
|
||||
|
||||
SwapBuffers();
|
||||
}
|
||||
|
||||
protected override void OnResize(EventArgs e)
|
||||
{
|
||||
base.OnResize(e);
|
||||
|
||||
GL.Viewport(ClientRectangle);
|
||||
_viewMatrix = Matrix4.LookAt(Vector3.UnitZ, Vector3.Zero, Vector3.UnitY) *
|
||||
Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90), Width / Height, 0.1f, 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -45,14 +45,23 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="GenericWindow.cs" />
|
||||
<Compile Include="Scene\IShowCollection.cs" />
|
||||
<Compile Include="Scene\IShowItem.cs" />
|
||||
<Compile Include="Shader\InstanceShader.cs" />
|
||||
<Compile Include="Shader\Shaders.cs" />
|
||||
<Compile Include="Window\Contexts\DrawContext.cs" />
|
||||
<Compile Include="Window\Contexts\UpdateContext.cs" />
|
||||
<Compile Include="Window\GenericWindow.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Scene\GenericCamera.cs" />
|
||||
<Compile Include="Scene\GenericScene.cs" />
|
||||
<Compile Include="StaticObjects\Plate.cs" />
|
||||
<Compile Include="TestShader.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="OpenTK.dll.config" />
|
||||
<None Include="packages.config" />
|
||||
<EmbeddedResource Include="Shader\Files\default.frag" />
|
||||
<EmbeddedResource Include="Shader\Files\default.vert" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SM.OGL\SM.OGL.csproj">
|
||||
|
|
@ -60,6 +69,8 @@
|
|||
<Name>SM.OGL</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<ItemGroup>
|
||||
<Folder Include="Types\" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
26
SMCode/SM.Base/Scene/GenericCamera.cs
Normal file
26
SMCode/SM.Base/Scene/GenericCamera.cs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
using OpenTK;
|
||||
|
||||
namespace SM.Base.Scene
|
||||
{
|
||||
public abstract class GenericCamera
|
||||
{
|
||||
public static Matrix4 OrthographicWorld { get; protected set; }
|
||||
public static Matrix4 PerspectiveWorld { get; protected set; }
|
||||
public static Vector3 UpVector { get; set; } = Vector3.UnitY;
|
||||
|
||||
public Matrix4 ViewMatrix { get; protected set; }
|
||||
|
||||
public Matrix4 World => Orthographic ? OrthographicWorld : PerspectiveWorld;
|
||||
|
||||
internal Matrix4 CalculateViewMatrix()
|
||||
{
|
||||
ViewMatrix = ViewCalculation();
|
||||
return ViewMatrix;
|
||||
}
|
||||
|
||||
public abstract Matrix4 ViewCalculation();
|
||||
|
||||
public abstract bool Orthographic { get; }
|
||||
public abstract void RecalculateWorld(float width, float height);
|
||||
}
|
||||
}
|
||||
23
SMCode/SM.Base/Scene/GenericScene.cs
Normal file
23
SMCode/SM.Base/Scene/GenericScene.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
using System.Collections.Generic;
|
||||
using SM.Base.Contexts;
|
||||
|
||||
namespace SM.Base.Scene
|
||||
{
|
||||
public abstract class GenericScene<TCamera> : IShowCollection
|
||||
where TCamera : GenericCamera
|
||||
{
|
||||
|
||||
public List<IShowItem> Objects { get; } = new List<IShowItem>();
|
||||
public TCamera Camera { get; set; }
|
||||
public Dictionary<string, TCamera> Cameras = new Dictionary<string, TCamera>();
|
||||
|
||||
public void Draw(DrawContext context)
|
||||
{
|
||||
if (!context.ForceViewport && Camera != null) context.View = Camera.ViewMatrix;
|
||||
|
||||
for(int i = 0; i < Objects.Count; i++)
|
||||
Objects[i].Draw(context);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
9
SMCode/SM.Base/Scene/IShowCollection.cs
Normal file
9
SMCode/SM.Base/Scene/IShowCollection.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace SM.Base.Scene
|
||||
{
|
||||
public interface IShowCollection
|
||||
{
|
||||
List<IShowItem> Objects { get; }
|
||||
}
|
||||
}
|
||||
10
SMCode/SM.Base/Scene/IShowItem.cs
Normal file
10
SMCode/SM.Base/Scene/IShowItem.cs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
using SM.Base.Contexts;
|
||||
|
||||
namespace SM.Base.Scene
|
||||
{
|
||||
public interface IShowItem
|
||||
{
|
||||
void Update(UpdateContext context);
|
||||
void Draw(DrawContext context);
|
||||
}
|
||||
}
|
||||
8
SMCode/SM.Base/Shader/Files/default.frag
Normal file
8
SMCode/SM.Base/Shader/Files/default.frag
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#version 330
|
||||
uniform vec4 Tint;
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
void main() {
|
||||
color = vec4(1,1,1,1) + Tint;
|
||||
}
|
||||
9
SMCode/SM.Base/Shader/Files/default.vert
Normal file
9
SMCode/SM.Base/Shader/Files/default.vert
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#version 330
|
||||
layout(location = 0) in vec3 aPos;
|
||||
|
||||
uniform mat4 MVP;
|
||||
uniform mat4 ModelMatrix;
|
||||
|
||||
void main() {
|
||||
gl_Position = MVP * ModelMatrix * vec4(aPos, 1);
|
||||
}
|
||||
32
SMCode/SM.Base/Shader/InstanceShader.cs
Normal file
32
SMCode/SM.Base/Shader/InstanceShader.cs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
using System;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using SM.Base.Contexts;
|
||||
using SM.Base.StaticObjects;
|
||||
using SM.OGL.Shaders;
|
||||
|
||||
namespace SM.Base.Shader
|
||||
{
|
||||
public class InstanceShader : GenericShader
|
||||
{
|
||||
protected override bool AutoCompile { get; } = true;
|
||||
|
||||
public Action<UniformCollection, DrawContext> SetUniform;
|
||||
|
||||
public InstanceShader(string vertex, string fragment, Action<UniformCollection, DrawContext> setUniform) : base(
|
||||
new ShaderFileCollection(vertex, fragment))
|
||||
{
|
||||
SetUniform = setUniform;
|
||||
}
|
||||
public void Draw(DrawContext context)
|
||||
{
|
||||
GL.UseProgram(this);
|
||||
|
||||
SetUniform.Invoke(Uniforms, context);
|
||||
|
||||
DrawObject(context.Mesh, true);
|
||||
|
||||
GL.UseProgram(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
18
SMCode/SM.Base/Shader/Shaders.cs
Normal file
18
SMCode/SM.Base/Shader/Shaders.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
using System.IO;
|
||||
using System.Reflection;
|
||||
using SM.OGL.Shaders;
|
||||
|
||||
namespace SM.Base.Shader
|
||||
{
|
||||
public class Shaders
|
||||
{
|
||||
public static InstanceShader Default = new InstanceShader(new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("SM.Base.Shader.Files.default.vert")).ReadToEnd(),
|
||||
new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("SM.Base.Shader.Files.default.frag")).ReadToEnd(),
|
||||
((u, context) =>
|
||||
{
|
||||
u["MVP"].SetMatrix4(context.View * context.World);
|
||||
u["ModelMatrix"].SetMatrix4(context.ModelMatrix);
|
||||
u["Tint"].SetUniform4(1,1,1,1);
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
using OpenTK;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using SM.Base.StaticObjects;
|
||||
using SM.OGL.Shaders;
|
||||
|
||||
namespace SM.Base
|
||||
{
|
||||
public class TestShader : GenericShader
|
||||
{
|
||||
public TestShader(ShaderFileCollection shaderFileFiles) : base(shaderFileFiles)
|
||||
{
|
||||
}
|
||||
|
||||
public void Draw(Matrix4 mvp)
|
||||
{
|
||||
GL.UseProgram(this);
|
||||
|
||||
Uniforms["MVP"].SetMatrix4(mvp);
|
||||
|
||||
DrawObject(Plate.Object, true);
|
||||
|
||||
GL.UseProgram(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
18
SMCode/SM.Base/Window/Contexts/DrawContext.cs
Normal file
18
SMCode/SM.Base/Window/Contexts/DrawContext.cs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
using OpenTK;
|
||||
using SM.Base.Scene;
|
||||
using SM.OGL.Mesh;
|
||||
|
||||
namespace SM.Base.Contexts
|
||||
{
|
||||
public struct DrawContext
|
||||
{
|
||||
public bool ForceViewport;
|
||||
|
||||
public Matrix4 World;
|
||||
public Matrix4 View;
|
||||
public Matrix4 ModelMatrix;
|
||||
|
||||
public Mesh Mesh;
|
||||
|
||||
}
|
||||
}
|
||||
7
SMCode/SM.Base/Window/Contexts/UpdateContext.cs
Normal file
7
SMCode/SM.Base/Window/Contexts/UpdateContext.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
namespace SM.Base.Contexts
|
||||
{
|
||||
public struct UpdateContext
|
||||
{
|
||||
public double Deltatime;
|
||||
}
|
||||
}
|
||||
65
SMCode/SM.Base/Window/GenericWindow.cs
Normal file
65
SMCode/SM.Base/Window/GenericWindow.cs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using OpenTK;
|
||||
using OpenTK.Graphics;
|
||||
using OpenTK.Graphics.OpenGL4;
|
||||
using SM.Base.Contexts;
|
||||
using SM.Base.Scene;
|
||||
using SM.Base.StaticObjects;
|
||||
using SM.OGL.Shaders;
|
||||
|
||||
namespace SM.Base
|
||||
{
|
||||
public class GenericWindow<TScene, TCamera> : GameWindow
|
||||
where TScene : GenericScene<TCamera>, new()
|
||||
where TCamera : GenericCamera, new()
|
||||
{
|
||||
private TCamera _viewportCamera;
|
||||
|
||||
public TScene CurrentScene { get; private set; }
|
||||
public bool ForceViewportCamera { get; set; } = false;
|
||||
|
||||
public GenericWindow() : base(1280, 720, GraphicsMode.Default, "Testing", GameWindowFlags.Default, DisplayDevice.Default, 0, 0, GraphicsContextFlags.Default, null, true)
|
||||
{
|
||||
_viewportCamera = new TCamera();
|
||||
}
|
||||
|
||||
protected override void OnLoad(EventArgs e)
|
||||
{
|
||||
base.OnLoad(e);
|
||||
}
|
||||
|
||||
protected override void OnRenderFrame(FrameEventArgs e)
|
||||
{
|
||||
DrawContext drawContext = new DrawContext()
|
||||
{
|
||||
World = _viewportCamera.World,
|
||||
View = _viewportCamera.CalculateViewMatrix(),
|
||||
ModelMatrix = Matrix4.Identity,
|
||||
Mesh = Plate.Object,
|
||||
ForceViewport = ForceViewportCamera
|
||||
};
|
||||
|
||||
base.OnRenderFrame(e);
|
||||
|
||||
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
||||
|
||||
CurrentScene.Draw(drawContext);
|
||||
|
||||
SwapBuffers();
|
||||
}
|
||||
|
||||
protected override void OnResize(EventArgs e)
|
||||
{
|
||||
base.OnResize(e);
|
||||
|
||||
GL.Viewport(ClientRectangle);
|
||||
_viewportCamera.RecalculateWorld(Width, Height);
|
||||
}
|
||||
|
||||
public virtual void SetScene(TScene scene)
|
||||
{
|
||||
CurrentScene = scene;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue