To reduce shader state changes and compilations, just use the

four-bone-per-vertex configuration for all skinned meshes.  Depending on 
the models we have, it may be worth compiling a separate one for two 
bones per vertex.  Added a hook to customize the material color (for 
per-vertex colors, e.g.)


git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@245 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Andrzej Kapolka
2007-05-15 00:55:19 +00:00
parent 124af1e0b1
commit c755fd626a
3 changed files with 34 additions and 46 deletions
@@ -408,7 +408,7 @@ public class SkinMesh extends ModelMesh
if (bonesPerVertex > MAX_SHADER_BONES_PER_VERTEX) {
return;
}
_sconfig = new SkinShaderConfig(scache, bonesPerVertex, _emissiveMap != null);
_sconfig = new SkinShaderConfig(scache, _emissiveMap != null);
if (_sconfig.update(getBatch(0).states)) {
setShaderAttributes();
setRenderState(_sconfig.getState());
@@ -618,20 +618,19 @@ public class SkinMesh extends ModelMesh
*/
protected void setShaderAttributes ()
{
int bonesPerVertex = _sconfig.getBonesPerVertex();
int size = getBatch(0).getVertexCount() * bonesPerVertex;
int size = getBatch(0).getVertexCount() * 4;
ByteBuffer bibuf = BufferUtils.createByteBuffer(size);
FloatBuffer bwbuf = BufferUtils.createFloatBuffer(size);
for (WeightGroup group : _weightGroups) {
byte[] indices = new byte[bonesPerVertex];
for (int ii = 0; ii < indices.length; ii++) {
byte[] indices = new byte[4];
for (int ii = 0; ii < 4; ii++) {
indices[ii] = (byte)((ii < group.bones.length) ?
ListUtil.indexOf(_bones, group.bones[ii]) : 0);
}
for (int ii = 0, widx = 0; ii < group.vertexCount; ii++) {
bibuf.put(indices);
for (int jj = 0; jj < bonesPerVertex; jj++) {
for (int jj = 0; jj < 4; jj++) {
bwbuf.put((jj < group.bones.length) ? group.weights[widx++] : 0f);
}
}
@@ -640,17 +639,16 @@ public class SkinMesh extends ModelMesh
bwbuf.rewind();
GLSLShaderObjectsState sstate = _sconfig.getState();
sstate.setAttributePointer("boneIndices", bonesPerVertex, false, false, 0, bibuf);
sstate.setAttributePointer("boneWeights", bonesPerVertex, false, 0, bwbuf);
sstate.setAttributePointer("boneIndices", 4, false, false, 0, bibuf);
sstate.setAttributePointer("boneWeights", 4, false, 0, bwbuf);
}
/** Tracks the configuration of a skin shader. */
protected static class SkinShaderConfig extends ShaderConfig
{
public SkinShaderConfig (ShaderCache scache, int bonesPerVertex, boolean emissiveMapped)
public SkinShaderConfig (ShaderCache scache, boolean emissiveMapped)
{
super(scache);
_bonesPerVertex = bonesPerVertex;
_emissiveMapped = emissiveMapped;
// set bindings from texture units to samplers
@@ -662,11 +660,6 @@ public class SkinMesh extends ModelMesh
}
}
public int getBonesPerVertex ()
{
return _bonesPerVertex;
}
@Override // documentation inherited
protected String getVertexShader ()
{
@@ -683,7 +676,6 @@ public class SkinMesh extends ModelMesh
protected void getDefinitions (ArrayList<String> defs)
{
super.getDefinitions(defs);
defs.add("BONES_PER_VERTEX " + _bonesPerVertex);
if (_emissiveMapped) {
defs.add("EMISSIVE_MAPPED");
}
@@ -696,7 +688,6 @@ public class SkinMesh extends ModelMesh
ddefs.add("MAX_BONE_COUNT " + MAX_SHADER_BONE_COUNT);
}
protected int _bonesPerVertex;
protected boolean _emissiveMapped;
}
@@ -240,12 +240,8 @@ public abstract class ShaderConfig
// add snippets for each of the lights
for (int ii = 0; ii < _lights.length; ii++) {
LightConfig light = _lights[ii];
if (light.type == Light.LT_POINT) {
buf.append(POINT_LIGHT_SNIPPET.replace("%", Integer.toString(ii)));
} else if (light.type == Light.LT_DIRECTIONAL) {
buf.append(DIRECTIONAL_LIGHT_SNIPPET.replace("%", Integer.toString(ii)));
}
String snippet = getLightSnippet(_lights[ii].type);
buf.append(snippet.replace("%", Integer.toString(ii)));
}
// the alpha value comes from the diffuse color in the material
@@ -281,6 +277,21 @@ public abstract class ShaderConfig
ddefs.add(buf.toString());
}
/**
* Returns a code snippet that adds the influence of a light of the specified type (after
* replacing '%' with the light index).
*/
protected String getLightSnippet (int type)
{
if (type == Light.LT_POINT) {
return POINT_LIGHT_SNIPPET;
} else if (type == Light.LT_DIRECTIONAL) {
return DIRECTIONAL_LIGHT_SNIPPET;
} else {
return "";
}
}
/** The configuration of a single light in a {@link LightState}. */
protected static class LightConfig
implements Cloneable