Various tweaks, plus prune nodes with no geometry descendants that

aren't used as bones in models.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4074 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Andrzej Kapolka
2006-04-28 04:21:30 +00:00
parent 33ab4146c8
commit ecee4e292e
4 changed files with 37 additions and 30 deletions
@@ -58,7 +58,7 @@ public abstract class EmissionController extends ModelController
*/ */
protected void getEmitterLocation (Vector3f result) protected void getEmitterLocation (Vector3f result)
{ {
result.set(_target.getWorldBound().getCenter()); result.set(_target.getWorldTranslation());
} }
/** /**
+5 -4
View File
@@ -233,10 +233,8 @@ public class Model extends ModelNode
FileChannel fc = fis.getChannel(); FileChannel fc = fis.getChannel();
if (map) { if (map) {
long pos = fc.position(); long pos = fc.position();
MappedByteBuffer buf = fc.map(FileChannel.MapMode.READ_ONLY, model.sliceBuffers(fc.map(FileChannel.MapMode.READ_ONLY,
pos, fc.size() - pos); pos, fc.size() - pos));
buf.order(ByteOrder.LITTLE_ENDIAN);
model.sliceBuffers(buf);
} else { } else {
model.readBuffers(fc); model.readBuffers(fc);
} }
@@ -343,6 +341,9 @@ public class Model extends ModelNode
*/ */
public Animation getAnimation (String name) public Animation getAnimation (String name)
{ {
if (_anims == null) {
return null;
}
Animation anim = _anims.get(name); Animation anim = _anims.get(name);
if (anim != null) { if (anim != null) {
return anim; return anim;
@@ -134,19 +134,12 @@ public class ModelMesh extends TriMesh
} }
if (_solid) { if (_solid) {
setRenderState(_backCull); setRenderState(_backCull);
} else {
setRenderState(_noCull);
} }
if (_transparent) { if (_transparent) {
setRenderQueueMode(Renderer.QUEUE_TRANSPARENT); setRenderQueueMode(Renderer.QUEUE_TRANSPARENT);
setRenderState(_blendAlpha); setRenderState(_blendAlpha);
setRenderState(_overlayZBuffer); setRenderState(_overlayZBuffer);
} else {
setRenderQueueMode(Renderer.QUEUE_OPAQUE);
setRenderState(_noAlpha);
setRenderState(_lequalZBuffer);
} }
setTextureCombineMode(TextureState.REPLACE);
} }
/** /**
@@ -432,15 +425,11 @@ public class ModelMesh extends TriMesh
Renderer renderer = DisplaySystem.getDisplaySystem().getRenderer(); Renderer renderer = DisplaySystem.getDisplaySystem().getRenderer();
_backCull = renderer.createCullState(); _backCull = renderer.createCullState();
_backCull.setCullMode(CullState.CS_BACK); _backCull.setCullMode(CullState.CS_BACK);
_noCull = renderer.createCullState();
_blendAlpha = renderer.createAlphaState(); _blendAlpha = renderer.createAlphaState();
_blendAlpha.setBlendEnabled(true); _blendAlpha.setBlendEnabled(true);
_noAlpha = renderer.createAlphaState();
_overlayZBuffer = renderer.createZBufferState(); _overlayZBuffer = renderer.createZBufferState();
_overlayZBuffer.setFunction(ZBufferState.CF_LEQUAL); _overlayZBuffer.setFunction(ZBufferState.CF_LEQUAL);
_overlayZBuffer.setWritable(false); _overlayZBuffer.setWritable(false);
_lequalZBuffer = renderer.createZBufferState();
_lequalZBuffer.setFunction(ZBufferState.CF_LEQUAL);
} }
/** The sizes of the various buffers (zero for <code>null</code>). */ /** The sizes of the various buffers (zero for <code>null</code>). */
@@ -469,21 +458,12 @@ public class ModelMesh extends TriMesh
/** The shared state for back face culling. */ /** The shared state for back face culling. */
protected static CullState _backCull; protected static CullState _backCull;
/** The shared state for no back face culling. */
protected static CullState _noCull;
/** The shared state for alpha blending. */ /** The shared state for alpha blending. */
protected static AlphaState _blendAlpha; protected static AlphaState _blendAlpha;
/** The shared state for no blending. */
protected static AlphaState _noAlpha;
/** The shared state for checking, but not writing to, the z buffer. */ /** The shared state for checking, but not writing to, the z buffer. */
protected static ZBufferState _overlayZBuffer; protected static ZBufferState _overlayZBuffer;
/** The shared state for checking and writing to the z buffer. */
protected static ZBufferState _lequalZBuffer;
/** Indicates that this mesh should use a bounding box. */ /** Indicates that this mesh should use a bounding box. */
protected static final int BOX_BOUND = 0; protected static final int BOX_BOUND = 0;
@@ -30,6 +30,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@@ -90,11 +91,13 @@ public class ModelDef
public abstract Spatial createSpatial (Properties props); public abstract Spatial createSpatial (Properties props);
/** Resolves any name references using the supplied map. */ /** Resolves any name references using the supplied map. */
public void resolveReferences (HashMap<String, Spatial> nodes) public void resolveReferences (
HashMap<String, Spatial> nodes, HashSet<Spatial> referenced)
{ {
Spatial pnode = nodes.get(parent); Spatial pnode = nodes.get(parent);
if (pnode instanceof ModelNode) { if (pnode instanceof ModelNode) {
((ModelNode)pnode).attachChild(_spatial); ((ModelNode)pnode).attachChild(_spatial);
} else if (parent != null) { } else if (parent != null) {
Log.warning("Missing or invalid parent node [spatial=" + Log.warning("Missing or invalid parent node [spatial=" +
name + ", parent=" + parent + "]."); name + ", parent=" + parent + "].");
@@ -226,9 +229,10 @@ public class ModelDef
} }
@Override // documentation inherited @Override // documentation inherited
public void resolveReferences (HashMap<String, Spatial> nodes) public void resolveReferences (
HashMap<String, Spatial> nodes, HashSet<Spatial> referenced)
{ {
super.resolveReferences(nodes); super.resolveReferences(nodes, referenced);
if (_mesh == null) { if (_mesh == null) {
return; return;
} }
@@ -258,6 +262,7 @@ public class ModelDef
SkinMesh.WeightGroup wgroup = new SkinMesh.WeightGroup(); SkinMesh.WeightGroup wgroup = new SkinMesh.WeightGroup();
wgroup.indices = toArray(entry.getValue().indices); wgroup.indices = toArray(entry.getValue().indices);
HashSet<ModelNode> bones = entry.getKey(); HashSet<ModelNode> bones = entry.getKey();
referenced.addAll(bones);
wgroup.bones = bones.toArray(new ModelNode[bones.size()]); wgroup.bones = bones.toArray(new ModelNode[bones.size()]);
wgroup.weights = toArray(entry.getValue().weights); wgroup.weights = toArray(entry.getValue().weights);
wgroups[ii++] = wgroup; wgroups[ii++] = wgroup;
@@ -403,9 +408,10 @@ public class ModelDef
// then go through again, resolving any name references and attaching // then go through again, resolving any name references and attaching
// root children // root children
HashSet<Spatial> referenced = new HashSet<Spatial>();
for (int ii = 0, nn = spatials.size(); ii < nn; ii++) { for (int ii = 0, nn = spatials.size(); ii < nn; ii++) {
SpatialDef sdef = spatials.get(ii); SpatialDef sdef = spatials.get(ii);
sdef.resolveReferences(nodes); sdef.resolveReferences(nodes, referenced);
if (sdef.getSpatial(props).getParent() == null) { if (sdef.getSpatial(props).getParent() == null) {
model.attachChild(sdef.getSpatial(props)); model.attachChild(sdef.getSpatial(props));
} }
@@ -429,6 +435,9 @@ public class ModelDef
} }
} }
// get rid of any nodes that serve no purpose
pruneUnusedNodes(model, referenced);
return model; return model;
} }
@@ -450,6 +459,23 @@ public class ModelDef
return ctrl; return ctrl;
} }
/** Recursively removes any unused nodes. */
protected boolean pruneUnusedNodes (
ModelNode node, HashSet<Spatial> referenced)
{
boolean hasValidChildren = false;
for (Iterator it = node.getChildren().iterator(); it.hasNext(); ) {
Spatial child = (Spatial)it.next();
if (!(child instanceof ModelNode) ||
pruneUnusedNodes((ModelNode)child, referenced)) {
hasValidChildren = true;
} else {
it.remove();
}
}
return referenced.contains(node) || hasValidChildren;
}
/** Converts a boxed Integer list to an unboxed int array. */ /** Converts a boxed Integer list to an unboxed int array. */
protected static int[] toArray (ArrayList<Integer> list) protected static int[] toArray (ArrayList<Integer> list)
{ {