changeset 5576:73bb1f669a2c

RT-30371: Fixed parallel camera position.
author Pavel Safrata <pavel.safrata@oracle.com>
date Wed, 30 Oct 2013 08:27:30 +0100
parents 5113f2c3f1ae
children 29bc314ef771
files modules/graphics/src/main/java/com/sun/javafx/geom/PickRay.java modules/graphics/src/main/java/com/sun/javafx/sg/prism/NGParallelCamera.java modules/graphics/src/main/java/javafx/scene/ParallelCamera.java modules/graphics/src/main/java/javafx/scene/Scene.java modules/graphics/src/test/java/javafx/scene/CameraTest.java modules/graphics/src/test/java/javafx/scene/Mouse3DTest.java modules/graphics/src/test/java/javafx/scene/NodeTest.java
diffstat 7 files changed, 63 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/modules/graphics/src/main/java/com/sun/javafx/geom/PickRay.java	Tue Oct 29 18:52:03 2013 -0700
+++ b/modules/graphics/src/main/java/com/sun/javafx/geom/PickRay.java	Wed Oct 30 08:27:30 2013 +0100
@@ -47,8 +47,8 @@
         set(origin, direction, nearClip, farClip);
     }
 
-    public PickRay(double x, double y, double nearClip, double farClip) {
-        set(x, y, nearClip, farClip);
+    public PickRay(double x, double y, double z, double nearClip, double farClip) {
+        set(x, y, z, nearClip, farClip);
     }
 
     public static PickRay computePerspectivePickRay(
@@ -93,7 +93,7 @@
     }
 
     public static PickRay computeParallelPickRay(
-            double x, double y,
+            double x, double y, double viewHeight,
             Affine3D cameraTransform,
             double nearClip, double farClip,
             PickRay pickRay) {
@@ -102,7 +102,12 @@
             pickRay = new PickRay();
         }
 
-        pickRay.set(x, y, nearClip, farClip);
+        // This is the same math as in the perspective case, fixed
+        // for the default 30 degrees vertical field of view.
+        final double distanceZ = (viewHeight / 2.0)
+                / Math.tan(Math.toRadians(15.0));
+
+        pickRay.set(x, y, distanceZ, nearClip, farClip);
 
         if (cameraTransform != null) {
             pickRay.transform(cameraTransform);
@@ -118,17 +123,9 @@
         this.farClip = farClip;
     }
 
-    public final void set(double x, double y, double nearClip, double farClip) {
-        // Right now the parallel camera picks nodes even on negative distances
-        // (behind the camera). Therefore, it doesn't matter
-        // what is the Z coordinate of the origin. Also the reported distance
-        // is always an infinity so it doesn't matter what is the magnitude
-        // of the direction.
-        // Right now we need the (origin+direction) point be in the XY plane
-        // for correct picking of the scene. This requirement will be removed
-        // when the projection plane is properly used for the intersection.
-        setOrigin(x, y, -1);
-        setDirection(0, 0, 1);
+    public final void set(double x, double y, double z, double nearClip, double farClip) {
+        setOrigin(x, y, -z);
+        setDirection(0, 0, z);
         this.nearClip = nearClip;
         this.farClip = farClip;
     }
--- a/modules/graphics/src/main/java/com/sun/javafx/sg/prism/NGParallelCamera.java	Tue Oct 29 18:52:03 2013 -0700
+++ b/modules/graphics/src/main/java/com/sun/javafx/sg/prism/NGParallelCamera.java	Wed Oct 30 08:27:30 2013 +0100
@@ -36,7 +36,7 @@
 
     @Override
     public PickRay computePickRay(float x, float y, PickRay pickRay) {
-        return PickRay.computeParallelPickRay(x, y, worldTransform, zNear, zFar,
-                pickRay);
+        return PickRay.computeParallelPickRay(x, y, viewHeight, worldTransform,
+                zNear, zFar, pickRay);
     }
 }
--- a/modules/graphics/src/main/java/javafx/scene/ParallelCamera.java	Tue Oct 29 18:52:03 2013 -0700
+++ b/modules/graphics/src/main/java/javafx/scene/ParallelCamera.java	Wed Oct 30 08:27:30 2013 +0100
@@ -71,7 +71,8 @@
 
     @Override
     final PickRay computePickRay(double x, double y, PickRay pickRay) {
-        return PickRay.computeParallelPickRay(x, y, getCameraTransform(),
+        return PickRay.computeParallelPickRay(x, y, getViewHeight(),
+                getCameraTransform(),
                 //TODO: use actual clips after rendering uses them
                 Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, pickRay);
     }
@@ -97,7 +98,13 @@
             position = new Vec3d();
         }
 
-        position.set(0.0, 0.0, -1.0);
+        // This is the same math as in PerspectiveCamera, fixed for the default
+        // 30 degrees vertical field of view.
+        final double halfViewWidth = getViewWidth() / 2.0;
+        final double halfViewHeight = getViewHeight() / 2.0;
+        final double distanceZ = halfViewHeight / Math.tan(Math.toRadians(15.0));
+        position.set(halfViewWidth, halfViewHeight, -distanceZ);
+
         return position;
     }
 }
--- a/modules/graphics/src/main/java/javafx/scene/Scene.java	Tue Oct 29 18:52:03 2013 -0700
+++ b/modules/graphics/src/main/java/javafx/scene/Scene.java	Wed Oct 30 08:27:30 2013 +0100
@@ -1794,7 +1794,7 @@
      */
     Node test_pick(double x, double y) {
         inMousePick = true;
-        PickResult result = mouseHandler.pickNode(new PickRay(x, y,
+        PickResult result = mouseHandler.pickNode(new PickRay(x, y, 1,
                 Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY));
         inMousePick = false;
         if (result != null) {
--- a/modules/graphics/src/test/java/javafx/scene/CameraTest.java	Tue Oct 29 18:52:03 2013 -0700
+++ b/modules/graphics/src/test/java/javafx/scene/CameraTest.java	Wed Oct 30 08:27:30 2013 +0100
@@ -488,9 +488,9 @@
         Camera cam = new ParallelCamera();
         scene.setCamera(cam);
         Vec3d v = cam.computePosition(null);
-        assertEquals(0.0, v.x, 0.000001);
-        assertEquals(0.0, v.y, 0.000001);
-        assertEquals(-1.0, v.z, 0.000001);
+        assertEquals(150.0, v.x, 0.000001);
+        assertEquals(100.0, v.y, 0.000001);
+        assertEquals(-373.205080, v.z, 0.000001);
     }
 
     @Test
@@ -584,9 +584,9 @@
         expected.ortho(0.0, 300, 200, 0.0, -150, 150);
 
         TransformHelper.assertMatrix(pc.getProjViewTx(), expected);
-        assertEquals(0.0, pc.getPosition().x, 0.000001);
-        assertEquals(0.0, pc.getPosition().y, 0.000001);
-        assertEquals(-1.0, pc.getPosition().z, 0.000001);
+        assertEquals(150.0, pc.getPosition().x, 0.000001);
+        assertEquals(100.0, pc.getPosition().y, 0.000001);
+        assertEquals(-373.205080, pc.getPosition().z, 0.000001);
     }
 
     @Test
@@ -610,9 +610,9 @@
         expected.ortho(0.0, 300, 200, 0.0, -150, 150);
 
         TransformHelper.assertMatrix(pc.getProjViewTx(), expected);
-        assertEquals(0.0, pc.getPosition().x, 0.000001);
-        assertEquals(0.0, pc.getPosition().y, 0.000001);
-        assertEquals(-1.0, pc.getPosition().z, 0.000001);
+        assertEquals(150.0, pc.getPosition().x, 0.000001);
+        assertEquals(100.0, pc.getPosition().y, 0.000001);
+        assertEquals(-373.205080, pc.getPosition().z, 0.000001);
     }
 
     @Test
@@ -635,9 +635,9 @@
         expected.ortho(0.0, 300, 200, 0.0, -150, 150);
 
         TransformHelper.assertMatrix(pc.getProjViewTx(), expected);
-        assertEquals(0.0, pc.getPosition().x, 0.000001);
-        assertEquals(0.0, pc.getPosition().y, 0.000001);
-        assertEquals(-1.0, pc.getPosition().z, 0.000001);
+        assertEquals(150.0, pc.getPosition().x, 0.000001);
+        assertEquals(100.0, pc.getPosition().y, 0.000001);
+        assertEquals(-373.205080, pc.getPosition().z, 0.000001);
     }
 
     @Test
@@ -661,9 +661,9 @@
         expected.ortho(0.0, 300, 200, 0.0, -150, 150);
 
         TransformHelper.assertMatrix(pc.getProjViewTx(), expected);
-        assertEquals(0.0, pc.getPosition().x, 0.000001);
-        assertEquals(0.0, pc.getPosition().y, 0.000001);
-        assertEquals(-1.0, pc.getPosition().z, 0.000001);
+        assertEquals(150.0, pc.getPosition().x, 0.000001);
+        assertEquals(100.0, pc.getPosition().y, 0.000001);
+        assertEquals(-373.205080, pc.getPosition().z, 0.000001);
     }
 
     @Test
--- a/modules/graphics/src/test/java/javafx/scene/Mouse3DTest.java	Tue Oct 29 18:52:03 2013 -0700
+++ b/modules/graphics/src/test/java/javafx/scene/Mouse3DTest.java	Wed Oct 30 08:27:30 2013 +0100
@@ -157,10 +157,10 @@
         PickRay pickRay = cam.computePickRay(10, 20, null);
         assertEquals(10.0, pickRay.getOriginNoClone().x, 0.00001);
         assertEquals(20.0, pickRay.getOriginNoClone().y, 0.00001);
-        assertEquals(-1, pickRay.getOriginNoClone().z, 0.1);
+        assertEquals(PERSPECTIVE_CAMERA_Z, pickRay.getOriginNoClone().z, 0.1);
         assertEquals(0.0, pickRay.getDirectionNoClone().x, 0.00001);
         assertEquals(0.0, pickRay.getDirectionNoClone().y, 0.00001);
-        assertEquals(1.0, pickRay.getDirectionNoClone().z, 0.1);
+        assertEquals(-PERSPECTIVE_CAMERA_Z, pickRay.getDirectionNoClone().z, 0.1);
     }
 
     @Test
@@ -173,10 +173,10 @@
         PickRay pickRay = cam.computePickRay(10, 20, null);
         assertEquals(60.0, pickRay.getOriginNoClone().x, 0.00001);
         assertEquals(50.0, pickRay.getOriginNoClone().y, 0.00001);
-        assertEquals(-1, pickRay.getOriginNoClone().z, 0.1);
+        assertEquals(PERSPECTIVE_CAMERA_Z, pickRay.getOriginNoClone().z, 0.1);
         assertEquals(0.0, pickRay.getDirectionNoClone().x, 0.00001);
         assertEquals(0.0, pickRay.getDirectionNoClone().y, 0.00001);
-        assertEquals(1.0, pickRay.getDirectionNoClone().z, 0.1);
+        assertEquals(-PERSPECTIVE_CAMERA_Z, pickRay.getDirectionNoClone().z, 0.1);
     }
 
 
@@ -317,7 +317,7 @@
         assertNotNull(e);
         assertCoordinates(e, 10, 40, 10, 40, -200);
         assertPickResult(e.getPickResult(),
-                b, point(10, 40, -200), -199, NOFACE, point(0.6, 0.7));
+                b, point(10, 40, -200), 1292.82032, NOFACE, point(0.6, 0.7));
     }
 
     @Test
@@ -331,7 +331,7 @@
         assertNotNull(e);
         assertCoordinates(e, 10, 40, 10, -40, 200);
         assertPickResult(e.getPickResult(),
-                b, point(10, -40, 200), -199, NOFACE, point(0.4, 0.3));
+                b, point(10, -40, 200), 1292.82032, NOFACE, point(0.4, 0.3));
     }
 
     @Test
@@ -458,7 +458,7 @@
         assertNotNull(e);
         assertCoordinates(e, 10, 40, 10, 40, -200);
         assertPickResult(e.getPickResult(),
-                b, point(10, 40, -200), -1199, NOFACE, point(0.6, 0.7));
+                b, point(10, 40, -200), 292.82032, NOFACE, point(0.6, 0.7));
     }
 
     @Test
@@ -487,7 +487,7 @@
         assertNotNull(e);
         assertCoordinates(e, 10, 40, 10, 40, -200);
         assertPickResult(e.getPickResult(),
-                b, point(10, 40, -200), -3199, NOFACE, point(0.6, 0.7));
+                b, point(10, 40, -200), -1707.17968, NOFACE, point(0.6, 0.7));
     }
 
     @Test
@@ -767,7 +767,7 @@
         assertNotNull(e);
         assertCoordinates(e, 10, 20, 10, 20, -97.46794);
         assertPickResult(e.getPickResult(),
-                sph, point(10, 20, -97.46794), -1096.46794, NOFACE, point(0.516273, 0.6));
+                sph, point(10, 20, -97.46794), 395.35238, NOFACE, point(0.516273, 0.6));
     }
 
     @Test
@@ -796,7 +796,7 @@
         assertNotNull(e);
         assertCoordinates(e, 10, 20, 10, 20, -97.46794);
         assertPickResult(e.getPickResult(),
-                sph, point(10, 20, -97.46794), -1194.46794, NOFACE, point(0.516273, 0.6));
+                sph, point(10, 20, -97.46794), 297.35238, NOFACE, point(0.516273, 0.6));
     }
 
     @Test
@@ -1172,7 +1172,7 @@
         assertNotNull(e);
         assertCoordinates(e, 10, 20, 10, 20, -48.98979);
         assertPickResult(e.getPickResult(),
-                c, point(10, 20, -48.98979), -1047.98979, NOFACE, point(0.532048, 0.6));
+                c, point(10, 20, -48.98979), 443.83053, NOFACE, point(0.532048, 0.6));
     }
 
     @Test
@@ -1201,7 +1201,7 @@
         assertNotNull(e);
         assertCoordinates(e, 10, 20, 10, 20, -48.98979);
         assertPickResult(e.getPickResult(),
-                c, point(10, 20, -48.98979), -1096.98979, NOFACE, point(0.532048, 0.6));
+                c, point(10, 20, -48.98979), 394.83053, NOFACE, point(0.532048, 0.6));
     }
 
     @Test
@@ -1532,7 +1532,7 @@
         assertNotNull(e);
         assertCoordinates(e, 60, 20, 60, 20, 0);
         assertPickResult(e.getPickResult(),
-                m, point(60, 20, 0), -2999, 0, point(0.6, 0.2));
+                m, point(60, 20, 0), -1507.17968, 0, point(0.6, 0.2));
     }
 
     @Test
@@ -1559,7 +1559,7 @@
         assertNotNull(e);
         assertCoordinates(e, 10, 20, 10, 20, 10);
         assertPickResult(e.getPickResult(),
-                m, point(10, 20, 10), -1000, 0, point(0.1, 0.2));
+                m, point(10, 20, 10), 491.82032, 0, point(0.1, 0.2));
     }
 
     @Test
@@ -2038,13 +2038,13 @@
         assertNotNull(e);
         assertCoordinates(e, 10, 50, 10, 50, 0);
         assertPickResult(e.getPickResult(), r, point(10, 50, 0), 
-                71, NOFACE, null);
+                1562.82032, NOFACE, null);
 
         e = sme.event;
         assertNotNull(e);
         assertCoordinates(e, 10, 50, 10, 50, 70);
         assertPickResult(e.getPickResult(), r, point(10, 50, 0),
-                71, NOFACE, null);
+                1562.82032, NOFACE, null);
     }
 
     @Test
@@ -2063,7 +2063,7 @@
         assertNotNull(e);
         assertCoordinates(e, 10, 50, 10, 50, 0);
         assertPickResult(e.getPickResult(), null, point(10, 50, 0),
-                1, NOFACE, null);
+                1492.82032, NOFACE, null);
         sme.clear();
 
         s.impl_processMouseEvent(g.generateMouseEvent(MouseEvent.MOUSE_MOVED, 30, 50));
@@ -2071,13 +2071,13 @@
         assertNotNull(e);
         assertCoordinates(e, 30, 50, 21.71572, 50, 0);
         assertPickResult(e.getPickResult(), r, point(21.71572, 50, 0),
-                21, NOFACE, null);
+                1512.82032, NOFACE, null);
 
         e = sme.event;
         assertNotNull(e);
         assertCoordinates(e, 30, 50, 30, 50, 20);
         assertPickResult(e.getPickResult(), r, point(21.71572, 50, 0),
-                21, NOFACE, null);
+                1512.82032, NOFACE, null);
     }
 
     @Test
@@ -2337,7 +2337,7 @@
         assertNotNull(e);
         assertCoordinates(e, 30, 40, 10, 40, -200);
         assertPickResult(e.getPickResult(),
-                b, point(10, 40, -200), -199, NOFACE, point(0.6, 0.7));
+                b, point(10, 40, -200), 1292.82032, NOFACE, point(0.6, 0.7));
     }
 
     /*****************   subscene   ********************/
--- a/modules/graphics/src/test/java/javafx/scene/NodeTest.java	Tue Oct 29 18:52:03 2013 -0700
+++ b/modules/graphics/src/test/java/javafx/scene/NodeTest.java	Wed Oct 30 08:27:30 2013 +0100
@@ -247,10 +247,10 @@
         scene.getRoot().getChildren().add(rect);
 
         PickResultChooser res = new PickResultChooser();
-        rect.impl_pickNode(new PickRay(50, 50, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), res);
+        rect.impl_pickNode(new PickRay(50, 50, 1, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), res);
         assertSame(rect, res.getIntersectedNode());
         res = new PickResultChooser();
-        rect.impl_pickNode(new PickRay(0, 0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), res);
+        rect.impl_pickNode(new PickRay(0, 0, 1, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), res);
         assertNull(res.getIntersectedNode());
     }
 
@@ -266,10 +266,10 @@
         scene.getRoot().getChildren().add(rect);
 
         PickResultChooser res = new PickResultChooser();
-        rect.impl_pickNode(new PickRay(50, 50, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), res);
+        rect.impl_pickNode(new PickRay(50, 50, 1, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), res);
         assertSame(rect, res.getIntersectedNode());
         res = new PickResultChooser();
-        rect.impl_pickNode(new PickRay(0, 0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), res);
+        rect.impl_pickNode(new PickRay(0, 0, 1, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), res);
         assertNull(res.getIntersectedNode());
     }