/
Texture.cs
125 lines (104 loc) · 4.94 KB
/
Texture.cs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace Device22
{
public class Texture
{
int filterFar;
int filterNear;
Bitmap bitmap;
int ID = -1;
float angle = 0.0f;
public Texture()
{
}
public static int loadFromFile(string url) { return loadFromFile(url, (int)TextureMinFilter.Linear, (int)TextureMagFilter.Linear); }
public static int loadFromFile(string url, int filter_far, int filter_near)
{
try
{
Texture tex = new Texture();
tex.bitmap = Texture.loadBMP(url);
if (tex.bitmap == null) { return -1; }
GL.GenTextures(1, out tex.ID);
tex.Bind();
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, filter_far);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, filter_near);
GL.GetTexParameter(TextureTarget.Texture2D, GetTextureParameter.TextureMagFilter, out tex.filterFar);
GL.GetTexParameter(TextureTarget.Texture2D, GetTextureParameter.TextureMinFilter, out tex.filterNear);
Rectangle rect = new Rectangle(0, 0, tex.bitmap.Width, tex.bitmap.Height);
System.Drawing.Imaging.BitmapData bitmapdata = tex.bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, tex.bitmap.Width, tex.bitmap.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, bitmapdata.Scan0);
tex.bitmap.Dispose();
return tex.ID;
}
catch(ArgumentException)
{
Debug.Trace("Class Texture: Error creating Bitmap: " + url);
return -1;
}
}
private static Bitmap loadBMP(string fileName)
{
if (fileName == null || fileName == string.Empty)
{ // Make Sure A Filename Was Given
Debug.Trace("Class Texture: No filename was provided");
return null; // If Not Return Null
}
string fileName1 = string.Format("Data{0}{1}", // Look For Data\Filename
Path.DirectorySeparatorChar, fileName);
string fileName2 = string.Format("{0}{1}{0}{1}Data{1}{2}", // Look For ..\..\Data\Filename
"..", Path.DirectorySeparatorChar, fileName);
// Make Sure The File Exists In One Of The Usual Directories
if (!File.Exists(fileName) && !File.Exists(fileName1) && !File.Exists(fileName2))
{
Debug.Trace("Class Texture: Bitmap: " + fileName + " does not exist");
return null; // If Not Return Null
}
if (File.Exists(fileName))
{ // Does The File Exist Here?
return new Bitmap(fileName); // Load The Bitmap
}
else if (File.Exists(fileName1))
{ // Does The File Exist Here?
return new Bitmap(fileName1); // Load The Bitmap
}
else if (File.Exists(fileName2))
{ // Does The File Exist Here?
return new Bitmap(fileName2); // Load The Bitmap
}
Debug.Trace("Class Texture: Error loading Bitmap: " + fileName);
return null; // If Load Failed Return Null
}
public static void Bind(int ID)
{
if (ID == -1)
return;
GL.MatrixMode(MatrixMode.Texture);
GL.LoadIdentity();
GL.BindTexture(TextureTarget.Texture2D, ID);
GL.MatrixMode(MatrixMode.Modelview);
}
public void Bind()
{
if (this.ID == -1)
return;
GL.MatrixMode(MatrixMode.Texture);
GL.LoadIdentity();
GL.Translate(0.5f, 0.5f, 0.0f);
GL.Rotate(this.angle, 0.0f, 0.0f, 1.0f);
GL.Translate(-0.5f, -0.5f, 0.0f);
GL.BindTexture(TextureTarget.Texture2D, this.ID);
GL.MatrixMode(MatrixMode.Modelview);
}
public void SetRotation(float rotAngle) { this.angle = rotAngle; }
public float GetRotationAngle() { return this.angle; }
public int GetID() { return this.ID; }
}
}