Problem in Java3D with Transparency causing occlusion problems.
http://s13.invisionfree.com/Heroes_of_Ardania_v2/index.php?showtopic=67&st=0#entry2033965
The attachment there is a zipfile that demos the problem I've been having with setting TransparencyAttributes to BLENDED- the smaller square I generate appears to disappear behind the larger one, which is impossible.?Using an opaque surface fixes this problem, but I definitely need to be able to present transparency.?Any clues as to the problem here?Any help would be greatly appreciated.
The complete code (cut down from a more elaborate project) is also here:
//import com.sun.j3d.loaders.Scene;
import javax.media.j3d.*;
import javax.swing.JFrame;
import java.io.*;
import com.sun.j3d.loaders.*;
import com.sun.j3d.utils.image.TextureLoader;
import java.awt.Color;
import javax.vecmath.*;
import java.awt.image.*;
publicclass Testing{
publicstaticvoid main(String args[]){
Engine engine =new Engine();
JFrame frame =new JFrame();
frame.add(engine.viewer);
frame.setBounds(100, 100, 450, 450);
frame.show();
/*Model.MS3DModel model = new Model.MS3DModel(args[0]);
Model.Animation animate = new Model.Animation("move", 4, 28), anim[] = new Model.Animation[1];
anim[0] = animate;
model.setAnimations(anim);
Sprite sprite = new Sprite(model);
sprite.setAnimation("move", 0);
engine.addSprite(sprite);*/
TestObject test =new TestObject();
engine.universe.addBranchGraph(test);
}
staticclass TestObjectextends BranchGroup{
Appearance appearance;
GeometryArray geometry;
TestObject(){
setAppearance("TestTexture.jpg","TestAlpha.jpg");
setGeometry(-5, 10);
Shape3D child =new Shape3D(geometry, appearance);
addChild(child);
setGeometry(0, 7.5f);
child =new Shape3D(geometry, appearance);
addChild(child);
compile();
}
/* Sets up a basic hanging plane, facing the x-axis, at z coordinate given and size s. */
void setGeometry(float z,float s){
Vector3f normal =new Vector3f(1, 0, 0), normals[] =new Vector3f[6];
Point3f points[] =new Point3f[4], coordinates[] =new Point3f[6];
TexCoord2f UV[] =new TexCoord2f[4], ST[] =new TexCoord2f[6];
points[0] =new Point3f(z, s, s);
points[1] =new Point3f(z, s, -s);
points[2] =new Point3f(z, -s, -s);
points[3] =new Point3f(z, -s, s);
UV[0] =new TexCoord2f(1, 1);
UV[1] =new TexCoord2f(1, 0);
UV[2] =new TexCoord2f(0, 0);
UV[3] =new TexCoord2f(0, 1);
for(int n = 6; n-- > 0;){
normals[n] =new Vector3f(normal);
coordinates[n] =new Point3f();
ST[n] =new TexCoord2f();
}
coordinates[2].set(points[0]);
coordinates[1].set(points[1]);
coordinates[0].set(points[3]);
coordinates[5].set(points[2]);
coordinates[4].set(points[3]);
coordinates[3].set(points[1]);
ST[2].set(UV[0]);
ST[1].set(UV[1]);
ST[0].set(UV[3]);
ST[5].set(UV[2]);
ST[4].set(UV[3]);
ST[3].set(UV[1]);
geometry =new TriangleArray(6, GeometryArray.COORDINATES | GeometryArray.NORMALS | GeometryArray.TEXTURE_COORDINATE_2);
geometry.setCoordinates(0, coordinates);
geometry.setNormals(0, normals);
geometry.setTextureCoordinates(0, 0, ST);
}
void setAppearance(String texture, String alpha){
appearance =new Appearance();
Material mat =new Material();
mat.setAmbientColor(0.2f, 0.2f, 0.2f);
mat.setDiffuseColor(1, 1, 1);
mat.setSpecularColor(0, 0, 0);
mat.setEmissiveColor(0, 0, 0);
mat.setShininess(1);
TransparencyAttributes transAtt =new TransparencyAttributes();
transAtt.setTransparency(0);
transAtt.setTransparencyMode(TransparencyAttributes.BLENDED);//THIS IS CAUSING THE ODD OCCLUSION EFECTS.
//transAtt.setTransparencyMode(TransparencyAttributes.NONE);
Texture tex =null;
try{
tex = loadTexture(texture, alpha);
}
catch(IOException e){
//fill in the blanks.
}
TextureAttributes texAtt =new TextureAttributes();
texAtt.setTextureMode(TextureAttributes.MODULATE);
texAtt.setTextureBlendColor(1, 1, 1, 0);
appearance.setMaterial(mat);
appearance.setTransparencyAttributes(transAtt);
appearance.setTexture(tex);
appearance.setTextureAttributes(texAtt);
//appearance.setRenderingAttributes(Model.RENDATT);
//appearance.setPolygonAttributes(Model.POLYATT);
}
static Texture loadTexture(String texture, String alpha)throws IOException{
TextureLoader texLoader = null, alphLoader =null;
boolean alphad = alpha.length() > 0;
if(texture.length() == 0)returnnull;
texLoader =new TextureLoader(texture,null);
if(alphad) alphLoader =new TextureLoader(alpha,null);
Texture loaded = texLoader.getTexture();
if(! alphad)return loaded;
int w = loaded.getWidth(), h = loaded.getHeight(), l = w * h;
BufferedImage alphImage = alphLoader.getScaledImage(w, h).getImage(),
texImage = texLoader.getImage().getImage();
int alphVals[] =newint[l], texVals[] =newint[l];
alphImage.getRGB(0, 0, w, h, alphVals, 0, w);
texImage.getRGB(0, 0, w, h, texVals, 0, w);
for(int p = l, v = 0; p-- > 0;){
v = (texVals[p] & 0x00ffffff) | (alphVals[p] << 24);
texVals[p] = v;
}
texImage.setRGB(0, 0, w, h, texVals, 0, w);
loaded =new Texture2D(Texture2D.BASE_LEVEL, Texture2D.RGBA, w, h);
loaded.setImage(0,new ImageComponent2D(ImageComponent2D.FORMAT_RGBA, texImage));
return loaded;
}
}
}
import java.applet.Applet;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.loaders.Loader;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
publicclass Engine{
SimpleUniverse universe;
Viewer viewer;
public Engine(){
stage();
lights();
camera();
action();//not implemented.
}
void stage(){
viewer =new Viewer();
universe =new SimpleUniverse(viewer);
}
void lights(){
BranchGroup graphRoot =new BranchGroup();
BoundingSphere bounds =new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100000.0);// Create a bounds for the light source influence
Color3f alColor =new Color3f(1, 1, 1);
AmbientLight aLgt =new AmbientLight(alColor);
aLgt.setInfluencingBounds(bounds);
graphRoot.addChild(aLgt);
DirectionalLight lgt1 =new DirectionalLight(new Color3f(1, 1, 1),new Vector3f(-0.8f, -0.5f, -0.3f));
lgt1.setInfluencingBounds(bounds);
graphRoot.addChild(lgt1);
graphRoot.compile();
universe.addBranchGraph(graphRoot);
}
void camera(){
OrbitBehavior orbit =new OrbitBehavior(viewer);
orbit.setRotXFactor(5);
orbit.setRotYFactor(0);
orbit.setZoomFactor(5);
orbit.setTransXFactor(5);
orbit.setTransYFactor(5);
orbit.setSchedulingBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 10000.0));
universe.getViewingPlatform().setViewPlatformBehavior(orbit);
TransformGroup vtg = universe.getViewingPlatform().getViewPlatformTransform();
Transform3D moveback =new Transform3D();
moveback.setTranslation(new Vector3f(0.0f, 0.0f, 100.0f));
vtg.setTransform(moveback);
universe.getViewer().getView().setBackClipDistance(10000.0);
}
void action(){
}
/*void addSprite(Sprite sprite) {
sprite.update();
sprite.compile();
universe.addBranchGraph(sprite);
}*/
staticclass Viewerextends Canvas3D{
finalstatic GraphicsConfiguration CONFIGURATION = CONFIGURATION();
finalstatic GraphicsConfiguration CONFIGURATION(){
return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(new GraphicsConfigTemplate3D());
}
Viewer(){
super(CONFIGURATION);
}
}
}

