1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package test3d;
  27 
  28 import javafx.application.ConditionalFeature;
  29 import javafx.application.Platform;
  30 import javafx.scene.Group;
  31 import javafx.scene.PerspectiveCamera;
  32 import javafx.scene.Scene;
  33 import javafx.scene.paint.Color;
  34 import javafx.scene.shape.Rectangle;
  35 import javafx.stage.Stage;
  36 import org.junit.Test;
  37 import testharness.VisualTestBase;
  38 
  39 /**
  40  * Basic visual near and far clipping tests using glass Robot to sample pixels.
  41  */
  42 public class NearAndFarClipTest extends VisualTestBase {
  43 
  44     private Stage testStage;
  45     private Scene testScene;
  46 
  47     private static final double TOLERANCE = 0.07;
  48     private static final double OFFSET_PERCENT = 0.01; // 1 percent tolerance
  49 
  50     @Test(timeout=5000)
  51     public void testNearAndFarClips() {
  52         final int WIDTH = 500;
  53         final int HEIGHT = 500;
  54         final double FOV = 30.0;
  55         final double NEAR = 0.1;
  56         final double FAR = 10.0;
  57 
  58         runAndWait(new Runnable() {
  59             @Override
  60             public void run() {
  61                 testStage = getStage();
  62                 testStage.setTitle("Near and Far Clip Test");
  63 
  64                 final double tanOfHalfFOV = Math.tan(Math.toRadians(FOV) / 2.0);
  65                 final double halfHeight = HEIGHT / 2;
  66                 final double focalLength = halfHeight / tanOfHalfFOV;
  67                 final double eyePositionZ = -1.0 * focalLength;
  68                 final double nearClipDistance = focalLength * NEAR + eyePositionZ;
  69                 final double farClipDistance =  focalLength * FAR + eyePositionZ;
  70                 final double nearClipDistanceOffset = Math.abs(nearClipDistance * OFFSET_PERCENT);
  71                 final double farClipDistanceOffset = Math.abs(farClipDistance * OFFSET_PERCENT);
  72         
  73 //                System.out.println("In scene coordinate: focalLength = " + focalLength
  74 //                        + ", nearClipDistance = " + nearClipDistance
  75 //                        + ", nearClipDistanceOffset = " + nearClipDistanceOffset
  76 //                        + ", farClipDistance = " + farClipDistance
  77 //                        + ", farClipDistanceOffset = " + farClipDistanceOffset);
  78 
  79                 Rectangle insideRect = new Rectangle(220, 220, Color.GREEN);
  80                 insideRect.setLayoutX(140);
  81                 insideRect.setLayoutY(140);
  82 
  83                 Rectangle insideNearClip = new Rectangle(16, 16, Color.BLUE);
  84                 insideNearClip.setLayoutX(242);
  85                 insideNearClip.setLayoutY(242);
  86                 insideNearClip.setTranslateZ(nearClipDistance + nearClipDistanceOffset);
  87 
  88                 Rectangle outsideNearClip = new Rectangle(16, 16, Color.YELLOW);
  89                 outsideNearClip.setLayoutX(242);
  90                 outsideNearClip.setLayoutY(242);
  91                 outsideNearClip.setTranslateZ(nearClipDistance - nearClipDistanceOffset);
  92 
  93                 Rectangle insideFarClip = new Rectangle(3000, 3000, Color.RED);
  94                 insideFarClip.setTranslateX(-1250);
  95                 insideFarClip.setTranslateY(-1250);
  96                 insideFarClip.setTranslateZ(farClipDistance - farClipDistanceOffset);
  97 
  98                 Rectangle outsideFarClip = new Rectangle(4000, 4000, Color.CYAN);
  99                 outsideFarClip.setTranslateX(-1750);
 100                 outsideFarClip.setTranslateY(-1750);
 101                 outsideFarClip.setTranslateZ(farClipDistance + farClipDistanceOffset);
 102 
 103                 Group root = new Group();
 104 
 105                 // Render in painter order (far to near)
 106                 root.getChildren().addAll(outsideFarClip, insideFarClip, insideRect, insideNearClip, outsideNearClip);
 107 
 108                 // Intentionally set depth buffer to false to reduce test complexity
 109                 testScene = new Scene(root, WIDTH, HEIGHT, false);
 110 
 111                 PerspectiveCamera camera = new PerspectiveCamera();
 112                 camera.setFieldOfView(FOV);
 113                 camera.setNearClip(NEAR);
 114                 camera.setFarClip(FAR);
 115                 testScene.setCamera(camera);
 116 
 117                 testStage.setScene(testScene);
 118                 testStage.show();
 119             }
 120         });
 121         waitFirstFrame();
 122         runAndWait(new Runnable() {
 123             @Override public void run() {
 124                 
 125                 if (!Platform.isSupported(ConditionalFeature.SCENE3D)) {
 126                     System.out.println("*************************************************************");
 127                     System.out.println("*      Platform isn't SCENE3D capable, skipping 3D test.    *");
 128                     System.out.println("*************************************************************");
 129                     return;
 130                 }
 131 
 132                 Color color;
 133                 // Verify Near Clip
 134                 color = getColor(testScene, WIDTH / 2, HEIGHT / 2);
 135                 assertColorEquals(Color.BLUE, color, TOLERANCE);
 136 
 137                 // Verify Inside Rect
 138                 color = getColor(testScene, (WIDTH / 3), HEIGHT / 2);
 139                 assertColorEquals(Color.GREEN, color, TOLERANCE);
 140 
 141                 // Verify Far Clip
 142                 color = getColor(testScene, WIDTH / 5, HEIGHT / 2);
 143                 assertColorEquals(Color.RED, color, TOLERANCE);
 144 
 145                 // Verify Fill
 146                 color = getColor(testScene, WIDTH / 8, HEIGHT / 2);
 147                 assertColorEquals(Color.WHITE, color, TOLERANCE);
 148             }
 149         });
 150     }
 151 
 152 }