41package com.jogamp.opengl.util.awt;
44import java.awt.Dimension;
45import java.awt.Graphics2D;
47import java.awt.Rectangle;
48import java.awt.image.*;
50import com.jogamp.opengl.*;
51import com.jogamp.opengl.fixedfunc.GLLightingFunc;
52import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
53import com.jogamp.opengl.glu.gl2.*;
55import com.jogamp.opengl.util.texture.*;
56import com.jogamp.opengl.util.texture.awt.*;
76 private final boolean alpha;
79 private final boolean intensity;
82 private boolean mipmap;
86 private boolean smoothing =
true;
87 private boolean smoothingChanged;
90 private BufferedImage image;
94 private boolean mustReallocateTexture;
95 private Rectangle dirtyRegion;
100 private float r = 1.0f;
101 private float g = 1.0f;
102 private float b = 1.0f;
103 private float a = 1.0f;
115 this(width, height, alpha,
false);
129 public TextureRenderer(
final int width,
final int height,
final boolean alpha,
final boolean mipmap) {
130 this(width, height, alpha,
false, mipmap);
135 private TextureRenderer(
final int width,
final int height,
final boolean alpha,
final boolean intensity,
final boolean mipmap) {
137 this.intensity = intensity;
138 this.mipmap = mipmap;
165 return image.getWidth();
173 return image.getHeight();
197 d.setSize(image.getWidth(), image.getHeight());
235 this.smoothing = smoothing;
236 smoothingChanged =
true;
258 return image.createGraphics();
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;
286 dirtyRegion.add(curRegion);
297 if (dirtyRegion !=
null) {
298 sync(dirtyRegion.x, dirtyRegion.y, dirtyRegion.width, dirtyRegion.height);
312 if (texture !=
null) {
362 beginRendering(
true, width, height, disableDepthTest);
379 beginRendering(
false, 0, 0,
false);
412 gl.
glColor4f(this.r, this.g, this.b, this.a);
415 private float[] compArray;
425 if (compArray ==
null) {
426 compArray =
new float[4];
428 color.getRGBComponents(compArray);
429 setColor(compArray[0], compArray[1], compArray[2], compArray[3]);
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);
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,
514 gl.
glVertex3f(x + width * scaleFactor, y + height * scaleFactor, z);
516 gl.
glVertex3f(x, y + height * scaleFactor, z);
556 private void beginRendering(
final boolean ortho,
final int width,
final int height,
final boolean disableDepthTestForOrtho) {
558 final int attribBits =
559 GL2.GL_ENABLE_BIT | GL2.GL_TEXTURE_BIT | GL.GL_COLOR_BUFFER_BIT |
564 if (disableDepthTestForOrtho) {
587 if (smoothingChanged) {
588 smoothingChanged =
false;
603 private void endRendering(
final boolean ortho) {
618 private void init(
final int width,
final int height) {
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);
639 mustReallocateTexture =
true;
656 private void sync(
final int x,
final int y,
final int width,
final int height)
throws GLException {
658 final boolean canSkipUpdate = ensureTexture();
660 if (!canSkipUpdate) {
672 private boolean ensureTexture() {
674 if (mustReallocateTexture) {
675 if (texture !=
null) {
679 mustReallocateTexture =
false;
682 if (texture ==
null) {
Abstraction for an OpenGL rendering context.
static GL getCurrentGL()
Returns the GL object bound to this thread current context.
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)
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.
Represents an OpenGL texture object.
void setTexParameteri(final GL gl, final int parameterName, final int value)
Sets the OpenGL integer texture parameter for the texture's target.
void bind(final GL gl)
Binds this texture to the given GL context.
void disable(final GL gl)
Disables this texture's target (e.g., GL_TEXTURE_2D) in the given GL state.
void enable(final GL gl)
Enables this texture's target (e.g., GL_TEXTURE_2D) in the given GL context's state.
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.
boolean isUsingAutoMipmapGeneration()
Indicates whether this Texture is using automatic mipmap generation (via the OpenGL texture parameter...
void destroy(final GL gl)
Destroys and nulls the underlying native texture used by this Texture instance if owned,...
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.
static final int GL_MODULATE
GL_VERSION_ES_1_0, GL_VERSION_1_0 Define "GL_MODULATE" with expression '0x2100', CType: int
static final int GL_TEXTURE_ENV_MODE
GL_VERSION_ES_1_0, GL_VERSION_1_0 Define "GL_TEXTURE_ENV_MODE" with expression '0x2200',...
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',...
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...
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
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...
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...
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:...
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"...
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...
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...
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" ...
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...
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...
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...
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...
static final int GL_LIGHTING
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)