Repository navigation
Expand file tree
/
Copy pathTexture.java
More file actions
155 lines (106 loc) · 4.19 KB
/
Copy pathTexture.java
File metadata and controls
155 lines (106 loc) · 4.19 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import org.newdawn.slick.opengl.TextureLoader;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import static org.lwjgl.opengl.GL11.*;
public class Texture {
// Note, loading textures with 2^n x 2^m dimensions is optimal
private int id;
private float width;
private float height;
public Texture(String filename){
this(loadTexture2(filename)); // Uses below constructor
float[] dims = getDims(filename);
this.width = dims[0];
this.height = dims[1];
}
public Texture(int id){
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void bindTexture(){
glBindTexture(GL_TEXTURE_2D, this.id);
}
private static int loadTexture(String filename){
String[] splitArray = filename.split("\\.");
String fileExt = splitArray[splitArray.length - 1];
try {
int id = TextureLoader.getTexture(fileExt, new FileInputStream(new File(".\\res\\textures\\" + filename)), GL_NEAREST).getTextureID();
return id;
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
return 0;
}
// Alternate texture loading method that doesn't use SlickUtil as there is a texture clipping issue with it.
private static int loadTexture2(String fileName)
{
try
{
BufferedImage image = ImageIO.read(new File("./res/textures/" + fileName));
boolean hasAlpha = image.getColorModel().hasAlpha();
int[] pixels = image.getRGB(0, 0, image.getWidth(),
image.getHeight(), null, 0, image.getWidth());
ByteBuffer buffer = Util.createByteBuffer(image.getWidth() * image.getHeight() * 4);
for (int y = 0; y < image.getHeight(); y++)
{
for (int x = 0; x < image.getWidth(); x++)
{
int pixel = pixels[y * image.getWidth() + x];
buffer.put((byte) ((pixel >> 16) & 0xFF));
buffer.put((byte) ((pixel >> 8) & 0xFF));
buffer.put((byte) ((pixel) & 0xFF));
if (hasAlpha)
buffer.put((byte) ((pixel >> 24) & 0xFF));
else
buffer.put((byte) (0xFF));
}
}
buffer.flip();
int texture = glGenTextures();
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
return texture;
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
return 0;
}
private static float[] getDims(String filename){
float[] res = new float[2];
String[] splitArray = filename.split("\\.");
String fileExt = splitArray[splitArray.length - 1];
try {
float width = TextureLoader.getTexture(fileExt, new FileInputStream(new File(".\\res\\textures\\" + filename)), GL_NEAREST).getImageWidth();
float height = TextureLoader.getTexture(fileExt, new FileInputStream(new File(".\\res\\textures\\" + filename)), GL_NEAREST).getImageHeight();
res[0] = width;
res[1] = height;
return res;
} catch (Exception e){
e.printStackTrace();
System.exit(1);
}
return res;
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
}