JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TextureRenderer.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright (c) 2010 JogAmp Community. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * - Redistribution of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistribution in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any kind. ALL
21 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that this software is not designed or intended for use
34 * in the design, construction, operation or maintenance of any nuclear
35 * facility.
36 *
37 * Sun gratefully acknowledges that this software was originally authored
38 * and developed by Kenneth Bradley Russell and Christopher John Kline.
39 */
40
41package com.jogamp.opengl.util.awt;
42
43import java.awt.Color;
44import java.awt.Dimension;
45import java.awt.Graphics2D;
46import java.awt.Image;
47import java.awt.Rectangle;
48import java.awt.image.*;
49
50import com.jogamp.opengl.*;
51import com.jogamp.opengl.fixedfunc.GLLightingFunc;
52import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
53import com.jogamp.opengl.glu.gl2.*;
54
55import com.jogamp.opengl.util.texture.*;
56import com.jogamp.opengl.util.texture.awt.*;
57
58/** Provides the ability to render into an OpenGL {@link
59 com.jogamp.opengl.util.texture.Texture Texture} using the Java 2D
60 APIs. This renderer class uses an internal Java 2D image (of
61 unspecified type) for its backing store and flushes portions of
62 that image to an OpenGL texture on demand. The resulting OpenGL
63 texture can then be mapped on to a polygon for display. */
64
65public class TextureRenderer {
66 // For now, we supply only a BufferedImage back-end for this
67 // renderer. In theory we could use the Java 2D/JOGL bridge to fully
68 // accelerate the rendering paths, but there are restrictions on
69 // what work can be done where; for example, Graphics2D-related work
70 // must not be done on the Queue Flusher Thread, but JOGL's
71 // OpenGL-related work must be. This implies that the user's code
72 // would need to be split up into multiple callbacks run from the
73 // appropriate threads, which would be somewhat unfortunate.
74
75 // Whether we have an alpha channel in the (RGB/A) backing store
76 private final boolean alpha;
77
78 // Whether we're using only a GL_INTENSITY backing store
79 private final boolean intensity;
80
81 // Whether we're attempting to use automatic mipmap generation support
82 private boolean mipmap;
83
84 // Whether smoothing is enabled for the OpenGL texture (switching
85 // between GL_LINEAR and GL_NEAREST filtering)
86 private boolean smoothing = true;
87 private boolean smoothingChanged;
88
89 // The backing store itself
90 private BufferedImage image;
91
92 private Texture texture;
93 private AWTTextureData textureData;
94 private boolean mustReallocateTexture;
95 private Rectangle dirtyRegion;
96
97 private final GLUgl2 glu = new GLUgl2();
98
99 // Current color
100 private float r = 1.0f;
101 private float g = 1.0f;
102 private float b = 1.0f;
103 private float a = 1.0f;
104
105 /** Creates a new renderer with backing store of the specified width
106 and height. If <CODE>alpha</CODE> is true, allocates an alpha
107 channel in the backing store image. No mipmap support is
108 requested.
109
110 @param width the width of the texture to render into
111 @param height the height of the texture to render into
112 @param alpha whether to allocate an alpha channel for the texture
113 */
114 public TextureRenderer(final int width, final int height, final boolean alpha) {
115 this(width, height, alpha, false);
116 }
117
118 /** Creates a new renderer with backing store of the specified width
119 and height. If <CODE>alpha</CODE> is true, allocates an alpha channel in the
120 backing store image. If <CODE>mipmap</CODE> is true, attempts to use OpenGL's
121 automatic mipmap generation for better smoothing when rendering
122 the TextureRenderer's contents at a distance.
123
124 @param width the width of the texture to render into
125 @param height the height of the texture to render into
126 @param alpha whether to allocate an alpha channel for the texture
127 @param mipmap whether to attempt use of automatic mipmap generation
128 */
129 public TextureRenderer(final int width, final int height, final boolean alpha, final boolean mipmap) {
130 this(width, height, alpha, false, mipmap);
131 }
132
133 // Internal constructor to avoid confusion since alpha only makes
134 // sense when intensity is not set
135 private TextureRenderer(final int width, final int height, final boolean alpha, final boolean intensity, final boolean mipmap) {
136 this.alpha = alpha;
137 this.intensity = intensity;
138 this.mipmap = mipmap;
139 init(width, height);
140 }
141
142 /** Creates a new renderer with a special kind of backing store
143 which acts only as an alpha channel. No mipmap support is
144 requested. Internally, this associates a GL_INTENSITY OpenGL
145 texture with the backing store. */
146 public static TextureRenderer createAlphaOnlyRenderer(final int width, final int height) {
147 return createAlphaOnlyRenderer(width, height, false);
148 }
149
150 /** Creates a new renderer with a special kind of backing store
151 which acts only as an alpha channel. If <CODE>mipmap</CODE> is
152 true, attempts to use OpenGL's automatic mipmap generation for
153 better smoothing when rendering the TextureRenderer's contents
154 at a distance. Internally, this associates a GL_INTENSITY OpenGL
155 texture with the backing store. */
156 public static TextureRenderer createAlphaOnlyRenderer(final int width, final int height, final boolean mipmap) {
157 return new TextureRenderer(width, height, false, true, mipmap);
158 }
159
160 /** Returns the width of the backing store of this renderer.
161
162 @return the width of the backing store of this renderer
163 */
164 public int getWidth() {
165 return image.getWidth();
166 }
167
168 /** Returns the height of the backing store of this renderer.
169
170 @return the height of the backing store of this renderer
171 */
172 public int getHeight() {
173 return image.getHeight();
174 }
175
176 /** Returns the size of the backing store of this renderer in a
177 newly-allocated {@link java.awt.Dimension Dimension} object.
178
179 @return the size of the backing store of this renderer
180 */
182 return getSize(null);
183 }
184
185 /** Returns the size of the backing store of this renderer. Uses the
186 {@link java.awt.Dimension Dimension} object if one is supplied,
187 or allocates a new one if null is passed.
188
189 @param d a {@link java.awt.Dimension Dimension} object in which
190 to store the results, or null to allocate a new one
191
192 @return the size of the backing store of this renderer
193 */
195 if (d == null)
196 d = new Dimension();
197 d.setSize(image.getWidth(), image.getHeight());
198 return d;
199 }
200
201 /** Sets the size of the backing store of this renderer. This may
202 cause the OpenGL texture object associated with this renderer to
203 be invalidated; it is not recommended to cache this texture
204 object outside this class but to instead call {@link #getTexture
205 getTexture} when it is needed.
206
207 @param width the new width of the backing store of this renderer
208 @param height the new height of the backing store of this renderer
209 @throws GLException If an OpenGL context is not current when this method is called
210 */
211 public void setSize(final int width, final int height) throws GLException {
212 init(width, height);
213 }
214
215 /** Sets the size of the backing store of this renderer. This may
216 cause the OpenGL texture object associated with this renderer to
217 be invalidated.
218
219 @param d the new size of the backing store of this renderer
220 @throws GLException If an OpenGL context is not current when this method is called
221 */
222 public void setSize(final Dimension d) throws GLException {
223 setSize(d.width, d.height);
224 }
225
226 /** Sets whether smoothing is enabled for the OpenGL texture; if so,
227 uses GL_LINEAR interpolation for the minification and
228 magnification filters. Defaults to true. Changes to this setting
229 will not take effect until the next call to {@link
230 #beginOrthoRendering beginOrthoRendering}.
231
232 @param smoothing whether smoothing is enabled for the OpenGL texture
233 */
234 public void setSmoothing(final boolean smoothing) {
235 this.smoothing = smoothing;
236 smoothingChanged = true;
237 }
238
239 /** Returns whether smoothing is enabled for the OpenGL texture; see
240 {@link #setSmoothing setSmoothing}. Defaults to true.
241
242 @return whether smoothing is enabled for the OpenGL texture
243 */
244 public boolean getSmoothing() {
245 return smoothing;
246 }
247
248 /** Creates a {@link java.awt.Graphics2D Graphics2D} instance for
249 rendering to the backing store of this renderer. The returned
250 object should be disposed of using the normal {@link
251 java.awt.Graphics#dispose() Graphics.dispose()} method once it
252 is no longer being used.
253
254 @return a new {@link java.awt.Graphics2D Graphics2D} object for
255 rendering into the backing store of this renderer
256 */
257 public Graphics2D createGraphics() {
258 return image.createGraphics();
259 }
260
261 /** Returns the underlying Java 2D {@link java.awt.Image Image}
262 being rendered into. */
263 public Image getImage() {
264 return image;
265 }
266
267 /** Marks the given region of the TextureRenderer as dirty. This
268 region, and any previously set dirty regions, will be
269 automatically synchronized with the underlying Texture during
270 the next {@link #getTexture getTexture} operation, at which
271 point the dirty region will be cleared. It is not necessary for
272 an OpenGL context to be current when this method is called.
273
274 @param x the x coordinate (in Java 2D coordinates -- relative to
275 upper left) of the region to update
276 @param y the y coordinate (in Java 2D coordinates -- relative to
277 upper left) of the region to update
278 @param width the width of the region to update
279 @param height the height of the region to update
280 */
281 public void markDirty(final int x, final int y, final int width, final int height) {
282 final Rectangle curRegion = new Rectangle(x, y, width, height);
283 if (dirtyRegion == null) {
284 dirtyRegion = curRegion;
285 } else {
286 dirtyRegion.add(curRegion);
287 }
288 }
289
290 /** Returns the underlying OpenGL Texture object associated with
291 this renderer, synchronizing any dirty regions of the
292 TextureRenderer with the underlying OpenGL texture.
293
294 @throws GLException If an OpenGL context is not current when this method is called
295 */
297 if (dirtyRegion != null) {
298 sync(dirtyRegion.x, dirtyRegion.y, dirtyRegion.width, dirtyRegion.height);
299 dirtyRegion = null;
300 }
301
302 ensureTexture();
303 return texture;
304 }
305
306 /** Disposes all resources associated with this renderer. It is not
307 valid to use this renderer after calling this method.
308
309 @throws GLException If an OpenGL context is not current when this method is called
310 */
311 public void dispose() throws GLException {
312 if (texture != null) {
313 texture.destroy(GLContext.getCurrentGL());
314 texture = null;
315 }
316 if (image != null) {
317 image.flush();
318 image = null;
319 }
320 }
321
322 /** Convenience method which assists in rendering portions of the
323 OpenGL texture to the screen, if the application intends to draw
324 them as a flat overlay on to the screen. Pushes OpenGL state
325 bits (GL_ENABLE_BIT, GL_DEPTH_BUFFER_BIT and GL_TRANSFORM_BIT);
326 disables the depth test, back-face culling, and lighting;
327 enables the texture in this renderer; and sets up the viewing
328 matrices for orthographic rendering where the coordinates go
329 from (0, 0) at the lower left to (width, height) at the upper
330 right. Equivalent to beginOrthoRendering(width, height, true).
331 {@link #endOrthoRendering} must be used in conjunction with this
332 method to restore all OpenGL states.
333
334 @param width the width of the current on-screen OpenGL drawable
335 @param height the height of the current on-screen OpenGL drawable
336
337 @throws GLException If an OpenGL context is not current when this method is called
338 */
339 public void beginOrthoRendering(final int width, final int height) throws GLException {
340 beginOrthoRendering(width, height, true);
341 }
342
343 /** Convenience method which assists in rendering portions of the
344 OpenGL texture to the screen, if the application intends to draw
345 them as a flat overlay on to the screen. Pushes OpenGL state
346 bits (GL_ENABLE_BIT, GL_DEPTH_BUFFER_BIT and GL_TRANSFORM_BIT);
347 disables the depth test (if the "disableDepthTest" argument is
348 true), back-face culling, and lighting; enables the texture in
349 this renderer; and sets up the viewing matrices for orthographic
350 rendering where the coordinates go from (0, 0) at the lower left
351 to (width, height) at the upper right. {@link
352 #endOrthoRendering} must be used in conjunction with this method
353 to restore all OpenGL states.
354
355 @param width the width of the current on-screen OpenGL drawable
356 @param height the height of the current on-screen OpenGL drawable
357 @param disableDepthTest whether the depth test should be disabled
358
359 @throws GLException If an OpenGL context is not current when this method is called
360 */
361 public void beginOrthoRendering(final int width, final int height, final boolean disableDepthTest) throws GLException {
362 beginRendering(true, width, height, disableDepthTest);
363 }
364
365 /** Convenience method which assists in rendering portions of the
366 OpenGL texture to the screen as 2D quads in 3D space. Pushes
367 OpenGL state (GL_ENABLE_BIT); disables lighting; and enables the
368 texture in this renderer. Unlike {@link #beginOrthoRendering
369 beginOrthoRendering}, does not modify the depth test, back-face
370 culling, lighting, or the modelview or projection matrices. The
371 user is responsible for setting up the view matrices for correct
372 results of {@link #draw3DRect draw3DRect}. {@link
373 #end3DRendering} must be used in conjunction with this method to
374 restore all OpenGL states.
375
376 @throws GLException If an OpenGL context is not current when this method is called
377 */
378 public void begin3DRendering() throws GLException {
379 beginRendering(false, 0, 0, false);
380 }
381
382 /** Changes the color of the polygons, and therefore the drawn
383 images, this TextureRenderer produces. Use of this method is
384 optional. The TextureRenderer uses the GL_MODULATE texture
385 environment mode, which causes the portions of the rendered
386 texture to be multiplied by the color of the rendered
387 polygons. The polygon color can be varied to achieve effects
388 like tinting of the overall output or fading in and out by
389 changing the alpha of the color. <P>
390
391 Each component ranges from 0.0f - 1.0f. The alpha component, if
392 used, does not need to be premultiplied into the color channels
393 as described in the documentation for {@link
394 com.jogamp.opengl.util.texture.Texture Texture}, although
395 premultiplied colors are used internally. The default color is
396 opaque white.
397
398 @param r the red component of the new color
399 @param g the green component of the new color
400 @param b the blue component of the new color
401 @param a the alpha component of the new color, 0.0f = completely
402 transparent, 1.0f = completely opaque
403 @throws GLException If an OpenGL context is not current when this method is called
404 */
405 public void setColor(final float r, final float g, final float b, final float a) throws GLException {
406 final GL2 gl = GLContext.getCurrentGL().getGL2();
407 this.r = r * a;
408 this.g = g * a;
409 this.b = b * a;
410 this.a = a;
411
412 gl.glColor4f(this.r, this.g, this.b, this.a);
413 }
414
415 private float[] compArray;
416 /** Changes the current color of this TextureRenderer to the
417 supplied one. The default color is opaque white. See {@link
418 #setColor(float,float,float,float) setColor} for more details.
419
420 @param color the new color to use for rendering
421 @throws GLException If an OpenGL context is not current when this method is called
422 */
423 public void setColor(final Color color) throws GLException {
424 // Get color's RGBA components as floats in the range [0,1].
425 if (compArray == null) {
426 compArray = new float[4];
427 }
428 color.getRGBComponents(compArray);
429 setColor(compArray[0], compArray[1], compArray[2], compArray[3]);
430 }
431
432 /** Draws an orthographically projected rectangle containing all of
433 the underlying texture to the specified location on the
434 screen. All (x, y) coordinates are specified relative to the
435 lower left corner of either the texture image or the current
436 OpenGL drawable. This method is equivalent to
437 <code>drawOrthoRect(screenx, screeny, 0, 0, getWidth(),
438 getHeight());</code>.
439
440 @param screenx the on-screen x coordinate at which to draw the rectangle
441 @param screeny the on-screen y coordinate (relative to lower left) at
442 which to draw the rectangle
443
444 @throws GLException If an OpenGL context is not current when this method is called
445 */
446 public void drawOrthoRect(final int screenx, final int screeny) throws GLException {
447 drawOrthoRect(screenx, screeny, 0, 0, getWidth(), getHeight());
448 }
449
450 /** Draws an orthographically projected rectangle of the underlying
451 texture to the specified location on the screen. All (x, y)
452 coordinates are specified relative to the lower left corner of
453 either the texture image or the current OpenGL drawable.
454
455 @param screenx the on-screen x coordinate at which to draw the rectangle
456 @param screeny the on-screen y coordinate (relative to lower left) at
457 which to draw the rectangle
458 @param texturex the x coordinate of the pixel in the texture of
459 the lower left portion of the rectangle to draw
460 @param texturey the y coordinate of the pixel in the texture
461 (relative to lower left) of the lower left portion of the
462 rectangle to draw
463 @param width the width of the rectangle to draw
464 @param height the height of the rectangle to draw
465
466 @throws GLException If an OpenGL context is not current when this method is called
467 */
468 public void drawOrthoRect(final int screenx, final int screeny,
469 final int texturex, final int texturey,
470 final int width, final int height) throws GLException {
471 draw3DRect(screenx, screeny, 0, texturex, texturey, width, height, 1);
472 }
473
474 /** Draws a rectangle of the underlying texture to the specified 3D
475 location. In the current coordinate system, the lower left
476 corner of the rectangle is placed at (x, y, z), and the upper
477 right corner is placed at (x + width * scaleFactor, y + height *
478 scaleFactor, z). The lower left corner of the sub-rectangle of
479 the texture is (texturex, texturey) and the upper right corner
480 is (texturex + width, texturey + height). For back-face culling
481 purposes, the rectangle is drawn with counterclockwise
482 orientation of the vertices when viewed from the front.
483
484 @param x the x coordinate at which to draw the rectangle
485 @param y the y coordinate at which to draw the rectangle
486 @param z the z coordinate at which to draw the rectangle
487 @param texturex the x coordinate of the pixel in the texture of
488 the lower left portion of the rectangle to draw
489 @param texturey the y coordinate of the pixel in the texture
490 (relative to lower left) of the lower left portion of the
491 rectangle to draw
492 @param width the width in texels of the rectangle to draw
493 @param height the height in texels of the rectangle to draw
494 @param scaleFactor the scale factor to apply (multiplicatively)
495 to the size of the drawn rectangle
496
497 @throws GLException If an OpenGL context is not current when this method is called
498 */
499 public void draw3DRect(final float x, final float y, final float z,
500 final int texturex, final int texturey,
501 final int width, final int height,
502 final float scaleFactor) throws GLException {
503 final GL2 gl = GLContext.getCurrentGL().getGL2();
504 final Texture texture = getTexture();
505 final TextureCoords coords = texture.getSubImageTexCoords(texturex, texturey,
506 texturex + width,
507 texturey + height);
509 gl.glTexCoord2f(coords.left(), coords.bottom());
510 gl.glVertex3f(x, y, z);
511 gl.glTexCoord2f(coords.right(), coords.bottom());
512 gl.glVertex3f(x + width * scaleFactor, y, z);
513 gl.glTexCoord2f(coords.right(), coords.top());
514 gl.glVertex3f(x + width * scaleFactor, y + height * scaleFactor, z);
515 gl.glTexCoord2f(coords.left(), coords.top());
516 gl.glVertex3f(x, y + height * scaleFactor, z);
517 gl.glEnd();
518 }
519
520 /** Convenience method which assists in rendering portions of the
521 OpenGL texture to the screen, if the application intends to draw
522 them as a flat overlay on to the screen. Must be used if {@link
523 #beginOrthoRendering} is used to set up the rendering stage for
524 this overlay.
525
526 @throws GLException If an OpenGL context is not current when this method is called
527 */
528 public void endOrthoRendering() throws GLException {
529 endRendering(true);
530 }
531
532 /** Convenience method which assists in rendering portions of the
533 OpenGL texture to the screen as 2D quads in 3D space. Must be
534 used if {@link #begin3DRendering} is used to set up the
535 rendering stage for this overlay.
536
537 @throws GLException If an OpenGL context is not current when this method is called
538 */
539 public void end3DRendering() throws GLException {
540 endRendering(false);
541 }
542
543 /** Indicates whether automatic mipmap generation is in use for this
544 TextureRenderer. The result of this method may change from true
545 to false if it is discovered during allocation of the
546 TextureRenderer's backing store that automatic mipmap generation
547 is not supported at the OpenGL level. */
549 return mipmap;
550 }
551
552 //----------------------------------------------------------------------
553 // Internals only below this point
554 //
555
556 private void beginRendering(final boolean ortho, final int width, final int height, final boolean disableDepthTestForOrtho) {
557 final GL2 gl = GLContext.getCurrentGL().getGL2();
558 final int attribBits =
559 GL2.GL_ENABLE_BIT | GL2.GL_TEXTURE_BIT | GL.GL_COLOR_BUFFER_BIT |
560 (ortho ? (GL.GL_DEPTH_BUFFER_BIT | GL2.GL_TRANSFORM_BIT) : 0);
561 gl.glPushAttrib(attribBits);
563 if (ortho) {
564 if (disableDepthTestForOrtho) {
566 }
569 gl.glPushMatrix();
570 gl.glLoadIdentity();
571 glu.gluOrtho2D(0, width, 0, height);
573 gl.glPushMatrix();
574 gl.glLoadIdentity();
576 gl.glPushMatrix();
577 gl.glLoadIdentity();
578 }
579 gl.glEnable(GL.GL_BLEND);
581 final Texture texture = getTexture();
582 texture.enable(gl);
583 texture.bind(gl);
585 // Change polygon color to last saved
586 gl.glColor4f(r, g, b, a);
587 if (smoothingChanged) {
588 smoothingChanged = false;
589 if (smoothing) {
591 if (mipmap) {
593 } else {
595 }
596 } else {
599 }
600 }
601 }
602
603 private void endRendering(final boolean ortho) {
604 final GL2 gl = GLContext.getCurrentGL().getGL2();
605 final Texture texture = getTexture();
606 texture.disable(gl);
607 if (ortho) {
608 gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
609 gl.glPopMatrix();
610 gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
611 gl.glPopMatrix();
613 gl.glPopMatrix();
614 }
615 gl.glPopAttrib();
616 }
617
618 private void init(final int width, final int height) {
619 final GL2 gl = GLContext.getCurrentGL().getGL2();
620 // Discard previous BufferedImage if any
621 if (image != null) {
622 image.flush();
623 image = null;
624 }
625
626 // Infer the internal format if not an intensity texture
627 final int internalFormat = (intensity ? GL2.GL_INTENSITY : 0);
628 final int imageType =
629 (intensity ? BufferedImage.TYPE_BYTE_GRAY :
630 (alpha ? BufferedImage.TYPE_INT_ARGB_PRE : BufferedImage.TYPE_INT_RGB));
631 image = new BufferedImage(width, height, imageType);
632 // Always realllocate the TextureData associated with this
633 // BufferedImage; it's just a reference to the contents but we
634 // need it in order to update sub-regions of the underlying
635 // texture
636 textureData = new AWTTextureData(gl.getGLProfile(), internalFormat, 0, mipmap, image);
637 // For now, always reallocate the underlying OpenGL texture when
638 // the backing store size changes
639 mustReallocateTexture = true;
640 }
641
642 /** Synchronizes the specified region of the backing store down to
643 the underlying OpenGL texture. If {@link #markDirty markDirty}
644 is used instead to indicate the regions that are out of sync,
645 this method does not need to be called.
646
647 @param x the x coordinate (in Java 2D coordinates -- relative to
648 upper left) of the region to update
649 @param y the y coordinate (in Java 2D coordinates -- relative to
650 upper left) of the region to update
651 @param width the width of the region to update
652 @param height the height of the region to update
653
654 @throws GLException If an OpenGL context is not current when this method is called
655 */
656 private void sync(final int x, final int y, final int width, final int height) throws GLException {
657 // Force allocation if necessary
658 final boolean canSkipUpdate = ensureTexture();
659
660 if (!canSkipUpdate) {
661 // Update specified region.
662 // NOTE that because BufferedImage-based TextureDatas now don't
663 // do anything to their contents, the coordinate systems for
664 // OpenGL and Java 2D actually line up correctly for
665 // updateSubImage calls, so we don't need to do any argument
666 // conversion here (i.e., flipping the Y coordinate).
667 texture.updateSubImage(GLContext.getCurrentGL(), textureData, 0, x, y, x, y, width, height);
668 }
669 }
670
671 // Returns true if the texture was newly allocated, false if not
672 private boolean ensureTexture() {
673 final GL gl = GLContext.getCurrentGL();
674 if (mustReallocateTexture) {
675 if (texture != null) {
676 texture.destroy(gl);
677 texture = null;
678 }
679 mustReallocateTexture = false;
680 }
681
682 if (texture == null) {
683 texture = TextureIO.newTexture(textureData);
684 if (mipmap && !texture.isUsingAutoMipmapGeneration()) {
685 // Only try this once
686 texture.destroy(gl);
687 mipmap = false;
688 textureData.setMipmap(false);
689 texture = TextureIO.newTexture(textureData);
690 }
691
692 if (!smoothing) {
693 // The TextureIO classes default to GL_LINEAR filtering
696 }
697 return true;
698 }
699
700 return false;
701 }
702}
Abstraction for an OpenGL rendering context.
Definition: GLContext.java:74
static GL getCurrentGL()
Returns the GL object bound to this thread current context.
Definition: GLContext.java:500
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
final void gluOrtho2D(float left, float right, float bottom, float top)
Definition: GLUgl2.java:282
Provides the ability to render into an OpenGL Texture using the Java 2D APIs.
void dispose()
Disposes all resources associated with this renderer.
int getHeight()
Returns the height of the backing store of this renderer.
void draw3DRect(final float x, final float y, final float z, final int texturex, final int texturey, final int width, final int height, final float scaleFactor)
Draws a rectangle of the underlying texture to the specified 3D location.
void setSize(final int width, final int height)
Sets the size of the backing store of this renderer.
void setSmoothing(final boolean smoothing)
Sets whether smoothing is enabled for the OpenGL texture; if so, uses GL_LINEAR interpolation for the...
boolean getSmoothing()
Returns whether smoothing is enabled for the OpenGL texture; see setSmoothing.
void setSize(final Dimension d)
Sets the size of the backing store of this renderer.
boolean isUsingAutoMipmapGeneration()
Indicates whether automatic mipmap generation is in use for this TextureRenderer.
static TextureRenderer createAlphaOnlyRenderer(final int width, final int height, final boolean mipmap)
Creates a new renderer with a special kind of backing store which acts only as an alpha channel.
static TextureRenderer createAlphaOnlyRenderer(final int width, final int height)
Creates a new renderer with a special kind of backing store which acts only as an alpha channel.
Dimension getSize()
Returns the size of the backing store of this renderer in a newly-allocated Dimension object.
void drawOrthoRect(final int screenx, final int screeny)
Draws an orthographically projected rectangle containing all of the underlying texture to the specifi...
void end3DRendering()
Convenience method which assists in rendering portions of the OpenGL texture to the screen as 2D quad...
void begin3DRendering()
Convenience method which assists in rendering portions of the OpenGL texture to the screen as 2D quad...
TextureRenderer(final int width, final int height, final boolean alpha, final boolean mipmap)
Creates a new renderer with backing store of the specified width and height.
void markDirty(final int x, final int y, final int width, final int height)
Marks the given region of the TextureRenderer as dirty.
Texture getTexture()
Returns the underlying OpenGL Texture object associated with this renderer, synchronizing any dirty r...
void drawOrthoRect(final int screenx, final int screeny, final int texturex, final int texturey, final int width, final int height)
Draws an orthographically projected rectangle of the underlying texture to the specified location on ...
Dimension getSize(Dimension d)
Returns the size of the backing store of this renderer.
Image getImage()
Returns the underlying Java 2D Image being rendered into.
void setColor(final Color color)
Changes the current color of this TextureRenderer to the supplied one.
int getWidth()
Returns the width of the backing store of this renderer.
Graphics2D createGraphics()
Creates a Graphics2D instance for rendering to the backing store of this renderer.
void setColor(final float r, final float g, final float b, final float a)
Changes the color of the polygons, and therefore the drawn images, this TextureRenderer produces.
void beginOrthoRendering(final int width, final int height, final boolean disableDepthTest)
Convenience method which assists in rendering portions of the OpenGL texture to the screen,...
TextureRenderer(final int width, final int height, final boolean alpha)
Creates a new renderer with backing store of the specified width and height.
void beginOrthoRendering(final int width, final int height)
Convenience method which assists in rendering portions of the OpenGL texture to the screen,...
void endOrthoRendering()
Convenience method which assists in rendering portions of the OpenGL texture to the screen,...
Specifies texture coordinates for a rectangular area of a texture.
void setMipmap(final boolean mipmap)
Sets whether mipmaps should be generated for the texture data.
static Texture newTexture(final TextureData data)
Creates an OpenGL texture object from the specified TextureData using the current OpenGL context.
Definition: TextureIO.java:459
Represents an OpenGL texture object.
Definition: Texture.java:173
void setTexParameteri(final GL gl, final int parameterName, final int value)
Sets the OpenGL integer texture parameter for the texture's target.
Definition: Texture.java:925
void bind(final GL gl)
Binds this texture to the given GL context.
Definition: Texture.java:377
void disable(final GL gl)
Disables this texture's target (e.g., GL_TEXTURE_2D) in the given GL state.
Definition: Texture.java:357
void enable(final GL gl)
Enables this texture's target (e.g., GL_TEXTURE_2D) in the given GL context's state.
Definition: Texture.java:330
TextureCoords getSubImageTexCoords(final int x1, final int y1, final int x2, final int y2)
Returns the set of texture coordinates corresponding to the specified sub-image.
Definition: Texture.java:497
boolean isUsingAutoMipmapGeneration()
Indicates whether this Texture is using automatic mipmap generation (via the OpenGL texture parameter...
Definition: Texture.java:1005
void destroy(final GL gl)
Destroys and nulls the underlying native texture used by this Texture instance if owned,...
Definition: Texture.java:388
void updateSubImage(final GL gl, final TextureData data, final int mipmapLevel, final int x, final int y)
Updates a subregion of the content area of this texture using the given data.
Definition: Texture.java:818
static final int GL_MODULATE
GL_VERSION_ES_1_0, GL_VERSION_1_0 Define "GL_MODULATE" with expression '0x2100', CType: int
Definition: GL2ES1.java:147
static final int GL_TEXTURE_ENV_MODE
GL_VERSION_ES_1_0, GL_VERSION_1_0 Define "GL_TEXTURE_ENV_MODE" with expression '0x2200',...
Definition: GL2ES1.java:202
void glTexEnvi(int target, int pname, int param)
Entry point to C language function: void {@native glTexEnvi}(GLenum target, GLenum pname,...
static final int GL_TEXTURE_ENV
GL_VERSION_ES_1_0, GL_VERSION_1_0 Define "GL_TEXTURE_ENV" with expression '0x2300',...
Definition: GL2ES1.java:154
static final int GL_QUADS
GL_ES_VERSION_3_2, GL_VERSION_1_1, GL_VERSION_1_0, GL_OES_tessellation_shader, GL_EXT_tessellation_sh...
Definition: GL2ES3.java:734
void glTexCoord2f(float s, float t)
Entry point to C language function: void {@native glTexCoord2f}(GLfloat s, GLfloat t) Part of GL_V...
static final int GL_TRANSFORM_BIT
GL_VERSION_1_0 Define "GL_TRANSFORM_BIT" with expression '0x00001000', CType: int
Definition: GL2.java:1027
void glPopAttrib()
Entry point to C language function: void {@native glPopAttrib}() Part of GL_VERSION_1_0
void glBegin(int mode)
Entry point to C language function: void {@native glBegin}(GLenum mode) Part of GL_VERSION_1_0
void glVertex3f(float x, float y, float z)
Entry point to C language function: void {@native glVertex3f}(GLfloat x, GLfloat y,...
void glEnd()
Entry point to C language function: void {@native glEnd}() Part of GL_VERSION_1_0
void glPushAttrib(int mask)
Entry point to C language function: void {@native glPushAttrib}(GLbitfield mask) Part of GL_VERSIO...
GLProfile getGLProfile()
Returns the GLProfile associated with this GL object.
GL2 getGL2()
Casts this object to the GL2 interface.
static final int GL_TEXTURE_MAG_FILTER
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TEXTURE_MAG_FILTER" w...
Definition: GL.java:197
static final int GL_TEXTURE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TEXTURE" with express...
Definition: GL.java:615
static final int GL_ONE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0, GL_EXT_vertex_shader Alias for:...
Definition: GL.java:137
static final int GL_LINEAR_MIPMAP_LINEAR
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_LINEAR_MIPMAP_LINEAR"...
Definition: GL.java:354
void glDisable(int cap)
Entry point to C language function: void {@native glDisable}(GLenum cap) Part of GL_ES_VERSION_2_0...
static final int GL_LINEAR
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_LINEAR" with expressi...
Definition: GL.java:323
static final int GL_TEXTURE_MIN_FILTER
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_TEXTURE_MIN_FILTER" w...
Definition: GL.java:372
static final int GL_ONE_MINUS_SRC_ALPHA
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_ONE_MINUS_SRC_ALPHA" ...
Definition: GL.java:166
void glEnable(int cap)
Entry point to C language function: void {@native glEnable}(GLenum cap) Part of GL_ES_VERSION_2_0,...
void glBlendFunc(int sfactor, int dfactor)
Entry point to C language function: void {@native glBlendFunc}(GLenum sfactor, GLenum dfactor) Par...
static final int GL_DEPTH_TEST
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_TEST" with expr...
Definition: GL.java:43
static final int GL_NEAREST
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_NEAREST" with express...
Definition: GL.java:715
static final int GL_BLEND
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_BLEND" with expressio...
Definition: GL.java:704
static final int GL_CULL_FACE
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_CULL_FACE" with expre...
Definition: GL.java:720
Subset of OpenGL fixed function pipeline's matrix operations.
static final int GL_PROJECTION
Matrix mode projection.
void glPushMatrix()
Push the current matrix to it's stack, while preserving it's values.
void glPopMatrix()
Pop the current matrix from it's stack.
static final int GL_MODELVIEW
Matrix mode modelview.
void glLoadIdentity()
Load the current matrix with the identity matrix.
void glMatrixMode(int mode)
Sets the current matrix mode.
void glColor4f(float red, float green, float blue, float alpha)