-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageTexture.cs
More file actions
78 lines (77 loc) · 2.26 KB
/
ImageTexture.cs
File metadata and controls
78 lines (77 loc) · 2.26 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
using OpenTK.Graphics.OpenGL4;
using StbImageSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JLGraphics
{
public class ImageTexture : Texture
{
string path;
public static ImageTexture LoadTextureFromPath(string path, bool bilinearFilter = true, ColorComponents colorComponents = ColorComponents.RedGreenBlueAlpha)
{
var image = ImageResult.FromStream(File.OpenRead(path), colorComponents);
var m = new ImageTexture(image);
m.path = path;
if (bilinearFilter)
{
m.textureMinFilter = TextureMinFilter.LinearMipmapNearest;
m.textureMagFilter = TextureMagFilter.Linear;
}
else
{
m.textureMinFilter = TextureMinFilter.NearestMipmapNearest;
m.textureMagFilter = TextureMagFilter.Nearest;
}
m.textureWrapMode = TextureWrapMode.Repeat;
return m;
}
public override string Name => path;
public ImageResult image { get; }
public ImageTexture(ImageResult image)
{
textureTarget = TextureTarget.Texture2D;
this.image = image;
}
public override int Width
{
get => image.Width;
set
{
if (image != null)
{
image.Width = value;
}
base.Width = value;
}
}
public override int Height
{
get => image.Height;
set
{
if (image != null)
{
image.Width = value;
}
base.Height = value;
}
}
protected override (IntPtr, PixelType, PixelFormat) LoadPixelData()
{
if (image == null)
{
return (IntPtr.Zero, PixelType.UnsignedByte, PixelFormat.Red);
}
unsafe
{
fixed (byte* p = image.Data)
{
return ((IntPtr)p, PixelType.UnsignedByte, PixelFormat.Rgba);
}
}
}
}
}