From a81fff28e9380ebce877d974d402ef613b5ea850 Mon Sep 17 00:00:00 2001
From: Sven Gothel
+ * {@link #setPMVMatrixSetup(PMVMatrixSetup)} maybe used to provide a custom {@link PMVMatrix} setup. + *
* @see Shape */ public final class Scene implements GLEventListener { @@ -87,12 +93,11 @@ public final class Scene implements GLEventListener { /** Default projection z-far value is 7000. */ public static final float DEFAULT_ZFAR = 7000.0f; + @SuppressWarnings("unused") private static final boolean DEBUG = false; private final ArrayList+ * At the end of operations, the {@link GLMatrixFunc#GL_MODELVIEW} matrix has to be selected. + *
+ *+ * Implementation is being called by {@link Scene#setupMatrix(PMVMatrix, int, int, int, int)} + * and hence {@link Scene#reshape(GLAutoDrawable, int, int, int, int)}. + *
+ *+ * Custom implementations can be set via {@link Scene#setPMVMatrixSetup(PMVMatrixSetup)}. + *
+ *+ * The default implementation is described below + *
+ *+ * {@link GLMatrixFunc#GL_PROJECTION} is setup using perspective {@link Scene#DEFAULT_ANGLE} with {@link Scene#DEFAULT_ZNEAR} and {@link Scene#DEFAULT_ZFAR}. + *
+ *+ * Further {@link GLMatrixFunc#GL_MODELVIEW} is translated to given {@link Scene#DEFAULT_SCENE_DIST}. + *
*/ - public static void winToObjCoord(final PMVMatrix pmv, final int[] view, - final float zNear, final float zFar, - final float orthoX, final float orthoY, final float orthoDist, - final float[] winZ, final float[] objPos) { - winZ[0] = FloatUtil.getOrthoWinZ(orthoDist, zNear, zFar); - pmv.gluUnProject(orthoX, orthoY, winZ[0], view, 0, objPos, 0); + public static interface PMVMatrixSetup { + /** + * Setup {@link PMVMatrix}'s {@link GLMatrixFunc#GL_PROJECTION} and {@link GLMatrixFunc#GL_MODELVIEW}. + *+ * See {@link PMVMatrixSetup} for details. + *
+ *+ * At the end of operations, the {@link GLMatrixFunc#GL_MODELVIEW} matrix is selected. + *
+ * @param pmv the {@link PMVMatrix} to setup + * @param x lower left corner of the viewport rectangle + * @param y lower left corner of the viewport rectangle + * @param width width of the viewport rectangle + * @param height height of the viewport rectangle + */ + void set(PMVMatrix pmv, final int x, final int y, final int width, final int height); + + /** + * Optional method to set the {@link Scene#getBounds()} {@link AABBox}, maybe a {@code nop} if not desired. + *+ * Will be called by {@link Scene#reshape(GLAutoDrawable, int, int, int, int)} after {@link #set(PMVMatrix, int, int, int, int)}. + *
+ * @param x TODO + * @param y TODO + */ + void setPlaneBox(final AABBox planeBox, final PMVMatrix pmv, int x, int y, final int width, final int height); } - /** - * Map given window surface-size to object coordinates relative to this scene and {@link #getProjSceneDist() and projection settings. - * @param width surface width in pixel - * @param height surface height in pixel - * @param objSceneSize float[2] storage for object surface size result - */ - public void surfaceToObjSize(final int width, final int height, final float[/*2*/] objSceneSize) { - final int[] viewport = { 0, 0, width, height }; + /** Return the default or {@link #setPMVMatrixSetup(PMVMatrixSetup)} {@link PMVMatrixSetup}. */ + public final PMVMatrixSetup getPMVMatrixSetup() { return pmvMatrixSetup; } - final PMVMatrix pmv = new PMVMatrix(); - setupMatrix(pmv, width, height); - { - final float orthoDist = -sceneDist; - final float[] obj00Coord = new float[3]; - final float[] obj11Coord = new float[3]; - final float[] winZ = new float[1]; - - winToObjCoord(pmv, viewport, zNear, zFar, 0f, 0f, orthoDist, winZ, obj00Coord); - winToObjCoord(pmv, viewport, zNear, zFar, width, height, orthoDist, winZ, obj11Coord); - objSceneSize[0] = obj11Coord[0] - obj00Coord[0]; - objSceneSize[1] = obj11Coord[1] - obj00Coord[1]; - } - } + /** Set a custom {@link PMVMatrixSetup}. */ + public final void setPMVMatrixSetup(final PMVMatrixSetup setup) { pmvMatrixSetup = setup; } /** - * Reshape scene {@link #setupMatrix(PMVMatrix, int, int)}. - *- * {@link GLMatrixFunc#GL_PROJECTION} is setup using perspective {@link #getProjAngle()} with {@link #getProjZNear()} and {@link #getProjZFar()}. - *
+ * Reshape scene using {@link #setupMatrix(PMVMatrix, int, int, int, int)} using {@link PMVMatrixSetup}. *- * {@link GLMatrixFunc#GL_MODELVIEW} is translated to given {@link #getProjSceneDist()} - * and and origin 0/0 becomes the bottom-left corner. + * {@inheritDoc} *
- * @see #setupMatrix(PMVMatrix, int, int) + * @see PMVMatrixSetup + * @see #setPMVMatrixSetup(PMVMatrixSetup) + * @see #setupMatrix(PMVMatrix, int, int, int, int) * @see #getBounds() * @see #getBoundsCenter() */ - @SuppressWarnings("unused") @Override public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { renderer.reshapeNotify(x, y, width, height); - final PMVMatrix pmv = renderer.getMatrix(); - setupMatrix0(pmv, width, height); - pmv.glTranslatef(0f, 0f, sceneDist); - { - final float orthoDist = -sceneDist; - final float[] obj00Coord = new float[3]; - final float[] obj11Coord = new float[3]; - final float[] winZ = new float[1]; - - winToObjCoord(pmv, getViewport(), zNear, zFar, 0f, 0f, orthoDist, winZ, obj00Coord); - winToObjCoord(pmv, getViewport(), zNear, zFar, width, height, orthoDist, winZ, obj11Coord); - - pmv.glTranslatef(obj00Coord[0], obj00Coord[1], 0f); // bottom-left corder origin 0/0 - - planeBoxCtr.setSize( obj00Coord[0], // lx - obj00Coord[1], // ly - obj00Coord[2], // lz - obj11Coord[0], // hx - obj11Coord[1], // hy - obj11Coord[2] );// hz - - planBoxBL.setSize( 0, // lx - 0, // ly - 0, // lz - planeBoxCtr.getWidth(), // hx - planeBoxCtr.getHeight(),// hy - planeBoxCtr.getDepth());// hz - - if( true || DEBUG ) { - System.err.printf("Reshape: zNear %f, zFar %f, sceneDist %f%n", zNear, zFar, sceneDist); - System.err.printf("Reshape: Frustum: %s%n", pmv.glGetFrustum()); - System.err.printf("Reshape: mapped.00: [%f, %f, %f], winZ %f -> [%f, %f, %f]%n", 0f, 0f, orthoDist, winZ[0], obj00Coord[0], obj00Coord[1], obj00Coord[2]); - System.err.printf("Reshape: mapped.11: [%f, %f, %f], winZ %f -> [%f, %f, %f]%n", (float)width, (float)height, orthoDist, winZ[0], obj11Coord[0], obj11Coord[1], obj11Coord[2]); - System.err.printf("Reshape: scenePlaneBox: %s%n", planeBoxCtr); - } - } + setupMatrix(renderer.getMatrix(), x, y, width, height); + pmvMatrixSetup.setPlaneBox(planeBox, renderer.getMatrix(), x, y, width, height); + } - if( false ) { - final float[] sceneScale = new float[3]; - final float[] scenePlaneOrigin = new float[3]; - scenePlaneOrigin[0] = planeBoxCtr.getMinX() * sceneDist; - scenePlaneOrigin[1] = planeBoxCtr.getMinY() * sceneDist; - scenePlaneOrigin[2] = planeBoxCtr.getMinZ() * sceneDist; - sceneScale[0] = ( planeBoxCtr.getWidth() * sceneDist ) / width; - sceneScale[1] = ( planeBoxCtr.getHeight() * sceneDist ) / height; - sceneScale[2] = 1f; - System.err.printf("Scene Origin [%f, %f, %f]%n", scenePlaneOrigin[0], scenePlaneOrigin[1], scenePlaneOrigin[2]); - System.err.printf("Scene Scale %f * [%f x %f] / [%d x %d] = [%f, %f, %f]%n", - sceneDist, planeBoxCtr.getWidth(), planeBoxCtr.getHeight(), - width, height, - sceneScale[0], sceneScale[1], sceneScale[2]); - } + /** + * Setup {@link PMVMatrix} {@link GLMatrixFunc#GL_PROJECTION} and {@link GLMatrixFunc#GL_MODELVIEW} + * by calling {@link #getPMVMatrixSetup()}'s {@link PMVMatrixSetup#set(PMVMatrix, int, int, int, int)}. + * @param pmv the {@link PMVMatrix} to setup + * @param x lower left corner of the viewport rectangle + * @param y lower left corner of the viewport rectangle + * @param width width of the viewport rectangle + * @param height height of the viewport rectangle + */ + public void setupMatrix(final PMVMatrix pmv, final int x, final int y, final int width, final int height) { + pmvMatrixSetup.set(pmv, x, y, width, height); + } + + /** + * Setup {@link PMVMatrix} {@link GLMatrixFunc#GL_PROJECTION} and {@link GLMatrixFunc#GL_MODELVIEW} + * using implicit {@link #getViewport()} surface dimension by calling {@link #getPMVMatrixSetup()}'s {@link PMVMatrixSetup#set(PMVMatrix, int, int, int, int)}. + * @param pmv the {@link PMVMatrix} to setup + */ + public void setupMatrix(final PMVMatrix pmv) { + final int[] viewport = renderer.getViewport(); + setupMatrix(pmv, viewport[0], viewport[1], viewport[2], viewport[3]); } /** Copies the current int[4] viewport in given target and returns it for chaining. It is set after initial {@link #reshape(GLAutoDrawable, int, int, int, int)}. */ @@ -666,94 +640,79 @@ public final class Scene implements GLEventListener { /** Borrow the current {@link PMVMatrix}. */ public PMVMatrix getMatrix() { return renderer.getMatrix(); } - /** Translate current matrix to {@link #getBounds()}'s origin (minx/miny) and {@link #getProjSceneDist()}, a convenience method. */ - private void translate(final PMVMatrix pmv) { - pmv.glTranslatef(planeBoxCtr.getMinX(), planeBoxCtr.getMinY(), sceneDist); - } - /** - * Setup {@link PMVMatrix} {@link GLMatrixFunc#GL_PROJECTION} and {@link GLMatrixFunc#GL_MODELVIEW} - * using explicit surface width and height before {@link #reshape(GLAutoDrawable, int, int, int, int)} happened, a convenience method. + * Describing the scene's object model-dimensions of the plane at scene-distance covering the visible viewport rectangle. *- * {@link GLMatrixFunc#GL_PROJECTION} is setup using perspective {@link #getProjAngle()} with {@link #getProjZNear()} and {@link #getProjZFar()}. + * The value is evaluated at {@link #reshape(GLAutoDrawable, int, int, int, int)} via {@link } *
*- * {@link GLMatrixFunc#GL_MODELVIEW} is translated to given {@link #getProjSceneDist()} - * and and origin 0/0 becomes the bottom-left corner. + * {@link AABBox#getWidth()} and {@link AABBox#getHeight()} define scene's dimension covered by surface size. *
*- * At the end of operations, the {@link GLMatrixFunc#GL_MODELVIEW} matrix is selected. + * {@link AABBox} is setup via {@link #getPMVMatrixSetup()}'s {@link PMVMatrixSetup#setPlaneBox(AABBox, PMVMatrix, int, int, int, int)}. *
- * @param pmv the {@link PMVMatrix} to setup - * @param surface_width explicit surface width - * @param surface_height explicit surface height */ - public void setupMatrix(final PMVMatrix pmv, final int surface_width, final int surface_height) { - setupMatrix0(pmv, surface_width, surface_height); - translate(pmv); - } - private void setupMatrix0(final PMVMatrix pmv, final int surface_width, final int surface_height) { - final float ratio = (float)surface_width/(float)surface_height; - pmv.glMatrixMode(GLMatrixFunc.GL_PROJECTION); - pmv.glLoadIdentity(); - pmv.gluPerspective(projAngle, ratio, zNear, zFar); - - pmv.glMatrixMode(GLMatrixFunc.GL_MODELVIEW); - pmv.glLoadIdentity(); - } + public AABBox getBounds() { return planeBox; } /** - * Setup {@link PMVMatrix} {@link GLMatrixFunc#GL_PROJECTION} and {@link GLMatrixFunc#GL_MODELVIEW} - * using implicit {@link #getViewport()} surface dimension after {@link #reshape(GLAutoDrawable, int, int, int, int)} happened, a convenience method. - *- * {@link GLMatrixFunc#GL_PROJECTION} is setup using perspective {@link #getProjAngle()} with {@link #getProjZNear()} and {@link #getProjZFar()}. - *
- *- * {@link GLMatrixFunc#GL_MODELVIEW} is translated to given {@link #getProjSceneDist()} - * and and origin 0/0 becomes the bottom-left corner. - *
- *- * At the end of operations, the {@link GLMatrixFunc#GL_MODELVIEW} matrix is selected. - *
- * @param pmv the {@link PMVMatrix} to setup - * @param surface_width explicit surface width - * @param surface_height explicit surface height + * + * @param pmv + * @param viewport + * @param zNear + * @param zFar + * @param winX + * @param winY + * @param objOrthoZ + * @param objPos float[3] storage for object coord result + * @param winZ */ - public void setupMatrix(final PMVMatrix pmv) { - setupMatrix(pmv, getWidth(), getHeight()); + public static void winToPlaneCoord(final PMVMatrix pmv, final int[] viewport, + final float zNear, final float zFar, + final float winX, final float winY, final float objOrthoZ, + final float[] objPos) { + final float winZ = FloatUtil.getOrthoWinZ(objOrthoZ, zNear, zFar); + pmv.gluUnProject(winX, winY, winZ, viewport, 0, objPos, 0); } /** - * Describing the scene's object model-dimensions of the near-plane parallel at its scene-distance {@link #getProjSceneDist()} - * having the origin 0/0 on the bottom-left corner. - *- * The value is evaluated at {@link #reshape(GLAutoDrawable, int, int, int, int)}. - *
- *- * {@link AABBox#getWidth()} and {@link AABBox#getHeight()} define scene's dimension covered by surface size. - *
+ * Map given window surface-size to object coordinates relative to this scene using + * the give projection parameters. + * @param viewport + * @param zNear + * @param zFar + * @param objOrthoDist + * @param objSceneSize float[2] storage for object surface size result */ - public AABBox getBounds() { return planBoxBL; } + public void surfaceToPlaneSize(final int[] viewport, final float zNear, final float zFar, final float objOrthoDist, final float[/*2*/] objSceneSize) { + final PMVMatrix pmv = new PMVMatrix(); + setupMatrix(pmv, viewport[0], viewport[1], viewport[2], viewport[3]); + { + final float[] obj00Coord = new float[3]; + final float[] obj11Coord = new float[3]; + + winToPlaneCoord(pmv, viewport, DEFAULT_ZNEAR, DEFAULT_ZFAR, viewport[0], viewport[1], objOrthoDist, obj00Coord); + winToPlaneCoord(pmv, viewport, DEFAULT_ZNEAR, DEFAULT_ZFAR, viewport[2], viewport[3], objOrthoDist, obj11Coord); + objSceneSize[0] = obj11Coord[0] - obj00Coord[0]; + objSceneSize[1] = obj11Coord[1] - obj00Coord[1]; + } + } /** - * Describing the scene's object model-dimensions of the near-plane parallel at its scene-distance {@link #getProjSceneDist()} - * having the origin 0/0 in the center of the screen. - *- * The value is evaluated at {@link #reshape(GLAutoDrawable, int, int, int, int)} before translating to the bottom-left origin 0/0, - * i.e. its minimum values are negative of half dimension. - *
- *- * {@link AABBox#getWidth()} and {@link AABBox#getHeight()} define scene's dimension covered by surface size. - *
+ * Map given window surface-size to object coordinates relative to this scene using + * the default {@link PMVMatrixSetup}. + * @param viewport + * @param objSceneSize float[2] storage for object surface size result */ - public AABBox getBoundsCenter() { return planeBoxCtr; } + public void surfaceToPlaneSize(final int[] viewport, final float[/*2*/] objSceneSize) { + surfaceToPlaneSize(viewport, DEFAULT_ZNEAR, DEFAULT_ZFAR, -DEFAULT_SCENE_DIST, objSceneSize); + } public final Shape getActiveShape() { return activeShape; } - public void release() { - setActiveShape(null); + public void releaseActiveShape() { + activeShape = null; } private void setActiveShape(final Shape shape) { activeShape = shape; @@ -770,10 +729,11 @@ public final class Scene implements GLEventListener { // flip to GL window coordinates final int glWinX = e.getX(); final int glWinY = getHeight() - e.getY() - 1; + final PMVMatrix pmv = new PMVMatrix(); final float[] objPos = new float[3]; final Shape shape = activeShape; - winToObjCoord(shape, glWinX, glWinY, objPos, () -> { - shape.dispatchGestureEvent(Scene.this, gh, glWinX, glWinY, objPos); + winToObjCoord(shape, glWinX, glWinY, pmv, objPos, () -> { + shape.dispatchGestureEvent(gh, glWinX, glWinY, pmv, renderer.getViewport(), objPos); }); } } @@ -788,7 +748,7 @@ public final class Scene implements GLEventListener { */ final void dispatchMouseEvent(final MouseEvent e, final int glWinX, final int glWinY) { if( null == activeShape ) { - dispatchMouseEventPickShape(e, glWinX, glWinY, true); + dispatchMouseEventPickShape(e, glWinX, glWinY); } else { dispatchMouseEventForShape(activeShape, e, glWinX, glWinY); } @@ -798,17 +758,18 @@ public final class Scene implements GLEventListener { * @param e original Newt {@link MouseEvent} * @param glWinX in GL window coordinates, origin bottom-left * @param glWinY in GL window coordinates, origin bottom-left - * @param setActive */ - final void dispatchMouseEventPickShape(final MouseEvent e, final int glWinX, final int glWinY, final boolean setActive) { + final void dispatchMouseEventPickShape(final MouseEvent e, final int glWinX, final int glWinY) { + final PMVMatrix pmv = new PMVMatrix(); final float[] objPos = new float[3]; final Shape[] shape = { null }; - pickShape(glWinX, glWinY, objPos, shape, () -> { - if( setActive ) { - setActiveShape(shape[0]); - } + if( null == pickShape(glWinX, glWinY, pmv, objPos, shape, () -> { + setActiveShape(shape[0]); shape[0].dispatchMouseEvent(e, glWinX, glWinY, objPos); - } ); + } ) ) + { + releaseActiveShape(); + } } /** * Dispatch event to shape @@ -818,8 +779,9 @@ public final class Scene implements GLEventListener { * @param glWinY in GL window coordinates, origin bottom-left */ final void dispatchMouseEventForShape(final Shape shape, final MouseEvent e, final int glWinX, final int glWinY) { + final PMVMatrix pmv = new PMVMatrix(); final float[] objPos = new float[3]; - winToObjCoord(shape, glWinX, glWinY, objPos, () -> { shape.dispatchMouseEvent(e, glWinX, glWinY, objPos); }); + winToObjCoord(shape, glWinX, glWinY, pmv, objPos, () -> { shape.dispatchMouseEvent(e, glWinX, glWinY, objPos); }); } private class SBCMouseListener implements MouseListener { @@ -850,7 +812,7 @@ public final class Scene implements GLEventListener { dispatchMouseEvent(e, glWinX, glWinY); if( 1 == e.getPointerCount() ) { // Release active shape: last pointer has been lifted! - release(); + releaseActiveShape(); clear(); } } @@ -861,9 +823,9 @@ public final class Scene implements GLEventListener { final int glWinX = e.getX(); final int glWinY = getHeight() - e.getY() - 1; // activeId should have been released by mouseRelease() already! - dispatchMouseEventPickShape(e, glWinX, glWinY, false); + dispatchMouseEventPickShape(e, glWinX, glWinY); // Release active shape: last pointer has been lifted! - release(); + releaseActiveShape(); clear(); } @@ -887,7 +849,7 @@ public final class Scene implements GLEventListener { // flip to GL window coordinates final int glWinX = lx; final int glWinY = getHeight() - ly - 1; - dispatchMouseEventPickShape(e, glWinX, glWinY, true); + dispatchMouseEvent(e, glWinX, glWinY); } @Override @@ -897,12 +859,16 @@ public final class Scene implements GLEventListener { ly = e.getY(); lId = e.getPointerId(0); } + final int glWinX = lx; + final int glWinY = getHeight() - ly - 1; + // dispatchMouseEvent(e, glWinX, glWinY); + dispatchMouseEventPickShape(e, glWinX, glWinY); } @Override public void mouseEntered(final MouseEvent e) { } @Override public void mouseExited(final MouseEvent e) { - release(); + releaseActiveShape(); clear(); } } @@ -948,4 +914,35 @@ public final class Scene implements GLEventListener { return String.format("%03.1f/%03.1f fps, %.1f ms/f", lfps, tfps, td); } + private final PMVMatrixSetup defaultPMVMatrixSetup = new PMVMatrixSetup() { + @Override + public void set(final PMVMatrix pmv, final int x, final int y, final int width, final int height) { + final float ratio = (float)width/(float)height; + pmv.glMatrixMode(GLMatrixFunc.GL_PROJECTION); + pmv.glLoadIdentity(); + pmv.gluPerspective(DEFAULT_ANGLE, ratio, DEFAULT_ZNEAR, DEFAULT_ZFAR); + pmv.glTranslatef(0f, 0f, DEFAULT_SCENE_DIST); + + pmv.glMatrixMode(GLMatrixFunc.GL_MODELVIEW); + pmv.glLoadIdentity(); + } + + @Override + public void setPlaneBox(final AABBox planeBox, final PMVMatrix pmv, final int x, final int y, final int width, final int height) { + final float orthoDist = -DEFAULT_SCENE_DIST; + final float[] obj00Coord = new float[3]; + final float[] obj11Coord = new float[3]; + + winToPlaneCoord(pmv, getViewport(), DEFAULT_ZNEAR, DEFAULT_ZFAR, x, y, orthoDist, obj00Coord); + winToPlaneCoord(pmv, getViewport(), DEFAULT_ZNEAR, DEFAULT_ZFAR, width, height, orthoDist, obj11Coord); + + planeBox.setSize( obj00Coord[0], // lx + obj00Coord[1], // ly + obj00Coord[2], // lz + obj11Coord[0], // hx + obj11Coord[1], // hy + obj11Coord[2] );// hz } + } + }; + private PMVMatrixSetup pmvMatrixSetup = defaultPMVMatrixSetup; } -- cgit v1.2.3