# open faced cube / Box

3 replies [Last post]
benkammy
Offline
Joined: 2004-10-22

i am trying to create an open fced box (has all sides except the front)

is there a simple way to do this

seems like it hsould be trivial but i am new to Java3d and trying my best

thanks

benkammy

benkammy
Offline
Joined: 2004-10-22

how would i go about changing the appearance of these sides because looking at the code i only see 4 but know there are 5 sides.

i want to add appearances i have made so would i use

hope you can help - you came up massive trumps before thanks

benkammy

benkammy
Offline
Joined: 2004-10-22

thats excellent thank you.

is there a way using this to pick the sides that are missing. as in change the way the box appears to lie.

thanks again

benkammy

pete_kirkham
Offline
Joined: 2003-06-20

The easiest way is to use a quad array, adding a quad for each face. If you want the inside to be visible, you need to add quads for the internal face too. The winding order gives the direction of the quad - I think of them as 'rings' of vertices, 00, 01, 11, 10. If you wish to use appearance, you will need to add normals too.

[code]
import javax.media.j3d.GeometryArray;
import javax.media.j3d.Shape3D;

import javax.vecmath.Color3f;
import javax.vecmath.Point3f;

public class OpenBox extends Shape3D {
public OpenBox () {
this(1f, 1f, 1f, true);
}

public OpenBox (final float xSize, final float ySize, final float zSize, boolean internal) {
final Point3f[] vertices = new Point3f[8];

// set up vertices at +/- xSize, +/-ySize, +/-zSize
for (int i = 0; i < 8; i++) {
vertices[i] = new Point3f(
(((i & 0x01) << 1) - 1) * xSize,
( (i & 0x02) - 1) * ySize,
(((i & 0x04) >> 1) - 1) * zSize);
}

// quad array for the faces
final int faces = internal ? 10 : 5;
final int vertexCount = faces * 4;
GeometryArray.COORDINATES |
GeometryArray.COLOR_3);

// build each face by indexing into the vertex array
for (int face = 0; face < faces; face++) {
final int a = ring0[face];
final int b = ring1[face];
final int c = ring2[face];

quads.setCoordinate(face * 4 + 0, vertices[a]);
quads.setCoordinate(face * 4 + 1, vertices[a + b]);
quads.setCoordinate(face * 4 + 2, vertices[a + b + c]);
quads.setCoordinate(face * 4 + 3, vertices[a + c]);

quads.setColor(face * 4 + 0, colors[face]);
quads.setColor(face * 4 + 1, colors[face]);
quads.setColor(face * 4 + 2, colors[face]);
quads.setColor(face * 4 + 3, colors[face]);
}

}

// define each 'ring' to walk through the vertex array on a Gray code
private static final int[] ring0 = { 0, 4, 0, 1, 0, 0, 4, 0, 1, 0, };
private static final int[] ring1 = { 2, 1, 4, 2, 1, 1, 2, 2, 4, 4, };
private static final int[] ring2 = { 1, 2, 2, 4, 4, 2, 1, 4, 2, 1, };

// some pretty colors
private static final Color3f[] colors = {
new Color3f(1.0f, 0.0f, 0.0f),
new Color3f(0.0f, 1.0f, 0.0f),
new Color3f(1.0f, 1.0f, 0.0f),
new Color3f(0.0f, 0.0f, 1.0f),
new Color3f(1.0f, 0.0f, 1.0f),
new Color3f(1.0f, 0.5f, 0.5f),
new Color3f(0.5f, 1.0f, 0.5f),
new Color3f(1.0f, 1.0f, 0.5f),
new Color3f(0.5f, 0.5f, 1.0f),
new Color3f(1.0f, 0.5f, 1.0f),
};
}
[/code]