ホームに戻る
テクスチャ
図06_00:赤、半透明の青、青、白のテクスチャを貼る
/*
* JAVA3D でテクスチャを描くアプレット
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
/* <applet code="java3Dtest06" width=300 height=300></applet> */
public class java3Dtest06 extends Applet{
private SimpleUniverse universe = null;
public BranchGroup createSceneGraph(){
BranchGroup root = new BranchGroup();
TransformGroup trans = new TransformGroup();
trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
root.addChild(trans);
Point3d[] verteces = new Point3d[4];
verteces[0] = new Point3d(-0.5, -0.5, 0.0);
verteces[1] = new Point3d(0.5, -0.5, 0.0);
verteces[2] = new Point3d(0.5, 0.5, 0.0);
verteces[3] = new Point3d(-0.5, 0.5, 0.0);
TexCoord2f[] texcoords = new TexCoord2f[4];
texcoords[0] = new TexCoord2f(0.0f, 0.0f);
texcoords[1] = new TexCoord2f(0.99f, 0.0f);
texcoords[2] = new TexCoord2f(0.99f, 0.99f);
texcoords[3] = new TexCoord2f(0.0f, 0.99f);
BufferedImage bimage = new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB);
int[] tex_image = ((DataBufferInt)bimage.getRaster().getDataBuffer()).getData();
tex_image[0] = 0xFFFF0000;
tex_image[1] = 0x7F0000FF;
tex_image[2] = 0xFF0000FF;
tex_image[3] = 0xFFFFFFFF;
ImageComponent2D icomponent2d = new ImageComponent2D(ImageComponent.FORMAT_RGBA, bimage);
Texture2D tex2d = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, 2, 2);
tex2d.setImage(0, icomponent2d);
Appearance app = new Appearance();
app.setTexture(tex2d);
app.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED, 0.0f));
QuadArray geometry = new QuadArray(verteces.length, GeometryArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2);
geometry.setCoordinates(0, verteces);
geometry.setTextureCoordinates(0, 0, texcoords);
Shape3D shape = new Shape3D(geometry, app);
//shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
trans.addChild(shape);
Alpha rotationAlpha = new Alpha(-1, 0);
RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, trans);
root.addChild(rotator);
root.compile();
return root;
}
public void init() {
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3d = new Canvas3D(config);
this.setLayout(new BorderLayout());
this.add(canvas3d, BorderLayout.CENTER);
BranchGroup scene = createSceneGraph();
universe = new SimpleUniverse(canvas3d);
universe.getViewingPlatform().setNominalViewingTransform();
universe.addBranchGraph(scene);
}
public void destroy(){
universe.cleanup();
}
}