aboutsummaryrefslogtreecommitdiffstats
path: root/src/demos/com/jogamp/opengl
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2023-03-20 06:20:23 +0100
committerSven Gothel <[email protected]>2023-03-20 06:20:23 +0100
commitb2a271e289047a7e5bba6c09540061c43527db3c (patch)
treea4c5cf58a25042df61e2a9511c80a41f5f64f97f /src/demos/com/jogamp/opengl
parent89ccd3bec5eab5323f14b8b62a9f722ec65f702e (diff)
UISceneDemo02: The 'dirty' variant w/ listener (printf) attached
Diffstat (limited to 'src/demos/com/jogamp/opengl')
-rw-r--r--src/demos/com/jogamp/opengl/demos/graph/ui/UISceneDemo02.java335
1 files changed, 85 insertions, 250 deletions
diff --git a/src/demos/com/jogamp/opengl/demos/graph/ui/UISceneDemo02.java b/src/demos/com/jogamp/opengl/demos/graph/ui/UISceneDemo02.java
index 4655b773c..ffae6d8f8 100644
--- a/src/demos/com/jogamp/opengl/demos/graph/ui/UISceneDemo02.java
+++ b/src/demos/com/jogamp/opengl/demos/graph/ui/UISceneDemo02.java
@@ -30,20 +30,18 @@ package com.jogamp.opengl.demos.graph.ui;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
-import java.util.Locale;
import com.jogamp.common.net.Uri;
import com.jogamp.common.util.InterruptSource;
import com.jogamp.graph.curve.Region;
-import com.jogamp.graph.curve.opengl.RegionRenderer;
import com.jogamp.graph.font.Font;
import com.jogamp.graph.font.FontFactory;
import com.jogamp.graph.font.FontSet;
import com.jogamp.graph.geom.SVertex;
-import com.jogamp.graph.geom.plane.AffineTransform;
import com.jogamp.graph.ui.gl.Scene;
import com.jogamp.graph.ui.gl.Shape;
import com.jogamp.graph.ui.gl.shapes.Button;
+import com.jogamp.graph.ui.gl.shapes.CrossHair;
import com.jogamp.graph.ui.gl.shapes.GLButton;
import com.jogamp.graph.ui.gl.shapes.MediaButton;
import com.jogamp.newt.Window;
@@ -54,26 +52,24 @@ import com.jogamp.newt.event.WindowAdapter;
import com.jogamp.newt.event.WindowEvent;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.GL;
-import com.jogamp.opengl.GL2ES2;
-import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
-import com.jogamp.opengl.GLPipelineFactory;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.demos.es2.GearsES2;
-import com.jogamp.opengl.demos.graph.MSAATool;
import com.jogamp.opengl.demos.util.MiscUtils;
import com.jogamp.opengl.math.geom.AABBox;
import com.jogamp.opengl.util.Animator;
-import com.jogamp.opengl.util.GLReadBufferUtil;
import com.jogamp.opengl.util.PMVMatrix;
import com.jogamp.opengl.util.av.GLMediaPlayer;
import com.jogamp.opengl.util.av.GLMediaPlayerFactory;
/**
- * Basic UIScene demo using a single UIShape.
+ * Res independent Shape, in Scene attached to GLWindow w/ listener attached.
+ * <p>
+ * User can test Shape drag-move and drag-resize w/ 1-pointer
+ * </p>
*/
-public class UISceneDemo02 implements GLEventListener {
+public class UISceneDemo02 {
static final boolean DEBUG = false;
static final boolean TRACE = false;
@@ -117,12 +113,16 @@ public class UISceneDemo02 implements GLEventListener {
}
}
}
- if( null == font ) {
- font = FontFactory.get(FontFactory.UBUNTU).get(FontSet.FAMILY_LIGHT, FontSet.STYLE_SERIF);
+ final int renderModes;
+ if( graphVBAAMode ) {
+ renderModes = Region.VBAA_RENDERING_BIT;
+ } else if( graphMSAAMode ) {
+ renderModes = Region.MSAA_RENDERING_BIT;
+ } else {
+ renderModes = 0;
}
- System.err.println("Font: "+font.getFullFamilyName());
-
final GLProfile glp = GLProfile.getGL2ES2();
+
final GLCapabilities caps = new GLCapabilities(glp);
caps.setAlphaBits(4);
if( sceneMSAASamples > 0 ) {
@@ -130,27 +130,55 @@ public class UISceneDemo02 implements GLEventListener {
caps.setNumSamples(sceneMSAASamples);
}
System.out.println("Requested: " + caps);
+ final GLWindow window = GLWindow.create(caps);
- final int renderModes;
- if( graphVBAAMode ) {
- renderModes = Region.VBAA_RENDERING_BIT;
- } else if( graphMSAAMode ) {
- renderModes = Region.MSAA_RENDERING_BIT;
- } else {
- renderModes = 0;
+ //
+ // Resolution independent, no screen size
+ //
+ if( null == font ) {
+ font = FontFactory.get(FontFactory.UBUNTU).get(FontSet.FAMILY_LIGHT, FontSet.STYLE_SERIF);
}
+ System.err.println("Font: "+font.getFullFamilyName());
+ final Shape shape = makeShape(window, font, renderModes);
+ System.err.println("m0 shape bounds "+shape.getBounds(glp));
+ System.err.println("m0 "+shape);
+
+ // Scene for Shape ...
+ final Scene scene = new Scene();
+ scene.setClearParams(new float[] { 1f, 1f, 1f, 1f}, GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
+
+ shape.onMove(new Shape.Listener() {
+ @Override
+ public void run(final Shape shape) {
+ final float[] p = shape.getPosition();
+ System.err.println("Shape moved: "+p[0]+", "+p[1]+", "+p[2]);
+ }
+ });
+ shape.addMouseListener(new Shape.MouseGestureAdapter() {
+ @Override
+ public void mouseMoved(final MouseEvent e) {
+ final int[] viewport = scene.getViewport(new int[4]);
+ // flip to GL window coordinates, origin bottom-left
+ final int glWinX = e.getX();
+ final int glWinY = viewport[3] - e.getY() - 1;
+ testProject(scene, shape, glWinX, glWinY);
+ }
+ @Override
+ public void mouseDragged(final MouseEvent e) {
+ final int[] viewport = scene.getViewport(new int[4]);
+ // flip to GL window coordinates, origin bottom-left
+ final int glWinX = e.getX();
+ final int glWinY = viewport[3] - e.getY() - 1;
+ testProject(scene, shape, glWinX, glWinY);
+ }
+ } );
+ scene.addShape(shape);
- final GLWindow window = GLWindow.create(caps);
- // window.setPosition(10, 10);
window.setSize(width, height);
window.setTitle(UISceneDemo02.class.getSimpleName()+": "+window.getSurfaceWidth()+" x "+window.getSurfaceHeight());
- // final int renderModes = Region.COLORCHANNEL_RENDERING_BIT | Region.VBAA_RENDERING_BIT;
window.setVisible(true);
-
- final Scene scene = new Scene();
-
- final UISceneDemo02 demo = new UISceneDemo02(scene, font, renderModes, window.getSurfaceWidth(), window.getSurfaceHeight(), DEBUG, TRACE);
- window.addGLEventListener(demo);
+ window.addGLEventListener(scene);
+ scene.attachInputListenerTo(window);
final Animator animator = new Animator();
animator.setUpdateFPSFrames(5*60, null);
@@ -176,82 +204,37 @@ public class UISceneDemo02 implements GLEventListener {
});
animator.start();
- System.err.println("m1 "+demo.shape);
+ //
+ // After initial display we can use screen resolution post initial Scene.reshape(..)
+ // However, in this example we merely use the resolution to
+ // - Scale the shape to the sceneBox, i.e. normalizing to screen-size 1x1
scene.waitUntilDisplayed();
- final AABBox box = scene.getBounds();
- final float diag = (float)Math.sqrt(box.getHeight()*box.getHeight() + box.getWidth()*box.getWidth());
- System.err.println("box "+box+", diag "+diag);
- final float max = box.getWidth(); // diag;
- final float step = max/200f;
- System.err.println("m2 [0 .. "+max+"], step "+step+", "+box);
- demo.testProject();
- for(float x=0; x < max; x+=step) {
- demo.shape.move(-step, 0f, 0f);
- // System.err.println(demo.shape);
- final int[] glWinPos = demo.shape.shapeToWinCoord(scene.getPMVMatrixSetup(), scene.getViewport(), demo.shape.getBounds().getCenter(), new PMVMatrix(), new int[2]);
- System.err.printf("m3: objToWinCoord: winCoords %d / %d%n", glWinPos[0], glWinPos[1]);
- // demo.testProject(glWinPos[0], glWinPos[1]);
- window.warpPointer(glWinPos[0], window.getHeight() - glWinPos[1] - 1);
- System.err.println("mm x "+x+", [0 .. "+max+"], step "+step+", "+box);
- try { Thread.sleep(10); } catch (final InterruptedException e1) { }
- }
- }
- void testProject() {
- final int[] glWinPos = shape.shapeToWinCoord(scene.getPMVMatrixSetup(), scene.getViewport(), shape.getBounds().getCenter(), new PMVMatrix(), new int[2]);
- System.err.printf("m4: objToWinCoord: winCoords %d / %d%n", glWinPos[0], glWinPos[1]);
- testProject(glWinPos[0], glWinPos[1]);
+ final AABBox sceneBox = scene.getBounds();
+ shape.scale(sceneBox.getWidth(), sceneBox.getWidth(), 1f); // scale shape to sceneBox
+ System.err.println("m1.1 "+shape);
+ try { Thread.sleep(1000); } catch (final InterruptedException e1) { }
+
+ System.err.println("You may test moving the Shape by dragging the shape with 1-pointer.");
+ 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.");
+ System.err.println("Press F4 or 'window close' to exit ..");
}
- void testProject(final int glWinX, final int glWinY) {
+
+ static void testProject(final Scene scene, final Shape shape, final int glWinX, final int glWinY) {
final PMVMatrix pmv = new PMVMatrix();
final float[] objPos = shape.winToShapeCoord(scene.getPMVMatrixSetup(), scene.getViewport(), glWinX, glWinY, pmv, new float[3]);
- System.err.printf("m5: winToObjCoord: obj [%25.20ff, %25.20ff, %25.20ff]%n", objPos[0], objPos[1], objPos[2]);
+ System.err.printf("MM1: winToObjCoord: obj [%25.20ff, %25.20ff, %25.20ff]%n", objPos[0], objPos[1], objPos[2]);
final int[] glWinPos = shape.shapeToWinCoord(scene.getPMVMatrixSetup(), scene.getViewport(), objPos, pmv, new int[2]);
final int windx = glWinPos[0]-glWinX;
final int windy = glWinPos[1]-glWinY;
- if( windx > 10 || windy > 10 ) {
- System.err.printf("XX: objToWinCoord: winCoords %d / %d, diff %d x %d%n", glWinPos[0], glWinPos[1], windx, windy);
- // Thread.dumpStack();
- // System.exit(-1);
- } else {
- System.err.printf("m6: objToWinCoord: winCoords %d / %d, diff %d x %d%n", glWinPos[0], glWinPos[1], windx, windy);
- }
+ System.err.printf("MM2: objToWinCoord: winCoords %d / %d, diff %d x %d%n", glWinPos[0], glWinPos[1], windx, windy);
}
@SuppressWarnings("unused")
- private final Font font;
-
- private final GLReadBufferUtil screenshot;
- private final int renderModes;
- private final boolean debug;
- private final boolean trace;
-
- private final Scene scene;
- private final Shape shape;
-
- private final float[] position = new float[] {0,0,0};
-
- boolean ignoreInput = false;
-
- protected final AffineTransform tempT1 = new AffineTransform();
- protected final AffineTransform tempT2 = new AffineTransform();
-
- @SuppressWarnings("unused")
- public UISceneDemo02(final Scene scene, final Font font, final int renderModes, final int width, final int height, final boolean debug, final boolean trace) {
- this.font = font;
- this.renderModes = renderModes;
- this.debug = debug;
- this.trace = trace;
- this.screenshot = new GLReadBufferUtil(false, false);
- this.scene = scene;
-
- final float[/*2*/] sceneSize = { 0f, 0f };
- scene.surfaceToPlaneSize(new int[] { 0, 0, width, height}, sceneSize);
- System.err.println("Scene "+width+" x "+height+" pixel -> "+sceneSize[0]+" x "+sceneSize[1]+" model");
- final float sw = sceneSize[0] * 0.2f; // 0.084f; // 1/3f * sceneSize[0];
+ static Shape makeShape(final Window window, final Font font, final int renderModes) {
+ final float sw = 0.10f;
final float sh = sw / 2.5f;
- System.err.println("Shape "+sw+" x "+sh);
if( false ) {
Uri filmUri;
@@ -274,183 +257,35 @@ public class UISceneDemo02 implements GLEventListener {
mPlayer.setAudioVolume( b.isToggleOn() ? 1f : 0f );
} } );
mPlayer.playStream(filmUri, GLMediaPlayer.STREAM_ID_AUTO, GLMediaPlayer.STREAM_ID_AUTO, GLMediaPlayer.TEXTURE_COUNT_DEFAULT);
- shape = b;
- } else if( false ) {
+ return b;
+ } else if( true ) {
final GLEventListener glel;
{
final GearsES2 gears = new GearsES2(0);
gears.setVerbose(false);
gears.setClearColor(new float[] { 0.9f, 0.9f, 0.9f, 1f } );
+ window.addKeyListener(gears.getKeyListener());
glel = gears;
}
final int texUnit = 1;
- final GLButton b = new GLButton(scene.getVertexFactory(), renderModes,
+ final GLButton b = new GLButton(SVertex.factory(), renderModes,
sw, sh, texUnit, glel, false /* useAlpha */);
b.setToggleable(true);
b.setToggle(true); // toggle == true -> animation
b.setAnimate(true);
- b.addMouseListener(shapeGesture);
b.addMouseListener(new Shape.MouseGestureAdapter() {
@Override
public void mouseClicked(final MouseEvent e) {
b.setAnimate( b.isToggleOn() );
} } );
- shape = b;
- } else {
+ return b;
+ } else if( true ){
final Button b = new Button(SVertex.factory(), renderModes, font, "+", sw, sh);
- b.addMouseListener(shapeGesture);
- b.setLabelColor(0.0f,0.0f,0.0f);
b.setCorner(0.0f);
- b.scale(0.4f, 0.4f, 1f);
- // b.setLabelColor(1.0f,0.0f,0.0f);
- // b.setColor(0.0f,0.0f,1.0f, 1f);
- /** Button defaults !
- button.setLabelColor(1.0f,1.0f,1.0f);
- button.setButtonColor(0.6f,0.6f,0.6f);
- button.setCorner(1.0f);
- button.setSpacing(2.0f);
- */
- shape = b;
- }
- // shape.move((sceneSize[0] - sw)/2f, (sceneSize[1] - sh)/2f, 0f); // center
- // shape.move( 1/8f * sceneSize[0], (sceneSize[1] - sh)/2f, 0f); // left-center
- shape.move( sceneSize[0] - sw, sceneSize[1] - sh, 0f); // top-right
-
- scene.addShape(shape);
- }
-
- final Shape.MouseGestureAdapter shapeGesture = new Shape.MouseGestureAdapter() {
- @Override
- public void mouseMoved(final MouseEvent e) {
- final int[] viewport = scene.getViewport(new int[4]);
- // flip to GL window coordinates, origin bottom-left
- final int glWinX = e.getX();
- final int glWinY = viewport[3] - e.getY() - 1;
- testProject(glWinX, glWinY);
- }
-
- @Override
- public void mouseClicked(final MouseEvent e) {
- final Shape.EventInfo shapeEvent = (Shape.EventInfo) e.getAttachment();
- System.err.println("Shape.EventInfo: "+shapeEvent);
- final Shape s = shapeEvent.shape;
-
- final int[] viewport = scene.getViewport(new int[4]);
- final PMVMatrix pmv = new PMVMatrix();
- scene.setupMatrix(pmv, 0, 0, viewport[2], viewport[3]);
-
- // flip to GL window coordinates, origin bottom-left
- final int glWinX = e.getX();
- final int glWinY = viewport[3] - e.getY() - 1;
- {
- pmv.glPushMatrix();
- s.setTransform(pmv);
-
- final float[] objPos = s.winToShapeCoord(pmv, viewport, glWinX, glWinY, new float[3]);
- System.err.println("Button: Click: win["+glWinX+", "+glWinY+"] -> obj["+objPos[0]+", "+objPos[1]+", "+objPos[1]+"]");
-
- final int[] surfaceSize = s.getSurfaceSize(pmv, viewport, new int[2]);
- System.err.println("Button: Size: pixel "+surfaceSize[0]+" x "+surfaceSize[1]);
-
- pmv.glPopMatrix();
- }
- } };
-
- public final float[] getPosition() { return position; }
-
- @Override
- public void init(final GLAutoDrawable drawable) {
- GL2ES2 gl = drawable.getGL().getGL2ES2();
- if(debug) {
- gl = gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Debug", null, gl, null) ).getGL2ES2();
- }
- if(trace) {
- gl = gl.getContext().setGL( GLPipelineFactory.create("com.jogamp.opengl.Trace", null, gl, new Object[] { System.err } ) ).getGL2ES2();
- }
-
- if(drawable instanceof GLWindow) {
- final GLWindow glw = (GLWindow) drawable;
- scene.attachInputListenerTo(glw);
- }
- gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
-
- gl.setSwapInterval(1);
- gl.glEnable(GL.GL_DEPTH_TEST);
- // gl.glEnable(GL.GL_POLYGON_OFFSET_FILL);
- MSAATool.dump(drawable);
-
- System.err.println("Init: Shape "+shape);
- scene.init(drawable);
- }
-
- @Override
- public void reshape(final GLAutoDrawable drawable, final int xstart, final int ystart, final int width, final int height) {
- // Layout all shapes: Just stay centered
- //
- // shape.move(0f, 0f, 0f);
- System.err.println("Reshape: Shape "+shape);
-
- System.err.println("Reshape: Scene Plane.R "+scene.getBounds());
-
- scene.reshape(drawable, xstart, ystart, width, height);
-
- System.err.println("Reshape: Scene Plane.R "+scene.getBounds());
-
- if( drawable instanceof Window ) {
- ((Window)drawable).setTitle(UISceneDemo02.class.getSimpleName()+": "+drawable.getSurfaceWidth()+" x "+drawable.getSurfaceHeight());
- }
- }
-
- final int[] sampleCount = { 4 };
-
- @Override
- public void display(final GLAutoDrawable drawable) {
- scene.display(drawable);
- if( once ) {
- once = false;
- final RegionRenderer renderer = scene.getRenderer();
- final PMVMatrix pmv = renderer.getMatrix();
- pmv.glPushMatrix();
- shape.setTransform(pmv);
-
- System.err.println("draw.0: "+shape);
- final int[] winSize = shape.getSurfaceSize(pmv, renderer.getViewport(), new int[2]);
- System.err.println("draw.1: surfaceSize "+winSize[0]+" x "+winSize[1]);
- final int[] winPos = shape.shapeToWinCoord(pmv, renderer.getViewport(), shape.getPosition(), new int[2]);
- System.err.println("draw.2: winCoord "+winPos[0]+" x "+winPos[1]);
-
- pmv.glPopMatrix();
- }
- }
- static boolean once = true;
-
- @Override
- public void dispose(final GLAutoDrawable drawable) {
- final GL2ES2 gl = drawable.getGL().getGL2ES2();
- scene.dispose(drawable); // disposes all registered UIShapes
-
- screenshot.dispose(gl);
- }
-
- public void printScreen(final GL gl) {
- final RegionRenderer renderer = scene.getRenderer();
- final String modeS = Region.getRenderModeString(renderModes);
- final String filename = String.format((Locale)null, "UISceneDemo01-shot%03d-%03dx%03d-S_%s_%02d.png",
- shotCount++, renderer.getWidth(), renderer.getHeight(),
- modeS, scene.getSampleCount());
- gl.glFinish(); // just make sure rendering finished ..
- if(screenshot.readPixels(gl, false)) {
- screenshot.write(new File(filename));
- System.err.println("Wrote: "+filename);
+ return b;
+ } else {
+ final CrossHair b = new CrossHair(SVertex.factory(), renderModes, sw, sw, 1f/100f);
+ return b;
}
}
- private int shotCount = 0;
-
- public void setIgnoreInput(final boolean v) {
- ignoreInput = v;
- }
- public boolean getIgnoreInput() {
- return ignoreInput;
- }
}