Skip to main content

Adding a floor?

1 reply [Last post]
seh2
Offline
Joined: 2006-04-04

Hi,
I have a background in my java3d world but want to add a floor/ground. Made from an image if possible, a colour would do. Does anyone have a bit of code that can help me do this.
Thanks

Sean

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ruanonline
Offline
Joined: 2004-12-19

I was setting a floor in my universe and I saw your post, so I decided to post my code. I've just done it and I know it works but I'm sure you can improve it. Maybe you don't need it because you sent your question a few days ago and perhaps you've already found a solution :D.

[code]/*
* Floor.java
*/

import com.sun.j3d.utils.image.TextureLoader;
import java.awt.Color;
import java.io.File;
import java.net.MalformedURLException;
import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Texture;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TriangleStripArray;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.TexCoord2f;

/**
*
* @author Carlos
*/
public class Floor extends BranchGroup {
private Color color = Color.WHITE;
private final Appearance app = new Appearance();
private final TransformGroup tg = new TransformGroup();
private TriangleStripArray tsa = null;

/** Creates a new instance of Floor */
public Floor() {
this.setCapability(BranchGroup.ALLOW_DETACH);

tsa = new TriangleStripArray(4,
TriangleStripArray.COORDINATES |
TriangleStripArray.TEXTURE_COORDINATE_2, new int[]{4});
tsa.setCoordinate(0, new Point3d(-1d, 0d, -1d));
tsa.setCoordinate(1, new Point3d(-1d, 0d, 1d));
tsa.setCoordinate(2, new Point3d(1d, 0d, -1d));
tsa.setCoordinate(3, new Point3d(1d, 0d, 1d));
tsa.setTextureCoordinate(0, 0, new TexCoord2f(0.0f, 0.0f));
tsa.setTextureCoordinate(0, 1, new TexCoord2f(1.0f, 0.0f));
tsa.setTextureCoordinate(0, 2, new TexCoord2f(0.0f, 1.0f));
tsa.setTextureCoordinate(0, 3, new TexCoord2f(1.0f, 1.0f));

ColoringAttributes coloringAttributes = new ColoringAttributes();
coloringAttributes.setCapability(ColoringAttributes.ALLOW_COLOR_WRITE);
app.setColoringAttributes(coloringAttributes);
app.setCapability(Appearance.ALLOW_TEXTURE_WRITE);

tg.addChild(new Shape3D(tsa, app));

this.addChild(tg);
}

public void setScale(double escale) {
Transform3D t3d = new Transform3D();
tg.getTransform(t3d);
t3d.setScale(scale);
tg.setTransform(t3d);

/* Change texture coordinates so that the image
* will appear with the same size when the scale
* changes.
*/
tsa.setTextureCoordinate(0, 0, new TexCoord2f(
0.0f,
0.0f));
tsa.setTextureCoordinate(0, 1, new TexCoord2f(
(float)(1.0f*scale),
0.0f));
tsa.setTextureCoordinate(0, 2, new TexCoord2f(
0.0f,
(float)(1.0f*scale)));
tsa.setTextureCoordinate(0, 3, new TexCoord2f(
(float)(1.0f*scale),
(float)(1.0f*scale)));
}

public void setColor(Color color) {
this.color = color;

app.getColoringAttributes().setColor(new Color3f(color));
}

public void setTexture(File fileTexture) {
try {
TextureLoader loader = new TextureLoader(fileTexture.toURL(), null);
Texture texture = loader.getTexture();
texture.setBoundaryModeS(Texture.WRAP);
texture.setBoundaryModeT(Texture.WRAP);
app.setTexture(texture);
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
}[/code]