forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.java
More file actions
153 lines (134 loc) · 3.6 KB
/
Copy pathTexture.java
File metadata and controls
153 lines (134 loc) · 3.6 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
package javaforce.gl;
import java.io.*;
import javaforce.*;
import javaforce.awt.*;
import javaforce.ui.*;
import static javaforce.gl.GL.*;
/** Stores a 2D Texture (image).
* Textures are usually loaded after a new model is loaded.
* All model's share the same set of textures. */
public class Texture {
public Image image;
public int refcnt;
public int tid;
public boolean loaded;
public int idx; //GL texture unit
public String name;
private static boolean mipmaps = false;
public Texture(int idx) {
refcnt = 0;
tid = -1;
image = new Image();
loaded = false;
this.idx = idx;
}
public Texture(int idx, int width, int height) {
refcnt = 0;
tid = -1;
image = new Image(width, height);
loaded = false;
this.idx = idx;
}
public Image getImage() {
return image;
}
public int getWidth() {
return image.getWidth();
}
public int getHeight() {
return image.getHeight();
}
public void set(int[] pixels, int x, int y) {
image.setSize(x,y);
image.putPixels(pixels, 0, 0, x, y, 0);
}
public void setImage(Image img) {
image = img;
}
public boolean loadPNG(String filename) {
JFLog.log("Loading Texture:" + filename.toString());
try {
return loadPNG(new FileInputStream(filename));
} catch (FileNotFoundException e) {
JFLog.log("File not found:" + filename);
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean loadPNG(InputStream is) {
if (!image.loadPNG(is)) {
return false;
}
return true;
}
public boolean loadJPG(String filename) {
JFLog.log("Loading Texture:" + filename.toString());
try {
return loadJPG(new FileInputStream(filename));
} catch (FileNotFoundException e) {
JFLog.log("File not found:" + filename);
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean loadJPG(InputStream is) {
if (!image.loadJPG(is)) {
return false;
}
return true;
}
public boolean load() {
if (tid == -1) {
int[] id = new int[1];
id[0] = -1;
glGenTextures(1, id);
if (id[0] == -1) {
JFLog.log("glGenTextures failed:Error=0x" + Integer.toString(glGetError(), 16));
return false;
}
tid = id[0];
if (debug) {
JFLog.log("GLTexture:id=" + tid);
}
}
if (loaded) {
return true;
}
glActiveTexture(GL_TEXTURE0 + idx);
glBindTexture(GL_TEXTURE_2D, tid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
if (mipmaps) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
if (debug) {
JFLog.log("GLTexture.load:" + image.getWidth() + "x" + image.getHeight());
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getWidth(), image.getHeight(), 0, GL_BGRA
, GL_UNSIGNED_BYTE, image.getBuffer());
loaded = true;
return true;
}
public void unload() {
if (tid == -1) return;
int[] id = new int[1];
id[0] = tid;
glDeleteTextures(1, id);
tid = -1;
}
public void bind() {
if (debug) {
JFLog.log("GLTexture.bind:" + tid + "," + idx);
}
glActiveTexture(GL_TEXTURE0 + idx);
glBindTexture(GL_TEXTURE_2D, tid);
}
}