Change To OpenGL
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Silk.NET.OpenGL" Version="2.22.0" />
|
||||
<PackageReference Include="StbImageSharp" Version="2.30.15" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,91 @@
|
||||
using Silk.NET.OpenGL;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.Rendering
|
||||
{
|
||||
public class FirebirdShader
|
||||
{
|
||||
private readonly GL _gl;
|
||||
private readonly uint _program;
|
||||
|
||||
public FirebirdShader(GL gl, string vertexPath, string fragmentPath)
|
||||
{
|
||||
_gl = gl;
|
||||
|
||||
string vertexSource = File.ReadAllText(vertexPath);
|
||||
string fragmentSource = File.ReadAllText(fragmentPath);
|
||||
|
||||
uint vertexShader = CompileShader(ShaderType.VertexShader, vertexSource);
|
||||
uint fragmentShader = CompileShader(ShaderType.FragmentShader, fragmentSource);
|
||||
|
||||
_program = _gl.CreateProgram();
|
||||
_gl.AttachShader(_program, vertexShader);
|
||||
_gl.AttachShader(_program, fragmentShader);
|
||||
_gl.LinkProgram(_program);
|
||||
|
||||
// Fehlerprüfung
|
||||
_gl.GetProgram(_program, GLEnum.LinkStatus, out int status);
|
||||
if (status == 0)
|
||||
{
|
||||
string info = _gl.GetProgramInfoLog(_program);
|
||||
throw new Exception($"Shader-Linkfehler: {info}");
|
||||
}
|
||||
|
||||
_gl.DeleteShader(vertexShader);
|
||||
_gl.DeleteShader(fragmentShader);
|
||||
}
|
||||
|
||||
private uint CompileShader(ShaderType type, string source)
|
||||
{
|
||||
uint shader = _gl.CreateShader(type);
|
||||
_gl.ShaderSource(shader, source);
|
||||
_gl.CompileShader(shader);
|
||||
|
||||
// Fehlerprüfung
|
||||
_gl.GetShader(shader, ShaderParameterName.CompileStatus, out int status);
|
||||
if (status == 0)
|
||||
{
|
||||
string info = _gl.GetShaderInfoLog(shader);
|
||||
throw new Exception($"Shader-Kompilierungsfehler ({type}): {info}");
|
||||
}
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
public void Use()
|
||||
{
|
||||
_gl.UseProgram(_program);
|
||||
}
|
||||
|
||||
public void SetInt(string name, int value)
|
||||
{
|
||||
int location = _gl.GetUniformLocation(_program, name);
|
||||
_gl.Uniform1(location, value);
|
||||
}
|
||||
|
||||
public void SetFloat(string name, float value)
|
||||
{
|
||||
int location = _gl.GetUniformLocation(_program, name);
|
||||
_gl.Uniform1(location, value);
|
||||
}
|
||||
|
||||
public void SetMatrix4(string name, Matrix4x4 matrix)
|
||||
{
|
||||
int location = _gl.GetUniformLocation(_program, name);
|
||||
unsafe
|
||||
{
|
||||
_gl.UniformMatrix4(location, 1, false, (float*)&matrix);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_gl.DeleteProgram(_program);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using Silk.NET.OpenGL;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.Rendering
|
||||
{
|
||||
public static class Renderer
|
||||
{
|
||||
private static uint _vao = 0;
|
||||
private static uint _vbo = 0;
|
||||
private static bool _initialized = false;
|
||||
|
||||
private static float[] _quadVertices = new float[]
|
||||
{
|
||||
// positions // tex coords
|
||||
0f, 0f, 0f, 0f,
|
||||
1f, 0f, 1f, 0f,
|
||||
1f, 1f, 1f, 1f,
|
||||
0f, 1f, 0f, 1f
|
||||
};
|
||||
|
||||
private static ushort[] _indices = new ushort[] { 0, 1, 2, 2, 3, 0 };
|
||||
|
||||
public static void DrawQuad(GL gl, FirebirdShader shader, Vector2 pos, Vector2 size)
|
||||
{
|
||||
if (!_initialized)
|
||||
{
|
||||
_vao = gl.GenVertexArray();
|
||||
_vbo = gl.GenBuffer();
|
||||
var ebo = gl.GenBuffer();
|
||||
|
||||
gl.BindVertexArray(_vao);
|
||||
|
||||
gl.BindBuffer(BufferTargetARB.ArrayBuffer, _vbo);
|
||||
unsafe
|
||||
{
|
||||
fixed (float* v = _quadVertices)
|
||||
gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(sizeof(float) * _quadVertices.Length), v, BufferUsageARB.StaticDraw);
|
||||
|
||||
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo);
|
||||
fixed (ushort* i = _indices)
|
||||
gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(sizeof(ushort) * _indices.Length), i, BufferUsageARB.StaticDraw);
|
||||
|
||||
gl.EnableVertexAttribArray(0);
|
||||
gl.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, 4 * sizeof(float), (void*)0);
|
||||
gl.EnableVertexAttribArray(1);
|
||||
gl.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 4 * sizeof(float), (void*)(2 * sizeof(float)));
|
||||
}
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
Matrix4x4 model = Matrix4x4.CreateScale(size.X, size.Y, 1) * Matrix4x4.CreateTranslation(pos.X, pos.Y, 0);
|
||||
shader.SetMatrix4("model", model);
|
||||
|
||||
gl.BindVertexArray(_vao);
|
||||
unsafe
|
||||
{
|
||||
gl.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedShort, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Silk.NET.OpenGL;
|
||||
using StbImageSharp;
|
||||
using System.IO;
|
||||
|
||||
namespace Firebird2D.Rendering
|
||||
{
|
||||
public class Texture2D
|
||||
{
|
||||
private uint _handle;
|
||||
private GL _gl;
|
||||
|
||||
public Texture2D(GL gl, string path)
|
||||
{
|
||||
_gl = gl;
|
||||
_handle = gl.GenTexture();
|
||||
gl.BindTexture(TextureTarget.Texture2D, _handle);
|
||||
|
||||
using var stream = File.OpenRead(path);
|
||||
var image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
|
||||
unsafe
|
||||
{
|
||||
fixed (byte* data = image.Data)
|
||||
{
|
||||
gl.TexImage2D(TextureTarget.Texture2D, 0, InternalFormat.Rgba, (uint)image.Width, (uint)image.Height,
|
||||
0, PixelFormat.Rgba, PixelType.UnsignedByte, data);
|
||||
}
|
||||
}
|
||||
|
||||
gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
|
||||
gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
|
||||
}
|
||||
|
||||
public void Bind()
|
||||
{
|
||||
_gl.BindTexture(TextureTarget.Texture2D, _handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user