apps/samples/3DViewer/src/main/java/com/javafx/experiments/importers/max/MaxLoader.java

Print this page


   1 /*
   2  * Copyright (c) 2010, 2014, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR


  31  */
  32 package com.javafx.experiments.importers.max;
  33 
  34 import com.javafx.experiments.importers.Importer;
  35 import java.io.File;
  36 import java.io.FileNotFoundException;
  37 import java.io.IOException;
  38 import java.net.URL;
  39 import java.util.Map;
  40 import javafx.geometry.Point3D;
  41 import javafx.scene.Group;
  42 import javafx.scene.Node;
  43 import javafx.scene.PointLight;
  44 import javafx.scene.image.Image;
  45 import javafx.scene.paint.Color;
  46 import javafx.scene.paint.Material;
  47 import javafx.scene.paint.PhongMaterial;
  48 import javafx.scene.shape.Mesh;
  49 import javafx.scene.shape.MeshView;
  50 import javafx.scene.shape.TriangleMesh;

  51 import javafx.scene.transform.NonInvertibleTransformException;
  52 import javafx.scene.transform.Transform;
  53 import com.sun.javafx.scene.transform.TransformUtils;
  54 
  55 /** Max ASCII file loader */
  56 public class MaxLoader extends Importer {
  57     private static Mesh createMaxMesh(MaxData.GeomNode maxNode, Transform tm) {
  58         Transform tmr = null;
  59         try {
  60             tmr = tm != null ? tm.createInverse() : null;
  61         } catch (NonInvertibleTransformException ex) {
  62             throw new RuntimeException(ex);
  63         }
  64         float vts[] = new float[maxNode.mesh.nPoints * 3];
  65         if (tmr != null) {
  66             for (int i = 0; i < maxNode.mesh.nPoints; i++) {
  67                 Point3D pt = tmr.transform(
  68                         maxNode.mesh.points[i * 3],
  69                         maxNode.mesh.points[i * 3 + 1],
  70                         maxNode.mesh.points[i * 3 + 2]);
  71                 vts[i * 3] = (float) pt.getX();
  72                 vts[i * 3 + 1] = (float) pt.getY();
  73                 vts[i * 3 + 2] = (float) pt.getZ();


 103             faces[i * 6 + 2] = f[i * 4 + 1];
 104             faces[i * 6 + 3] = mf[i * 3 + 1];
 105             faces[i * 6 + 4] = f[i * 4 + 2];
 106             faces[i * 6 + 5] = mf[i * 3 + 2];
 107             sg[i] = f[i * 4 + 3];
 108         }
 109 
 110         TriangleMesh mesh = new TriangleMesh();
 111         mesh.getPoints().setAll(vts);
 112         mesh.getTexCoords().setAll(uvs);
 113         mesh.getFaces().setAll(faces);
 114         mesh.getFaceSmoothingGroups().setAll(sg);
 115         return mesh;
 116     }
 117 
 118     private static Color color(Point3D v) {
 119         return Color.color(v.getX(), v.getY(), v.getZ());
 120     }
 121 
 122     private static Transform loadNodeTM(MaxData.NodeTM tm) {
 123         return TransformUtils.immutableTransform(
 124                 tm.tm[0].getX(), tm.tm[1].getX(), tm.tm[2].getX(), tm.pos.getX(),
 125                 tm.tm[0].getY(), tm.tm[1].getY(), tm.tm[2].getY(), tm.pos.getY(),
 126                 tm.tm[0].getZ(), tm.tm[1].getZ(), tm.tm[2].getZ(), tm.pos.getZ());

 127     }
 128 
 129     private Material[] materials;
 130 
 131     private MeshView loadMaxMeshView(MaxData.GeomNode maxNode, MaxData maxData, Transform tm) {
 132         Mesh mesh = createMaxMesh(maxNode, tm);
 133         MeshView meshView = new MeshView();
 134         meshView.setMesh(mesh);
 135         meshView.setMaterial(materials[maxNode.materialRef]);
 136         // Color amb = color(maxData.materials[maxNode.materialRef].ambientColor);
 137         //meshView.setAmbient(amb);
 138 
 139         meshView.setUserData(maxNode.name);
 140         // meshView.setWireframe(true);
 141         return meshView;
 142     }
 143 
 144     public static String appendSuffix(String fileName, String suffix) {
 145         int dot = fileName.lastIndexOf('.');
 146         String ext = fileName.substring(dot, fileName.length());


   1 /*
   2  * Copyright (c) 2010, 2015, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR


  31  */
  32 package com.javafx.experiments.importers.max;
  33 
  34 import com.javafx.experiments.importers.Importer;
  35 import java.io.File;
  36 import java.io.FileNotFoundException;
  37 import java.io.IOException;
  38 import java.net.URL;
  39 import java.util.Map;
  40 import javafx.geometry.Point3D;
  41 import javafx.scene.Group;
  42 import javafx.scene.Node;
  43 import javafx.scene.PointLight;
  44 import javafx.scene.image.Image;
  45 import javafx.scene.paint.Color;
  46 import javafx.scene.paint.Material;
  47 import javafx.scene.paint.PhongMaterial;
  48 import javafx.scene.shape.Mesh;
  49 import javafx.scene.shape.MeshView;
  50 import javafx.scene.shape.TriangleMesh;
  51 import javafx.scene.transform.Affine;
  52 import javafx.scene.transform.NonInvertibleTransformException;
  53 import javafx.scene.transform.Transform;

  54 
  55 /** Max ASCII file loader */
  56 public class MaxLoader extends Importer {
  57     private static Mesh createMaxMesh(MaxData.GeomNode maxNode, Transform tm) {
  58         Transform tmr = null;
  59         try {
  60             tmr = tm != null ? tm.createInverse() : null;
  61         } catch (NonInvertibleTransformException ex) {
  62             throw new RuntimeException(ex);
  63         }
  64         float vts[] = new float[maxNode.mesh.nPoints * 3];
  65         if (tmr != null) {
  66             for (int i = 0; i < maxNode.mesh.nPoints; i++) {
  67                 Point3D pt = tmr.transform(
  68                         maxNode.mesh.points[i * 3],
  69                         maxNode.mesh.points[i * 3 + 1],
  70                         maxNode.mesh.points[i * 3 + 2]);
  71                 vts[i * 3] = (float) pt.getX();
  72                 vts[i * 3 + 1] = (float) pt.getY();
  73                 vts[i * 3 + 2] = (float) pt.getZ();


 103             faces[i * 6 + 2] = f[i * 4 + 1];
 104             faces[i * 6 + 3] = mf[i * 3 + 1];
 105             faces[i * 6 + 4] = f[i * 4 + 2];
 106             faces[i * 6 + 5] = mf[i * 3 + 2];
 107             sg[i] = f[i * 4 + 3];
 108         }
 109 
 110         TriangleMesh mesh = new TriangleMesh();
 111         mesh.getPoints().setAll(vts);
 112         mesh.getTexCoords().setAll(uvs);
 113         mesh.getFaces().setAll(faces);
 114         mesh.getFaceSmoothingGroups().setAll(sg);
 115         return mesh;
 116     }
 117 
 118     private static Color color(Point3D v) {
 119         return Color.color(v.getX(), v.getY(), v.getZ());
 120     }
 121 
 122     private static Transform loadNodeTM(MaxData.NodeTM tm) {
 123         return new Affine(
 124                 tm.tm[0].getX(), tm.tm[1].getX(), tm.tm[2].getX(), tm.pos.getX(),
 125                 tm.tm[0].getY(), tm.tm[1].getY(), tm.tm[2].getY(), tm.pos.getY(),
 126                 tm.tm[0].getZ(), tm.tm[1].getZ(), tm.tm[2].getZ(), tm.pos.getZ());
 127 
 128     }
 129 
 130     private Material[] materials;
 131 
 132     private MeshView loadMaxMeshView(MaxData.GeomNode maxNode, MaxData maxData, Transform tm) {
 133         Mesh mesh = createMaxMesh(maxNode, tm);
 134         MeshView meshView = new MeshView();
 135         meshView.setMesh(mesh);
 136         meshView.setMaterial(materials[maxNode.materialRef]);
 137         // Color amb = color(maxData.materials[maxNode.materialRef].ambientColor);
 138         //meshView.setAmbient(amb);
 139 
 140         meshView.setUserData(maxNode.name);
 141         // meshView.setWireframe(true);
 142         return meshView;
 143     }
 144 
 145     public static String appendSuffix(String fileName, String suffix) {
 146         int dot = fileName.lastIndexOf('.');
 147         String ext = fileName.substring(dot, fileName.length());