-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.java
More file actions
71 lines (60 loc) · 2.12 KB
/
Texture.java
File metadata and controls
71 lines (60 loc) · 2.12 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
package renderer;
import org.lwjgl.BufferUtils;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.stb.STBImage.*;
public class Texture
{
private String filepath;
private int textureID;
public Texture(String filepath)
{
this.filepath = filepath;
// Generate texture on gpu
textureID = glGenTextures();
glBindTexture(GL_TEXTURE_2D, textureID);
// Set the texture parameters
// Repeat the image in both directions
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// When stretching the image, pixelate
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// When shrinking the image, pixelate
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
IntBuffer width = BufferUtils.createIntBuffer(1);
IntBuffer height = BufferUtils.createIntBuffer(1);
IntBuffer channels = BufferUtils.createIntBuffer(1);
stbi_set_flip_vertically_on_load(true);
ByteBuffer image = stbi_load(filepath, width, height, channels, 0);
if (image != null)
{
if (channels.get(0) == 3)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width.get(0), height.get(0), 0, GL_RGB, GL_UNSIGNED_BYTE, image);
}
else if (channels.get(0) == 4)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width.get(0), height.get(0), 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
}
else
{
assert false : "Error: ('TEXTURE') Unknown number of channels '" + channels.get(0) + "'";
}
}
else
{
assert false : "Error: ('TEXTURE') Could not load image '" + filepath + "'";
}
stbi_image_free(image);
}
public void bind()
{
glBindTexture(GL_TEXTURE_2D, textureID);
}
public void unbind()
{
glBindTexture(GL_TEXTURE_2D, 0);
}
}