Skip to content

Commit c1bcd03

Browse files
Merge pull request #4 from frederikja163/tutorials-cs-1.4
Tutorials cs 1.4
2 parents 54ca717 + 760cea1 commit c1bcd03

File tree

10 files changed

+422
-0
lines changed

10 files changed

+422
-0
lines changed

Silk.NET.sln

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,11 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Tutorial 1.1 - Hello window
241241
EndProject
242242
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Tutorial 1.2 - Hello quad", "examples\FSharp\OpenGL\Tutorial 1.2 - Hello quad\Tutorial 1.2 - Hello quad.fsproj", "{7BAB0B50-AF2B-4F65-AC6E-C5F952C81665}"
243243
EndProject
244+
<<<<<<< HEAD
244245
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tutorial 1.4 - Textures", "examples\CSharp\OpenGL\Tutorial 1.4 - Textures\Tutorial 1.4 - Textures.csproj", "{B4E96437-3464-4BF9-976C-ADA09916CE0D}"
246+
=======
247+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tutorial 1.5 - Transformations", "examples\CSharp\OpenGL\Tutorial 1.5 - Transformations\Tutorial 1.5 - Transformations.csproj", "{1DF2F3F5-0763-408D-A26F-50AB9447D978}"
248+
>>>>>>> tutorials-cs-1.5
245249
EndProject
246250
Global
247251
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -1417,6 +1421,7 @@ Global
14171421
{7BAB0B50-AF2B-4F65-AC6E-C5F952C81665}.Release|x64.Build.0 = Release|Any CPU
14181422
{7BAB0B50-AF2B-4F65-AC6E-C5F952C81665}.Release|x86.ActiveCfg = Release|Any CPU
14191423
{7BAB0B50-AF2B-4F65-AC6E-C5F952C81665}.Release|x86.Build.0 = Release|Any CPU
1424+
<<<<<<< HEAD
14201425
{B4E96437-3464-4BF9-976C-ADA09916CE0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
14211426
{B4E96437-3464-4BF9-976C-ADA09916CE0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
14221427
{B4E96437-3464-4BF9-976C-ADA09916CE0D}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -1429,6 +1434,20 @@ Global
14291434
{B4E96437-3464-4BF9-976C-ADA09916CE0D}.Release|x64.Build.0 = Release|Any CPU
14301435
{B4E96437-3464-4BF9-976C-ADA09916CE0D}.Release|x86.ActiveCfg = Release|Any CPU
14311436
{B4E96437-3464-4BF9-976C-ADA09916CE0D}.Release|x86.Build.0 = Release|Any CPU
1437+
=======
1438+
{1DF2F3F5-0763-408D-A26F-50AB9447D978}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1439+
{1DF2F3F5-0763-408D-A26F-50AB9447D978}.Debug|Any CPU.Build.0 = Debug|Any CPU
1440+
{1DF2F3F5-0763-408D-A26F-50AB9447D978}.Debug|x64.ActiveCfg = Debug|Any CPU
1441+
{1DF2F3F5-0763-408D-A26F-50AB9447D978}.Debug|x64.Build.0 = Debug|Any CPU
1442+
{1DF2F3F5-0763-408D-A26F-50AB9447D978}.Debug|x86.ActiveCfg = Debug|Any CPU
1443+
{1DF2F3F5-0763-408D-A26F-50AB9447D978}.Debug|x86.Build.0 = Debug|Any CPU
1444+
{1DF2F3F5-0763-408D-A26F-50AB9447D978}.Release|Any CPU.ActiveCfg = Release|Any CPU
1445+
{1DF2F3F5-0763-408D-A26F-50AB9447D978}.Release|Any CPU.Build.0 = Release|Any CPU
1446+
{1DF2F3F5-0763-408D-A26F-50AB9447D978}.Release|x64.ActiveCfg = Release|Any CPU
1447+
{1DF2F3F5-0763-408D-A26F-50AB9447D978}.Release|x64.Build.0 = Release|Any CPU
1448+
{1DF2F3F5-0763-408D-A26F-50AB9447D978}.Release|x86.ActiveCfg = Release|Any CPU
1449+
{1DF2F3F5-0763-408D-A26F-50AB9447D978}.Release|x86.Build.0 = Release|Any CPU
1450+
>>>>>>> tutorials-cs-1.5
14321451
EndGlobalSection
14331452
GlobalSection(SolutionProperties) = preSolution
14341453
HideSolutionNode = FALSE
@@ -1537,7 +1556,11 @@ Global
15371556
{65787DEC-4B92-4562-AB99-E0F75586BF56} = {3427E7AB-4ADC-4DEE-A1D5-71CE7DA0CAC1}
15381557
{08A71FB9-D40B-44C5-8D62-212530566280} = {4FF95A03-14E7-4982-9BAF-A007ED1EDF46}
15391558
{7BAB0B50-AF2B-4F65-AC6E-C5F952C81665} = {4FF95A03-14E7-4982-9BAF-A007ED1EDF46}
1559+
<<<<<<< HEAD
15401560
{B4E96437-3464-4BF9-976C-ADA09916CE0D} = {3427E7AB-4ADC-4DEE-A1D5-71CE7DA0CAC1}
1561+
=======
1562+
{1DF2F3F5-0763-408D-A26F-50AB9447D978} = {3427E7AB-4ADC-4DEE-A1D5-71CE7DA0CAC1}
1563+
>>>>>>> tutorials-cs-1.5
15411564
EndGlobalSection
15421565
GlobalSection(ExtensibilityGlobals) = postSolution
15431566
SolutionGuid = {A8E73A98-30BA-4585-AF7E-F3315A359235}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Silk.NET.OpenGL;
2+
using System;
3+
4+
namespace Tutorial
5+
{
6+
public class BufferObject<TDataType> : IDisposable
7+
where TDataType : unmanaged
8+
{
9+
private uint _handle;
10+
private GLEnum _bufferType;
11+
private GL _gl;
12+
13+
public unsafe BufferObject(GL gl, TDataType[] data, GLEnum bufferType)
14+
{
15+
_gl = gl;
16+
_bufferType = bufferType;
17+
18+
_handle = _gl.GenBuffer();
19+
Bind();
20+
fixed (void* d = data)
21+
{
22+
_gl.BufferData(bufferType, (UIntPtr)(data.Length * sizeof(TDataType)), d, GLEnum.StaticDraw);
23+
}
24+
}
25+
26+
public void Bind()
27+
{
28+
_gl.BindBuffer(_bufferType, _handle);
29+
}
30+
31+
public void Dispose()
32+
{
33+
_gl.DeleteBuffer(_handle);
34+
}
35+
}
36+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using Silk.NET.Input;
2+
using Silk.NET.Input.Common;
3+
using Silk.NET.OpenGL;
4+
using Silk.NET.Windowing;
5+
using Silk.NET.Windowing.Common;
6+
using System;
7+
using System.Drawing;
8+
using System.Numerics;
9+
10+
namespace Tutorial
11+
{
12+
class Program
13+
{
14+
private static IWindow window;
15+
private static GL Gl;
16+
17+
private static BufferObject<float> Vbo;
18+
private static BufferObject<uint> Ebo;
19+
private static VertexArrayObject<float, uint> Vao;
20+
private static Texture Texture;
21+
private static Shader Shader;
22+
//Creating matrix transformations
23+
private static Matrix4x4[] Matrices = new Matrix4x4[4];
24+
25+
private static readonly float[] Vertices =
26+
{
27+
//X Y Z U V
28+
0.5f, 0.5f, 0.0f, 1f, 1f,
29+
0.5f, -0.5f, 0.0f, 1f, 0f,
30+
-0.5f, -0.5f, 0.0f, 0f, 0f,
31+
-0.5f, 0.5f, 0.5f, 0f, 1f
32+
};
33+
34+
private static readonly uint[] Indices =
35+
{
36+
0, 1, 3,
37+
1, 2, 3
38+
};
39+
40+
41+
private static void Main(string[] args)
42+
{
43+
var options = WindowOptions.Default;
44+
options.Size = new Size(800, 600);
45+
options.Title = "LearnOpenGL with Silk.NET";
46+
window = Window.Create(options);
47+
48+
window.Load += OnLoad;
49+
window.Render += OnRender;
50+
window.Closing += OnClose;
51+
52+
window.Run();
53+
}
54+
55+
56+
private unsafe static void OnLoad()
57+
{
58+
IInputContext input = window.GetInput();
59+
for (int i = 0; i < input.Keyboards.Count; i++)
60+
{
61+
input.Keyboards[i].KeyDown += KeyDown;
62+
}
63+
64+
Gl = GL.GetApi();
65+
66+
Ebo = new BufferObject<uint>(Gl, Indices, GLEnum.ElementArrayBuffer);
67+
Vbo = new BufferObject<float>(Gl, Vertices, GLEnum.ArrayBuffer);
68+
Vao = new VertexArrayObject<float, uint>(Gl, Vbo, Ebo);
69+
70+
Vao.VertexAttributePointer(0, 3, GLEnum.Float, 5, 0);
71+
Vao.VertexAttributePointer(1, 2, GLEnum.Float, 5, 3);
72+
73+
Shader = new Shader(Gl, "shader.vert", "shader.frag");
74+
75+
Texture = new Texture(Gl, "silk.png");
76+
77+
//Translation.
78+
Matrices[0] = Matrix4x4.Identity * Matrix4x4.CreateTranslation(0.5f, 0.5f, 0f);
79+
//Rotation.
80+
Matrices[1] = Matrix4x4.Identity * Matrix4x4.CreateRotationZ(1);
81+
//Scaling.
82+
Matrices[2] = Matrix4x4.Identity * Matrix4x4.CreateScale(0.5f);
83+
//Mixed transformation.
84+
Matrices[3] = Matrix4x4.Identity * Matrix4x4.CreateScale(0.5f) * Matrix4x4.CreateRotationZ(2) * Matrix4x4.CreateTranslation(-0.5f, 0.5f, 0f);
85+
}
86+
87+
private static void OnRender(double obj)
88+
{
89+
Gl.Clear((uint)ClearBufferMask.ColorBufferBit);
90+
91+
Vao.Bind();
92+
Texture.Bind();
93+
Shader.Use();
94+
Shader.SetUniform("uTexture0", 0);
95+
96+
for (int i = 0; i < Matrices.Length; i++)
97+
{
98+
//Using the transformations.
99+
Shader.SetUniform("uModel", Matrices[i]);
100+
101+
Gl.DrawElements(GLEnum.Triangles, (uint)Indices.Length, GLEnum.UnsignedInt, 0);
102+
}
103+
}
104+
105+
private static void OnClose()
106+
{
107+
Vbo.Dispose();
108+
Ebo.Dispose();
109+
Vao.Dispose();
110+
Shader.Dispose();
111+
Texture.Dispose();
112+
}
113+
114+
private static void KeyDown(IKeyboard arg1, Key arg2, int arg3)
115+
{
116+
if (arg2 == Key.Escape)
117+
{
118+
window.Close();
119+
}
120+
}
121+
}
122+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using System.IO;
3+
using System.Numerics;
4+
using Silk.NET.OpenGL;
5+
6+
namespace Tutorial
7+
{
8+
public class Shader : IDisposable
9+
{
10+
private uint _handle;
11+
private GL _gl;
12+
13+
public Shader(GL gl, string vertexPath, string fragmentPath)
14+
{
15+
_gl = gl;
16+
17+
uint vertex = LoadShader(ShaderType.VertexShader, vertexPath);
18+
uint fragment = LoadShader(ShaderType.FragmentShader, fragmentPath);
19+
_handle = _gl.CreateProgram();
20+
_gl.AttachShader(_handle, vertex);
21+
_gl.AttachShader(_handle, fragment);
22+
_gl.LinkProgram(_handle);
23+
string infoLog = _gl.GetProgramInfoLog(_handle);
24+
if (!string.IsNullOrWhiteSpace(infoLog))
25+
{
26+
throw new Exception($"Program failed to link with error: {infoLog}");
27+
}
28+
_gl.DetachShader(_handle, vertex);
29+
_gl.DetachShader(_handle, fragment);
30+
}
31+
32+
public void Use()
33+
{
34+
_gl.UseProgram(_handle);
35+
}
36+
37+
public void SetUniform(string name, int value)
38+
{
39+
int location = _gl.GetUniformLocation(_handle, name);
40+
if (location == -1)
41+
{
42+
throw new Exception($"{name} uniform not found on shader.");
43+
}
44+
Use();
45+
_gl.Uniform1(location, value);
46+
}
47+
48+
public unsafe void SetUniform(string name, Matrix4x4 value)
49+
{
50+
int location = _gl.GetUniformLocation(_handle, name);
51+
if (location == -1)
52+
{
53+
throw new Exception($"{name} uniform not found on shader.");
54+
}
55+
Use();
56+
_gl.UniformMatrix4(location, 1, false, (float*)&value);
57+
}
58+
59+
public void SetUniform(string name, float value)
60+
{
61+
int location = _gl.GetUniformLocation(_handle, name);
62+
if (location == -1)
63+
{
64+
throw new Exception($"{name} uniform not found on shader.");
65+
}
66+
Use();
67+
_gl.Uniform1(location, value);
68+
}
69+
70+
public void Dispose()
71+
{
72+
_gl.DeleteProgram(_handle);
73+
}
74+
75+
private uint LoadShader(ShaderType type, string path)
76+
{
77+
string src = File.ReadAllText(path);
78+
uint handle = _gl.CreateShader(type);
79+
_gl.ShaderSource(handle, src);
80+
_gl.CompileShader(handle);
81+
string infoLog = _gl.GetShaderInfoLog(handle);
82+
if (!string.IsNullOrWhiteSpace(infoLog))
83+
{
84+
throw new Exception($"Error compiling shader of type {type}, failed with error {infoLog}");
85+
}
86+
87+
return handle;
88+
}
89+
}
90+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 330 core
2+
in vec2 fUv;
3+
4+
uniform sampler2D uTexture0;
5+
6+
out vec4 FragColor;
7+
8+
void main()
9+
{
10+
FragColor = texture(uTexture0, fUv);
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#version 330 core
2+
layout (location = 0) in vec3 vPos;
3+
layout (location = 1) in vec2 vUv;
4+
5+
uniform mat4 uModel;
6+
7+
out vec2 fUv;
8+
9+
void main()
10+
{
11+
gl_Position = uModel * vec4(vPos, 1.0);
12+
fUv = vUv;
13+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using Silk.NET.OpenGL;
2+
using System;
3+
using SixLabors.ImageSharp;
4+
using SixLabors.ImageSharp.PixelFormats;
5+
using System.Runtime.InteropServices;
6+
using SixLabors.ImageSharp.Advanced;
7+
8+
namespace Tutorial
9+
{
10+
public class Texture : IDisposable
11+
{
12+
private uint _handle;
13+
private GL _gl;
14+
15+
public unsafe Texture(GL gl, string path)
16+
{
17+
Image<Rgba32> img = (Image<Rgba32>)Image.Load(path);
18+
19+
fixed (void* data = &MemoryMarshal.GetReference(img.GetPixelSpan()))
20+
{
21+
Load(gl, data, (uint)img.Width, (uint)img.Height);
22+
}
23+
24+
img.Dispose();
25+
}
26+
27+
public unsafe Texture(GL gl, byte[] data, uint width, uint height)
28+
{
29+
fixed (void* d = &data[0])
30+
{
31+
Load(gl, d, width, height);
32+
}
33+
}
34+
35+
private unsafe void Load(GL gl, void* data, uint width, uint height)
36+
{
37+
_gl = gl;
38+
39+
_handle = _gl.GenTexture();
40+
Bind();
41+
42+
_gl.TexImage2D(TextureTarget.Texture2D, 0, (int)GLEnum.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, data);
43+
_gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)GLEnum.ClampToEdge);
44+
_gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)GLEnum.ClampToEdge);
45+
_gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)GLEnum.Linear);
46+
_gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)GLEnum.Linear);
47+
_gl.GenerateMipmap(TextureTarget.Texture2D);
48+
}
49+
50+
public void Bind(TextureUnit textureSlot = TextureUnit.Texture0)
51+
{
52+
_gl.ActiveTexture(textureSlot);
53+
_gl.BindTexture(TextureTarget.Texture2D, _handle);
54+
}
55+
56+
public void Dispose()
57+
{
58+
_gl.DeleteTexture(_handle);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)