BatchNode and GeometryBatchFactory now uses the material's isEqual method to gather the geometries to batch

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9257 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
This commit is contained in:
rem..om 2012-03-23 10:00:49 +00:00
parent 55b8188d20
commit 1945a0c1c0
2 changed files with 25 additions and 9 deletions
engine/src
core/com/jme3/scene
tools/jme3tools/optimize

@ -236,6 +236,14 @@ public class BatchNode extends Node implements Savable {
throw new IllegalStateException("No material is set for Geometry: " + g.getName() + " please set a material before batching"); throw new IllegalStateException("No material is set for Geometry: " + g.getName() + " please set a material before batching");
} }
List<Geometry> list = map.get(g.getMaterial()); List<Geometry> list = map.get(g.getMaterial());
if (list == null) {
//trying to compare materials with the isEqual method
for (Material mat : map.keySet()) {
if (g.getMaterial().isEqual(mat)) {
list = map.get(mat);
}
}
}
if (list == null) { if (list == null) {
list = new ArrayList<Geometry>(); list = new ArrayList<Geometry>();
map.put(g.getMaterial(), list); map.put(g.getMaterial(), list);
@ -482,7 +490,7 @@ public class BatchNode extends Node implements Savable {
} }
} else if (VertexBuffer.Type.Position.ordinal() == bufType) { } else if (VertexBuffer.Type.Position.ordinal() == bufType) {
FloatBuffer inPos = (FloatBuffer) inBuf.getData(); FloatBuffer inPos = (FloatBuffer) inBuf.getData();
FloatBuffer outPos = (FloatBuffer) outBuf.getData(); FloatBuffer outPos = (FloatBuffer) outBuf.getData();
doCopyBuffer(inPos, globalVertIndex, outPos, 3); doCopyBuffer(inPos, globalVertIndex, outPos, 3);
} else if (VertexBuffer.Type.Normal.ordinal() == bufType || VertexBuffer.Type.Tangent.ordinal() == bufType) { } else if (VertexBuffer.Type.Normal.ordinal() == bufType || VertexBuffer.Type.Tangent.ordinal() == bufType) {
FloatBuffer inPos = (FloatBuffer) inBuf.getData(); FloatBuffer inPos = (FloatBuffer) inBuf.getData();
@ -647,8 +655,8 @@ public class BatchNode extends Node implements Savable {
protected void setNeedsFullRebatch(boolean needsFullRebatch) { protected void setNeedsFullRebatch(boolean needsFullRebatch) {
this.needsFullRebatch = needsFullRebatch; this.needsFullRebatch = needsFullRebatch;
} }
public int getOffsetIndex(Geometry batchedGeometry){ public int getOffsetIndex(Geometry batchedGeometry) {
return batchedGeometry.startIndex; return batchedGeometry.startIndex;
} }
} }

@ -61,10 +61,10 @@ public class GeometryBatchFactory {
outBuf.put(offset + i * 3 + 2, norm.z); outBuf.put(offset + i * 3 + 2, norm.z);
} }
} }
private static void doTransformTangents(FloatBuffer inBuf, int offset, int components, FloatBuffer outBuf, Matrix4f transform) { private static void doTransformTangents(FloatBuffer inBuf, int offset, int components, FloatBuffer outBuf, Matrix4f transform) {
Vector3f tan = new Vector3f(); Vector3f tan = new Vector3f();
// offset is given in element units // offset is given in element units
// convert to be in component units // convert to be in component units
offset *= components; offset *= components;
@ -79,8 +79,8 @@ public class GeometryBatchFactory {
outBuf.put(offset + i * components + 0, tan.x); outBuf.put(offset + i * components + 0, tan.x);
outBuf.put(offset + i * components + 1, tan.y); outBuf.put(offset + i * components + 1, tan.y);
outBuf.put(offset + i * components + 2, tan.z); outBuf.put(offset + i * components + 2, tan.z);
if (components == 4){ if (components == 4) {
outBuf.put(offset + i * components + 3, inBuf.get(i * components + 3)); outBuf.put(offset + i * components + 3, inBuf.get(i * components + 3));
} }
} }
@ -130,7 +130,7 @@ public class GeometryBatchFactory {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
for (VertexBuffer vb : geom.getMesh().getBufferList().getArray()){ for (VertexBuffer vb : geom.getMesh().getBufferList().getArray()) {
compsForBuf[vb.getBufferType().ordinal()] = vb.getNumComponents(); compsForBuf[vb.getBufferType().ordinal()] = vb.getNumComponents();
formatForBuf[vb.getBufferType().ordinal()] = vb.getFormat(); formatForBuf[vb.getBufferType().ordinal()] = vb.getFormat();
} }
@ -209,7 +209,7 @@ public class GeometryBatchFactory {
FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly(); FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly();
FloatBuffer outPos = (FloatBuffer) outBuf.getData(); FloatBuffer outPos = (FloatBuffer) outBuf.getData();
doTransformNorms(inPos, globalVertIndex, outPos, worldMatrix); doTransformNorms(inPos, globalVertIndex, outPos, worldMatrix);
}else if(Type.Tangent.ordinal() == bufType){ } else if (Type.Tangent.ordinal() == bufType) {
FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly(); FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly();
FloatBuffer outPos = (FloatBuffer) outBuf.getData(); FloatBuffer outPos = (FloatBuffer) outBuf.getData();
int components = inBuf.getNumComponents(); int components = inBuf.getNumComponents();
@ -288,6 +288,14 @@ public class GeometryBatchFactory {
for (Geometry geom : geometries) { for (Geometry geom : geometries) {
List<Geometry> outList = matToGeom.get(geom.getMaterial()); List<Geometry> outList = matToGeom.get(geom.getMaterial());
if (outList == null) {
//trying to compare materials with the isEqual method
for (Material mat : matToGeom.keySet()) {
if (geom.getMaterial().isEqual(mat)) {
outList = matToGeom.get(mat);
}
}
}
if (outList == null) { if (outList == null) {
outList = new ArrayList<Geometry>(); outList = new ArrayList<Geometry>();
matToGeom.put(geom.getMaterial(), outList); matToGeom.put(geom.getMaterial(), outList);