private TextureRenderer build(String pKey) { Color color = ColorUtil.parseColor(pKey.substring(3)); if (color == null) { return null; } TextureRenderer img = new TextureRenderer(1, 1, true, true); Graphics2D g = img.createGraphics(); g.setClip(0, 0, 1, 1); g.setColor(color); g.fillRect(0, 0, 1, 1); return img; }
@Override public Texture buildTexture(String pKey) { TextureRenderer build = build(pKey); if (build != null) { return build.getTexture(); } return null; }
newRenderer.markDirty(0, 0, newRenderer.getWidth(), newRenderer.getHeight()); ((TextureRenderer) newBackingStore).beginOrthoRendering(beginRenderingWidth, beginRenderingHeight, beginRenderingDepthTestDisabled); } else { ((TextureRenderer) newBackingStore).begin3DRendering(); ((TextureRenderer) newBackingStore).setColor(cachedR, cachedG, cachedB, cachedA); } else { ((TextureRenderer) newBackingStore).setColor(cachedColor);
@Override public Object allocateBackingStore(final int w, final int h) { // FIXME: should consider checking Font's attributes to see // whether we're likely to need to support a full RGBA backing // store (i.e., non-default Paint, foreground color, etc.), but // for now, let's just be more efficient TextureRenderer renderer; if (renderDelegate.intensityOnly()) { renderer = TextureRenderer.createAlphaOnlyRenderer(w, h, mipmap); } else { renderer = new TextureRenderer(w, h, true, mipmap); } renderer.setSmoothing(smoothing); if (DEBUG) { System.err.println(" TextRenderer allocating backing store " + w + " x " + h); } return renderer; }
private void validateRenderer() { if (renderer == null) { renderer = new TextureRenderer(drawable.getSurfaceWidth(), drawable.getSurfaceHeight(), true); contentsLost = true; } else if (renderer.getWidth() != drawable.getSurfaceWidth() || renderer.getHeight() != drawable.getSurfaceHeight()) { renderer.setSize(drawable.getSurfaceWidth(), drawable.getSurfaceHeight()); contentsLost = true; } else { contentsLost = false; } } }
@Override public void beginMovement(final Object oldBackingStore, final Object newBackingStore) { // Exit the begin / end pair if necessary if (inBeginEndPair) { // Draw any outstanding glyphs flush(); final GL2 gl = GLContext.getCurrentGL().getGL2(); // Pop client attrib bits used by the pipelined quad renderer gl.glPopClientAttrib(); // The OpenGL spec is unclear about whether this changes the // buffer bindings, so preemptively zero out the GL_ARRAY_BUFFER // binding if (getUseVertexArrays() && is15Available(gl)) { try { gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0); } catch (final Exception e) { isExtensionAvailable_GL_VERSION_1_5 = false; } } if (isOrthoMode) { ((TextureRenderer) oldBackingStore).endOrthoRendering(); } else { ((TextureRenderer) oldBackingStore).end3DRendering(); } } final TextureRenderer newRenderer = (TextureRenderer) newBackingStore; g = newRenderer.createGraphics(); }
final TextureCoords wholeImageTexCoords = renderer.getTexture().getImageTexCoords(); final float xScale = wholeImageTexCoords.right(); final float yScale = wholeImageTexCoords.bottom(); final int texturey = renderer.getHeight() - rect.y() - (int) origRect.getHeight() - (data.origin().y - data.origOriginY()); final int width = (int) origRect.getWidth(); final int height = (int) origRect.getHeight(); final float tx1 = xScale * texturex / renderer.getWidth(); final float ty1 = yScale * (1.0f - ((float) texturey / (float) renderer.getHeight())); final float tx2 = xScale * (texturex + width) / renderer.getWidth(); final float ty2 = yScale * (1.0f - ((float) (texturey + height) / (float) renderer.getHeight()));
@Override public void display(final GLAutoDrawable drawable) { final GL2 gl = GLContext.getCurrentGL().getGL2(); gl.glClear(GL.GL_DEPTH_BUFFER_BIT | GL.GL_COLOR_BUFFER_BIT); if (packer == null) { return; } final TextureRenderer rend = getBackingStore(); final int w = rend.getWidth(); final int h = rend.getHeight(); rend.beginOrthoRendering(w, h); rend.drawOrthoRect(0, 0); rend.endOrthoRendering(); if ((frame.getWidth() != w) || (frame.getHeight() != h)) { EventQueue.invokeLater(new Runnable() { @Override public void run() { frame.setSize(w, h); } }); } }
/** Creates a {@link java.awt.Graphics2D Graphics2D} instance for rendering into the overlay. The returned object should be disposed of using the normal {@link java.awt.Graphics#dispose() Graphics.dispose()} method once it is no longer being used. @return a new {@link java.awt.Graphics2D Graphics2D} object for rendering into the backing store of this renderer */ public Graphics2D createGraphics() { // Validate the size of the renderer against the current size of // the drawable validateRenderer(); return renderer.createGraphics(); }
/** Creates a new renderer with a special kind of backing store which acts only as an alpha channel. If <CODE>mipmap</CODE> is true, attempts to use OpenGL's automatic mipmap generation for better smoothing when rendering the TextureRenderer's contents at a distance. Internally, this associates a GL_INTENSITY OpenGL texture with the backing store. */ public static TextureRenderer createAlphaOnlyRenderer(final int width, final int height, final boolean mipmap) { return new TextureRenderer(width, height, false, true, mipmap); }
getBackingStore().beginOrthoRendering(width, height, disableDepthTestForOrtho); } else { getBackingStore().begin3DRendering(); getBackingStore().setColor(cachedR, cachedG, cachedB, cachedA); } else { getBackingStore().setColor(cachedColor); if (mipmap && !getBackingStore().isUsingAutoMipmapGeneration()) { if (DEBUG) { System.err.println("Disabled mipmapping in TextRenderer");
@Override public BufferedImage buildImage(String pKey) { TextureRenderer build = build(pKey); if (build != null) { return imageToBufferedImage(build.getImage()); } return null; }
/** Convenience method which assists in rendering portions of the OpenGL texture to the screen, if the application intends to draw them as a flat overlay on to the screen. Pushes OpenGL state bits (GL_ENABLE_BIT, GL_DEPTH_BUFFER_BIT and GL_TRANSFORM_BIT); disables the depth test, back-face culling, and lighting; enables the texture in this renderer; and sets up the viewing matrices for orthographic rendering where the coordinates go from (0, 0) at the lower left to (width, height) at the upper right. Equivalent to beginOrthoRendering(width, height, true). {@link #endOrthoRendering} must be used in conjunction with this method to restore all OpenGL states. @param width the width of the current on-screen OpenGL drawable @param height the height of the current on-screen OpenGL drawable @throws GLException If an OpenGL context is not current when this method is called */ public void beginOrthoRendering(final int width, final int height) throws GLException { beginOrthoRendering(width, height, true); }
/** Convenience method which assists in rendering portions of the OpenGL texture to the screen as 2D quads in 3D space. Pushes OpenGL state (GL_ENABLE_BIT); disables lighting; and enables the texture in this renderer. Unlike {@link #beginOrthoRendering beginOrthoRendering}, does not modify the depth test, back-face culling, lighting, or the modelview or projection matrices. The user is responsible for setting up the view matrices for correct results of {@link #draw3DRect draw3DRect}. {@link #end3DRendering} must be used in conjunction with this method to restore all OpenGL states. @throws GLException If an OpenGL context is not current when this method is called */ public void begin3DRendering() throws GLException { beginRendering(false, 0, 0, false); }
private Graphics2D getGraphics2D() { final TextureRenderer renderer = getBackingStore(); if (cachedGraphics == null) { cachedGraphics = renderer.createGraphics(); // Set up composite, font and rendering hints cachedGraphics.setComposite(AlphaComposite.Src); cachedGraphics.setColor(Color.WHITE); cachedGraphics.setFont(font); cachedGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, (antialiased ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)); cachedGraphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, (useFractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON : RenderingHints.VALUE_FRACTIONALMETRICS_OFF)); } return cachedGraphics; }
@Override public void move(final Object oldBackingStore, final Rect oldLocation, final Object newBackingStore, final Rect newLocation) { final TextureRenderer oldRenderer = (TextureRenderer) oldBackingStore; final TextureRenderer newRenderer = (TextureRenderer) newBackingStore; if (oldRenderer == newRenderer) { // Movement on the same backing store -- easy case g.copyArea(oldLocation.x(), oldLocation.y(), oldLocation.w(), oldLocation.h(), newLocation.x() - oldLocation.x(), newLocation.y() - oldLocation.y()); } else { // Need to draw from the old renderer's image into the new one final Image img = oldRenderer.getImage(); g.drawImage(img, newLocation.x(), newLocation.y(), newLocation.x() + newLocation.w(), newLocation.y() + newLocation.h(), oldLocation.x(), oldLocation.y(), oldLocation.x() + oldLocation.w(), oldLocation.y() + oldLocation.h(), null); } }
/** Begins the OpenGL rendering process for the overlay. This is separated out so advanced applications can render independent pieces of the overlay to different portions of the drawable. @throws GLException If an OpenGL context is not current when this method is called */ public void beginRendering() throws GLException { renderer.beginOrthoRendering(drawable.getSurfaceWidth(), drawable.getSurfaceHeight()); }
/** Convenience method which assists in rendering portions of the OpenGL texture to the screen, if the application intends to draw them as a flat overlay on to the screen. Pushes OpenGL state bits (GL_ENABLE_BIT, GL_DEPTH_BUFFER_BIT and GL_TRANSFORM_BIT); disables the depth test (if the "disableDepthTest" argument is true), back-face culling, and lighting; enables the texture in this renderer; and sets up the viewing matrices for orthographic rendering where the coordinates go from (0, 0) at the lower left to (width, height) at the upper right. {@link #endOrthoRendering} must be used in conjunction with this method to restore all OpenGL states. @param width the width of the current on-screen OpenGL drawable @param height the height of the current on-screen OpenGL drawable @param disableDepthTest whether the depth test should be disabled @throws GLException If an OpenGL context is not current when this method is called */ public void beginOrthoRendering(final int width, final int height, final boolean disableDepthTest) throws GLException { beginRendering(true, width, height, disableDepthTest); }
private TextureRenderer loadTextureRenderer(String pKey) { BufferedImage img = loadBufferedImage(pKey); if (img == null) { return null; } TextureRenderer tr = new TextureRenderer(img.getWidth(), img.getHeight(), true, true); Graphics2D g = tr.createGraphics(); g.setClip(0, 0, img.getWidth(), img.getHeight()); g.drawImage(img, new AffineTransform(1f, 0f, 0f, 1f, 0, 0), null); g.dispose(); return tr; }
@Override public Texture buildTexture(String pKey) throws Exception { if (pKey == null) { return null; } if (pKey.startsWith(getBuilderPrefix())) { pKey = pKey.substring(getBuilderPrefix().length()); } TextureRenderer tr = loadTextureRenderer(pKey); if (tr != null) { return tr.getTexture(); } return null; }