JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
UISceneDemo10.java
Go to the documentation of this file.
1/**
2 * Copyright 2010-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.opengl.demos.graph.ui;
29
30import java.io.File;
31import java.io.IOException;
32import java.net.URISyntaxException;
33
34import com.jogamp.common.net.Uri;
35import com.jogamp.common.util.InterruptSource;
36import com.jogamp.graph.curve.Region;
37import com.jogamp.graph.font.Font;
38import com.jogamp.graph.font.FontFactory;
39import com.jogamp.graph.font.FontSet;
40import com.jogamp.graph.ui.Scene;
41import com.jogamp.graph.ui.Shape;
42import com.jogamp.graph.ui.shapes.Button;
43import com.jogamp.graph.ui.shapes.CrossHair;
44import com.jogamp.graph.ui.shapes.GLButton;
45import com.jogamp.graph.ui.shapes.MediaButton;
46import com.jogamp.math.Recti;
47import com.jogamp.math.Vec3f;
48import com.jogamp.math.geom.AABBox;
49import com.jogamp.math.util.PMVMatrix4f;
50import com.jogamp.newt.Window;
51import com.jogamp.newt.event.KeyAdapter;
52import com.jogamp.newt.event.KeyEvent;
53import com.jogamp.newt.event.MouseEvent;
54import com.jogamp.newt.event.WindowAdapter;
55import com.jogamp.newt.event.WindowEvent;
56import com.jogamp.newt.opengl.GLWindow;
57import com.jogamp.opengl.GL;
58import com.jogamp.opengl.GLCapabilities;
59import com.jogamp.opengl.GLEventListener;
60import com.jogamp.opengl.demos.es2.GearsES2;
61import com.jogamp.opengl.demos.util.CommandlineOptions;
62import com.jogamp.opengl.util.Animator;
63import com.jogamp.opengl.util.av.GLMediaPlayer;
64import com.jogamp.opengl.util.av.GLMediaPlayerFactory;
65
66/**
67 * Res independent Shape, in Scene attached to GLWindow w/ listener attached.
68 * <p>
69 * User can test Shape drag-move and drag-resize w/ 1-pointer
70 * </p>
71 */
72public class UISceneDemo10 {
73 static final boolean DEBUG = false;
74 static final boolean TRACE = false;
75
76 static CommandlineOptions options = new CommandlineOptions(1280, 720, Region.VBAA_RENDERING_BIT);
77
78 static private final String defaultMediaPath = "http://archive.org/download/BigBuckBunny_328/BigBuckBunny_512kb.mp4";
79 static private String filmPath = defaultMediaPath;
80
81 public static void main(final String[] args) throws IOException {
82 Font font = null;
83
84 if( 0 != args.length ) {
85 final int[] idx = { 0 };
86 for (idx[0] = 0; idx[0] < args.length; ++idx[0]) {
87 if( options.parse(args, idx) ) {
88 continue;
89 } else if(args[idx[0]].equals("-font")) {
90 ++idx[0];
91 font = FontFactory.get(new File(args[idx[0]]));
92 } else if(args[idx[0]].equals("-film")) {
93 ++idx[0];
94 filmPath = args[idx[0]];
95 }
96 }
97 }
98 System.err.println(options);
99
100 final GLCapabilities reqCaps = options.getGLCaps();
101 System.out.println("Requested: " + reqCaps);
102
103 final GLWindow window = GLWindow.create(reqCaps);
104
105 //
106 // Resolution independent, no screen size
107 //
108 if( null == font ) {
110 }
111 System.err.println("Font: "+font.getFullFamilyName());
112 final Shape shape = makeShape(window, font, options.renderModes);
113 System.err.println("m0 shape bounds "+shape.getBounds(reqCaps.getGLProfile()));
114 System.err.println("m0 "+shape);
115
116 // Scene for Shape ...
117 final Scene scene = new Scene(options.graphAASamples);
118 scene.setPMVMatrixSetup(new MyPMVMatrixSetup());
119 scene.setClearParams(new float[] { 1f, 1f, 1f, 1f}, GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
120
121 shape.onMove((final Shape s, final Vec3f origin, final Vec3f dest, MouseEvent e) -> {
122 final Vec3f p = shape.getPosition();
123 System.err.println("Shape moved: "+origin+" -> "+p);
124 } );
126 @Override
127 public void mouseMoved(final MouseEvent e) {
128 final Recti viewport = scene.getViewport(new Recti());
129 // flip to GL window coordinates, origin bottom-left
130 final int glWinX = e.getX();
131 final int glWinY = viewport.height() - e.getY() - 1;
132 testProject(scene, shape, glWinX, glWinY);
133 }
134 @Override
135 public void mouseDragged(final MouseEvent e) {
136 final Recti viewport = scene.getViewport(new Recti());
137 // flip to GL window coordinates, origin bottom-left
138 final int glWinX = e.getX();
139 final int glWinY = viewport.height() - e.getY() - 1;
140 testProject(scene, shape, glWinX, glWinY);
141 }
142 } );
143 scene.addShape(shape);
144
145 window.setSize(options.surface_width, options.surface_height);
146 window.setTitle(UISceneDemo10.class.getSimpleName()+": "+window.getSurfaceWidth()+" x "+window.getSurfaceHeight());
147 window.setVisible(true);
148 window.addGLEventListener(scene);
149 scene.attachInputListenerTo(window);
150
151 final Animator animator = new Animator(0 /* w/o AWT */);
152 animator.setUpdateFPSFrames(5*60, null);
153 animator.add(window);
154
155 window.addKeyListener(new KeyAdapter() {
156 @Override
157 public void keyPressed(final KeyEvent arg0) {
158 final short keySym = arg0.getKeySymbol();
159 if( keySym == KeyEvent.VK_F4 || keySym == KeyEvent.VK_ESCAPE || keySym == KeyEvent.VK_Q ) {
160 new InterruptSource.Thread( () -> { window.destroy(); } ).start();
161 }
162 }
163 });
164 window.addWindowListener(new WindowAdapter() {
165 @Override
166 public void windowResized(final WindowEvent e) {
167 window.setTitle(UISceneDemo10.class.getSimpleName()+": "+window.getSurfaceWidth()+" x "+window.getSurfaceHeight());
168 }
169 @Override
170 public void windowDestroyed(final WindowEvent e) {
171 animator.stop();
172 }
173 });
174
175 animator.start();
176
177 //
178 // After initial display we can use screen resolution post initial Scene.reshape(..)
179 // However, in this example we merely use the resolution to
180 // - Scale the shape to the sceneBox, i.e. normalizing to screen-size 1x1
181 scene.waitUntilDisplayed();
182
183 System.err.println("m1.1 Scene "+scene.getBounds());
184 System.err.println("m1.1 "+shape);
185 try { Thread.sleep(1000); } catch (final InterruptedException e1) { }
186
187 System.err.println("You may test moving the Shape by dragging the shape with 1-pointer.");
188 System.err.println("You may test resizing the Shape by dragging the shape on 1/5th of the bottom-left or bottom-right corner with 1-pointer.");
189 System.err.println("Press F4 or 'window close' to exit ..");
190 }
191
192 static void testProject(final Scene scene, final Shape shape, final int glWinX, final int glWinY) {
193 final PMVMatrix4f pmv = new PMVMatrix4f();
194 final Vec3f objPos = shape.winToShapeCoord(scene.getPMVMatrixSetup(), scene.getViewport(), glWinX, glWinY, pmv, new Vec3f());
195 System.err.printf("MM1: winToObjCoord: obj %s%n", objPos);
196 final int[] glWinPos = shape.shapeToWinCoord(scene.getPMVMatrixSetup(), scene.getViewport(), objPos, pmv, new int[2]);
197 final int windx = glWinPos[0]-glWinX;
198 final int windy = glWinPos[1]-glWinY;
199 System.err.printf("MM2: objToWinCoord: winCoords %d / %d, diff %d x %d%n", glWinPos[0], glWinPos[1], windx, windy);
200 }
201
202 @SuppressWarnings("unused")
203 static Shape makeShape(final Window window, final Font font, final int renderModes) {
204 final float sw = 0.25f;
205 final float sh = sw / 2.5f;
206
207 if( false ) {
208 Uri filmUri;
209 try {
210 filmUri = Uri.cast( filmPath );
211 } catch (final URISyntaxException e1) {
212 throw new RuntimeException(e1);
213 }
214 final GLMediaPlayer mPlayer = GLMediaPlayerFactory.createDefault();
215 // mPlayer.setTextureUnit(texUnitMediaPlayer);
216 final MediaButton b = new MediaButton(renderModes, sw, sh, mPlayer);
217 b.setVerbose(false);
218 b.addDefaultEventListener();
219 b.setToggleable(true);
220 b.setToggle(true);
221 b.setToggleOffColorMod(0f, 1f, 0f, 1.0f);
222 b.addMouseListener(new Shape.MouseGestureAdapter() {
223 @Override
224 public void mouseClicked(final MouseEvent e) {
225 mPlayer.setAudioVolume( b.isToggleOn() ? 1f : 0f );
226 } } );
227 mPlayer.playStream(filmUri, GLMediaPlayer.STREAM_ID_AUTO, GLMediaPlayer.STREAM_ID_AUTO, GLMediaPlayer.STREAM_ID_NONE, GLMediaPlayer.TEXTURE_COUNT_DEFAULT);
228 return b;
229 } else if( true ) {
230 final GLEventListener glel;
231 {
232 final GearsES2 gears = new GearsES2(0);
233 gears.setVerbose(false);
234 gears.setClearColor(new float[] { 0.9f, 0.9f, 0.9f, 1f } );
235 window.addKeyListener(gears.getKeyListener());
236 glel = gears;
237 }
238 final int texUnit = 1;
239 final GLButton b = new GLButton(renderModes, sw,
240 sh, texUnit, glel, false /* useAlpha */);
241 b.setToggleable(true);
242 b.setToggle(true); // toggle == true -> animation
243 b.setAnimate(true);
244 b.addMouseListener(new Shape.MouseGestureAdapter() {
245 @Override
246 public void mouseClicked(final MouseEvent e) {
247 b.setAnimate( b.isToggleOn() );
248 } } );
249 return b;
250 } else if( true ){
251 return new Button(renderModes, font, "+", sw, sh).setPerp();
252 } else {
253 final CrossHair b = new CrossHair(renderModes, sw, sw, 1f/100f);
254 return b;
255 }
256 }
257 static class MyPMVMatrixSetup extends Scene.DefaultPMVMatrixSetup {
258 @Override
259 public void set(final PMVMatrix4f pmv, final Recti viewport) {
260 super.set(pmv, viewport);
261
262 // Scale (back) to have normalized plane dimensions, 1 for the greater of width and height.
263 final AABBox planeBox0 = new AABBox();
264 setPlaneBox(planeBox0, pmv, viewport);
265 final float sx = planeBox0.getWidth();
266 final float sy = planeBox0.getHeight();
267 final float sxy = sx > sy ? sx : sy;
268 pmv.scaleP(sxy, sxy, 1f);
269 }
270 };
271
272}
Abstract Outline shape representation define the method an OutlineShape(s) is bound and rendered.
Definition: Region.java:62
static final int VBAA_RENDERING_BIT
Rendering-Mode bit for Region.
Definition: Region.java:115
The optional property jogamp.graph.font.ctor allows user to specify the FontConstructor implementatio...
static final FontSet get(final int font)
static final int UBUNTU
Ubuntu is the default font family, {@value}.
GraphUI Scene.
Definition: Scene.java:102
final void setClearParams(final float[] clearColor, final int clearMask)
Sets the clear parameter for glClearColor(..) and glClear(..) to be issued at display(GLAutoDrawable)...
Definition: Scene.java:221
final void setPMVMatrixSetup(final PMVMatrixSetup setup)
Set a custom PMVMatrixSetup.
Definition: Scene.java:745
final Recti getViewport(final Recti target)
Copies the current int[4] viewport in given target and returns it for chaining.
Definition: Scene.java:768
Convenient adapter combining dummy implementation for MouseListener and GestureListener.
Definition: Shape.java:1884
Generic Shape, potentially using a Graph via GraphShape or other means of representing content.
Definition: Shape.java:87
final Vec3f getPosition()
Returns position Vec3f reference, i.e.
Definition: Shape.java:587
final AABBox getBounds()
Returns the unscaled bounding AABBox for this shape, borrowing internal instance.
Definition: Shape.java:732
final Shape addMouseListener(final MouseGestureListener l)
Definition: Shape.java:1807
final Shape setVisible(final boolean v)
Enable (default) or disable this shape's visibility.
Definition: Shape.java:363
final void onMove(final MoveListener l)
Set user callback to be notified when shape is move(Vec3f)'ed.
Definition: Shape.java:485
Rectangle with x, y, width and height integer components.
Definition: Recti.java:34
3D Vector based upon three float components.
Definition: Vec3f.java:37
static final short VK_F4
Constant for the F4 function key.
Definition: KeyEvent.java:686
static final short VK_ESCAPE
Constant for the ESCAPE function key.
Definition: KeyEvent.java:485
final short getKeySymbol()
Returns the virtual key symbol reflecting the current keyboard layout.
Definition: KeyEvent.java:176
static final short VK_Q
See VK_A.
Definition: KeyEvent.java:627
Pointer event of type PointerType.
Definition: MouseEvent.java:74
final int getY()
See details for multiple-pointer events.
final int getX()
See details for multiple-pointer events.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Definition: GLWindow.java:169
Specifies a set of OpenGL capabilities.
final GLProfile getGLProfile()
Returns the GL profile you desire or used by the drawable.
Res independent Shape, in Scene attached to GLWindow w/ listener attached.
static void main(final String[] args)
int graphAASamples
Sample count for Graph Region AA render-modes: Region#VBAA_RENDERING_BIT or Region#MSAA_RENDERING_BIT...
final synchronized void add(final GLAutoDrawable drawable)
Adds a drawable to this animator's list of rendering drawables.
final void setUpdateFPSFrames(final int frames, final PrintStream out)
final synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368
static final int FAMILY_LIGHT
Font family LIGHT, {@value}.
Definition: FontSet.java:39
Font get(int family, int stylebits)
static final int STYLE_SERIF
SERIF style/family bit flag.
Definition: FontSet.java:54
Interface wrapper for font implementation.
Definition: Font.java:60
String getFullFamilyName()
Shall return the family and subfamily name, separated a dash.
static final int GL_DEPTH_BUFFER_BIT
GL_ES_VERSION_2_0, GL_VERSION_1_1, GL_VERSION_1_0, GL_VERSION_ES_1_0 Define "GL_DEPTH_BUFFER_BIT" wit...
Definition: GL.java:738