📄 tdsfile.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.maxutils;
import java.io.DataInput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import com.jme.animation.SpatialTransformer;
import com.jme.light.Light;
import com.jme.light.PointLight;
import com.jme.light.SpotLight;
import com.jme.math.TransformMatrix;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.TexCoords;
import com.jme.scene.TriMesh;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.LightState;
import com.jme.system.DisplaySystem;
import com.jme.system.JmeException;
import com.jme.util.geom.BufferUtils;
import com.jmex.model.converters.FormatConverter;
/**
* Started Date: Jul 2, 2004<br><br>
*
* type=4d4d=MAIN_3DS
* parent=nothing
* @author Jack Lindamood
*/
public class TDSFile extends ChunkerClass{
private static final Logger logger = Logger.getLogger(TDSFile.class
.getName());
private EditableObjectChunk objects=null;
private KeyframeChunk keyframes=null;
private List<Spatial> spatialNodes;
private List<String> spatialNodesNames;
private SpatialTransformer st;
private List<Light> spatialLights;
private BlendState alpha;
private FormatConverter formatConverter;
public TDSFile(DataInput myIn, FormatConverter converter) throws IOException {
super(myIn);
this.formatConverter = converter;
ChunkHeader c=new ChunkHeader(myIn);
if (c.type!=MAIN_3DS)
throw new IOException("Header doesn't match 0x4D4D; Header=" + Integer.toHexString(c.type));
c.length-=6;
setHeader(c);
chunk();
}
protected boolean processChildChunk(ChunkHeader i) throws IOException {
switch(i.type){
case TDS_VERSION:
readVersion();
return true;
case EDIT_3DS:
objects=new EditableObjectChunk(myIn, i, formatConverter);
return true;
case KEYFRAMES:
keyframes=new KeyframeChunk(myIn,i);
return true;
default:
return false;
}
}
private void readVersion() throws IOException{
int version=myIn.readInt();
if (DEBUG || DEBUG_LIGHT) logger.info("Version:" + version);
}
public Node buildScene() throws IOException {
buildObject();
putTranslations();
Node uberNode=new Node("TDS Scene");
for ( Spatial spatialNode : spatialNodes ) {
if ( spatialNode != null ) {
Spatial toAttach = spatialNode;
if ( toAttach.getParent() == null ) {
uberNode.attachChild( toAttach );
}
}
}
LightState ls = null;
for ( Light spatialLight : spatialLights ) {
if ( ls == null ) {
ls = DisplaySystem.getDisplaySystem().getRenderer().createLightState();
ls.setEnabled( true );
}
ls.attach( spatialLight );
}
if (ls!=null)
uberNode.setRenderState(ls);
if (keyframes!=null){
st.interpolateMissing();
if (st.keyframes.size() == 1) {
// one keyframe: update controller once and disregard it
st.update(0);
}
else {
// multiple keyframes: add controller to node
uberNode.addController(st);
st.setActive(true);
}
}
return uberNode;
}
private void putTranslations() {
if (keyframes==null) return;
int spatialCount=0;
for ( Spatial spatialNode : spatialNodes ) {
if ( spatialNode != null ) {
spatialCount++;
}
}
st=new SpatialTransformer(spatialCount);
spatialCount=0;
for (int i=0;i<spatialNodes.size();i++){
if (spatialNodes.get(i) != null ){
// hand the Spatial over to the SpatialTransformer
// the parent ID is not passed here, as that would produce wrong results
// because of the ST applying hierarchichal transformations, which the
// scene graph applies anyway
st.setObject( spatialNodes.get(i),spatialCount++,-1);//getParentIndex(i));
}
}
Object[] keysetKeyframe=keyframes.objKeyframes.keySet().toArray();
for ( Object aKeysetKeyframe : keysetKeyframe ) {
KeyframeInfoChunk thisOne = keyframes.objKeyframes.get( aKeysetKeyframe );
if ( "$$$DUMMY".equals( thisOne.name ) ) {
continue;
}
int indexInST = findIndex( thisOne.name );
for ( Object aTrack : thisOne.track ) {
KeyframeInfoChunk.KeyPointInTime thisTime = (KeyframeInfoChunk.KeyPointInTime) aTrack;
if ( thisTime.rot != null ) {
st.setRotation( indexInST, thisTime.frame, thisTime.rot );
}
if ( thisTime.position != null ) {
st.setPosition( indexInST, thisTime.frame, thisTime.position );
}
if ( thisTime.scale != null ) {
st.setScale( indexInST, thisTime.frame, thisTime.scale );
}
}
}
st.setSpeed(10);
}
private int findIndex(String name) {
int j=0;
for (int i=0;i<spatialNodesNames.size();i++){
if (spatialNodesNames.get(i).equals(name)) return j;
if (spatialNodes.get(i) != null ) j++;
}
throw new JmeException("Logic error. Unknown keyframe name " + name);
}
private int getParentIndex(int objectIndex) {
if (keyframes.objKeyframes.get(spatialNodesNames.get(objectIndex)) ==null)
return -2;
short parentID=keyframes.objKeyframes.get(spatialNodesNames.get(objectIndex)).parent;
if (parentID==-1) return -1;
Object[] objs=keyframes.objKeyframes.keySet().toArray();
for (int i=0;i<objs.length;i++){
if (keyframes.objKeyframes.get(objs[i]).myID==parentID)
return i;
}
throw new JmeException("Logic error. Unknown parent ID for " + objectIndex);
}
private void buildObject() throws IOException {
spatialNodes=new ArrayList<Spatial>(); // An ArrayList of Nodes
spatialLights=new ArrayList<Light>();
spatialNodesNames=new ArrayList<String>(); // Their names
Map<Short, Node> nodesByID = new HashMap<Short, Node>();
if ( keyframes != null ) {
for ( Object o : keyframes.objKeyframes.entrySet() ) {
Map.Entry entry = (Map.Entry) o;
String name = (String) entry.getKey();
if ( !objects.namedObjects.containsKey( name ) ) {
KeyframeInfoChunk info = (KeyframeInfoChunk) entry.getValue();
Node node = new Node( info.name );
nodesByID.put( info.myID, node );
spatialNodesNames.add( name );
spatialNodes.add( node );
}
}
}
for ( Object o1 : objects.namedObjects.entrySet() ) {
Map.Entry entry = (Map.Entry) o1;
String objectKey = (String) entry.getKey();
NamedObjectChunk noc = (NamedObjectChunk) entry.getValue();
KeyframeInfoChunk kfInfo = null;
if ( keyframes != null && keyframes.objKeyframes != null ) {
kfInfo = keyframes.objKeyframes.get( objectKey );
}
if ( noc.whatIAm instanceof TriMeshChunk ) {
Node myNode = new Node( objectKey );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -