-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTextureImage.cs
More file actions
107 lines (89 loc) · 3.68 KB
/
TextureImage.cs
File metadata and controls
107 lines (89 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using Silk.NET.OpenGL;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System;
namespace OpenGL_Demo
{
public class TextureImage : Texture
{
static Configuration customConfig;
static TextureImage()
{
customConfig = Configuration.Default.Clone();
customConfig.PreferContiguousImageBuffers = true;
}
public TextureImage(string path)
{
_handle = Program.GL.GenTexture();
Bind();
using (var img = Image.Load<Rgba32>(customConfig, path))
{
img.Mutate(x => x.Flip(FlipMode.Vertical));
if (!img.DangerousTryGetSinglePixelMemory(out var memory))
{
throw new Exception("This can only happen with multi-GB images or when PreferContiguousImageBuffers is not set to true.");
}
Load(memory.Span, (uint)img.Width, (uint)img.Height);
}
Setup();
}
public TextureImage(byte[] buffer)
{
_handle = Program.GL.GenTexture();
Bind();
using (var img = Image.Load<Rgba32>(customConfig, buffer))
{
img.Mutate(x => x.Flip(FlipMode.Vertical));
if (!img.DangerousTryGetSinglePixelMemory(out var memory))
{
throw new Exception("This can only happen with multi-GB images or when PreferContiguousImageBuffers is not set to true.");
}
Load(memory.Span, (uint)img.Width, (uint)img.Height);
}
Setup();
}
public TextureImage(Image<Rgba32> img)
{
_handle = Program.GL.GenTexture();
Bind();
if (!img.DangerousTryGetSinglePixelMemory(out var memory))
{
throw new Exception("This can only happen with multi-GB images or when PreferContiguousImageBuffers is not set to true.");
}
Load(memory.Span, (uint)img.Width, (uint)img.Height);
Setup();
}
public TextureImage(Span<byte> data, uint width, uint height)
{
_handle = Program.GL.GenTexture();
Bind();
Load(data, width, height);
Setup();
}
unsafe void Load(Span<byte> data, uint width, uint height)
{
fixed (void* d = &data[0])
{
Program.GL.TexImage2D(TextureTarget.Texture2D, 0, InternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, d);
}
}
unsafe void Load(Span<Rgba32> data, uint width, uint height)
{
fixed (void* d = &data[0])
{
Program.GL.TexImage2D(TextureTarget.Texture2D, 0, InternalFormat.Rgba, width, height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, d);
}
}
void Setup()
{
Program.GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)GLEnum.Repeat);
Program.GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)GLEnum.Repeat);
Program.GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)GLEnum.LinearMipmapLinear);
Program.GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)GLEnum.Linear);
Program.GL.TexParameter(TextureTarget.Texture2D, GLEnum.TextureMaxAnisotropy, maxAnisotropy);
Program.GL.GenerateMipmap(TextureTarget.Texture2D);
Program.GL.BindTexture(TextureTarget.Texture2D, 0);
}
}
}