⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modelloader.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (c) 2003-2009 jMonkeyEngine
 * All rights reserved.
 *
 * 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 'jMonkeyEngine' 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.jmex.model.util;

import java.awt.Dimension;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingVolume;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.export.Savable;
import com.jme.util.export.binary.BinaryExporter;
import com.jme.util.export.binary.BinaryImporter;
import com.jme.util.resource.ResourceLocatorTool;
import com.jme.util.resource.SimpleResourceLocator;
import com.jmex.game.StandardGame;
import com.jmex.game.state.DebugGameState;
import com.jmex.game.state.GameStateManager;
import com.jmex.game.state.load.LoadingGameState;
import com.jmex.model.collada.ColladaImporter;
import com.jmex.model.converters.AseToJme;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.MaxToJme;
import com.jmex.model.converters.Md2ToJme;
import com.jmex.model.converters.Md3ToJme;
import com.jmex.model.converters.MilkToJme;
import com.jmex.model.converters.ObjToJme;
import com.jmex.model.converters.X3dToJme;

/**
 * This is a utility that will prompt for a model file and then load it into
 * a scene.
 * 
 * @author Matthew D. Hicks
 * @author Alexander J. Gilpin
 */
public class ModelLoader {
    private static final Logger logger = Logger.getLogger(ModelLoader.class
            .getName());
    
    public static void main(String[] args) {
        // Store the texture in the binary file
//      Texture.DEFAULT_STORE_TEXTURE = true;
    	// Enable jme to gather statistics, which are used in the StatisticsGameState
    	System.setProperty("jme.stats", "set");
        
        try {
            JFileChooser chooser = new JFileChooser();
            Preferences preferences = Preferences.userNodeForPackage(ModelLoader.class);
            File directory = new File(preferences.get("StartDirectory", "."));
            chooser.setCurrentDirectory(directory);
            if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                if (isValidModelFile(file)) {
                    // Set it in preferences so we remember next time
                    preferences.put("StartDirectory", file.getAbsolutePath());
                    
                    StandardGame game = new StandardGame("Model Loader");
                    try {
                        game.getSettings().clear();
                    } catch(Exception exc) {
                        logger.logp(Level.SEVERE, ModelLoader.class.toString(),
                                "main(args)", "Exception", exc);
                    }
                    game.start();
                    
                    GameTaskQueueManager.getManager().update(new Callable<Object>() {
                        public Object call() throws Exception {
                            //MouseInput.get().setCursorVisible(true);
                            return null;
                        }
                    });
                    
                    DebugGameState debug = new DebugGameState();
                    GameStateManager.getInstance().attachChild(debug);
                    debug.setActive(true);
                    
                    LoadingGameState loading = new LoadingGameState();
                    GameStateManager.getInstance().attachChild(loading);
                    loading.setActive(true);
                    loading.setProgress(0.5f, "Loading Model: " + file.getName());
                    long time = System.currentTimeMillis();

                    // Add to resource locator
                    SimpleResourceLocator locator = new SimpleResourceLocator(file.getParentFile().toURI());
                    ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, locator);
                    ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_MODEL, locator);

                    final Node modelNode = loadModel(file);
                    outputElapsed(time);
                    if (modelNode != null) {
                        modelNode.updateRenderState();
                        if (file.getName().toLowerCase().endsWith(".jme")) {
                            loading.setProgress(1.0f, "Loaded Successfully");
                        } else {
                            loading.setProgress(0.8f, "Loaded Successfully - Saving");
                            try {
                                BinaryExporter.getInstance().save(modelNode, createJMEFile(file.getAbsoluteFile()));
                                loading.setProgress(1.0f, "Binary File Written Successfully");
                            } catch(IOException exc) {
                                logger.logp(Level.SEVERE, ModelLoader.class.toString(),
                                        "main(args)", "Exception", exc);
                                loading.setProgress(0.9f, "Binary Save Failure");
                                try {
                                    Thread.sleep(5000);
                                } catch(InterruptedException exc2) {
                                    logger.logp(Level.SEVERE, ModelLoader.class.toString(),
                                            "main(args)", "Exception", exc2);
                                }
                                loading.setProgress(1.0f);
                            }
                        }
                        debug.getRootNode().attachChild( scale( modelNode ) );
                        debug.getRootNode().updateRenderState();
                    } else {
                        loading.setProgress(0.9f, "Model Not Loaded");
                        try {
                            Thread.sleep(5000);
                        } catch(InterruptedException exc) {
                            logger.logp(Level.SEVERE, ModelLoader.class.toString(),
                                    "main(args)", "Exception", exc);
                        }
                        loading.setProgress(1.0f);
                    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -