Colin McMillen
11 months ago
3 changed files with 318 additions and 0 deletions
-
278Program.cs
-
15Totte.csproj
-
25Totte.sln
@ -0,0 +1,278 @@ |
|||||
|
using OpenTK.Graphics.OpenGL4; |
||||
|
using OpenTK.Mathematics; |
||||
|
using OpenTK.Windowing.Common; |
||||
|
using OpenTK.Windowing.Desktop; |
||||
|
using OpenTK.Windowing.GraphicsLibraryFramework; |
||||
|
// https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.Image.html
|
||||
|
using SixLabors.ImageSharp; |
||||
|
using SixLabors.ImageSharp.Metadata.Profiles.Exif; |
||||
|
using System.Reflection.Metadata; |
||||
|
using System.Runtime.CompilerServices; |
||||
|
using static System.Runtime.InteropServices.JavaScript.JSType; |
||||
|
using System.Xml.Linq; |
||||
|
using Image = SixLabors.ImageSharp.Image; |
||||
|
|
||||
|
namespace SemiColinGames; |
||||
|
|
||||
|
public class Shader : IDisposable { |
||||
|
int Handle; |
||||
|
|
||||
|
public Shader() { |
||||
|
int VertexShader; |
||||
|
int FragmentShader; |
||||
|
|
||||
|
string VertexShaderSource = @"
|
||||
|
#version 330 |
||||
|
|
||||
|
layout(location = 0) in vec3 aPosition; |
||||
|
layout(location = 1) in vec2 aTexCoord; |
||||
|
|
||||
|
out vec2 texCoord; |
||||
|
|
||||
|
uniform mat4 projection; |
||||
|
|
||||
|
void main(void) { |
||||
|
texCoord = aTexCoord; |
||||
|
gl_Position = vec4(aPosition, 1.0) * projection; |
||||
|
}";
|
||||
|
|
||||
|
string FragmentShaderSource = @"
|
||||
|
#version 330 |
||||
|
|
||||
|
out vec4 outputColor; |
||||
|
in vec2 texCoord; |
||||
|
uniform sampler2D texture0; |
||||
|
|
||||
|
void main() { |
||||
|
outputColor = texture(texture0, texCoord); |
||||
|
}";
|
||||
|
|
||||
|
VertexShader = GL.CreateShader(ShaderType.VertexShader); |
||||
|
GL.ShaderSource(VertexShader, VertexShaderSource); |
||||
|
|
||||
|
FragmentShader = GL.CreateShader(ShaderType.FragmentShader); |
||||
|
GL.ShaderSource(FragmentShader, FragmentShaderSource); |
||||
|
|
||||
|
GL.CompileShader(VertexShader); |
||||
|
|
||||
|
int success; |
||||
|
GL.GetShader(VertexShader, ShaderParameter.CompileStatus, out success); |
||||
|
if (success == 0) { |
||||
|
string infoLog = GL.GetShaderInfoLog(VertexShader); |
||||
|
Console.WriteLine(infoLog); |
||||
|
} |
||||
|
|
||||
|
GL.CompileShader(FragmentShader); |
||||
|
|
||||
|
GL.GetShader(FragmentShader, ShaderParameter.CompileStatus, out success); |
||||
|
if (success == 0) { |
||||
|
string infoLog = GL.GetShaderInfoLog(FragmentShader); |
||||
|
Console.WriteLine(infoLog); |
||||
|
} |
||||
|
Handle = GL.CreateProgram(); |
||||
|
|
||||
|
GL.AttachShader(Handle, VertexShader); |
||||
|
GL.AttachShader(Handle, FragmentShader); |
||||
|
|
||||
|
GL.LinkProgram(Handle); |
||||
|
|
||||
|
GL.GetProgram(Handle, GetProgramParameterName.LinkStatus, out success); |
||||
|
if (success == 0) { |
||||
|
string infoLog = GL.GetProgramInfoLog(Handle); |
||||
|
Console.WriteLine(infoLog); |
||||
|
} |
||||
|
|
||||
|
GL.DetachShader(Handle, VertexShader); |
||||
|
GL.DetachShader(Handle, FragmentShader); |
||||
|
GL.DeleteShader(FragmentShader); |
||||
|
GL.DeleteShader(VertexShader); |
||||
|
} |
||||
|
|
||||
|
public void Use() { |
||||
|
GL.UseProgram(Handle); |
||||
|
} |
||||
|
|
||||
|
private bool disposedValue = false; |
||||
|
|
||||
|
protected virtual void Dispose(bool disposing) { |
||||
|
if (!disposedValue) { |
||||
|
GL.DeleteProgram(Handle); |
||||
|
disposedValue = true; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
~Shader() { |
||||
|
if (disposedValue == false) { |
||||
|
Console.WriteLine("GPU Resource leak! Did you forget to call Dispose()?"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void Dispose() { |
||||
|
Dispose(true); |
||||
|
GC.SuppressFinalize(this); |
||||
|
} |
||||
|
|
||||
|
public int GetAttribLocation(string name) { |
||||
|
return GL.GetAttribLocation(Handle, name); |
||||
|
} |
||||
|
|
||||
|
public int GetUniformLocation(string name) { |
||||
|
return GL.GetUniformLocation(Handle, name); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public class Game : GameWindow { |
||||
|
public Game(GameWindowSettings gwSettings, NativeWindowSettings nwSettings) : base(gwSettings, nwSettings) { } |
||||
|
|
||||
|
int frameCount; |
||||
|
int windowWidth; |
||||
|
int windowHeight; |
||||
|
float[] vertices = { |
||||
|
// Position Texture coordinates
|
||||
|
0f, 0f, 0.0f, 0.0f, 0.0f, // top left
|
||||
|
2560f, 0f, 0.0f, 1.0f, 0.0f, // top right
|
||||
|
2560f, 1440f, 0.0f, 1.0f, 1.0f, // bottom right
|
||||
|
0f, 1440f, 0.0f, 0.0f, 1.0f, // bottom left
|
||||
|
}; |
||||
|
|
||||
|
uint[] indices = { |
||||
|
0, 1, 3, // first triangle
|
||||
|
1, 2, 3 // second triangle
|
||||
|
}; |
||||
|
|
||||
|
int VertexBufferObject; |
||||
|
int ElementBufferObject; |
||||
|
int VertexArrayObject; |
||||
|
int texture; |
||||
|
Shader shader; |
||||
|
Matrix4 projection; |
||||
|
|
||||
|
protected override void OnUpdateFrame(FrameEventArgs e) { |
||||
|
base.OnUpdateFrame(e); |
||||
|
|
||||
|
KeyboardState input = KeyboardState; |
||||
|
|
||||
|
if (input.IsKeyDown(Keys.Escape)) { |
||||
|
Close(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected override void OnLoad() { |
||||
|
base.OnLoad(); |
||||
|
|
||||
|
GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f); |
||||
|
|
||||
|
VertexArrayObject = GL.GenVertexArray(); |
||||
|
GL.BindVertexArray(VertexArrayObject); |
||||
|
|
||||
|
VertexBufferObject = GL.GenBuffer(); |
||||
|
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject); |
||||
|
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw); |
||||
|
|
||||
|
ElementBufferObject = GL.GenBuffer(); |
||||
|
GL.BindBuffer(BufferTarget.ElementArrayBuffer, ElementBufferObject); |
||||
|
GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length * sizeof(uint), indices, BufferUsageHint.StaticDraw); |
||||
|
|
||||
|
shader = new Shader(); |
||||
|
shader.Use(); |
||||
|
|
||||
|
// Because there's now 5 floats between the start of the first vertex and the start of the second,
|
||||
|
// we modify the stride from 3 * sizeof(float) to 5 * sizeof(float).
|
||||
|
// This will now pass the new vertex array to the buffer.
|
||||
|
var vertexLocation = shader.GetAttribLocation("aPosition"); |
||||
|
GL.EnableVertexAttribArray(vertexLocation); |
||||
|
GL.VertexAttribPointer(vertexLocation, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0); |
||||
|
|
||||
|
// Next, we also setup texture coordinates. It works in much the same way.
|
||||
|
// We add an offset of 3, since the texture coordinates comes after the position data.
|
||||
|
// We also change the amount of data to 2 because there's only 2 floats for texture coordinates.
|
||||
|
var texCoordLocation = shader.GetAttribLocation("aTexCoord"); |
||||
|
GL.EnableVertexAttribArray(texCoordLocation); |
||||
|
GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float)); |
||||
|
|
||||
|
// Decode a JPEG image.
|
||||
|
//Image<Rgba32> image = Image.Load<Rgba32>(@"c:\users\colin\pictures\texture.jpg");
|
||||
|
Image<Rgba32> image = Image.Load<Rgba32>(@"c:\users\colin\pictures\photos\2023\06\27\DSC_0035.jpg"); |
||||
|
Console.WriteLine($"{image.Width}x{image.Height}"); |
||||
|
//foreach (IExifValue exif in image.Metadata.ExifProfile.Values) {
|
||||
|
// Console.WriteLine($"{exif.Tag} : {exif.GetValue()}");
|
||||
|
//}
|
||||
|
byte[] pixelBytes = new byte[image.Width * image.Height * Unsafe.SizeOf<Rgba32>()]; |
||||
|
image.CopyPixelDataTo(pixelBytes); |
||||
|
image.Dispose(); |
||||
|
|
||||
|
// TODO: put the texture into an actual class.
|
||||
|
texture = GL.GenTexture(); |
||||
|
GL.ActiveTexture(TextureUnit.Texture0); |
||||
|
GL.BindTexture(TextureTarget.Texture2D, texture); |
||||
|
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, image.Width, image.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, pixelBytes); |
||||
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.LinearMipmapLinear); // FIXME: is this right?
|
||||
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) TextureMagFilter.Nearest); |
||||
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) TextureWrapMode.ClampToBorder); |
||||
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) TextureWrapMode.ClampToBorder); |
||||
|
float[] borderColor = { 0.0f, 0.0f, 0.0f, 1.0f }; |
||||
|
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBorderColor, borderColor); |
||||
|
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); |
||||
|
} |
||||
|
|
||||
|
protected override void OnUnload() { |
||||
|
base.OnUnload(); |
||||
|
} |
||||
|
|
||||
|
protected override void OnRenderFrame(FrameEventArgs e) { |
||||
|
base.OnRenderFrame(e); |
||||
|
frameCount++; |
||||
|
GL.Clear(ClearBufferMask.ColorBufferBit); |
||||
|
GL.BindVertexArray(VertexArrayObject); |
||||
|
GL.ActiveTexture(TextureUnit.Texture0); |
||||
|
GL.BindTexture(TextureTarget.Texture2D, texture); |
||||
|
shader.Use(); |
||||
|
GL.DrawElements(PrimitiveType.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0); |
||||
|
SwapBuffers(); |
||||
|
} |
||||
|
|
||||
|
protected override void OnResize(ResizeEventArgs e) { |
||||
|
base.OnResize(e); |
||||
|
Console.WriteLine($"OnResize: {e.Width}x{e.Height}"); |
||||
|
windowWidth = e.Width; |
||||
|
windowHeight = e.Height; |
||||
|
|
||||
|
projection = Matrix4.CreateOrthographicOffCenter(0f, windowWidth, windowHeight, 0f, -1f, 1f); |
||||
|
GL.UniformMatrix4(shader.GetUniformLocation("projection"), true, ref projection); |
||||
|
GL.Viewport(0, 0, windowWidth, windowHeight); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
static class Program { |
||||
|
static void Main(string[] args) { |
||||
|
List<MonitorInfo> monitors = Monitors.GetMonitors(); |
||||
|
MonitorInfo bestMonitor = monitors[0]; |
||||
|
int bestResolution = bestMonitor.HorizontalResolution * bestMonitor.VerticalResolution; |
||||
|
for (int i = 1; i < monitors.Count; i++) { |
||||
|
MonitorInfo monitor = monitors[i]; |
||||
|
int resolution = monitor.HorizontalResolution * monitor.VerticalResolution; |
||||
|
if (resolution > bestResolution) { |
||||
|
bestResolution = resolution; |
||||
|
bestMonitor = monitor; |
||||
|
} |
||||
|
} |
||||
|
Console.WriteLine($"best monitor: {bestMonitor.HorizontalResolution}x{bestMonitor.VerticalResolution}"); |
||||
|
Console.WriteLine($"{bestMonitor.ClientArea}, {bestMonitor.WorkArea}"); |
||||
|
GameWindowSettings gwSettings = new GameWindowSettings(); |
||||
|
gwSettings.RenderFrequency = 60.0; |
||||
|
|
||||
|
NativeWindowSettings nwSettings = new NativeWindowSettings(); |
||||
|
nwSettings.WindowState = WindowState.Normal; |
||||
|
nwSettings.CurrentMonitor = bestMonitor.Handle; |
||||
|
nwSettings.Location = new Vector2i(bestMonitor.WorkArea.Min.X + 1, bestMonitor.WorkArea.Min.Y + 31); |
||||
|
nwSettings.Size = new Vector2i(bestMonitor.WorkArea.Size.X - 2, bestMonitor.WorkArea.Size.Y - 32); |
||||
|
nwSettings.Title = "Totte"; |
||||
|
// FIXME: nwSettings.Icon = ...
|
||||
|
|
||||
|
using (Game game = new Game(gwSettings, nwSettings)) { |
||||
|
game.Run(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,15 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<OutputType>Exe</OutputType> |
||||
|
<TargetFramework>net7.0</TargetFramework> |
||||
|
<ImplicitUsings>enable</ImplicitUsings> |
||||
|
<Nullable>enable</Nullable> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="OpenTK" Version="4.7.7" /> |
||||
|
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.1" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
@ -0,0 +1,25 @@ |
|||||
|
|
||||
|
Microsoft Visual Studio Solution File, Format Version 12.00 |
||||
|
# Visual Studio Version 17 |
||||
|
VisualStudioVersion = 17.6.33815.320 |
||||
|
MinimumVisualStudioVersion = 10.0.40219.1 |
||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Totte", "Totte.csproj", "{DD5955F2-11CB-471F-9271-2B99267B4E5D}" |
||||
|
EndProject |
||||
|
Global |
||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution |
||||
|
Debug|Any CPU = Debug|Any CPU |
||||
|
Release|Any CPU = Release|Any CPU |
||||
|
EndGlobalSection |
||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution |
||||
|
{DD5955F2-11CB-471F-9271-2B99267B4E5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
||||
|
{DD5955F2-11CB-471F-9271-2B99267B4E5D}.Debug|Any CPU.Build.0 = Debug|Any CPU |
||||
|
{DD5955F2-11CB-471F-9271-2B99267B4E5D}.Release|Any CPU.ActiveCfg = Release|Any CPU |
||||
|
{DD5955F2-11CB-471F-9271-2B99267B4E5D}.Release|Any CPU.Build.0 = Release|Any CPU |
||||
|
EndGlobalSection |
||||
|
GlobalSection(SolutionProperties) = preSolution |
||||
|
HideSolutionNode = FALSE |
||||
|
EndGlobalSection |
||||
|
GlobalSection(ExtensibilityGlobals) = postSolution |
||||
|
SolutionGuid = {207645D6-6F33-4FCE-8DC4-4F3C832A8897} |
||||
|
EndGlobalSection |
||||
|
EndGlobal |
Write
Preview
Loading…
Cancel
Save
Reference in new issue