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 package fx83dfeatures; 26 27 import javafx.application.Application; 28 import javafx.event.EventHandler; 29 import javafx.scene.Group; 30 import javafx.scene.PerspectiveCamera; 31 import javafx.scene.Scene; 32 import javafx.scene.input.MouseEvent; 33 import javafx.scene.paint.Color; 34 import javafx.scene.shape.Rectangle; 35 import javafx.stage.Stage; 36 37 public class NearAndFarClipTest extends Application { 38 39 private static final double OFFSET_PERCENT = 0.01; // 1 percent tolerance 40 private static final int WIDTH = 500; 41 private static final int HEIGHT = 500; 42 private static final double FOV = 30.0; 43 private static final double NEAR = 0.1; 44 private static final double FAR = 10.0; 45 46 private Scene createClipPlanes(Stage stage) { 47 stage.setTitle("Near and Far Clip Test"); 48 49 final double tanOfHalfFOV = Math.tan(Math.toRadians(FOV) / 2.0); 50 final double halfHeight = HEIGHT / 2; 51 final double focalLength = halfHeight / tanOfHalfFOV; 52 final double eyePositionZ = -1.0 * focalLength; 53 final double nearClipDistance = focalLength * NEAR + eyePositionZ; 54 final double farClipDistance = focalLength * FAR + eyePositionZ; 55 final double nearClipDistanceOffset = Math.abs(nearClipDistance * OFFSET_PERCENT); 56 final double farClipDistanceOffset = Math.abs(farClipDistance * OFFSET_PERCENT); 57 58 System.out.println("In scene coordinate: focalLength = " + focalLength 59 + ", nearClipDistance = " + nearClipDistance 60 + ", nearClipDistanceOffset = " + nearClipDistanceOffset 61 + ", farClipDistance = " + farClipDistance 62 + ", farClipDistanceOffset = " + farClipDistanceOffset); 63 64 Rectangle insideRect = new Rectangle(220, 220, Color.GREEN); 65 insideRect.setLayoutX(140); 66 insideRect.setLayoutY(140); 67 insideRect.setId("Green"); 68 69 Rectangle insideNearClip = new Rectangle(16, 16, Color.BLUE); 70 insideNearClip.setLayoutX(242); 71 insideNearClip.setLayoutY(242); 72 insideNearClip.setTranslateZ(nearClipDistance + nearClipDistanceOffset); 73 insideNearClip.setId("Blue"); 74 75 Rectangle outsideNearClip = new Rectangle(16, 16, Color.YELLOW); 76 outsideNearClip.setLayoutX(242); 77 outsideNearClip.setLayoutY(242); 78 outsideNearClip.setTranslateZ(nearClipDistance - nearClipDistanceOffset); 79 outsideNearClip.setId("Yellow"); 80 81 Rectangle insideFarClip = new Rectangle(3000, 3000, Color.RED); 82 insideFarClip.setTranslateX(-1250); 83 insideFarClip.setTranslateY(-1250); 84 insideFarClip.setTranslateZ(farClipDistance - farClipDistanceOffset); 85 insideFarClip.setId("Red"); 86 87 Rectangle outsideFarClip = new Rectangle(4000, 4000, Color.CYAN); 88 outsideFarClip.setTranslateX(-1750); 89 outsideFarClip.setTranslateY(-1750); 90 outsideFarClip.setTranslateZ(farClipDistance + farClipDistanceOffset); 91 outsideFarClip.setId("Cyan"); 92 93 Group root = new Group(); 94 95 // Render in painter order (far to near) 96 root.getChildren().addAll(outsideFarClip, insideFarClip, insideRect, insideNearClip, outsideNearClip); 97 98 // Intentionally set depth buffer to false to reduce test complexity 99 Scene scene = new Scene(root, WIDTH, HEIGHT, false); 100 scene.setOnMousePressed(new EventHandler<MouseEvent>() { 101 @Override public void handle(MouseEvent event) { 102 System.out.println("Clicked: " + event.getTarget()); 103 } 104 }); 105 106 107 PerspectiveCamera camera = new PerspectiveCamera(); 108 camera.setFieldOfView(FOV); 109 camera.setNearClip(NEAR); 110 camera.setFarClip(FAR); 111 scene.setCamera(camera); 112 return scene; 113 } 114 115 @Override public void start(Stage stage) { 116 Scene scene = createClipPlanes(stage); 117 scene.setFill(Color.GRAY); 118 stage.setScene(scene); 119 stage.sizeToScene(); 120 stage.setResizable(false); 121 stage.show(); 122 System.out.println("You should expect to see 3 overlapping squares in" 123 + " the order of Blue is on top of Green and Green is on top Red.");; 124 } 125 126 public static void main(String[] args) { 127 Application.launch(args); 128 } 129 130 }