JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
UISceneDemo11.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.IOException;
31
32import com.jogamp.common.os.Clock;
33import com.jogamp.graph.curve.Region;
34import com.jogamp.graph.font.Font;
35import com.jogamp.graph.font.FontFactory;
36import com.jogamp.graph.font.FontSet;
37import com.jogamp.graph.ui.Group;
38import com.jogamp.graph.ui.Scene;
39import com.jogamp.graph.ui.Shape;
40import com.jogamp.graph.ui.layout.Alignment;
41import com.jogamp.graph.ui.layout.Gap;
42import com.jogamp.graph.ui.layout.GridLayout;
43import com.jogamp.graph.ui.shapes.Button;
44import com.jogamp.math.Recti;
45import com.jogamp.math.geom.AABBox;
46import com.jogamp.math.util.PMVMatrix4f;
47import com.jogamp.newt.event.WindowAdapter;
48import com.jogamp.newt.event.WindowEvent;
49import com.jogamp.newt.opengl.GLWindow;
50import com.jogamp.opengl.GL;
51import com.jogamp.opengl.GLCapabilities;
52import com.jogamp.opengl.demos.util.CommandlineOptions;
53import com.jogamp.opengl.util.Animator;
54
55import jogamp.graph.ui.TreeTool;
56
57/**
58 * Res independent {@link Shape}s in a {@link Group} using a {@link GridLayout}, contained within a Scene attached to GLWindow.
59 * <p>
60 * Pass '-keep' to main-function to keep running after animation,
61 * then user can test Shape drag-move and drag-resize w/ 1-pointer.
62 * </p>
63 */
64public class UISceneDemo11 {
65 static CommandlineOptions options = new CommandlineOptions(1280, 720, Region.VBAA_RENDERING_BIT);
66
67 public static void main(final String[] args) throws IOException {
68 if( 0 != args.length ) {
69 final int[] idx = { 0 };
70 for (idx[0] = 0; idx[0] < args.length; ++idx[0]) {
71 if( options.parse(args, idx) ) {
72 continue;
73 }
74 }
75 }
76 System.err.println(options);
77
78 final GLCapabilities reqCaps = options.getGLCaps();
79 System.out.println("Requested: " + reqCaps);
80
81 //
82 // Resolution independent, no screen size
83 //
85 System.err.println("Font: "+font.getFullFamilyName());
86
87 final Group groupA0 = new Group(new GridLayout(2, 1f, 1/2f, Alignment.Fill, new Gap(0.10f)));
88 {
89 groupA0.addShape( new Button(options.renderModes, font, "r1 c1", 1f, 1f/2f).setPerp().setDragAndResizable(false) );
90 groupA0.addShape( new Button(options.renderModes, font, "r1 c2", 1f, 1f/2f).setPerp().setDragAndResizable(false) );
91 groupA0.addShape( new Button(options.renderModes, font, "r2 c1", 1f, 1f/2f).setPerp().setDragAndResizable(false) );
92 groupA0.addShape( new Button(options.renderModes, font, "r2 c2", 1f, 1f/2f).setPerp().setDragAndResizable(false) );
93 }
94 groupA0.setInteractive(true);
95 groupA0.scale(1/8f, 1/8f, 1);
96 groupA0.validate(reqCaps.getGLProfile());
97 System.err.println("Group-A0 "+groupA0);
98 System.err.println("Group-A0 Layout "+groupA0.getLayout());
99 TreeTool.forAll(groupA0, (shape) -> { System.err.println("Shape... "+shape); return false; });
100
101 final Scene scene = new Scene(options.graphAASamples);
102 scene.setPMVMatrixSetup(new MyPMVMatrixSetup());
103 scene.setClearParams(new float[] { 1f, 1f, 1f, 1f}, GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
104 scene.addShape(groupA0);
105 scene.setPMvCullingEnabled(true);
106
107 final Animator animator = new Animator(0 /* w/o AWT */);
108
109 final GLWindow window = GLWindow.create(reqCaps);
110 window.setSize(options.surface_width, options.surface_height);
111 window.setTitle(UISceneDemo11.class.getSimpleName()+": "+window.getSurfaceWidth()+" x "+window.getSurfaceHeight());
112 window.setVisible(true);
113 window.addGLEventListener(scene);
114 window.addWindowListener(new WindowAdapter() {
115 @Override
116 public void windowResized(final WindowEvent e) {
117 window.setTitle(UISceneDemo11.class.getSimpleName()+": "+window.getSurfaceWidth()+" x "+window.getSurfaceHeight());
118 }
119 @Override
120 public void windowDestroyNotify(final WindowEvent e) {
121 animator.stop();
122 }
123 });
124
125 scene.attachInputListenerTo(window);
126
127 animator.setUpdateFPSFrames(1*60, null); // System.err);
128 animator.add(window);
129 animator.start();
130
131 //
132 // After initial display we can use screen resolution post initial Scene.reshape(..)
133 // However, in this example we merely use the resolution to
134 // - Compute the animation values with DPI
135 scene.waitUntilDisplayed();
136
137 final AABBox sceneBox = scene.getBounds();
138 System.err.println("SceneBox "+sceneBox);
139 System.err.println("Group-A0 "+groupA0);
140 TreeTool.forAll(groupA0, (shape) -> { System.err.println("Shape... "+shape); return false; });
141 groupA0.moveTo(0, sceneBox.getMinY(), 0f); // move shape to min start position
142 try { Thread.sleep(1000); } catch (final InterruptedException e1) { }
143
144 final Shape mobileShape = groupA0;
145
146 if( true ) {
147 //
148 // Compute the metric animation values -> shape obj-velocity
149 //
150 final float min_obj = sceneBox.getMinX();
151 final float max_obj = sceneBox.getMaxX() - mobileShape.getScaledWidth();
152
153 final int[] shapeSizePx = mobileShape.getSurfaceSize(scene, new PMVMatrix4f(), new int[2]); // [px]
154 final float[] pixPerShapeUnit = mobileShape.getPixelPerShapeUnit(shapeSizePx, new float[2]); // [px]/[shapeUnit]
155
156 final float pixPerMM = window.getPixelsPerMM(new float[2])[0]; // [px]/[mm]
157 final float dist_px = scene.getWidth() - shapeSizePx[0]; // [px]
158 final float dist_m = dist_px/pixPerMM/1e3f; // [m]
159 final float velocity = 50/1e3f; // [m]/[s]
160 final float velocity_px = velocity * 1e3f * pixPerMM; // [px]/[s]
161 final float velovity_obj = velocity_px / pixPerShapeUnit[0]; // [shapeUnit]/[s]
162 final float exp_dur_s = dist_m / velocity; // [s]
163
164 System.err.println();
165 System.err.printf("Shape: %d x %d [pixel], %.4f px/shape_unit%n", shapeSizePx[0], shapeSizePx[1], pixPerShapeUnit[0]);
166 System.err.printf("Shape: %s%n", mobileShape);
167 System.err.println();
168 System.err.printf("Distance: %.0f pixel @ %.3f px/mm, %.3f mm%n", dist_px, pixPerMM, dist_m*1e3f);
169 System.err.printf("Velocity: %.3f mm/s, %.3f px/s, %.6f obj/s, expected travel-duration %.3f s%n",
170 velocity*1e3f, velocity_px, velovity_obj, exp_dur_s);
171
172 final long t0_us = Clock.currentNanos() / 1000; // [us]
173 long t1_us = t0_us;
174 mobileShape.moveTo(min_obj, sceneBox.getMinY(), 0f); // move shape to min start position
175 while( mobileShape.getPosition().x() < max_obj && window.isNativeValid() ) {
176 final long t2_us = Clock.currentNanos() / 1000;
177 final float dt_s = ( t2_us - t1_us ) / 1e6f;
178 t1_us = t2_us;
179
180 final float dx = velovity_obj * dt_s; // [shapeUnit]
181 // System.err.println("move ")
182
183 // Move on GL thread to have vsync for free
184 // Otherwise we would need to employ a sleep(..) w/ manual vsync
185 window.invoke(true, (drawable) -> {
186 mobileShape.move(dx, 0f, 0f);
187 return true;
188 });
189 }
190 mobileShape.moveTo(max_obj, sceneBox.getMinY(), 0f); // move shape to min start position
191 final float has_dur_s = ( ( Clock.currentNanos() / 1000 ) - t0_us ) / 1e6f; // [us]
192 System.err.printf("Actual travel-duration %.3f s, delay %.3f s%n", has_dur_s, has_dur_s-exp_dur_s);
193 System.err.println("Group-A0 bounds "+groupA0);
194 TreeTool.forAll(groupA0, (shape) -> { System.err.println("Shape... "+shape); return false; });
195 try { Thread.sleep(1000); } catch (final InterruptedException e1) { }
196 }
197 if( !options.stayOpen ) {
198 window.destroy();
199 }
200 }
201 static class MyPMVMatrixSetup extends Scene.DefaultPMVMatrixSetup {
202 @Override
203 public void set(final PMVMatrix4f pmv, final Recti viewport) {
204 super.set(pmv, viewport);
205
206 // Scale (back) to have normalized plane dimensions, 1 for the greater of width and height.
207 final AABBox planeBox0 = new AABBox();
208 setPlaneBox(planeBox0, pmv, viewport);
209 final float sx = planeBox0.getWidth();
210 final float sy = planeBox0.getHeight();
211 final float sxy = sx > sy ? sx : sy;
212 pmv.scaleP(sxy, sxy, 1f);
213 }
214 };
215}
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}.
Group of Shapes, optionally utilizing a Group.Layout.
Definition: Group.java:61
void addShape(final Shape s)
Adds a Shape.
Definition: Group.java:225
Layout getLayout()
Return current Group.Layout.
Definition: Group.java:150
GraphUI Scene.
Definition: Scene.java:102
void addShape(final Shape s)
Adds a Shape.
Definition: Scene.java:287
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 setPMvCullingEnabled(final boolean v)
Enable or disable Project-Modelview (PMv) frustum culling per Shape for this container.
Definition: Scene.java:230
void waitUntilDisplayed()
Blocks until first display(GLAutoDrawable) has completed after construction or dispose(GLAutoDrawable...
Definition: Scene.java:584
final void setPMVMatrixSetup(final PMVMatrixSetup setup)
Set a custom PMVMatrixSetup.
Definition: Scene.java:745
int getWidth()
Returns the getViewport()'s width, set after initial reshape(GLAutoDrawable, int, int,...
Definition: Scene.java:774
AABBox getBounds(final PMVMatrix4f pmv, final Shape shape)
Returns AABBox dimension of given Shape from this container's perspective, i.e.
Definition: Scene.java:676
synchronized void attachInputListenerTo(final GLWindow window)
Definition: Scene.java:246
Generic Shape, potentially using a Graph via GraphShape or other means of representing content.
Definition: Shape.java:87
final Shape move(final float dtx, final float dty, final float dtz)
Move about scaled distance.
Definition: Shape.java:557
final Shape setInteractive(final boolean v)
Set whether this shape is interactive in general, i.e.
Definition: Shape.java:1711
final Shape moveTo(final float tx, final float ty, final float tz)
Move to scaled position.
Definition: Shape.java:543
final float getScaledWidth()
Returns the scaled width of the bounding AABBox for this shape.
Definition: Shape.java:745
final Vec3f getPosition()
Returns position Vec3f reference, i.e.
Definition: Shape.java:587
final float[] getPixelPerShapeUnit(final int[] shapeSizePx, final float[] pixPerShape)
Retrieve pixel per scaled shape-coordinate unit, i.e.
Definition: Shape.java:1165
final Shape setDragAndResizable(final boolean v)
Set whether this shape is draggable and resizable.
Definition: Shape.java:1801
final Shape validate(final GL2ES2 gl)
Validates the shape's underlying GLRegion.
Definition: Shape.java:850
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
final Shape scale(final Vec3f s)
Multiply current scale factor by given scale.
Definition: Shape.java:661
Immutable layout alignment options, including Bit#Fill.
Definition: Alignment.java:35
static final Alignment Fill
Bit#Fill alignment constant.
Definition: Alignment.java:43
GraphUI CSS property Gap, scaled spacing between (grid) cells not belonging to the cell element.
Definition: Gap.java:38
GraphUI Grid Group.Layout.
Definition: GridLayout.java:56
BaseButton setPerp()
Sets a perpendicular corner.
A GraphUI text labeled BaseButton GraphShape.
Definition: Button.java:61
Rectangle with x, y, width and height integer components.
Definition: Recti.java:34
Axis Aligned Bounding Box.
Definition: AABBox.java:54
final float getWidth()
Definition: AABBox.java:879
final float getHeight()
Definition: AABBox.java:883
PMVMatrix4f implements the basic computer graphics Matrix4f pack using projection (P),...
NEWT Window events are provided for notification purposes ONLY.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
Definition: GLWindow.java:121
final int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:466
final void setTitle(final String title)
Definition: GLWindow.java:297
final float[] getPixelsPerMM(final float[] ppmmStore)
Returns the pixels per millimeter of this window's NativeSurface according to the main monitor's curr...
Definition: GLWindow.java:520
final int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
Definition: GLWindow.java:461
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
Definition: GLWindow.java:625
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
Definition: GLWindow.java:615
final void addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
Definition: GLWindow.java:882
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
Definition: GLWindow.java:605
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 Shapes in a Group using a GridLayout, contained within a Scene attached to GLWindow.
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.
boolean invoke(boolean wait, GLRunnable glRunnable)
Enqueues a one-shot GLRunnable, which will be executed within the next display() call after all regis...
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
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