/* * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package meshviewer; import javafx.animation.Interpolator; import javafx.animation.RotateTransition; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.KeyEvent; import javafx.scene.paint.Color; import javafx.scene.paint.PhongMaterial; import javafx.scene.shape.*; import javafx.scene.transform.Rotate; import javafx.stage.Stage; import javafx.util.Duration; public class Shape3DCulling extends Application { Group root, shapes, snapshots; MeshView meshView; TriangleMesh triMesh; PhongMaterial material; Box box; Cylinder cylinder; Sphere sphere; Scene scene; static TriangleMesh buildBox(int subDivX, int subDivY, int subDivZ) { float hw = subDivX / 2f; float hh = subDivY / 2f; float hd = subDivZ / 2f; float points[] = { -hw, -hh, -hd, hw, -hh, -hd, hw, hh, -hd, -hw, hh, -hd, -hw, -hh, hd, hw, -hh, hd, hw, hh, hd, -hw, hh, hd}; float texCoords[] = {0, 0, 1, 0, 1, 1, 0, 1}; int faceSmoothingGroups[] = { 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4 }; int faces[] = { 0, 0, 2, 2, 1, 1, 2, 2, 0, 0, 3, 3, 1, 0, 6, 2, 5, 1, 6, 2, 1, 0, 2, 3, 5, 0, 7, 2, 4, 1, 7, 2, 5, 0, 6, 3, 4, 0, 3, 2, 0, 1, 3, 2, 4, 0, 7, 3, 3, 0, 6, 2, 2, 1, 6, 2, 3, 0, 7, 3, 4, 0, 1, 2, 5, 1, 1, 2, 4, 0, 0, 3, }; TriangleMesh mesh = new TriangleMesh(points, texCoords, faces); mesh.setFaceSmoothingGroups(faceSmoothingGroups); return mesh; } private Group buildScene() { triMesh = buildBox(100, 200, 300); Image diffuseMap = new Image("/resources/cup_diffuseMap_1024.png"); CullFace cullFace = CullFace.FRONT; material = new PhongMaterial(); material.setDiffuseMap(diffuseMap); material.setSpecularColor(Color.rgb(30, 30, 30)); meshView = new MeshView(triMesh); meshView.setMaterial(material); meshView.setTranslateX(200); meshView.setTranslateY(200); meshView.setCullFace(cullFace); box = new Box(100, 85, 90); box.setMaterial(material); box.setTranslateX(300); box.setTranslateY(400); box.setTranslateZ(50); box.setCullFace(cullFace); cylinder = new Cylinder(50, 70, 100); cylinder.setMaterial(material); cylinder.setTranslateX(500); cylinder.setTranslateY(400); cylinder.setTranslateZ(50); cylinder.setCullFace(cullFace); sphere = new Sphere(50, 50); sphere.setMaterial(material); sphere.setTranslateX(700); sphere.setTranslateY(400); sphere.setTranslateZ(50); sphere.setCullFace(cullFace); shapes = new Group(meshView, box, cylinder, sphere); RotateTransition rotTrans = new RotateTransition(Duration.seconds(35), shapes); rotTrans.setAutoReverse(false); rotTrans.setAxis(Rotate.X_AXIS); rotTrans.setInterpolator(Interpolator.EASE_BOTH); rotTrans.setCycleCount(Timeline.INDEFINITE); rotTrans.setByAngle(-360); rotTrans.play(); root = new Group(shapes, new AmbientLight()); return root; } private PerspectiveCamera addCamera(Scene scene) { PerspectiveCamera perspectiveCamera = new PerspectiveCamera(false); scene.setCamera(perspectiveCamera); return perspectiveCamera; } @Override public void start(Stage primaryStage) { scene = new Scene(buildScene(), 800, 800, true); scene.setFill(Color.rgb(10, 10, 40)); scene.setOnKeyTyped(new EventHandler() { @Override public void handle(KeyEvent e) { switch (e.getCharacter()) { case "s": System.out.println("snapshot"); Image img = scene.snapshot(null); ImageView iv = new ImageView(img); iv.setTranslateX(800); if (root.getChildren().size() == 2) { root.getChildren().add(iv); } else { root.getChildren().set(2, iv); } } } }); addCamera(scene); primaryStage.setTitle("Shape3DCulling"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { System.setProperty("prism.dirtyopts", "false"); System.setProperty("prism.verbose", "true"); launch(args); } }