17.09.2020

+ Generic Scene
+ Generic Camera
+ Generic Window
+ Contexts for drawing and updateing

+ very basic 2D-implermention
This commit is contained in:
Michel Fedde 2020-09-17 21:28:16 +02:00
parent 9889366317
commit 589d131246
25 changed files with 383 additions and 78 deletions

View file

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

View file

@ -45,14 +45,23 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<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="Properties\AssemblyInfo.cs" />
<Compile Include="Scene\GenericCamera.cs" />
<Compile Include="Scene\GenericScene.cs" />
<Compile Include="StaticObjects\Plate.cs" /> <Compile Include="StaticObjects\Plate.cs" />
<Compile Include="TestShader.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="OpenTK.dll.config" /> <None Include="OpenTK.dll.config" />
<None Include="packages.config" /> <None Include="packages.config" />
<EmbeddedResource Include="Shader\Files\default.frag" />
<EmbeddedResource Include="Shader\Files\default.vert" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\SM.OGL\SM.OGL.csproj"> <ProjectReference Include="..\SM.OGL\SM.OGL.csproj">
@ -60,6 +69,8 @@
<Name>SM.OGL</Name> <Name>SM.OGL</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup /> <ItemGroup>
<Folder Include="Types\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

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

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

View file

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace SM.Base.Scene
{
public interface IShowCollection
{
List<IShowItem> Objects { get; }
}
}

View file

@ -0,0 +1,10 @@
using SM.Base.Contexts;
namespace SM.Base.Scene
{
public interface IShowItem
{
void Update(UpdateContext context);
void Draw(DrawContext context);
}
}

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

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

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

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

View file

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

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

View file

@ -0,0 +1,7 @@
namespace SM.Base.Contexts
{
public struct UpdateContext
{
public double Deltatime;
}
}

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

View file

@ -15,7 +15,7 @@ namespace SM.OGL.Shaders
ShaderFileFiles = shaderFileFiles; ShaderFileFiles = shaderFileFiles;
} }
public void Compile() public void Load()
{ {
_id = GL.CreateProgram(); _id = GL.CreateProgram();
@ -41,6 +41,11 @@ namespace SM.OGL.Shaders
} }
protected override void Compile()
{
Load();
}
public void DrawObject(Mesh.Mesh mesh, bool bindVAO = false) public void DrawObject(Mesh.Mesh mesh, bool bindVAO = false)
{ {
if (bindVAO) GL.BindVertexArray(mesh); if (bindVAO) GL.BindVertexArray(mesh);

View file

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using OpenTK.Graphics.OpenGL4; using OpenTK.Graphics.OpenGL4;
namespace SM.OGL.Shaders namespace SM.OGL.Shaders
@ -9,6 +10,8 @@ namespace SM.OGL.Shaders
public ShaderFile Geometry; public ShaderFile Geometry;
public ShaderFile Fragment; public ShaderFile Fragment;
public Action<UniformCollection> SetUniforms;
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)) {}
public ShaderFileCollection(ShaderFile vertex, ShaderFile fragment, ShaderFile geometry = default) public ShaderFileCollection(ShaderFile vertex, ShaderFile fragment, ShaderFile geometry = default)
@ -16,6 +19,8 @@ namespace SM.OGL.Shaders
Vertex = vertex; Vertex = vertex;
Geometry = geometry; Geometry = geometry;
Fragment = fragment; Fragment = fragment;
SetUniforms = u => { };
} }
internal void Append(GenericShader shader) internal void Append(GenericShader shader)

View file

@ -0,0 +1,24 @@
using OpenTK;
using SM.Base.Contexts;
using SM.Base.Scene;
using SM.Base.Shader;
using SM.Base.StaticObjects;
using SM.OGL.Mesh;
namespace SM2D.Drawing
{
public class DrawEmpty : IShowItem
{
public void Update(UpdateContext context)
{
throw new System.NotImplementedException();
}
public void Draw(DrawContext context)
{
context.ModelMatrix = Matrix4.CreateScale(100, 100, 1);
Shaders.Default.Draw(context);
}
}
}

10
SMCode/SM2D/GLWindow2D.cs Normal file
View file

@ -0,0 +1,10 @@
using SM.Base;
using SM2D.Scene;
namespace SM2D
{
public class GLWindow2D : GenericWindow<Scene.Scene, Camera>
{
}
}

View file

@ -0,0 +1,25 @@
<configuration>
<dllmap os="linux" dll="opengl32.dll" target="libGL.so.1"/>
<dllmap os="linux" dll="glu32.dll" target="libGLU.so.1"/>
<dllmap os="linux" dll="openal32.dll" target="libopenal.so.1"/>
<dllmap os="linux" dll="alut.dll" target="libalut.so.0"/>
<dllmap os="linux" dll="opencl.dll" target="libOpenCL.so"/>
<dllmap os="linux" dll="libX11" target="libX11.so.6"/>
<dllmap os="linux" dll="libXi" target="libXi.so.6"/>
<dllmap os="linux" dll="SDL2.dll" target="libSDL2-2.0.so.0"/>
<dllmap os="osx" dll="opengl32.dll" target="/System/Library/Frameworks/OpenGL.framework/OpenGL"/>
<dllmap os="osx" dll="openal32.dll" target="/System/Library/Frameworks/OpenAL.framework/OpenAL" />
<dllmap os="osx" dll="alut.dll" target="/System/Library/Frameworks/OpenAL.framework/OpenAL" />
<dllmap os="osx" dll="libGLES.dll" target="/System/Library/Frameworks/OpenGLES.framework/OpenGLES" />
<dllmap os="osx" dll="libGLESv1_CM.dll" target="/System/Library/Frameworks/OpenGLES.framework/OpenGLES" />
<dllmap os="osx" dll="libGLESv2.dll" target="/System/Library/Frameworks/OpenGLES.framework/OpenGLES" />
<dllmap os="osx" dll="opencl.dll" target="/System/Library/Frameworks/OpenCL.framework/OpenCL"/>
<dllmap os="osx" dll="SDL2.dll" target="libSDL2.dylib"/>
<!-- XQuartz compatibility (X11 on Mac) -->
<dllmap os="osx" dll="libGL.so.1" target="/usr/X11/lib/libGL.dylib"/>
<dllmap os="osx" dll="libX11" target="/usr/X11/lib/libX11.dylib"/>
<dllmap os="osx" dll="libXcursor.so.1" target="/usr/X11/lib/libXcursor.dylib"/>
<dllmap os="osx" dll="libXi" target="/usr/X11/lib/libXi.dylib"/>
<dllmap os="osx" dll="libXinerama" target="/usr/X11/lib/libXinerama.dylib"/>
<dllmap os="osx" dll="libXrandr.so.2" target="/usr/X11/lib/libXrandr.dylib"/>
</configuration>

View file

@ -31,6 +31,9 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <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>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
@ -41,7 +44,26 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Drawing\DrawEmpty.cs" />
<Compile Include="GLWindow2D.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scene\Camera.cs" />
<Compile Include="Scene\Scene.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SM.Base\SM.Base.csproj">
<Project>{8e733844-4204-43e7-b3dc-3913cddabb0d}</Project>
<Name>SM.Base</Name>
</ProjectReference>
<ProjectReference Include="..\SM.OGL\SM.OGL.csproj">
<Project>{f604d684-bc1d-4819-88b5-8b5d03a17be0}</Project>
<Name>SM.OGL</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="OpenTK.dll.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View file

@ -0,0 +1,22 @@
using OpenTK;
using SM.Base.Scene;
namespace SM2D.Scene
{
public class Camera : GenericCamera
{
public override bool Orthographic { get; } = true;
public Vector2 Position;
public override Matrix4 ViewCalculation()
{
return Matrix4.LookAt(Position.X, Position.Y, -1, Position.X, Position.Y, 0, 0, 1, 0);
}
public override void RecalculateWorld(float width, float height)
{
OrthographicWorld = Matrix4.CreateOrthographic(width, height, 0.1f, 100);
}
}
}

View file

@ -0,0 +1,9 @@
using SM.Base.Scene;
namespace SM2D.Scene
{
public class Scene : GenericScene<Camera>
{
}
}

View file

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

View file

@ -4,15 +4,26 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using SM.Base; using SM.Base;
using SM2D;
using SM2D.Drawing;
using SM2D.Scene;
namespace SM_TEST namespace SM_TEST
{ {
class Program class Program
{ {
static Scene scene;
static void Main(string[] args) static void Main(string[] args)
{ {
GenericWindow window = new GenericWindow(); GLWindow2D window = new GLWindow2D();
window.SetScene(scene = new Scene());
window.Load += WindowOnLoad;
window.Run(); window.Run();
} }
private static void WindowOnLoad(object sender, EventArgs e)
{
scene.Objects.Add(new DrawEmpty());
}
} }
} }

View file

@ -63,6 +63,10 @@
<Project>{f604d684-bc1d-4819-88b5-8b5d03a17be0}</Project> <Project>{f604d684-bc1d-4819-88b5-8b5d03a17be0}</Project>
<Name>SM.OGL</Name> <Name>SM.OGL</Name>
</ProjectReference> </ProjectReference>
<ProjectReference Include="..\SMCode\SM2D\SM2D.csproj">
<Project>{a4565538-625a-42c6-a330-dd4f1abb3986}</Project>
<Name>SM2D</Name>
</ProjectReference>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>