JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
GLButton.java
Go to the documentation of this file.
1/**
2 * Copyright 2014-2023 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28package com.jogamp.graph.ui.shapes;
29
30import com.jogamp.opengl.GL;
31import com.jogamp.opengl.GL2ES2;
32import com.jogamp.opengl.GLCapabilities;
33import com.jogamp.opengl.GLCapabilitiesImmutable;
34import com.jogamp.opengl.GLContext;
35import com.jogamp.opengl.GLDrawable;
36import com.jogamp.opengl.GLDrawableFactory;
37import com.jogamp.opengl.GLEventListener;
38import com.jogamp.opengl.GLOffscreenAutoDrawable;
39import com.jogamp.graph.curve.Region;
40import com.jogamp.graph.curve.opengl.RegionRenderer;
41import com.jogamp.graph.ui.GraphShape;
42import com.jogamp.opengl.FBObject;
43import com.jogamp.opengl.util.texture.ImageSequence;
44import com.jogamp.opengl.util.texture.Texture;
45
46/**
47 * A GraphUI {@link GLEventListener} based {@link TexSeqButton} {@link GraphShape}.
48 * <p>
49 * GraphUI is GPU based and resolution independent.
50 * </p>
51 * <p>
52 * The {@link GLEventListener} is rendered via an {@link GLOffscreenAutoDrawable.FBO} into an {@link ImageSequence}.
53 * </p>
54 * <p>
55 * This button is rendered with a round oval shape.
56 * To render it rectangular, {@link #setCorner(float)} to zero.
57 * </p>
58 * <p>
59 * Default colors (toggle-on is full color):
60 * - non-toggle: 1 * color
61 * - pressed: 0.9 * color
62 * - toggle-off: 0.8 * color
63 * - toggle-on: 1.0 * color
64 * </p>
65 */
66public class GLButton extends TexSeqButton {
67 private final GLEventListener glel;
68 private final boolean useAlpha;
69 private volatile int fboWidth = 200;
70 private volatile int fboHeight = 200;
71 private volatile GLOffscreenAutoDrawable.FBO fboGLAD = null;
72 private boolean animateGLEL = false;
73
74 public GLButton(final int renderModes, final float width, final float height,
75 final int textureUnit, final GLEventListener glel, final boolean useAlpha) {
76 super(renderModes & ~Region.AA_RENDERING_MASK, width, height, new ImageSequence(textureUnit, true));
77 this.glel = glel;
78 this.useAlpha = useAlpha;
79
80 setColor(1.0f, 1.0f, 1.0f, 0.0f);
81 setPressedColorMod(0.9f, 0.9f, 0.9f, 0.7f);
82 setToggleOffColorMod(0.8f, 0.8f, 0.8f, 1.0f);
83 setToggleOnColorMod(1.0f, 1.0f, 1.0f, 1.0f);
84
85 // fake surface-size, will be overriden in initial FBO setup @ display
86 this.fboWidth = 320;
87 this.fboHeight = Math.round( 640 * height / width );
88 }
89
90 public final void setAnimate(final boolean v) { animateGLEL = v; }
91 public final boolean getAnimate() { return animateGLEL; }
92
93 public final void setFBOSize(final int fboWidth, final int fboHeight) {
94 this.fboWidth = fboWidth;
95 this.fboHeight = fboHeight;
96 }
97
98 public final GLOffscreenAutoDrawable.FBO getFBOAutoDrawable() { return fboGLAD; }
99
100 @Override
101 protected void destroyImpl(final GL2ES2 gl, final RegionRenderer renderer) {
102 ((ImageSequence)texSeq).destroy(gl);
103 fboGLAD.destroy();
104 }
105
106 @Override
107 public void draw(final GL2ES2 gl, final RegionRenderer renderer) {
108 final int[/*2*/] surfaceSize = getSurfaceSize(renderer.getMatrix(), renderer.getViewport(), new int[2]);
109 final boolean got_sz = null != surfaceSize && 0 < surfaceSize[0] && 0 < surfaceSize[1];
110
111 if( null == fboGLAD ) {
112 final ImageSequence imgSeq = (ImageSequence)texSeq;
113
114 final GLContext ctx = gl.getContext();
115 final GLDrawable drawable = ctx.getGLDrawable();
116 final GLCapabilitiesImmutable reqCaps = drawable.getRequestedGLCapabilities();
117 final GLCapabilities caps = (GLCapabilities) reqCaps.cloneMutable();
118 caps.setFBO(true);
119 caps.setDoubleBuffered(false);
120 if( !useAlpha ) {
121 caps.setAlphaBits(0);
122 }
124
125 // System.err.println("XXX FBO initSurfaceSize got_sz "+got_sz+", "+fboWidth+" x "+fboHeight+" -> "+surfaceSize[0]+" x "+surfaceSize[1]);
126 if( got_sz ) {
127 // override with real surface-size
128 fboWidth = surfaceSize[0];
129 fboHeight = surfaceSize[1];
130 }
133 caps, null, fboWidth, fboHeight);
134 fboWidth = 0;
135 fboHeight = 0;
136 fboGLAD.setSharedContext(ctx);
137 fboGLAD.setTextureUnit(imgSeq.getTextureUnit());
138 fboGLAD.addGLEventListener(glel);
139 fboGLAD.display(); // 1st init!
140
141 final FBObject.TextureAttachment texA01 = fboGLAD.getColorbuffer(GL.GL_FRONT).getTextureAttachment();
142 final Texture tex = new Texture(texA01.getName(), false /* ownsTextureID */,
143 imgSeq.getTextureTarget(),
144 fboGLAD.getSurfaceWidth(), fboGLAD.getSurfaceHeight(),
145 fboGLAD.getSurfaceWidth(), fboGLAD.getSurfaceHeight(),
146 false /* mustFlipVertically */);
147 imgSeq.addFrame(gl, tex);
149 } else if( 0 != fboWidth*fboHeight ) {
150 fboGLAD.setSurfaceSize(fboWidth, fboHeight);
151 fboWidth = 0;
152 fboHeight = 0;
154 } else if( got_sz && ( fboGLAD.getSurfaceWidth() != surfaceSize[0] || fboGLAD.getSurfaceHeight() != surfaceSize[1] ) ) {
155 // System.err.println("XXX FBO setSurfaceSize "+fboGLAD.getSurfaceWidth()+" x "+fboGLAD.getSurfaceHeight()+" -> "+surfaceSize[0]+" x "+surfaceSize[1]);
156 final ImageSequence imgSeq = (ImageSequence)texSeq;
157
158 fboGLAD.setSurfaceSize(surfaceSize[0], surfaceSize[1]);
159 fboGLAD.display(); // re-init!
160
161 imgSeq.destroy(gl);
162 final FBObject.TextureAttachment texA01 = fboGLAD.getColorbuffer(GL.GL_FRONT).getTextureAttachment();
163 final Texture tex = new Texture(texA01.getName(), false /* ownsTextureID */,
164 imgSeq.getTextureTarget(),
165 fboGLAD.getSurfaceWidth(), fboGLAD.getSurfaceHeight(),
166 fboGLAD.getSurfaceWidth(), fboGLAD.getSurfaceHeight(),
167 false /* mustFlipVertically */);
168 imgSeq.addFrame(gl, tex);
169 fboWidth = 0;
170 fboHeight = 0;
172 } else if( animateGLEL ) {
173 fboGLAD.display();
174 }
175
176 super.draw(gl, renderer);
177
178 if( animateGLEL ) {
179 markStateDirty(); // keep on going
180 }
181 }
182}
Abstract Outline shape representation define the method an OutlineShape(s) is bound and rendered.
Definition: Region.java:62
static final int AA_RENDERING_MASK
2-pass rendering bit-mask including MSAA_RENDERING_BIT and VBAA_RENDERING_BIT.
Definition: Region.java:118
final PMVMatrix4f getMatrix()
Borrow the current PMVMatrix4f.
final Recti getViewport(final Recti target)
Copies the current Rect4i viewport in given target and returns it for chaining.
Shape setColor(final float r, final float g, final float b, final float a)
Set base color.
Definition: Shape.java:1389
final void markStateDirty()
Marks the rendering state dirty, causing next draw() to notify the Graph region to reselect shader an...
Definition: Shape.java:696
Shape setPressedColorMod(final float r, final float g, final float b, final float a)
Set pressed color, modulating getColor() if isPressed().
Definition: Shape.java:1423
final Shape setToggleOnColorMod(final float r, final float g, final float b, final float a)
Set toggle-on color, modulating getColor() if isToggleOn() and setToggleable(boolean).
Definition: Shape.java:1437
final Shape setToggleOffColorMod(final float r, final float g, final float b, final float a)
Set toggle-off color, modulating getColor() if !isToggleOn() and setToggleable(boolean).
Definition: Shape.java:1451
final int[] getSurfaceSize(final PMVMatrix4f pmv, final Recti viewport, final int[] surfaceSize)
Retrieve surface (view) size in pixels of this shape.
Definition: Shape.java:1100
A GraphUI GLEventListener based TexSeqButton GraphShape.
Definition: GLButton.java:66
final void setFBOSize(final int fboWidth, final int fboHeight)
Definition: GLButton.java:93
final GLOffscreenAutoDrawable.FBO getFBOAutoDrawable()
Definition: GLButton.java:98
final void setAnimate(final boolean v)
Definition: GLButton.java:90
void draw(final GL2ES2 gl, final RegionRenderer renderer)
Renders the shape.
Definition: GLButton.java:107
void destroyImpl(final GL2ES2 gl, final RegionRenderer renderer)
Definition: GLButton.java:101
GLButton(final int renderModes, final float width, final float height, final int textureUnit, final GLEventListener glel, final boolean useAlpha)
Definition: GLButton.java:74
An abstract GraphUI TextureSequence BaseButton GraphShape.
void setAlphaBits(final int alphaBits)
Sets the number of bits requested for the color buffer's alpha component.
Specifies a set of OpenGL capabilities.
void setDoubleBuffered(final boolean enable)
Enables or disables double buffering.
void setFBO(final boolean enable)
Requesting offscreen FBO mode.
final GLProfile getGLProfile()
Returns the GL profile you desire or used by the drawable.
Abstraction for an OpenGL rendering context.
Definition: GLContext.java:74
abstract GLDrawable getGLDrawable()
Returns the write-drawable this context uses for framebuffer operations.
abstract GLOffscreenAutoDrawable createOffscreenAutoDrawable(AbstractGraphicsDevice device, GLCapabilitiesImmutable caps, GLCapabilitiesChooser chooser, int width, int height)
Creates a realized GLOffscreenAutoDrawable incl it's offscreen NativeSurface with the given capabilit...
static GLDrawableFactory getFactory(final GLProfile glProfile)
Returns the sole GLDrawableFactory instance.
Simple TextureSequence implementation allowing existing textures or image streams to be used and repl...
int getTextureTarget()
Returns the texture target used by implementation.
final TextureSequence.TextureFrame addFrame(final GL gl, final Texture tex)
int getTextureUnit()
Return the texture unit used to render the current frame.
Represents an OpenGL texture object.
Definition: Texture.java:173
AbstractGraphicsScreen getScreen()
Return the screen this graphics configuration is valid for.
AbstractGraphicsDevice getDevice()
Return the device this graphics configuration is valid for.
AbstractGraphicsConfiguration getGraphicsConfiguration()
Returns the graphics configuration corresponding to this window.
GLContext getContext()
Returns the GLContext associated which this GL object.
Specifies an immutable set of OpenGL capabilities.
An abstraction for an OpenGL rendering target.
Definition: GLDrawable.java:51
GLCapabilitiesImmutable getRequestedGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the user requested OpenGL capabilities (pixel fo...
NativeSurface getNativeSurface()
Returns the associated NativeSurface of this NativeSurfaceHolder.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
FBObject based GLOffscreenAutoDrawable specialization
Platform-independent GLAutoDrawable specialization, exposing offscreen functionality.
void setSharedContext(GLContext sharedContext)
Specifies an OpenGL context, which shall be shared by this GLAutoDrawable's GLContext.
static final int GL_FRONT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_FRONT" with expressio...
Definition: GL.java:597