|
Hello,
I have a room formed for four walls, left, right,
plane and floor, but I do not obtain to make to appear the right wall. It
would like to know if somebody can help to decide the problem me.
Thanks a lot.
Silvano
Malfatti |
import java.applet.Applet;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
class Room extends Applet {
public class LitQuad extends Shape3D {
LitQuad(Point3f A, Point3f B, Point3f C, Point3f D) {
this.setGeometry(createGeometry(A, B, C, D));
this.setAppearance(createAppearance());
}
Geometry createGeometry(Point3f A, Point3f B, Point3f C, Point3f D){
QuadArray plane = new QuadArray(4, GeometryArray.COORDINATES
| GeometryArray.NORMALS
);
plane.setCoordinate(0, A);
plane.setCoordinate(1, B);
plane.setCoordinate(2, C);
plane.setCoordinate(3, D);
Vector3f a = new Vector3f(A.x - B.x, A.y - B.y, A.z - B.z);
Vector3f b = new Vector3f(C.x - B.x, C.y - B.y, C.z - B.z);
Vector3f n = new Vector3f();
n.cross(b, a);
n.normalize();
plane.setNormal(0, n);
plane.setNormal(1, n);
plane.setNormal(2, n);
plane.setNormal(3, n);
return plane;
}
Appearance createAppearance() {
Appearance appear = new Appearance();
Material material = new Material();
appear.setMaterial(material);
return appear;
}
}
public class SimpleShadow extends Shape3D {
SimpleShadow(GeometryArray geom, Vector3f direction,
Color3f col, float height) {
int vCount = geom.getVertexCount();
QuadArray poly = new QuadArray(vCount, GeometryArray.COORDINATES
| GeometryArray.COLOR_3
);
int v;
Point3f vertex = new Point3f();
Point3f shadow = new Point3f();
for (v = 0; v < vCount; v++) {
geom.getCoordinate(v, vertex);
shadow.set( vertex.x + (vertex.y-height) * direction.x,
height + 0.0001f,
vertex.z + (vertex.y-height) * direction.y);
poly.setCoordinate(v, shadow);
poly.setColor(v, col);
}
this.setGeometry(poly);
}
} // end of PolyFour class
public Room (Canvas3D c)
{
setLayout(new BorderLayout());
BranchGroup scene = new BranchGroup();
TransformGroup troom = new TransformGroup();
scene.setCapability(BranchGroup.ALLOW_BOUNDS_READ);
Shape3D plane = new LitQuad(new Point3f(-1.0f,-0.5f,-1.0f),
new Point3f( 1.0f,-0.5f,-1.0f),
new Point3f( 1.0f, 0.9f,-1.0f),
new Point3f(-1.0f, 0.9f,-1.0f));
Shape3D leftWall = new LitQuad(new Point3f(-1.0f,-0.5f,-1.0f),
new Point3f(-1.0f, 0.9f,-1.0f),
new Point3f(-1.0f, 0.9f, 1.0f),
new Point3f(-1.0f,-0.5f, 1.0f));
Shape3D rigthWall = new LitQuad(new Point3f( 1.0f,-0.5f,-1.0f),
new Point3f( 1.0f, 0.9f,-1.0f),
new Point3f( 1.0f, 0.9f, 1.0f),
new Point3f( 1.0f,-0.5f, 1.0f));
Shape3D floor = new LitQuad(new Point3f(-1.0f, -0.5f, -1.0f),
new Point3f(-1.0f, -0.5f, 1.0f),
new Point3f( 1.0f, -0.5f, 1.0f),
new Point3f( 1.0f, -0.5f, -1.0f));
AmbientLight lightA = new AmbientLight();
AmbientLight lightB = new AmbientLight();
lightA.setInfluencingBounds(new BoundingSphere());
lightB.setInfluencingBounds(new BoundingSphere());
Vector3f directionA = new Vector3f(-1.0f, 0.5f, -0.5f);
Vector3f directionB = new Vector3f(1.0f, 0.5f, 0.5f);
directionA.normalize();
directionB.normalize();
DirectionalLight lightD1 = new DirectionalLight();
DirectionalLight lightD2 = new DirectionalLight();
lightD1.setInfluencingBounds(new BoundingSphere());
lightD2.setInfluencingBounds(new BoundingSphere());
lightD1.setDirection(directionA);
lightD2.setDirection(directionB);
Shape3D shadow = new SimpleShadow((GeometryArray) floor.getGeometry(),
directionA,
new Color3f( 1.0f,0.0f,0.0f),-0.5f);
troom.addChild(plane);
troom.addChild(leftWall);
troom.addChild(rigthWall);
troom.addChild(floor);
troom.addChild(shadow);
troom.addChild(lightD1);
troom.addChild(lightD2);
scene.addChild(troom);
scene.compile();
SimpleUniverse u = new SimpleUniverse(c);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
}
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class FrameRoom extends Frame
{
Room trainingRoom;
Panel panel;
Canvas3D canvas3D;
public FrameRoom()
{
this.setTitle("Room");
this.setSize(800,600);
this.setBackground(Color.white);
setLayout(new BorderLayout());
canvas3D = new Canvas3D(null);
canvas3D.setSize(780,500);
canvas3D.setBackground(Color.black);
panel = new Panel();
MyWindowAdapter adapter = new MyWindowAdapter(this);
addWindowListener(adapter);
trainingRoom = new Room(canvas3D);
setLayout(new FlowLayout(FlowLayout.CENTER));
panel.add(canvas3D);
this.add("Center",panel);
}
public static void main(String args[])
{
FrameRoom frame = new FrameRoom();
frame.show();
}
}
class MyWindowAdapter extends WindowAdapter{
FrameRoom frameRoom;
public MyWindowAdapter(FrameRoom frameRoom){
this.frameRoom = frameRoom;
}
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
