Fixed a bug with the texture controllers and moved some shared code into

a utility class.


git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@132 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Andrzej Kapolka
2007-01-31 22:05:46 +00:00
parent d49e50db89
commit fb8f4ce0a8
6 changed files with 197 additions and 105 deletions
@@ -23,9 +23,12 @@ package com.threerings.jme.model;
import java.util.HashMap;
import org.lwjgl.opengl.Display;
import com.jme.image.Texture;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.threerings.jme.util.SpatialVisitor;
@@ -37,28 +40,49 @@ public abstract class TextureController extends ModelController
@Override // documentation inherited
public void resolveTextures (TextureProvider tprov)
{
// reinitialize the cloned textures if we re-resolve
super.resolveTextures(tprov);
_textures = null;
}
// documentation inherited
public void update (float time)
{
// initialize the textures before the first update
if (_textures == null) {
initTextures();
}
}
/**
* Performs any per-instance initialization required for textures.
*/
protected void initTextures ()
{
// find and clone all textures under the target
final HashMap<Texture, Texture> clones = new HashMap<Texture, Texture>();
new SpatialVisitor<ModelMesh>(ModelMesh.class) {
protected void visit (ModelMesh mesh) {
TextureState tstate = (TextureState)mesh.getRenderState(RenderState.RS_TEXTURE);
if (tstate == null) {
TextureState otstate = (TextureState)mesh.getRenderState(RenderState.RS_TEXTURE);
if (otstate == null) {
return;
}
for (int ii = 0, nn = tstate.getNumberOfSetTextures(); ii < nn; ii++) {
Texture tex = tstate.getTexture(ii), ctex = clones.get(tex);
TextureState ntstate =
DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
for (int ii = 0, nn = otstate.getNumberOfSetTextures(); ii < nn; ii++) {
Texture tex = otstate.getTexture(ii), ctex = clones.get(tex);
if (ctex == null) {
if (tex.getTextureId() == 0) {
tstate.load(ii);
otstate.load(ii);
}
clones.put(tex, ctex = tex.createSimpleClone());
}
tstate.setTexture(ctex, ii);
ntstate.setTexture(ctex, ii);
}
mesh.setRenderState(ntstate);
}
}.traverse(_target);
_target.updateRenderState();
// remember them for updates
_textures = clones.values().toArray(new Texture[0]);