JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
UISceneDemo01b.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.Scene;
38import com.jogamp.graph.ui.Shape;
39import com.jogamp.graph.ui.shapes.Button;
40import com.jogamp.graph.ui.shapes.GLButton;
41import com.jogamp.math.FloatUtil;
42import com.jogamp.math.Quaternion;
43import com.jogamp.math.geom.AABBox;
44import com.jogamp.math.util.PMVMatrix4f;
45import com.jogamp.newt.event.MouseEvent;
46import com.jogamp.newt.event.WindowAdapter;
47import com.jogamp.newt.event.WindowEvent;
48import com.jogamp.newt.opengl.GLWindow;
49import com.jogamp.opengl.GL;
50import com.jogamp.opengl.GLCapabilities;
51import com.jogamp.opengl.demos.util.CommandlineOptions;
52import com.jogamp.opengl.util.Animator;
53
54/**
55 * Res independent Shape, Scene attached to GLWindow showing simple linear Shape movement.
56 * <p>
57 * This variation of {@link UISceneDemo00} uses a {@link GLButton} shape with animating and rotating gears
58 * using the default {@link Scene.PMVMatrixSetup}.
59 * </p>
60 * <p>
61 * Pass '-keep' to main-function to keep running after animation,
62 * then user can test Shape drag-move and drag-resize w/ 1-pointer.
63 * </p>
64 */
65public class UISceneDemo01b {
66 static CommandlineOptions options = new CommandlineOptions(1280, 720, Region.VBAA_RENDERING_BIT);
67
68 public static void main(final String[] args) throws IOException {
69 options.parse(args);
70 System.err.println(options);
71 final GLCapabilities reqCaps = options.getGLCaps();
72 System.out.println("Requested: " + reqCaps);
73
74 //
75 // Resolution independent, no screen size
76 //
78 System.err.println("Font: "+font.getFullFamilyName());
79
80 final Shape shape = new Button(options.renderModes, font, "L", 1/8f, 1/8f/2.5f).setPerp(); // normalized: 1 is 100% surface size (width and/or height)
81 {
82 final Quaternion q = shape.getRotation().copy();
85 shape.setRotation(q);
86 }
87 System.err.println("Shape bounds "+shape.getBounds(reqCaps.getGLProfile()));
88 System.err.println("Shape "+shape);
89
90 final Scene scene = new Scene(options.graphAASamples);
91 scene.setClearParams(new float[] { 1f, 1f, 1f, 1f}, GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
92 scene.addShape(shape);
93 scene.setAAQuality(options.graphAAQuality);
94
95 final Animator animator = new Animator(0 /* w/o AWT */);
96
97 final GLWindow window = GLWindow.create(reqCaps);
98 window.setSize(options.surface_width, options.surface_height);
99 window.setTitle(UISceneDemo01b.class.getSimpleName()+": "+window.getSurfaceWidth()+" x "+window.getSurfaceHeight());
100 window.setVisible(true);
101 window.addGLEventListener(scene);
102 window.addWindowListener(new WindowAdapter() {
103 @Override
104 public void windowResized(final WindowEvent e) {
105 window.setTitle(UISceneDemo01b.class.getSimpleName()+": "+window.getSurfaceWidth()+" x "+window.getSurfaceHeight());
106 }
107 @Override
108 public void windowDestroyNotify(final WindowEvent e) {
109 animator.stop();
110 }
111 });
112
113 scene.attachInputListenerTo(window);
114
115 animator.setUpdateFPSFrames(1*60, null); // System.err);
116 animator.add(window);
117 animator.start();
118
119 //
120 // After initial display we can use screen resolution post initial Scene.reshape(..)
121 // However, in this example we merely use the resolution to
122 // - Compute the animation values with DPI
123 scene.waitUntilDisplayed();
124
125 final AABBox sceneBox = scene.getBounds();
126 System.err.println("SceneBox "+sceneBox);
127 System.err.println("Shape "+shape);
128 shape.scale(sceneBox.getWidth(), sceneBox.getWidth(), 1f);
129 System.err.println("Shape "+shape);
130 try { Thread.sleep(1000); } catch (final InterruptedException e1) { }
131
132 //
133 // Compute the metric animation values -> shape obj-velocity
134 //
135 final float min_obj = sceneBox.getMinX();
136 final float max_obj = sceneBox.getMaxX() - shape.getScaledWidth();
137
138 final int[] shapeSizePx = shape.getSurfaceSize(scene, new PMVMatrix4f(), new int[2]); // [px]
139 final float[] pixPerShapeUnit = shape.getPixelPerShapeUnit(shapeSizePx, new float[2]); // [px]/[shapeUnit]
140
141 final float pixPerMM = window.getPixelsPerMM(new float[2])[0]; // [px]/[mm]
142 final float dist_px = scene.getWidth() - shapeSizePx[0]; // [px]
143 final float dist_m = dist_px/pixPerMM/1e3f; // [m]
144 final float velocity = 50/1e3f; // [m]/[s]
145 final float velocity_px = velocity * 1e3f * pixPerMM; // [px]/[s]
146 final float velocity_obj = velocity_px / pixPerShapeUnit[0]; // [shapeUnit]/[s]
147 final float exp_dur_s = dist_m / velocity; // [s]
148
149 System.err.println();
150 System.err.printf("Shape: %d x %d [pixel], %.4f px/shape_unit%n", shapeSizePx[0], shapeSizePx[1], pixPerShapeUnit[0]);
151 System.err.printf("Shape: %s%n", shape);
152 System.err.println();
153 System.err.printf("Distance: %.0f pixel @ %.3f px/mm, %.3f mm%n", dist_px, pixPerMM, dist_m*1e3f);
154 System.err.printf("Velocity: %.3f mm/s, %.3f px/s, %.6f obj/s, expected travel-duration %.3f s%n",
155 velocity*1e3f, velocity_px, velocity_obj, exp_dur_s);
156
158 @Override
159 public void mouseMoved(final MouseEvent e) {
160 final Shape.EventInfo shapeEvent = (Shape.EventInfo) e.getAttachment();
161 System.err.println("MouseOver "+shapeEvent);
162 // System.err.println("MouseOver "+shape.getPosition());
163 System.err.println();
164 }
165 @Override
166 public void mouseWheelMoved(final MouseEvent e) {
167 if( !e.isShiftDown() ) {
168 final float rad = e.getRotation()[1] < 0f ? FloatUtil.adegToRad(-10f) : FloatUtil.adegToRad(10f);
169 if( e.isAltDown() ) {
170 shape.getRotation().rotateByAngleZ(rad);
171 } else if( e.isControlDown() ) {
172 shape.getRotation().rotateByAngleX(rad);
173 } else {
174 shape.getRotation().rotateByAngleY(rad);
175 }
176 System.err.println("Shape "+shape);
177 final PMVMatrix4f pmv = new PMVMatrix4f();
178 shape.applyMatToMv(pmv);
179 System.err.println("Shape "+pmv);
180 }
181 }
182 });
183 final long t0_us = Clock.currentNanos() / 1000; // [us]
184 long t1_us = t0_us;
185 shape.moveTo(min_obj, 0f, 0f); // move shape to min start position
186 System.err.println("Shape Move: "+min_obj+" -> "+max_obj);
187 System.err.println("Shape Start Pos: "+shape);
188 try { Thread.sleep(1000); } catch (final InterruptedException e1) { }
189 while( shape.getPosition().x() < max_obj && window.isNativeValid() ) {
190 final long t2_us = Clock.currentNanos() / 1000;
191 final float dt_s = ( t2_us - t1_us ) / 1e6f;
192 t1_us = t2_us;
193
194 final float dx = velocity_obj * dt_s; // [shapeUnit]
195 // System.err.println("move ")
196
197 // Move on GL thread to have vsync for free
198 // Otherwise we would need to employ a sleep(..) w/ manual vsync
199 window.invoke(true, (drawable) -> {
200 shape.move(dx, 0f, 0f);
201 System.err.println("Moved: "+shape);
202 return true;
203 });
204 }
205 final float has_dur_s = ( ( Clock.currentNanos() / 1000 ) - t0_us ) / 1e6f; // [us]
206 System.err.printf("Actual travel-duration %.3f s, delay %.3f s%n", has_dur_s, has_dur_s-exp_dur_s);
207 System.err.println("Shape End Pos: "+shape);
208 try { Thread.sleep(1000); } catch (final InterruptedException e1) { }
209 if( !options.stayOpen ) {
210 window.destroy();
211 }
212 }
213}
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
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
int setAAQuality(final int v)
Sets RegionRenderer#setAAQuality(int).
Definition: Scene.java:383
void waitUntilDisplayed()
Blocks until first display(GLAutoDrawable) has completed after construction or dispose(GLAutoDrawable...
Definition: Scene.java:584
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
Shape event info for propagated NEWTEvents containing reference of the intended shape as well as the ...
Definition: Shape.java:1896
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 Shape move(final float dtx, final float dty, final float dtz)
Move about scaled distance.
Definition: Shape.java:557
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 AABBox getBounds()
Returns the unscaled bounding AABBox for this shape, borrowing internal instance.
Definition: Shape.java:732
final Quaternion getRotation()
Returns Quaternion for rotation.
Definition: Shape.java:595
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 addMouseListener(final MouseGestureListener l)
Definition: Shape.java:1807
final Shape setRotation(final Quaternion q)
Sets the rotation Quaternion.
Definition: Shape.java:604
final void applyMatToMv(final PMVMatrix4f pmv)
Applies the internal Matrix4f to the given modelview matrix, i.e.
Definition: Shape.java:908
BaseButton setPerp()
Sets a perpendicular corner.
A GraphUI text labeled BaseButton GraphShape.
Definition: Button.java:61
Basic Float math utility functions.
Definition: FloatUtil.java:83
static final float PI
The value PI, i.e.
static float adegToRad(final float arc_degree)
Converts arc-degree to radians.
Quaternion implementation supporting Gimbal-Lock free rotations.
Definition: Quaternion.java:45
Quaternion rotateByAngleY(final float angle)
Rotate this quaternion around Y axis with the given angle in radians.
Quaternion rotateByAngleZ(final float angle)
Rotate this quaternion around Z axis with the given angle in radians.
Quaternion rotateByAngleX(final float angle)
Rotate this quaternion around X axis with the given angle in radians.
Axis Aligned Bounding Box.
Definition: AABBox.java:54
final float getWidth()
Definition: AABBox.java:879
PMVMatrix4f implements the basic computer graphics Matrix4f pack using projection (P),...
final boolean isShiftDown()
getModifiers() contains SHIFT_MASK.
final boolean isControlDown()
getModifiers() contains CTRL_MASK.
final boolean isAltDown()
getModifiers() contains ALT_MASK.
Pointer event of type PointerType.
Definition: MouseEvent.java:74
final float[] getRotation()
Returns a 3-component float array filled with the values of the rotational axis in the following orde...
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 Shape, Scene attached to GLWindow showing simple linear Shape movement.
int graphAASamples
Sample count for Graph Region AA render-modes: Region#VBAA_RENDERING_BIT or Region#MSAA_RENDERING_BIT...
int graphAAQuality
Pass2 AA-quality rendering for Graph Region AA render-modes: VBAA_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