Started work on actual engine

This commit is contained in:
Adrian Ulbrich 2019-11-08 23:56:17 +01:00
parent e63155370c
commit 3b214e75ef
No known key found for this signature in database
GPG key ID: A0612DCF5D911645
28 changed files with 333 additions and 767 deletions

View file

@ -0,0 +1,27 @@
namespace Unicity.Engine
{
public class GameObject
{
public float X = 0;
public float Y = 0;
public float Z = 0;
public float ScaleX = 0;
public float ScaleY = 0;
public float ScaleZ = 0;
public Model Model = null;
public GameObject()
{
}
public GameObject(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
}
}

View file

@ -0,0 +1,63 @@
using System;
using Unicity.Renderer;
namespace Unicity.Engine
{
public class GameWindow : IDisposable
{
RenderWindow window = null;
GraphicsRenderer renderer = null;
public int Width { get => window.Width; set => window.Width = value; }
public int Height { get => window.Height; set => window.Height = value; }
public string Title { get => window.Title; set => window.Title = value; }
bool disposed = false;
public GameWindow(int width = 800, int height = 800, string title = "Unicity Renderer written by Adrian Ulbrich")
{
window = new RenderWindow(width, height, title);
renderer = new GraphicsRenderer(window);
window.Render += Window_Render;
renderer.TestInit();
}
private void Window_Render(object sender, EventArgs e)
{
renderer.TestLoop();
}
public void Open(double ups = 60.0, double fps = 60.0)
{
window.Open(ups, fps);
renderer.TestInit();
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
{
return;
}
if (disposing)
{
// Dispose of managed resources
}
// Dispose of unmanaged resources
window?.Dispose();
disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}

7
Unicity.Engine/Model.cs Normal file
View file

@ -0,0 +1,7 @@
namespace Unicity.Engine
{
public class Model
{
}
}

View file

@ -5,12 +5,12 @@ using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die einer Assembly zugeordnet sind.
[assembly: AssemblyTitle("Unicity Render Test")]
[assembly: AssemblyDescription("Test for the Unicity renderer")]
[assembly: AssemblyTitle("Unicity.Engine")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Alee Productions")]
[assembly: AssemblyProduct("Unicity Renderer Test")]
[assembly: AssemblyCopyright("Copyright © 2019 Alee Productions")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Unicity.Engine")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@ -20,7 +20,7 @@ using System.Runtime.InteropServices;
[assembly: ComVisible(false)]
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
[assembly: Guid("0155c998-9189-4c19-8dcc-13499797c7ea")]
[assembly: Guid("cbf47abd-0454-496a-b3e3-848d27317c59")]
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
//

View file

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{CBF47ABD-0454-496A-B3E3-848D27317C59}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>Unicity.Engine</RootNamespace>
<AssemblyName>Unicity.Engine</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<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="GameWindow.cs" />
<Compile Include="Model.cs" />
<Compile Include="GameObject.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Unicity.Renderer\Unicity.Renderer.csproj">
<Project>{7c0c6e9f-6bc9-4825-b657-8dace26cf4fd}</Project>
<Name>Unicity.Renderer</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

15
Unicity.Game/Program.cs Normal file
View file

@ -0,0 +1,15 @@
using Unicity.Engine;
namespace Unicity.Game
{
class Program
{
static void Main(string[] args)
{
using (GameWindow gameWindow = new GameWindow(400, 400, "Render Test"))
{
gameWindow.Open();
}
}
}
}

View file

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die einer Assembly zugeordnet sind.
[assembly: AssemblyTitle("Unicity.Game")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Unicity.Game")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
[assembly: ComVisible(false)]
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
[assembly: Guid("4c824664-944b-4dca-89be-8cf16d249f51")]
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
//
// Hauptversion
// Nebenversion
// Buildnummer
// Revision
//
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View file

@ -4,10 +4,10 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{0155C998-9189-4C19-8DCC-13499797C7EA}</ProjectGuid>
<ProjectGuid>{4C824664-944B-4DCA-89BE-8CF16D249F51}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Unicity.RenderTest</RootNamespace>
<AssemblyName>RenderTest</AssemblyName>
<RootNamespace>Unicity.Game</RootNamespace>
<AssemblyName>Unicity.Game</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
@ -50,9 +50,9 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Unicity.Renderer\Unicity.Renderer.csproj">
<Project>{f6ebb1f8-fca9-4a8d-a807-e877cd047908}</Project>
<Name>Unicity.Renderer</Name>
<ProjectReference Include="..\Unicity.Engine\Unicity.Engine.csproj">
<Project>{cbf47abd-0454-496a-b3e3-848d27317c59}</Project>
<Name>Unicity.Engine</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

View file

@ -1,88 +0,0 @@
using Unicity.Renderer;
using Unicity.Renderer.Shapes;
namespace Unicity.RenderTest
{
class Program
{
static RenderWindow window = null;
static Camera camera = null;
static GraphicsRenderer renderer = null;
static Rectangle[] cube = null;
static void Main(string[] args)
{
camera = new Camera(CameraProjectionMode.Perspective);
using (window = new RenderWindow(400, 400, "Render Test"))
using (renderer = new GraphicsRenderer(window, camera))
{
window.Init += Window_Init;
window.Update += Window_Update;
window.Render += Window_Render;
window.Destroy += Window_Destroy;
window.Open();
}
}
private static void Window_Init(object sender, System.EventArgs e)
{
renderer.SetClearColor(0.0f, 0.0f, 0.0f, 1.0f);
renderer.SetDrawColor(1.0f, 0.0f, 1.0f);
cube = new Rectangle[]
{
new Rectangle(-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f),
new Rectangle(-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f),
new Rectangle(-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f),
new Rectangle(-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f),
new Rectangle(-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f),
new Rectangle(1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f)
};
}
private static void Window_Update(object sender, System.EventArgs e)
{
}
private static void Window_Render(object sender, System.EventArgs e)
{
renderer.ClearScreen();
renderer.DrawShapes(cube);
}
private static void Window_Destroy(object sender, System.EventArgs e)
{
foreach (Rectangle rect in cube)
{
rect.Dispose();
}
}
}
}

View file

@ -1,50 +0,0 @@
using OpenTK;
namespace Unicity.Renderer
{
public enum CameraProjectionMode
{
Ortographic,
Perspective
}
public class Camera
{
float x = 0f;
float y = 0f;
float z = 0f;
Matrix4 projectionMatrix = Matrix4.Identity;
CameraProjectionMode projectionMode = CameraProjectionMode.Ortographic;
public Camera(CameraProjectionMode projectionMode = CameraProjectionMode.Ortographic)
{
this.projectionMode = projectionMode;
}
public void Initialize()
{
switch (projectionMode)
{
case CameraProjectionMode.Ortographic:
projectionMatrix = Matrix4.CreateOrthographic(600f, 600.0f, 0.1f, 100f);
break;
case CameraProjectionMode.Perspective:
projectionMatrix = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45f), 1f, 0.1f, 100f);
break;
}
}
public void UpdateView(Shader shader)
{
Matrix4 modelMatrix = Matrix4.CreateRotationX(MathHelper.DegreesToRadians(-55.0f));
Matrix4 viewMatrix = Matrix4.CreateTranslation(0.0f, 0.0f, -8.0f);
shader.SetUniform("model", modelMatrix);
shader.SetUniform("view", viewMatrix);
shader.SetUniform("projection", projectionMatrix);
}
}
}

View file

@ -1,103 +1,45 @@
using System;
using System.Text;
using OpenTK;
using System.IO;
using OpenTK.Graphics.OpenGL4;
using Unicity.Renderer.Shapes;
namespace Unicity.Renderer
{
public class GraphicsRenderer : IDisposable
public class GraphicsRenderer
{
public RenderWindow window { get; }
public Camera camera = null;
RenderWindow window = null;
// Tests
float[] vertices =
{
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
Shader shader = null;
public GraphicsRenderer(RenderWindow window, Camera camera)
int VBO = 0;
public GraphicsRenderer(RenderWindow window)
{
this.window = window;
this.camera = camera;
Console.WriteLine("Compiling GLSL shaders...");
string vertexShader = Encoding.UTF8.GetString(Properties.Resources.vertexShader);
string fragmentShader = Encoding.UTF8.GetString(Properties.Resources.fragmentShader);
shader = new Shader(vertexShader, fragmentShader);
shader.Use();
Console.WriteLine("Reticulating splines...");
GL.ClearDepth(1.0f);
GL.Enable(EnableCap.DepthTest);
GL.DepthFunc(DepthFunction.Lequal);
camera.Initialize();
window.Update += Window_Update;
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
}
private void Window_Update(object sender, EventArgs e)
public void TestInit()
{
camera.UpdateView(shader);
shader.Use();
window.MakeCurrent();
string vertexCode = File.ReadAllText("shaders/test.vert");
shader = new Shader(vertexCode, "");
VBO = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw);
}
public void SetClearColor(float red, float green, float blue, float alpha)
public void TestLoop()
{
GL.ClearColor(red, green, blue, alpha);
}
public void SetDrawColor(float red, float green, float blue)
{
shader.SetUniform("color", new Vector3(red, green, blue));
}
public void DrawShape(Shape shape)
{
shape.Draw(shader, this);
}
public void DrawShapes(Shape[] shapes)
{
foreach (Shape shape in shapes)
{
DrawShape(shape);
}
}
public void ClearScreen()
{
window.window.MakeCurrent();
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
}
bool disposed = false;
protected virtual void Dispose(bool disposing)
{
// Return of already disposed
if (disposed)
{
return;
}
if (disposing)
{
// Free managed objects here
}
// Dispose of any unmanaged resources
window?.Dispose();
shader.Delete();
// Set disposed flag to true
disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
window.MakeCurrent();
}
}
}

View file

@ -5,12 +5,12 @@ using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die einer Assembly zugeordnet sind.
[assembly: AssemblyTitle("Unicity Renderer")]
[assembly: AssemblyDescription("Renderer for the Unicity game")]
[assembly: AssemblyTitle("Unicity.Renderer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Alee Productions")]
[assembly: AssemblyProduct("Unicity Renderer")]
[assembly: AssemblyCopyright("Copyright © 2019 Alee Productions")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Unicity.Renderer")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@ -20,7 +20,7 @@ using System.Runtime.InteropServices;
[assembly: ComVisible(false)]
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
[assembly: Guid("f6ebb1f8-fca9-4a8d-a807-e877cd047908")]
[assembly: Guid("7c0c6e9f-6bc9-4825-b657-8dace26cf4fd")]
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
//

View file

@ -1,83 +0,0 @@
//------------------------------------------------------------------------------
// <auto-generated>
// Dieser Code wurde von einem Tool generiert.
// Laufzeitversion:4.0.30319.42000
//
// Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn
// der Code erneut generiert wird.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Unicity.Renderer.Properties {
using System;
/// <summary>
/// Eine stark typisierte Ressourcenklasse zum Suchen von lokalisierten Zeichenfolgen usw.
/// </summary>
// Diese Klasse wurde von der StronglyTypedResourceBuilder automatisch generiert
// -Klasse über ein Tool wie ResGen oder Visual Studio automatisch generiert.
// Um einen Member hinzuzufügen oder zu entfernen, bearbeiten Sie die .ResX-Datei und führen dann ResGen
// mit der /str-Option erneut aus, oder Sie erstellen Ihr VS-Projekt neu.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources() {
}
/// <summary>
/// Gibt die zwischengespeicherte ResourceManager-Instanz zurück, die von dieser Klasse verwendet wird.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Unicity.Renderer.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Überschreibt die CurrentUICulture-Eigenschaft des aktuellen Threads für alle
/// Ressourcenzuordnungen, die diese stark typisierte Ressourcenklasse verwenden.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// Sucht eine lokalisierte Ressource vom Typ System.Byte[].
/// </summary>
internal static byte[] fragmentShader {
get {
object obj = ResourceManager.GetObject("fragmentShader", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary>
/// Sucht eine lokalisierte Ressource vom Typ System.Byte[].
/// </summary>
internal static byte[] vertexShader {
get {
object obj = ResourceManager.GetObject("vertexShader", resourceCulture);
return ((byte[])(obj));
}
}
}
}

View file

@ -1,127 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="fragmentShader" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\fragmentShader.frag;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="vertexShader" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\vertexShader.vert;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
</root>

View file

@ -1,6 +1,4 @@
using System;
using System.Diagnostics;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
@ -9,45 +7,35 @@ namespace Unicity.Renderer
{
public class RenderWindow : IDisposable
{
const int UPS = 60;
internal GameWindow window = null;
Stopwatch loopTimer = new Stopwatch();
public event EventHandler Init;
public event EventHandler Update;
public event EventHandler Render;
public event EventHandler Destroy;
public int Width
{
get => window.ClientSize.Width;
set => window.ClientSize = new Size(value, window.ClientSize.Height);
}
GameWindow window = null;
public int Height
{
get => window.ClientSize.Height;
set => window.ClientSize = new Size(window.ClientSize.Width, value);
}
public int Width { get => window.Width; set => window.Width = value; }
public int Height { get => window.Height; set => window.Height = value; }
public string Title
{
get => window.Title;
set => window.Title = value;
}
public string Title { get => window.Title; set => window.Title = value; }
bool running = false;
bool disposed = false;
public RenderWindow(int width, int height, string title)
{
window = new GameWindow(width, height, GraphicsMode.Default, title, GameWindowFlags.Default);
window = new GameWindow(width, height, GraphicsMode.Default, title, GameWindowFlags.Default, DisplayDevice.Default, 4, 0, GraphicsContextFlags.ForwardCompatible);
window.Load += Window_Load;
window.Resize += Window_Resize;
window.UpdateFrame += Window_UpdateFrame;
window.RenderFrame += Window_RenderFrame;
window.Unload += Window_Unload;
window.Resize += Window_Resize;
}
private void Window_Load(object sender, EventArgs e)
{
GL.Viewport(0, 0, Width, Height);
GL.ClearColor(1.0f, 0.0f, 0.0f, 1.0f);
}
private void Window_Resize(object sender, EventArgs e)
{
GL.Viewport(0, 0, Width, Height);
}
private void Window_UpdateFrame(object sender, FrameEventArgs e)
@ -57,45 +45,29 @@ namespace Unicity.Renderer
private void Window_RenderFrame(object sender, FrameEventArgs e)
{
Render?.Invoke(this, EventArgs.Empty);
GL.Clear(ClearBufferMask.ColorBufferBit);
Update?.Invoke(this, EventArgs.Empty);
window.SwapBuffers();
}
private void Window_Unload(object sender, EventArgs e)
public void Open(double ups, double fps)
{
Destroy?.Invoke(this, EventArgs.Empty);
window.Run(ups, fps);
}
private void Window_Resize(object sender, EventArgs e)
public void MakeCurrent()
{
GL.Viewport(0, 0, Width, Height);
Render?.Invoke(this, EventArgs.Empty);
window.SwapBuffers();
}
public void Open()
{
if (running)
if (window.IsExiting)
{
return;
}
Init?.Invoke(this, EventArgs.Empty);
window.VSync = VSyncMode.Off;
window.Run(UPS, 0);
running = true;
window.MakeCurrent();
}
public double GetFPS()
{
return window.RenderFrequency;
}
bool disposed = false;
protected virtual void Dispose(bool disposing)
{
// Return of already disposed
if (disposed)
{
return;
@ -103,13 +75,12 @@ namespace Unicity.Renderer
if (disposing)
{
// Free managed objects here
// Dispose of managed resources
}
// Dispose of any unmanaged resources
// Dispose of unmanaged resources
window?.Dispose();
// Set disposed flag to true
disposed = true;
}

View file

@ -1,10 +0,0 @@
#version 330 core
out vec4 color;
in vec4 inColor;
void main()
{
color = inColor;
}

View file

@ -1,19 +0,0 @@
#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform vec3 color;
uniform float alpha;
out vec4 inColor;
void main()
{
gl_Position = projection * view * model * vec4(vertexPosition_modelspace, 1.0);
inColor = vec4(color, 1.0);
}

View file

@ -1,95 +1,31 @@
using OpenTK;
using System;
using OpenTK.Graphics.OpenGL4;
namespace Unicity.Renderer
{
public class Shader
class Shader
{
int id = 0;
//int program = 0;
public Shader(string vertexShaderSource, string fragmentShaderSource)
public Shader(string vertexCode, string fragmentCode)
{
int vertexShader = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(vertexShader, vertexShaderSource);
GL.CompileShader(vertexShader);
int vertex = GL.CreateShader(ShaderType.VertexShader);
int[] isCompiled = new int[1];
GL.GetShader(vertexShader, ShaderParameter.CompileStatus, isCompiled);
if (isCompiled[0] == 0)
GL.ShaderSource(vertex, vertexCode);
GL.CompileShader(vertex);
if (!Compiled(vertex))
{
string infoLog = GL.GetShaderInfoLog(vertexShader);
GL.DeleteShader(vertexShader);
throw new ShaderCompilationFailedException("Failed to compile vertex shader:\n" + infoLog);
GL.DeleteShader(vertex);
throw new Exception("Failed to compile vertex shader: " + GL.GetShaderInfoLog(vertex));
}
int fragmentShader = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(fragmentShader, fragmentShaderSource);
GL.CompileShader(fragmentShader);
GL.GetShader(fragmentShader, ShaderParameter.CompileStatus, isCompiled);
if (isCompiled[0] == 0)
{
string infoLog = GL.GetShaderInfoLog(fragmentShader);
GL.DeleteShader(fragmentShader);
GL.DeleteShader(vertexShader);
throw new ShaderCompilationFailedException("Failed to compile fragment shader:\n" + infoLog);
}
int program = GL.CreateProgram();
GL.AttachShader(program, vertexShader);
GL.AttachShader(program, fragmentShader);
GL.LinkProgram(program);
int[] isLinked = new int[1];
GL.GetProgram(program, GetProgramParameterName.LinkStatus, isLinked);
if (isLinked[0] == 0)
{
string infoLog = GL.GetProgramInfoLog(program);
GL.DeleteProgram(program);
GL.DeleteShader(fragmentShader);
GL.DeleteShader(vertexShader);
throw new ShaderCompilationFailedException("Failed to link shader program");
}
GL.DetachShader(program, vertexShader);
GL.DetachShader(program, fragmentShader);
GL.DeleteShader(vertexShader);
GL.DeleteShader(fragmentShader);
id = program;
}
public void Use()
private bool Compiled(int shader)
{
GL.UseProgram(id);
}
public void SetUniform(string name, Matrix4 value)
{
int uniformLocation = GL.GetUniformLocation(id, name);
GL.UniformMatrix4(uniformLocation, false, ref value);
}
public void SetUniform(string name, Vector3 value)
{
int uniformLocation = GL.GetUniformLocation(id, name);
GL.Uniform3(uniformLocation, ref value);
}
public void SetUniform(string name, float value)
{
int uniformLocation = GL.GetUniformLocation(id, name);
GL.Uniform1(uniformLocation, value);
}
public void Delete()
{
GL.DeleteProgram(id);
int[] output = new int[1];
GL.GetShader(shader, ShaderParameter.CompileStatus, output);
return output[0] != 0;
}
}
}

View file

@ -1,9 +0,0 @@
using System;
namespace Unicity.Renderer
{
class ShaderCompilationFailedException : Exception
{
public ShaderCompilationFailedException(string message) : base(message) { }
}
}

View file

@ -1,26 +0,0 @@
namespace Unicity.Renderer.Shapes
{
public class Rectangle : Shape
{
Triangle rect1 = null;
Triangle rect2 = null;
public Rectangle(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4)
{
rect1 = new Triangle(x1, y1, z1, x2, y2, z2, x3, y3, z3);
rect2 = new Triangle(x3, y3, z3, x2, y2, z2, x4, y4, z4);
}
internal override void Draw(Shader shader, GraphicsRenderer renderer)
{
rect1.Draw(shader, renderer);
rect2.Draw(shader, renderer);
}
public override void Dispose()
{
rect1.Dispose();
rect2.Dispose();
}
}
}

View file

@ -1,17 +0,0 @@
using System;
namespace Unicity.Renderer.Shapes
{
public class Shape : IDisposable
{
internal virtual void Draw(Shader shader, GraphicsRenderer renderer)
{
}
public virtual void Dispose()
{
}
}
}

View file

@ -1,50 +0,0 @@
using OpenTK.Graphics.OpenGL4;
namespace Unicity.Renderer.Shapes
{
public class Triangle : Shape
{
int vao = 0;
int vbo = 0;
float[] vertices = null;
public Triangle(float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3)
{
vertices = new float[]
{
x1, y1, z1,
x2, y2, z2,
x3, y3, z3
};
vao = GL.GenVertexArray();
vbo = GL.GenBuffer();
GL.BindVertexArray(vao);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0);
GL.EnableVertexAttribArray(0);
}
internal override void Draw(Shader shader, GraphicsRenderer renderer)
{
GL.BindVertexArray(vao);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
float[] newVertices = new float[vertices.Length];
GL.DrawArrays(PrimitiveType.Triangles, 0, 3);
}
public override void Dispose()
{
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.DeleteBuffer(vbo);
}
}
}

View file

@ -4,10 +4,10 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F6EBB1F8-FCA9-4A8D-A807-E877CD047908}</ProjectGuid>
<ProjectGuid>{7C0C6E9F-6BC9-4825-B657-8DACE26CF4FD}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>Unicity.Renderer</RootNamespace>
<AssemblyName>Renderer</AssemblyName>
<AssemblyName>Unicity.Renderer</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
@ -41,7 +41,6 @@
</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" />
@ -50,35 +49,20 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Camera.cs" />
<Compile Include="GraphicsRenderer.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="RenderWindow.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RenderWindow.cs" />
<Compile Include="Shader.cs" />
<Compile Include="ShaderCompilationFailedException.cs" />
<Compile Include="Shapes\Rectangle.cs" />
<Compile Include="Shapes\Shape.cs" />
<Compile Include="Shapes\Triangle.cs" />
<Compile Include="WindowCreationFailedException.cs" />
</ItemGroup>
<ItemGroup>
<None Include="OpenTK.dll.config" />
<None Include="packages.config" />
<None Include="Resources\fragmentShader.frag" />
<None Include="Resources\vertexShader.vert" />
<None Include="shaders\test.frag">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="shaders\test.vert">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<SubType>Designer</SubType>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View file

@ -1,12 +0,0 @@
using System;
namespace Unicity.Renderer
{
class WindowCreationFailedException : Exception
{
public WindowCreationFailedException(string message) : base(message)
{
}
}
}

View file

@ -0,0 +1 @@


View file

@ -0,0 +1,8 @@
#version 400 core
layout (location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos, 1.0);
}

View file

@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29418.71
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicity.RenderTest", "Unicity.RenderTest\Unicity.RenderTest.csproj", "{0155C998-9189-4C19-8DCC-13499797C7EA}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicity.Engine", "Unicity.Engine\Unicity.Engine.csproj", "{CBF47ABD-0454-496A-B3E3-848D27317C59}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicity.Renderer", "Unicity.Renderer\Unicity.Renderer.csproj", "{F6EBB1F8-FCA9-4A8D-A807-E877CD047908}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicity.Game", "Unicity.Game\Unicity.Game.csproj", "{4C824664-944B-4DCA-89BE-8CF16D249F51}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unicity.Renderer", "Unicity.Renderer\Unicity.Renderer.csproj", "{7C0C6E9F-6BC9-4825-B657-8DACE26CF4FD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -13,14 +15,18 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F6EBB1F8-FCA9-4A8D-A807-E877CD047908}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F6EBB1F8-FCA9-4A8D-A807-E877CD047908}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F6EBB1F8-FCA9-4A8D-A807-E877CD047908}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F6EBB1F8-FCA9-4A8D-A807-E877CD047908}.Release|Any CPU.Build.0 = Release|Any CPU
{0155C998-9189-4C19-8DCC-13499797C7EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0155C998-9189-4C19-8DCC-13499797C7EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0155C998-9189-4C19-8DCC-13499797C7EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0155C998-9189-4C19-8DCC-13499797C7EA}.Release|Any CPU.Build.0 = Release|Any CPU
{CBF47ABD-0454-496A-B3E3-848D27317C59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CBF47ABD-0454-496A-B3E3-848D27317C59}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBF47ABD-0454-496A-B3E3-848D27317C59}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CBF47ABD-0454-496A-B3E3-848D27317C59}.Release|Any CPU.Build.0 = Release|Any CPU
{4C824664-944B-4DCA-89BE-8CF16D249F51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C824664-944B-4DCA-89BE-8CF16D249F51}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C824664-944B-4DCA-89BE-8CF16D249F51}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C824664-944B-4DCA-89BE-8CF16D249F51}.Release|Any CPU.Build.0 = Release|Any CPU
{7C0C6E9F-6BC9-4825-B657-8DACE26CF4FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7C0C6E9F-6BC9-4825-B657-8DACE26CF4FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C0C6E9F-6BC9-4825-B657-8DACE26CF4FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C0C6E9F-6BC9-4825-B657-8DACE26CF4FD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE