JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
UIShapeClippingDemo01.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.RegionRenderer;
35import com.jogamp.graph.ui.GraphShape;
36import com.jogamp.graph.ui.Group;
37import com.jogamp.graph.ui.Scene;
38import com.jogamp.graph.ui.Shape;
39import com.jogamp.graph.ui.shapes.Rectangle;
40import com.jogamp.math.FloatUtil;
41import com.jogamp.math.Vec2f;
42import com.jogamp.math.Vec3f;
43import com.jogamp.math.geom.AABBox;
44import com.jogamp.math.geom.Cube;
45import com.jogamp.math.geom.Frustum;
46import com.jogamp.math.util.PMVMatrix4f;
47import com.jogamp.newt.event.KeyAdapter;
48import com.jogamp.newt.event.KeyEvent;
49import com.jogamp.newt.event.MouseEvent;
50import com.jogamp.newt.event.WindowAdapter;
51import com.jogamp.newt.event.WindowEvent;
52import com.jogamp.newt.opengl.GLWindow;
53import com.jogamp.opengl.GL;
54import com.jogamp.opengl.GLAutoDrawable;
55import com.jogamp.opengl.GLCapabilities;
56import com.jogamp.opengl.GLEventListener;
57import com.jogamp.opengl.GLProfile;
58import com.jogamp.opengl.demos.util.CommandlineOptions;
59import com.jogamp.opengl.util.Animator;
60
61/**
62 * Basic UIShape Clipping demo using a Scene and Shape within a clipping Group
63 */
65 static CommandlineOptions options = new CommandlineOptions(1280, 720, Region.VBAA_RENDERING_BIT);
66
67 public static void main(final String[] args) throws IOException {
68 boolean _useFixedSize = true;
69 if( 0 != args.length ) {
70 final int[] idx = { 0 };
71 for (idx[0] = 0; idx[0] < args.length; ++idx[0]) {
72 if( options.parse(args, idx) ) {
73 continue;
74 } else if(args[idx[0]].equals("-NoFixedSize")) {
75 _useFixedSize = false;
76 }
77 }
78 }
79 final boolean useFixedSize = _useFixedSize;
80 System.err.println(options);
81 System.err.println("useFixedSize "+useFixedSize);
82 final GLProfile reqGLP = GLProfile.get(options.glProfileName);
83 System.err.println("GLProfile: "+reqGLP);
84
85 // Resolution independent, no screen size
86 //
87 final Scene scene = new Scene(options.graphAASamples);
88 scene.setPMVMatrixSetup(new Scene.DefaultPMVMatrixSetup(-1f)); // better distance for perspective action
89 scene.setClearParams(new float[] { 1f, 1f, 1f, 1f}, GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
90 scene.setAAQuality(options.graphAAQuality);
91 final Animator animator = new Animator(0 /* w/o AWT */);
92
93 final GLCapabilities reqCaps = options.getGLCaps();
94 System.out.println("Requested: " + reqCaps);
95
96 final GLWindow window = GLWindow.create(reqCaps);
97 window.setSize(options.surface_width, options.surface_height);
98 window.setTitle(UIShapeClippingDemo01.class.getSimpleName()+": "+window.getSurfaceWidth()+" x "+window.getSurfaceHeight());
99 window.setVisible(true);
100 System.out.println("Chosen: " + window.getChosenGLCapabilities());
101 window.addGLEventListener(scene);
103 GraphShape shape = null;
104 Group contentBox = null;
105
106 @Override
107 public void init(final GLAutoDrawable drawable) {
108 final AABBox sbox = scene.getBounds();
109 System.err.println("Init Scene "+sbox);
110 // shape = new Button(options.renderModes, font, "Hello JogAmp", sbox.getWidth()/8f, sbox.getWidth()/16f);
111 shape = new Rectangle(options.renderModes, sbox.getWidth()/8f, sbox.getWidth()/16f, 0);
112
113 contentBox = new Group();
114 contentBox.setBorder(0.005f);
115 contentBox.setInteractive(true);
116 contentBox.setClipOnBounds(true);
117 contentBox.addShape(shape);
118 {
119 final float w = sbox.getWidth()*0.6f;
120 final float h = sbox.getHeight()*0.6f;
121 if( useFixedSize ) {
122 contentBox.setFixedSize(new Vec2f(w, h));
123 }
124 contentBox.move(-w/2f, -h/2f, 0);
125 System.err.println("XXX contentBox "+contentBox.getBounds(drawable.getGLProfile()));
126 System.err.println("XXX shape "+shape.getBounds());
127 }
128
129 contentBox.addMouseListener( new Shape.MouseGestureAdapter() {
130 @Override
131 public void mouseWheelMoved(final MouseEvent e) {
132 final Shape.EventInfo shapeEvent = (Shape.EventInfo) e.getAttachment();
133 final Vec3f rot = new Vec3f(e.getRotation()).scale( FloatUtil.PI / 180.0f );
134 // swap axis for onscreen rotation matching natural feel
135 final float tmp = rot.x(); rot.setX( rot.y() ); rot.setY( tmp );
136 shapeEvent.shape.setRotation( shapeEvent.shape.getRotation().rotateByEuler( rot.scale( 2f ) ) );
137 }
138 });
139 scene.addShape(contentBox);
140 }
141
142 @Override
143 public void dispose(final GLAutoDrawable drawable) { }
144 @Override
145 public void display(final GLAutoDrawable drawable) {
146 final RegionRenderer renderer = scene.getRenderer();
147 final PMVMatrix4f pmv = renderer.getMatrix();
148
149 pmv.pushMv();
150 contentBox.applyMatToMv(pmv);
151 {
152 final AABBox box = contentBox.getBounds();
153 final Cube cube = tempC00.set(box);
154 final Frustum frustumCbMv = tempC01.set(cube).transform(pmv.getMv()).updateFrustumPlanes(new Frustum());
155
156 pmv.pushMv();
157 shape.applyMatToMv(pmv);
158 {
159 final AABBox shapeBox = shape.getBounds();
160 final Cube shapedMv = tempC10.set(shapeBox).transform(pmv.getMv());
161
162 final boolean isOutMv = frustumCbMv.isOutside( shapedMv );
163
164 final Frustum frustumPMv = new Frustum().setFromMat(pmv.getPMv());
165 final boolean isOutPMv = frustumPMv.isOutside( shapeBox );
166
167 System.err.println("ClipBox "+box);
168 System.err.println("ShapeBox "+shapeBox);
169 System.err.println("FrusPMv "+isOutPMv+", "+frustumPMv);
170 System.err.println("FsCbMv 1 "+isOutMv+", "+frustumCbMv);
171 }
172 pmv.popMv();
173 }
174 pmv.popMv();
175 }
176 @Override
177 public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
178 private final Cube tempC00 = new Cube(); // OK, synchronized
179 private final Cube tempC01 = new Cube(); // OK, synchronized
180 private final Cube tempC10 = new Cube(); // OK, synchronized
181 });
182
183 window.addWindowListener(new WindowAdapter() {
184 @Override
185 public void windowResized(final WindowEvent e) {
186 window.setTitle(UIShapeClippingDemo01.class.getSimpleName()+": "+window.getSurfaceWidth()+" x "+window.getSurfaceHeight());
187 }
188 @Override
189 public void windowDestroyNotify(final WindowEvent e) {
190 animator.stop();
191 }
192 });
193
194 scene.attachInputListenerTo(window);
195 window.addKeyListener(new KeyAdapter() {
196 @Override
197 public void keyPressed(final KeyEvent arg0) {
198 final short keySym = arg0.getKeySymbol();
199 if( keySym == KeyEvent.VK_F4 || keySym == KeyEvent.VK_ESCAPE || keySym == KeyEvent.VK_Q ) {
200 new InterruptSource.Thread( () -> { window.destroy(); } ).start();
201 }
202 }
203 });
204 window.addWindowListener(new WindowAdapter() {
205 @Override
206 public void windowDestroyed(final WindowEvent e) {
207 animator.stop();
208 }
209 });
210
211 animator.add(window);
212 animator.start();
213 }
214}
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
final PMVMatrix4f getMatrix()
Borrow the current PMVMatrix4f.
Graph based GLRegion Shape.
Definition: GraphShape.java:55
Group of Shapes, optionally utilizing a Group.Layout.
Definition: Group.java:61
void addShape(final Shape s)
Adds a Shape.
Definition: Group.java:225
Group setClipOnBounds(final boolean v)
Enable Modelview (Mv) Frustum clipping on getBounds() for this group and its shapes as follows.
Definition: Group.java:187
AABBox getBounds(final PMVMatrix4f pmv, final Shape shape)
Returns AABBox dimension of given Shape from this container's perspective, i.e.
Definition: Group.java:686
Group setFixedSize(final Vec3f v)
Enforce size of this group for all given 3 dimensions getBounds() without adjusting 3D z-axis like se...
Definition: Group.java:156
Default implementation of Scene.PMVMatrixSetup, implementing Scene.PMVMatrixSetup#set(PMVMatrix4f,...
Definition: Scene.java:1506
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
RegionRenderer getRenderer()
Returns the associated RegionRenderer.
Definition: Scene.java:208
int setAAQuality(final int v)
Sets RegionRenderer#setAAQuality(int).
Definition: Scene.java:383
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
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 setInteractive(final boolean v)
Set whether this shape is interactive in general, i.e.
Definition: Shape.java:1711
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 setBorder(final float thickness)
Sets the thickness of the border, which is included in getBounds() and is outside of getPadding().
Definition: Shape.java:402
A GraphUI rectangle GraphShape.
Definition: Rectangle.java:47
2D Vector based upon two float components.
Definition: Vec2f.java:37
Axis Aligned Bounding Box.
Definition: AABBox.java:54
final float getWidth()
Definition: AABBox.java:879
final float getHeight()
Definition: AABBox.java:883
Simple 8-point Vec3f cube compound having z-far <= z-near @endiliteral.
Definition: Cube.java:48
Cube transform(final Matrix4f mat)
Affine 3f-vector transformation of all 8-points with given matrix, Matrix4f#mulVec3f(Vec3f).
Definition: Cube.java:163
Cube set(final AABBox box)
Setting this cube to given AABBox minimum and maximum.
Definition: Cube.java:126
Providing frustum planes derived by different inputs (P*MV, ..) used to classify objects.
Definition: Frustum.java:81
Frustum set(final Frustum o)
Definition: Frustum.java:141
Frustum setFromMat(final Matrix4f pmv)
Calculate the frustum planes in world coordinates using the passed column major order matrix,...
Definition: Frustum.java:378
Frustum updateFrustumPlanes(final Cube c)
Calculate the frustum planes using the given Cube.
Definition: Frustum.java:400
final boolean isOutside(final AABBox box)
Returns whether the given AABBox is completely outside of this frustum.
Definition: Frustum.java:424
PMVMatrix4f implements the basic computer graphics Matrix4f pack using projection (P),...
final Matrix4f getMv()
Returns the modelview matrix (Mv).
final Matrix4f getPMv()
Returns the pre-multiplied projection x modelview, P x Mv.
final PMVMatrix4f popMv()
Pop the modelview matrix from its stack.
final PMVMatrix4f pushMv()
Push the modelview matrix to its stack, while preserving its values.
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 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
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 Clipping demo using a Scene and Shape within a clipping Group.
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.
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
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...
GLProfile getGLProfile()
Fetches the GLProfile for this drawable.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.
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