Repository navigation
Expand file tree
/
Copy pathGLTextureWindow.java
More file actions
395 lines (336 loc) · 9.62 KB
/
Copy pathGLTextureWindow.java
File metadata and controls
395 lines (336 loc) · 9.62 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import processing.core.PApplet;
import processing.opengl.PGraphicsOpenGL;
import processing.opengl.Texture;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilitiesChooser;
import javax.media.opengl.GLCapabilitiesImmutable;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
/**
* An undecorated lightweight AWT window to show just a single texture created
* in the main OpenGL context of the parent PApplet. The renderer in parent must
* be of type OPENGL2.
*/
public class GLTextureWindow {
protected PApplet parent;
protected String name;
protected boolean hasBorder;
protected boolean resizable;
protected MyGLRenderer renderer;
protected GLCanvas canvas;
protected GLContext context;
protected GLCapabilitiesImmutable caps;
protected Frame frame;
protected Texture outTex;
protected PGraphicsOpenGL pgl;
protected boolean visible;
protected int x0, y0;
protected int width, height;
protected boolean override;
protected boolean initialized;
/**
* Creates a visible instance of GLTextureWindow with the specified size (w, h) and
* position (x, y), which will show texture tex in it.
*
* @param parent
* PApplet
* @param tex
* PTexture
* @param x
* int
* @param y
* int
* @param w
* int
* @param h
* int
*/
public GLTextureWindow(PApplet parent, int x, int y, int w, int h) {
this(parent, x, y, w, h, true);
}
public GLTextureWindow(PApplet parent, int x, int y, int w, int h, boolean v) {
this(parent, "texture window", x, y, w, h, v, false, false);
}
public GLTextureWindow(PApplet parent, int x, int y, int w, int h, boolean v, boolean b) {
this(parent, "texture window", x, y, w, h, v, b, false);
}
/**
* Creates an instance of GLTextureWindow with the specified size (w, h) and
* position (x, y), which will show texture tex in it.
*
* @param parent
* PApplet
* @param tex
* PTexture
* @param name
* String
* @param x
* int
* @param y
* int
* @param w
* int
* @param h
* int
* @param v
* boolean
* @param d
* boolean
* @param r
* boolean
*/
public GLTextureWindow(PApplet parent, String windowTitle, int x, int y, int w, int h,
boolean v, boolean b, boolean r) {
this.parent = parent;
pgl = (PGraphicsOpenGL) parent.g;
name = windowTitle;
x0 = x;
y0 = y;
width = w;
height = h;
visible = v;
hasBorder = b;
resizable = r;
initialized = false;
override = false;
outTex = null;
//pgl.pgl.addWindow(this);
}
public boolean init(GL2 gl) {
if (!initialized) {
// The GL canvas of the AWT window must have exactly the same GL capabilities
// as the main renderer for sharing with the main context to be possible.
context = gl.getContext();
//caps = PJOGL.pgl.gl.getGL().
initImpl(x0, y0, width, height);
//Image image = Toolkit.getDefaultToolkit().createImage(PApplet.ICON_IMAGE);
//frame.setIconImage(image);
frame.setVisible(visible);
frame.addKeyListener(this.parent);
initialized = true;
return(true);
} else if (context != gl.getContext()) {
// The window has been initialized, but the main context in the
// renderer has changed. The frame, canvas, and renderer are
// recreated.
// Getting current context and capabilities of the main renderer.
context = gl.getContext();
//caps = PJOGL.pgl.gl.capabilities;
// Getting current parameters of the frame.
boolean v = frame.isVisible();
int x = frame.getX();
int y = frame.getY();
int w = frame.getWidth();
int h = frame.getHeight();
frame.setVisible(false);
initImpl(x, y, w, h);
frame.setVisible(v);
return(false);
}
return(false);
}
@SuppressWarnings("deprecation")
protected void initImpl(int x, int y, int w, int h) {
frame = new Frame(name);
frame.setSize(w, h);
frame.setLocation(x, y);
if (!hasBorder) {
frame.setUndecorated(true);
} else {
if (!resizable) {
frame.setResizable(false);
}
}
canvas = new GLCanvas(null,(GLCapabilitiesChooser) null, context,(GraphicsDevice) null);
renderer = new MyGLRenderer();
canvas.addGLEventListener(renderer);
frame.add(canvas);
}
/**
* Sets the texture reference
* @param tex
*/
public void setTexture(Texture tex) {
outTex = tex;
}
/**
* Sets the override property to the desired value.
* @param override
* boolean
*/
public void setOverride(boolean override) {
this.override = override;
}
/**
* Shows the window.
*/
public void show() {
frame.setVisible(true);
}
/**
* Hides the window.
*/
public void hide() {
frame.setVisible(false);
}
/**
* Returns whether the window is visible or not.
* @return boolean
*/
public boolean isVisible() {
return frame.isVisible();
}
/**
* Sets the texture tint color.
*
* @param int color
*/
public void tint(int color) {
int ir, ig, ib, ia;
ia = (color >> 24) & 0xff;
ir = (color >> 16) & 0xff;
ig = (color >> 8) & 0xff;
ib = color & 0xff;
renderer.a = ia / 255.0f;
renderer.r = ir / 255.0f;
renderer.g = ig / 255.0f;
renderer.b = ib / 255.0f;
}
/**
* Returns true or false depending on whether or not the internal renderer has
* been initialized.
* @return boolean
*/
public boolean ready() {
return initialized && frame != null && renderer != null && renderer.initalized;
}
/**
* Draws the window, if the renderer has been initialized.
*/
public void render() {
if (ready()) {
renderer.started = true;
canvas.display();
}
}
/**
* Sets the window location.
*
* @param x
* int
* @param y
* int
*/
public void setLocation(int x, int y) {
frame.setLocation(x, y);
}
protected class MyGLRenderer implements GLEventListener {
public MyGLRenderer() {
super();
initalized = false;
started = false;
r = g = b = a = 1.0f;
}
public void init(GLAutoDrawable drawable) {
gl = drawable.getGL().getGL2();
context = drawable.getContext();
gl.glClearColor(0, 0, 0, 0);
initalized = true;
}
public void display(GLAutoDrawable drawable) {
if (!initalized || !started || (outTex == null))
return;
int w = drawable.getWidth();
int h = drawable.getHeight();
gl = drawable.getGL().getGL2();
context = drawable.getContext();
detainContext();
// Setting orthographics view to display the texture.
gl.glViewport(0, 0, w, h);
gl.getGL2().glMatrixMode(GL2.GL_PROJECTION);
gl.getGL2ES1().glLoadIdentity();
gl.getGL2ES1().glOrtho(0.0, w, 0.0, h, -100.0, +100.0);
gl.getGL2().glMatrixMode(GL2.GL_MODELVIEW);
gl.getGL2ES1().glLoadIdentity();
float uscale = outTex.maxTexcoordU();
float vscale = outTex.maxTexcoordV();
float cx = 0.0f;
float sx = +1.0f;
if (outTex.invertedX()) {
cx = 1.0f;
sx = -1.0f;
}
float cy = 0.0f;
float sy = +1.0f;
if (outTex.invertedY()) {
cy = 1.0f;
sy = -1.0f;
}
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glEnable(outTex.glTarget);
gl.glActiveTexture(GL2.GL_TEXTURE0);
gl.glBindTexture(outTex.glTarget, outTex.glName);
gl.getGL2ES1().glColor4f(r, g, b, a);
gl.getGL2().glBegin(GL2.GL_QUADS);
gl.getGL2().glTexCoord2f((cx + sx * 0.0f) * uscale, (cy + sy * 1.0f) * vscale);
gl.getGL2().glVertex2f(0.0f, 0.0f);
gl.getGL2().glTexCoord2f((cx + sx * 1.0f) * uscale, (cy + sy * 1.0f) * vscale);
gl.getGL2().glVertex2f(w, 0.0f);
gl.getGL2().glTexCoord2f((cx + sx * 1.0f) * uscale, (cy + sy * 0.0f) * vscale);
gl.getGL2().glVertex2f(w, h);
gl.getGL2().glTexCoord2f((cx + sx * 0.0f) * uscale, (cy + sy * 0.0f) * vscale);
gl.getGL2().glVertex2f(0.0f, h);
gl.getGL2().glEnd();
gl.glBindTexture(outTex.glTarget, 0);
gl.glFlush();
releaseContext();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
boolean deviceChanged) {
}
protected void detainContext() {
try {
while (context.makeCurrent() == GLContext.CONTEXT_NOT_CURRENT) {
Thread.sleep(10);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
protected void releaseContext() {
context.release();
}
float r, g, b, a;
GL2 gl;
GLContext context;
boolean initalized;
boolean started;
public void dispose(GLAutoDrawable arg0) {
// TODO Auto-generated method stub
}
}
/**
* Returns the value of the override variable, used to skip automatic
* rendering in the GLGraphics renderer.
*
* @return boolean
*/
public boolean getOverride() {
return override;
}
/**
* Returns the value of the initialized variable.
*
* @return boolean
*/
public boolean isInitialized() {
return initialized;
}
}