I will follow up on all of these at some stage. I should get the CVS and comit a few things. I spent many hours reading a range of Xith classes (TextureLoader, UIWindowManager amongst others) There are only a few little changes that I “had” to make but there were more that I could of.
Now for more on the transparency. This is particulary in relation to the banner. That is the bit in the game that contains tutorial information. Fly it in with i and out with o. Show and hide it with b. It is still not what I want. You can see that on the edges of the banner there are some fully transparent pixels (so the image used for the texture has transparency). What I want to be able to do is fade the whole thing in and out, without sacrificing the ability to have a partially transparent image.
The banner’s geometry is a simple TriangleStripArray created like this:
geom = new TriangleStripArray(widthN * 2 * heightN, GeometryArray.COORDINATES |
GeometryArray.TEXTURE_COORDINATE_2, stripVertexCounts);
I then do stuff to set the coordinates and texture coordinates point wise.
To create the appearance I use this:
Appearance app = new Appearance();
PolygonAttributes polygonAttributes = new PolygonAttributes(PolygonAttributes.POLYGON_FILL,
PolygonAttributes.CULL_NONE, 0f);
app.setPolygonAttributes(polygonAttributes);
tmp = DirectBufferedImage.make(w, h, DirectBufferedImage.DIRECT_RGBA);
try {
BufferedImage img = null;
img = ImageIO.read(this.getClass().getResource("data/parchment.png"));
BufferedImage rgb = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
rgb.getGraphics().drawImage(img, 0, 0, rgb.getWidth(), rgb.getHeight(), 0, 0, img.getWidth(), img.getHeight(), null);
Graphics2D g = (Graphics2D) tmp.getGraphics();
g.drawImage(rgb, 0, 0, null);
cache = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
cache.getGraphics().drawImage(tmp, 0, 0, null);
}
catch (IOException e) {
e.printStackTrace();
}
tex = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, tmp.getWidth(), tmp.getHeight());
ImageComponent2D im = new ImageComponent2D(ImageComponent.FORMAT_RGBA, tmp.getWidth(),
tmp.getHeight(), tmp);
tex.setImage(0, im);
app.setTexture(tex);
//this is vital!!
TransparencyAttributes transAttr =
new TransparencyAttributes(TransparencyAttributes.BLENDED, 0.0f, TransparencyAttributes.BLEND_SRC_ALPHA,
TransparencyAttributes.BLEND_ZERO);
app.setTransparencyAttributes(transAttr);
TextureAttributes texAtt = new TextureAttributes();
texAtt.setTextureMode(TextureAttributes.REPLACE);
app.setTextureAttributes(texAtt);
shape.setAppearance(app);
To explain this a little. The polygon attributes part is standard. tmp is one of the DirectBufferedImages that is in Xith. If I understand correctly these things are really great as the backing store for the direct buffered image is the same bytes that are used by jogl to send to the video card. Note I create a RGBA one. The image I actually want is read in to img using ImageIO. The type of this buffered image is unknown so I turn it into a TYPE_4BYTE_ABGR the cheats way using the graphics.drawImage method. I then do the same thing to put rgb onto tmp. (tmp is a bit of a misnomer - it is actually the one that goes on the texture). Finally on the image creation I store a copy of the image in cache for later (see below).
To create the texture I use the Texture2D aand ImageComponent2D constructors. Note that ImageComponent2D really likes DirectBufferedImage’s.
Now comes the bits I don’t really understand, and still have not got working the way I want. You can see what I have done to create the appropriate TransparencyAttributes and TextureAttributes. The combination of BLENDED, BLEND_SRC_ALPHA, REPLACE, etc in these has caused me heartache. I do not really understand what is going on under the hood. I would like to be able to set new transparency attributes over time and fade out the whole thing, while continuing to have a partially transparent image as I loaded above.
A few more nicities are listed below. I like that as I have this DirectBufferedImage I can draw on it using getGraphics and all I need to do is tell the Texture it is dirty and all will be well.
public void clearText() {
tmp.getGraphics().drawImage(cache, 0, 0, null);
}
public void addText(String text, int fontSize, int x, int y) {
Graphics2D g = (Graphics2D) tmp.getGraphics();
g.setColor(Color.white);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setFont(new Font("serif", Font.BOLD, fontSize));
g.drawString(text, x, y);
tex.setDirty(true);
}