changeset 3657:951b878804e0

3D Viewer App: Added very simple viewer app. Added command line option for initial 3D file. Moved obj import to using PolyMesh
author "Jasper Potts"
date Wed, 22 May 2013 15:57:12 -0700
parents 7a51619418ec
children bfadff5e8544
files apps/experiments/3DViewer/src/main/java/META-INF/MANIFEST.MF apps/experiments/3DViewer/src/main/java/com/javafx/experiments/importers/Importer3D.java apps/experiments/3DViewer/src/main/java/com/javafx/experiments/importers/obj/PolyObjImporter.java apps/experiments/3DViewer/src/main/java/com/javafx/experiments/jfx3dviewer/ContentModel.java apps/experiments/3DViewer/src/main/java/com/javafx/experiments/jfx3dviewer/Jfx3dViewerApp.java apps/experiments/3DViewer/src/main/java/com/javafx/experiments/jfx3dviewer/SimpleViewerApp.java
diffstat 6 files changed, 148 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/apps/experiments/3DViewer/src/main/java/META-INF/MANIFEST.MF	Wed May 22 15:57:12 2013 -0700
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Main-Class: com.javafx.experiments.jfx3dviewer.Jfx3dViewerApp
+
--- a/apps/experiments/3DViewer/src/main/java/com/javafx/experiments/importers/Importer3D.java	Wed May 22 18:22:29 2013 -0400
+++ b/apps/experiments/3DViewer/src/main/java/com/javafx/experiments/importers/Importer3D.java	Wed May 22 15:57:12 2013 -0700
@@ -13,6 +13,7 @@
 import com.javafx.experiments.importers.maya.MayaGroup;
 import com.javafx.experiments.importers.maya.MayaImporter;
 import com.javafx.experiments.importers.obj.ObjImporter;
+import com.javafx.experiments.importers.obj.PolyObjImporter;
 
 /**
  * Base Importer for all supported 3D file formats
@@ -78,10 +79,11 @@
     }
 
     private static Node loadObjFile(String fileUrl) throws IOException {
-        ObjImporter reader = new ObjImporter(fileUrl);
+//        ObjImporter reader = new ObjImporter(fileUrl);
+        PolyObjImporter reader = new PolyObjImporter(fileUrl);
         Group res = new Group();
         for (String key : reader.getMeshes()) {
-            res.getChildren().add(reader.buildMeshView(key));
+            res.getChildren().add(reader.buildPolygonMeshView(key));
         }
         return res;
     }
--- a/apps/experiments/3DViewer/src/main/java/com/javafx/experiments/importers/obj/PolyObjImporter.java	Wed May 22 18:22:29 2013 -0400
+++ b/apps/experiments/3DViewer/src/main/java/com/javafx/experiments/importers/obj/PolyObjImporter.java	Wed May 22 15:57:12 2013 -0700
@@ -39,6 +39,7 @@
 import com.javafx.experiments.shape3d.PolygonMeshView;
 
 import java.io.*;
+import java.net.URL;
 import java.util.*;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -65,7 +66,7 @@
         }
     }
 
-    private static boolean debug = true;
+    private static boolean debug = false;
     private static float scale = 1;
     private static boolean flatXZ = false;
 
@@ -86,8 +87,8 @@
 
     public PolyObjImporter(String filename) throws FileNotFoundException, IOException {
         this.objFilename = filename;
-        log("Reading filename = " + filename);
-        read(new FileInputStream(filename));
+        log("Reading filename = " + filename);;
+        read(new URL(filename).openStream());
     }
 
     public PolyObjImporter(InputStream inputStream) throws IOException {
@@ -282,11 +283,13 @@
                 newUVs.toFloatArray(),
                 faceArrays
         );
-        System.out.println("mesh.points = " + Arrays.toString(mesh.points));
-        System.out.println("mesh.texCoords = " + Arrays.toString(mesh.texCoords));
-        System.out.println("mesh.faces: ");
-        for (int[] face: mesh.faces) {
-            System.out.println("    face:: "+Arrays.toString(face));
+        if (debug) {
+            System.out.println("mesh.points = " + Arrays.toString(mesh.points));
+            System.out.println("mesh.texCoords = " + Arrays.toString(mesh.texCoords));
+            System.out.println("mesh.faces: ");
+            for (int[] face: mesh.faces) {
+                System.out.println("    face:: "+Arrays.toString(face));
+            }
         }
 
         int keyIndex = 2;
--- a/apps/experiments/3DViewer/src/main/java/com/javafx/experiments/jfx3dviewer/ContentModel.java	Wed May 22 18:22:29 2013 -0400
+++ b/apps/experiments/3DViewer/src/main/java/com/javafx/experiments/jfx3dviewer/ContentModel.java	Wed May 22 15:57:12 2013 -0700
@@ -136,7 +136,7 @@
     private boolean wireframe = false;
     private int subdivision = 0;
 
-    public ContentModel() {
+    public ContentModel(String fileToLoad) {
         subScene = new SubScene(root3D,400,400,true,false);
         subScene.setFill(Color.ALICEBLUE);
 
@@ -169,7 +169,11 @@
 
         // LOAD DROP HERE MODEL
         try {
-            content = Importer3D.load(ContentModel.class.getResource("drop-here.obj").toExternalForm());
+            if (fileToLoad != null) {
+                content = Importer3D.load(new File(fileToLoad).toURI().toURL().toExternalForm());
+            } else {
+                content = Importer3D.load(ContentModel.class.getResource("drop-here.obj").toExternalForm());
+            }
             autoScalingGroup.getChildren().add(content);
         } catch (IOException e) {
             e.printStackTrace();
--- a/apps/experiments/3DViewer/src/main/java/com/javafx/experiments/jfx3dviewer/Jfx3dViewerApp.java	Wed May 22 18:22:29 2013 -0400
+++ b/apps/experiments/3DViewer/src/main/java/com/javafx/experiments/jfx3dviewer/Jfx3dViewerApp.java	Wed May 22 15:57:12 2013 -0700
@@ -31,6 +31,7 @@
  */
 package com.javafx.experiments.jfx3dviewer;
 
+import java.util.List;
 import javafx.application.Application;
 import javafx.fxml.FXMLLoader;
 import javafx.scene.Parent;
@@ -48,7 +49,8 @@
     }
 
     @Override public void start(Stage stage) throws Exception {
-        contentModel = new ContentModel();
+        List<String> args = getParameters().getRaw();
+        contentModel = new ContentModel(args.isEmpty() ? null : args.get(0));
         Scene scene = new Scene(
                 FXMLLoader.<Parent>load(Jfx3dViewerApp.class.getResource("main.fxml")),
                 1024,600);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/apps/experiments/3DViewer/src/main/java/com/javafx/experiments/jfx3dviewer/SimpleViewerApp.java	Wed May 22 15:57:12 2013 -0700
@@ -0,0 +1,121 @@
+/*
+ * Copyright (c) 2010, 2013 Oracle and/or its affiliates.
+ * All rights reserved. Use is subject to license terms.
+ *
+ * This file is available and licensed under the following license:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ *  - Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  - Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the distribution.
+ *  - Neither the name of Oracle Corporation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package com.javafx.experiments.jfx3dviewer;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import javafx.animation.KeyFrame;
+import javafx.animation.KeyValue;
+import javafx.animation.Timeline;
+import javafx.application.Application;
+import javafx.event.ActionEvent;
+import javafx.event.EventHandler;
+import javafx.scene.Group;
+import javafx.scene.Node;
+import javafx.scene.PerspectiveCamera;
+import javafx.scene.Scene;
+import javafx.scene.paint.Color;
+import javafx.scene.transform.Rotate;
+import javafx.scene.transform.Translate;
+import javafx.stage.Stage;
+import javafx.util.Duration;
+import com.javafx.experiments.importers.Importer3D;
+import com.sun.javafx.perf.PerformanceTracker;
+
+/**
+ * JavaFX 3D Viewer Application
+ */
+public class SimpleViewerApp extends Application {
+    private final Group root3D = new Group();
+    private final PerspectiveCamera camera = new PerspectiveCamera(true);
+    private final Rotate cameraXRotate = new Rotate(-20,0,0,0,Rotate.X_AXIS);
+    private final Rotate cameraYRotate = new Rotate(-20,0,0,0,Rotate.Y_AXIS);
+    private final Rotate cameraLookXRotate = new Rotate(0,0,0,0,Rotate.X_AXIS);
+    private final Rotate cameraLookZRotate = new Rotate(0,0,0,0,Rotate.Z_AXIS);
+    private final Translate cameraPosition = new Translate(0,0,-7);
+    private AutoScalingGroup autoScalingGroup = new AutoScalingGroup(2);
+
+    @Override public void start(Stage stage) throws Exception {
+        List<String> args = getParameters().getRaw();
+        Scene scene = new Scene(root3D,1920,1080,true,false);
+        scene.setFill(Color.ALICEBLUE);
+
+        // CAMERA
+        camera.getTransforms().addAll(
+                cameraXRotate,
+                cameraYRotate,
+                cameraPosition,
+                cameraLookXRotate,
+                cameraLookZRotate);
+        camera.setNearClip(0.1);
+        camera.setFarClip(100);
+        scene.setCamera(camera);
+        root3D.getChildren().addAll(camera, autoScalingGroup);
+
+        // LOAD DROP HERE MODEL
+        try {
+            Node content;
+            if (args.isEmpty()) {
+                content = Importer3D.load(ContentModel.class.getResource("drop-here.obj").toExternalForm());
+            } else {
+                content = Importer3D.load(new File(args.get(0)).toURI().toURL().toExternalForm());
+            }
+            autoScalingGroup.getChildren().add(content);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+        Timeline timeline = new Timeline(
+                new KeyFrame(Duration.ZERO, new KeyValue(cameraYRotate.angleProperty(),0)),
+                new KeyFrame(Duration.seconds(4), new KeyValue(cameraYRotate.angleProperty(),360))
+        );
+        timeline.setCycleCount(Timeline.INDEFINITE);
+        timeline.play();
+
+        stage.setScene(scene);
+        stage.show();
+
+        // MEASURE FPS
+        Timeline fpsTimeline = new Timeline(new KeyFrame(Duration.seconds(2), new EventHandler<ActionEvent>() {
+            @Override public void handle(ActionEvent t) {
+                System.out.println("fps = " + PerformanceTracker.getSceneTracker(scene).getInstantFPS());
+            }
+        }));
+        fpsTimeline.setCycleCount(Timeline.INDEFINITE);
+        fpsTimeline.play();
+    }
+
+    public static void main(String[] args) {
+        launch(args);
+    }
+}