📄 asetojme.java
字号:
/*
* 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.converters;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Logger;
import com.jme.bounding.BoundingBox;
import com.jme.image.Image;
import com.jme.image.Texture;
import com.jme.image.Texture2D;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.TexCoords;
import com.jme.scene.TriMesh;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.system.dummy.DummySystemProvider;
import com.jme.util.TextureKey;
import com.jme.util.TextureManager;
import com.jme.util.export.binary.BinaryExporter;
import com.jme.util.geom.BufferUtils;
import com.jme.util.resource.ResourceLocatorTool;
import com.jmex.model.Face;
/**
* Started Date: Jul 1, 2004<br><br>
*
* Converts .ase files to .jme format. Loads models but not animations.
*
* @author Jack Lindamood
*/
public class AseToJme extends FormatConverter{
private static final Logger logger = Logger.getLogger(AseToJme.class
.getName());
/**
* Converts an ase file to jme format. The syntax is: AseToJme file.ase out.jme
* @param args The array of parameters
*/
public static void main(String[] args){
DisplaySystem.getDisplaySystem(DummySystemProvider.DUMMY_SYSTEM_IDENTIFIER);
new AseToJme().attemptFileConvert(args);
}
/**
* Creates a node from a .ase InputStream and then writes that node to the given
* OutputStream in jME's binary format
* @param ASEStream An InputStream pointing to the .ase file
* @param o The stream to write it's binary equivalent to
* @throws java.io.IOException If anything funky goes wrong with reading information
*/
public void convert(InputStream ASEStream,OutputStream o) throws IOException {
if (ASEStream==null)
throw new NullPointerException("Unable to load null streams");
Node newnode=new Node("ase model");
new AseToJme.ASEModelCopy(ASEStream,newnode);
BinaryExporter.getInstance().save(newnode,o);
}
/**
* <code>ASEModel</code> defines a model using the ASE model format.
* This loader builds the mesh of the model but currently does not
* build any animations defined for the format. Therefore, if a
* call to <code>getAnimationController</code> is made, null will
* be returned.
*
* @author Mark Powell
* @version $Id: AseToJme.java,v 1.2 2007/08/20 10:28:40 rherlitz Exp $
*/
private class ASEModelCopy{
private static final long serialVersionUID = 1L;
//ASE file tags.
private static final String OBJECT = "*GEOMOBJECT";
private static final String NUM_VERTEX = "*MESH_NUMVERTEX";
private static final String NUM_FACES = "*MESH_NUMFACES";
private static final String NUM_TVERTEX = "*MESH_NUMTVERTEX";
private static final String VERTEX = "*MESH_VERTEX";
private static final String FACE = "*MESH_FACE";
private static final String NORMALS = "*MESH_NORMALS";
private static final String FACE_NORMAL = "*MESH_FACENORMAL";
private static final String NVERTEX = "*MESH_VERTEXNORMAL";
private static final String TVERTEX = "*MESH_TVERT";
private static final String TFACE = "*MESH_TFACE";
private static final String TEXTURE = "*BITMAP";
private static final String UTILE = "*UVW_U_TILING";
private static final String VTILE = "*UVW_V_TILING";
private static final String UOFFSET = "*UVW_U_OFFSET";
private static final String VOFFSET = "*UVW_V_OFFSET";
private static final String MATERIAL_ID = "*MATERIAL_REF";
private static final String MATERIAL_COUNT = "*MATERIAL_COUNT";
private static final String MATERIAL = "*MATERIAL";
private static final String MATERIAL_NAME = "*MATERIAL_NAME";
private static final String MATERIAL_DIFFUSE = "*MATERIAL_DIFFUSE";
private static final String MATERIAL_AMBIENT = "*MATERIAL_AMBIENT";
private static final String MATERIAL_SPECULAR = "*MATERIAL_SPECULAR";
private static final String MATERIAL_SHINE = "*MATERIAL_SHINE";
//path to the model and texture file.
private BufferedReader reader = null;
private StringTokenizer tokenizer;
private String fileContents;
private int numOfObjects; // The number of objects in the model
private int numOfMaterials; // The number of materials for the model
private ArrayList<ASEMaterialInfo> materials = new ArrayList<ASEMaterialInfo>();
private ArrayList<ASEObject> objectList = new ArrayList<ASEObject>();
Node mynode;
/**
* Constructor instantiates a new <code>ASEModel</code> object.
* No data is loaded at this time and a call to <code>load</code>
* is required to initialize the model with data.
* @param name the name of the scene element. This is required for identification and
* comparision purposes.
*/
public ASEModelCopy(String name,Node mynode) {
this.mynode=mynode;
}
/**
* Constructor instantiates a new <code>ASEModel</code> object. The
* file provided is then read and the data loaded. Thefore, a call
* to <code>load</code> is not required.
*
* @param file the InputStream of a file to load.
*/
public ASEModelCopy(InputStream file,Node mynode) {
this.mynode=mynode;
load(file);
}
/**
* <code>load</code> parses a given Stream, loading the mesh data into
* a structure that jME can render. Each Geomobject the ase file defines
* is created as a <code>TriMesh</code> and attached to this
* <code>Model</code>. Animation is currently not supported.
* @param is the InputStream of the ase file to load.
*/
public void load(InputStream is) {
if (null == is) {
logger.warning("Null URL could not load ASE.");
return;
}
int fileSize = 0;
try {
fileSize = is.available();
reader = new BufferedReader(new InputStreamReader(is));
StringBuffer fc = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
fc.append(line + "\n");
}
fileContents = fc.toString();
reader.close();
parseFile();
computeNormals();
convertToTriMesh();
} catch (IOException e) {
logger.warning("Could not load " + is.toString());
}
}
/**
* <code>getAnimationController</code> returns the animation
* controller. Currently, no animation is loaded, and null will
* be returned until the animation is implemented.
*
* @return @see com.jmex.model.Model#getAnimationController()
*/
public Controller getAnimationController() {
return null;
}
/**
*
* <code>parseFile</code> reads the file contents. First, the
* number of materials and objects are read, then each material
* is read and each object is read.
*
*/
private void parseFile() {
ASEMaterialInfo textureInfo = new ASEMaterialInfo();
ASEObject mesh = new ASEObject("ASEMesh");
numOfObjects = getObjectCount();
numOfMaterials = getMaterialCount();
//Build texture list (not sure if this makes since, there can only be
//one texture per mesh, and the are reading it in for the entire
//object, not on a per object basis.
for (int i = 0; i < numOfMaterials; i++) {
materials.add(textureInfo);
getMaterialInfo(materials.get(i), i + 1);
}
for (int i = 0; i < numOfObjects; i++) {
mesh.materialID = -1;
moveToObject(i + 1);
readObjectInfo(mesh, i + 1);
readObjectData(mesh, i + 1);
objectList.add(mesh);
}
}
/**
*
* <code>convertToTriMesh</code> converts the data read into
* a collection of <code>TriMesh</code> classes that the
* jME renderer can display.
*
*/
private void convertToTriMesh() {
for (int i = 0; i < numOfObjects; i++) {
ASEObject object = objectList.get(i);
Vector2f[] texCoords2 = new Vector2f[object.tm.getVertexCount()];
for (int j = 0; j < object.faces.length; j++) {
int index = object.faces[j].vertIndex[0];
texCoords2[index] = new Vector2f();
texCoords2[index] =
object.tempTexVerts[object.faces[j].coordIndex[0]];
index = object.faces[j].vertIndex[1];
texCoords2[index] = new Vector2f();
texCoords2[index] =
object.tempTexVerts[object.faces[j].coordIndex[1]];
index = object.faces[j].vertIndex[2];
texCoords2[index] = new Vector2f();
texCoords2[index] =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -