JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
UIShapeDemo02a.java
Go to the documentation of this file.
1/**
2 * Copyright 2010-2024 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.util.InterruptSource;
33import com.jogamp.graph.curve.Region;
34import com.jogamp.graph.curve.opengl.GLRegion;
35import com.jogamp.graph.curve.opengl.RegionRenderer;
36import com.jogamp.graph.curve.opengl.RenderState;
37import com.jogamp.graph.ui.GraphShape;
38import com.jogamp.graph.ui.Scene;
39import com.jogamp.graph.ui.Shape;
40import com.jogamp.graph.ui.shapes.Rectangle;
41import com.jogamp.math.Recti;
42import com.jogamp.math.Vec3f;
43import com.jogamp.math.Vec4f;
44import com.jogamp.math.geom.AABBox;
45import com.jogamp.math.util.PMVMatrix4f;
46import com.jogamp.newt.event.KeyAdapter;
47import com.jogamp.newt.event.KeyEvent;
48import com.jogamp.newt.event.WindowAdapter;
49import com.jogamp.newt.event.WindowEvent;
50import com.jogamp.newt.opengl.GLWindow;
51import com.jogamp.opengl.GL;
52import com.jogamp.opengl.GL2ES2;
53import com.jogamp.opengl.GLAutoDrawable;
54import com.jogamp.opengl.GLCapabilities;
55import com.jogamp.opengl.GLEventAdapter;
56import com.jogamp.opengl.GLProfile;
57import com.jogamp.opengl.demos.graph.ui.testshapes.Glyph03FreeMonoRegular_M;
58import com.jogamp.opengl.demos.util.CommandlineOptions;
59import com.jogamp.opengl.util.Animator;
60
61/**
62 * Basic UIShape demo using a Scene and Shape
63 *
64 * Action Cursor-Keys:
65 * - With Shift : Move the clipping-rectangle itself
66 * - With Control: Resize Left and Bottom Clipping Edge of AABBox
67 * - No Modifiers: Resize Right and Top Clipping Edge of AABBox
68 */
69public class UIShapeDemo02a {
70 static CommandlineOptions options = new CommandlineOptions(1280, 720, Region.VBAA_RENDERING_BIT);
71
72 public static void main(final String[] args) throws IOException, InterruptedException {
73 boolean ok_shape = false;
74 boolean use_glyph = false;
75 if( 0 != args.length ) {
76 final int[] idx = { 0 };
77 for (idx[0] = 0; idx[0] < args.length; ++idx[0]) {
78 if( options.parse(args, idx) ) {
79 continue;
80 } else if(args[idx[0]].equals("-ok")) {
81 ok_shape = true;
82 } else if(args[idx[0]].equals("-glyph")) {
83 use_glyph = true;
84 }
85 }
86 }
87 System.err.println(options);
88 System.err.println("ok_shape "+ok_shape);
89 final GLProfile reqGLP = GLProfile.get(options.glProfileName);
90 System.err.println("GLProfile: "+reqGLP);
91
92 final Scene scene = new Scene(options.graphAASamples);
93 // scene.setPMVMatrixSetup(new Scene.DefaultPMVMatrixSetup(-1f)); // better distance for perspective action
94 scene.setPMVMatrixSetup(new MyMatrixSetup());
95 scene.setClearParams(new float[] { 1f, 1f, 1f, 1f}, GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
96 scene.setAAQuality(options.graphAAQuality);
97 final Animator animator = new Animator(0 /* w/o AWT */);
98
99 final GLCapabilities reqCaps = options.getGLCaps();
100 System.out.println("Requested: " + reqCaps);
101
102 final GLWindow window = GLWindow.create(reqCaps);
103 window.setSize(options.surface_width, options.surface_height);
104 window.setTitle(UIShapeDemo02a.class.getSimpleName()+": "+window.getSurfaceWidth()+" x "+window.getSurfaceHeight());
105 window.setVisible(true);
106 System.out.println("Chosen: " + window.getChosenGLCapabilities());
107 window.addGLEventListener(scene);
108 window.addWindowListener(new WindowAdapter() {
109 @Override
110 public void windowResized(final WindowEvent e) {
111 window.setTitle(UIShapeDemo02a.class.getSimpleName()+": "+window.getSurfaceWidth()+" x "+window.getSurfaceHeight());
112 }
113 @Override
114 public void windowDestroyNotify(final WindowEvent e) {
115 animator.stop();
116 }
117 });
118
119 final GraphShape testShape;
120 final Rectangle rectShape;
121 if( use_glyph ) {
122 testShape = new Glyph03FreeMonoRegular_M(options.renderModes);
123 testShape.setColor(0.8f, 0.8f, 0.8f, 1);
124 testShape.scale(1000, 1000, 1);
125 rectShape = null;
126 testShape.onDraw((final Shape s, final GL2ES2 gl, final RegionRenderer renderer) -> {
127 final GLRegion region = ((GraphShape)s).getRegion();
129 renderer.setColorStatic(new Vec4f(0, 0, 1, 1));
130 region.draw(gl, renderer);
132 return false;
133 });
134 scene.addShape(testShape);
135 } else {
136 testShape = null;
137 rectShape = new Rectangle(options.renderModes, 1, 1, 0);
138 rectShape.setColor(0, 0, 1, 1);
139 scene.addShape(rectShape);
140 }
141
142 scene.attachInputListenerTo(window);
143 window.addKeyListener(new KeyAdapter() {
144 @Override
145 public void keyPressed(final KeyEvent arg0) {
146 final short keySym = arg0.getKeySymbol();
147 if( keySym == KeyEvent.VK_F4 || keySym == KeyEvent.VK_ESCAPE || keySym == KeyEvent.VK_Q ) {
148 new InterruptSource.Thread( () -> { window.destroy(); } ).start();
149 }
150 }
151 });
152 window.addWindowListener(new WindowAdapter() {
153 @Override
154 public void windowDestroyed(final WindowEvent e) {
155 animator.stop();
156 }
157 });
158
159 animator.add(window);
160 animator.start();
161 scene.waitUntilDisplayed();
162 final AABBox sbox = scene.getBounds();
163 System.err.println("Scene "+sbox);
164 if( null != rectShape ) {
165 final float sw = sbox.getWidth();
166 final float sh = sbox.getHeight();
167 final float w, h, w2, lineWidth, delta;
168 {
169 w = 500f;
170 h = 500f;
171 lineWidth = ok_shape ? 1.0f : 1.1f;
172 delta = 1;
173 w2 = w-14*delta;
174 }
175 rectShape.setDimension(w2, h, lineWidth);
176 System.err.printf("R_0: w %30.30f x %30.30f%n", w2, h);
177 Thread.sleep(500);
178 if( false ) {
179 while( window.isNativeValid() ) {
180 for(int i=0; i<300; ++i) {
181 final float wi = w-i*delta;
182 rectShape.setDimension(wi, h, lineWidth);
183 System.err.printf("R_1: %d: w %30.30f x %30.30f%n", i, wi, h);
184 // System.err.println("R_1: "+rect);
185 Thread.sleep(17);
186 }
187 }
188 }
189 }
190 scene.screenshot(true, scene.nextScreenshotFile(null, UIShapeDemo02a.class.getSimpleName(), options.renderModes, reqCaps, null));
191 }
192
193 private static final class MyMatrixSetup implements Scene.PMVMatrixSetup {
194
195 @Override
196 public float getSceneDist() {
197 return -0.2f;
198 }
199
200 @Override
201 public float getAngle() {
202 return 0;
203 }
204
205 @Override
206 public float getZNear() {
207 return 0.1f;
208 }
209
210 @Override
211 public float getZFar() {
212 // return 7000f;
213 return 1f;
214 }
215
216 @Override
217 public void set(final PMVMatrix4f pmv, final Recti viewport) {
218 final float ratio = (float) viewport.width() / (float) viewport.height();
219 pmv.loadPIdentity();
220 pmv.orthoP(0, viewport.width(), 0, viewport.height(), getZNear(), getZFar());
221 // pmv.perspectiveP(FloatUtil.QUARTER_PI, ratio, getZNear(), getZFar());
222 pmv.translateP(0f, 0f, getSceneDist());
223
224 pmv.loadMvIdentity();
225 }
226
227 @Override
228 public void setPlaneBox(final AABBox planeBox, final PMVMatrix4f pmv, final Recti viewport) {
229 final float orthoDist = -getSceneDist();
230 final Vec3f obj00Coord = new Vec3f();
231 final Vec3f obj11Coord = new Vec3f();
232
233 Scene.winToPlaneCoord(pmv, viewport, getZNear(), getZFar(), viewport.x(), viewport.y(), orthoDist, obj00Coord);
234 Scene.winToPlaneCoord(pmv, viewport, getZNear(), getZFar(), viewport.width(), viewport.height(), orthoDist, obj11Coord);
235
236 planeBox.setSize( obj00Coord, obj11Coord );
237 }
238 }
239
240};
241
242
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
A GLRegion is the OGL binding of one or more OutlineShapes Defined by its vertices and generated tria...
Definition: GLRegion.java:70
final void draw(final GL2ES2 gl, final RegionRenderer renderer)
Renders the associated OGL objects specifying current width/hight of window for optional multi pass r...
Definition: GLRegion.java:518
final RenderState getRenderState()
Return the RenderState composition.
final void setColorStatic(final Vec4f rgbaColor)
The RenderState is owned by RegionRenderer.
final void clearDebugBits(final int mask)
final void setDebugBits(final int mask)
Graph based GLRegion Shape.
Definition: GraphShape.java:55
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
final void setPMVMatrixSetup(final PMVMatrixSetup setup)
Set a custom PMVMatrixSetup.
Definition: Scene.java:745
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
File nextScreenshotFile(final String dir, final String prefix, final int renderModes, final GLCapabilitiesImmutable caps, final String contentDetail)
Return the unique next technical screenshot PNG File instance as follows:
Definition: Scene.java:1434
Generic Shape, potentially using a Graph via GraphShape or other means of representing content.
Definition: Shape.java:87
Shape setColor(final float r, final float g, final float b, final float a)
Set base color.
Definition: Shape.java:1389
final void onDraw(final DrawListener l)
Set a user one-shot initializer callback or custom draw(GL2ES2, RegionRenderer) hook.
Definition: Shape.java:477
final Shape scale(final Vec3f s)
Multiply current scale factor by given scale.
Definition: Shape.java:661
A GraphUI rectangle GraphShape.
Definition: Rectangle.java:47
void setDimension(final float width, final float height, final float lineWidth)
Definition: Rectangle.java:124
4D Vector based upon four float components.
Definition: Vec4f.java:37
Axis Aligned Bounding Box.
Definition: AABBox.java:54
final float getWidth()
Definition: AABBox.java:879
final float getHeight()
Definition: AABBox.java:883
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
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 void addKeyListener(final KeyListener l)
Appends the given com.jogamp.newt.event.KeyListener to the end of the list.
Definition: GLWindow.java:902
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.
Specifies the the OpenGL profile.
Definition: GLProfile.java:77
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
Basic UIShape demo using a Scene and Shape.
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 synchronized boolean start()
Starts this animator, if not running.
Definition: Animator.java:344
final synchronized boolean stop()
Stops this animator.
Definition: Animator.java:368
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
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