1 /*
   2  * Copyright (c) 2013, 2014, 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(() -> {
  59             testStage = getStage();
  60             testStage.setTitle("Near and Far Clip Test");
  61 
  62             final double tanOfHalfFOV = Math.tan(Math.toRadians(FOV) / 2.0);
  63             final double halfHeight = HEIGHT / 2;
  64             final double focalLength = halfHeight / tanOfHalfFOV;
  65             final double eyePositionZ = -1.0 * focalLength;
  66             final double nearClipDistance = focalLength * NEAR + eyePositionZ;
  67             final double farClipDistance =  focalLength * FAR + eyePositionZ;
  68             final double nearClipDistanceOffset = Math.abs(nearClipDistance * OFFSET_PERCENT);
  69             final double farClipDistanceOffset = Math.abs(farClipDistance * OFFSET_PERCENT);
  70       
  71 //                System.out.println("In scene coordinate: focalLength = " + focalLength
  72 //                        + ", nearClipDistance = " + nearClipDistance
  73 //                        + ", nearClipDistanceOffset = " + nearClipDistanceOffset
  74 //                        + ", farClipDistance = " + farClipDistance
  75 //                        + ", farClipDistanceOffset = " + farClipDistanceOffset);
  76 
  77             Rectangle insideRect = new Rectangle(220, 220, Color.GREEN);
  78             insideRect.setLayoutX(140);
  79             insideRect.setLayoutY(140);
  80 
  81             Rectangle insideNearClip = new Rectangle(16, 16, Color.BLUE);
  82             insideNearClip.setLayoutX(242);
  83             insideNearClip.setLayoutY(242);
  84             insideNearClip.setTranslateZ(nearClipDistance + nearClipDistanceOffset);
  85 
  86             Rectangle outsideNearClip = new Rectangle(16, 16, Color.YELLOW);
  87             outsideNearClip.setLayoutX(242);
  88             outsideNearClip.setLayoutY(242);
  89             outsideNearClip.setTranslateZ(nearClipDistance - nearClipDistanceOffset);
  90 
  91             Rectangle insideFarClip = new Rectangle(3000, 3000, Color.RED);
  92             insideFarClip.setTranslateX(-1250);
  93             insideFarClip.setTranslateY(-1250);
  94             insideFarClip.setTranslateZ(farClipDistance - farClipDistanceOffset);
  95 
  96             Rectangle outsideFarClip = new Rectangle(4000, 4000, Color.CYAN);
  97             outsideFarClip.setTranslateX(-1750);
  98             outsideFarClip.setTranslateY(-1750);
  99             outsideFarClip.setTranslateZ(farClipDistance + farClipDistanceOffset);
 100 
 101             Group root = new Group();
 102 
 103             // Render in painter order (far to near)
 104             root.getChildren().addAll(outsideFarClip, insideFarClip, insideRect, insideNearClip, outsideNearClip);
 105 
 106             // Intentionally set depth buffer to false to reduce test complexity
 107             testScene = new Scene(root, WIDTH, HEIGHT, false);
 108 
 109             PerspectiveCamera camera = new PerspectiveCamera();
 110             camera.setFieldOfView(FOV);
 111             camera.setNearClip(NEAR);
 112             camera.setFarClip(FAR);
 113             testScene.setCamera(camera);
 114 
 115             testStage.setScene(testScene);
 116             testStage.show();
 117         });
 118         waitFirstFrame();
 119         runAndWait(() -> {
 120             
 121             if (!Platform.isSupported(ConditionalFeature.SCENE3D)) {
 122                 System.out.println("*************************************************************");
 123                 System.out.println("*      Platform isn't SCENE3D capable, skipping 3D test.    *");
 124                 System.out.println("*************************************************************");
 125                 return;
 126             }
 127 
 128             Color color;
 129             // Verify Near Clip
 130             color = getColor(testScene, WIDTH / 2, HEIGHT / 2);
 131             assertColorEquals(Color.BLUE, color, TOLERANCE);
 132 
 133             // Verify Inside Rect
 134             color = getColor(testScene, (WIDTH / 3), HEIGHT / 2);
 135             assertColorEquals(Color.GREEN, color, TOLERANCE);
 136 
 137             // Verify Far Clip
 138             color = getColor(testScene, WIDTH / 5, HEIGHT / 2);
 139             assertColorEquals(Color.RED, color, TOLERANCE);
 140 
 141             // Verify Fill
 142             color = getColor(testScene, WIDTH / 8, HEIGHT / 2);
 143             assertColorEquals(Color.WHITE, color, TOLERANCE);
 144         });
 145     }
 146 
 147 }