/**
* Compare opaque items by their texture states - generally the most expensive switch. Later this might expand
* to comparisons by other states as well, such as lighting or material.
*/
private int compareByStates(final Mesh mesh1, final Mesh mesh2) {
final TextureState ts1 = (TextureState) mesh1.getWorldRenderState(RenderState.StateType.Texture);
final TextureState ts2 = (TextureState) mesh2.getWorldRenderState(RenderState.StateType.Texture);
if (ts1 == ts2) {
return 0;
} else if (ts1 == null && ts2 != null) {
return -1;
} else if (ts2 == null && ts1 != null) {
return 1;
}
for (int x = 0, maxIndex = Math.min(ts1.getMaxTextureIndexUsed(), ts2.getMaxTextureIndexUsed()); x <= maxIndex; x++) {
final TextureKey key1 = ts1.getTextureKey(x);
final TextureKey key2 = ts2.getTextureKey(x);
if (key1 == null) {
if (key2 == null) {
continue;
} else {
return -1;
}
} else if (key2 == null) {
return 1;
}
final int tid1 = key1.hashCode();
final int tid2 = key2.hashCode();
if (tid1 == tid2) {
continue;
} else if (tid1 < tid2) {
return -1;
} else {
return 1;
}
}
if (ts1.getMaxTextureIndexUsed() != ts2.getMaxTextureIndexUsed()) {
return ts2.getMaxTextureIndexUsed() - ts1.getMaxTextureIndexUsed();
}
return 0;
}