import javax.swing.*;
import java.awt.*;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class WrapMaze3D extends JPanel
{
private static final int PWIDTH = 1450; // size of panel
private static final int PHEIGHT = 600;
private Boolean b1,b2,b3,b4;
private static final int BOUNDSIZE = 10000; // larger than world
private SimpleUniverse su;
private BranchGroup sceneBG;
private BoundingSphere bounds; // for environment nodes
private MazeManager mazeMan; // maze manager
private TransformGroup camera2TG; // back-facing camera
void createStarBackground(BranchGroup bg)
{
java.util.Random rand = new java.util.Random();
float mag;
BranchGroup BGBranch=new BranchGroup();
Background BG=new Background();
Sphere BGSphere;
PointArray starfield = new PointArray(15000, PointArray.COORDINATES|PointArray.COLOR_3);
float[] point = new float[3];
float[] brightness = new float[3];
for (int i = 0; i < 15000; i++)
{
point[0] = (rand.nextInt(2) == 0) ? rand.nextFloat() * -1.0f :rand.nextFloat();
point[1] = (rand.nextInt(2) == 0) ? rand.nextFloat() * -1.0f :rand.nextFloat();
point[2] = (rand.nextInt(2) == 0) ? rand.nextFloat() * -1.0f :rand.nextFloat();
starfield.setCoordinate(i, point);
mag=(rand.nextFloat()+0.5f)/1.5f;
brightness[0]=mag;
brightness[1]=mag;
brightness[2]=mag;
starfield.setColor(i, brightness);
}
Shape3D StarShape=new Shape3D(starfield);
StarShape.setAppearance(new Appearance());
StarShape.getAppearance().setPointAttributes(new PointAttributes(1f,true));
BGBranch.addChild(StarShape);
BG.setGeometry(BGBranch);
BG.setApplicationBounds(new BoundingSphere(new Point3d(),100.0));
Appearance SphereApp = new Appearance();
SphereApp.setTexture((new TextureLoader("F:/Rayman/BonusGame_1/Sky.jpg",null)).getTexture());
SphereApp.setTextureAttributes(new TextureAttributes(TextureAttributes.MODULATE,new Transform3D(),new Color4f(),TextureAttributes.FASTEST));
BGSphere=new Sphere(1.0f,Sphere.GENERATE_NORMALS_INWARD|Sphere.GENERATE_TEXTURE_COORDS,50,SphereApp);
BGBranch.addChild(BGSphere);
bg.addChild(BG);
}
public WrapMaze3D(MazeManager mm, BirdsEye be, TransformGroup c2TG)
// construct the scene and the main camera
{
mazeMan = mm; // ref to maze manager
camera2TG = c2TG; // ref to second back-facong camera
setLayout( new BorderLayout() );
setOpaque( false );
setPreferredSize( new Dimension(PWIDTH, PHEIGHT));
GraphicsConfiguration config =
SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
add("Center", canvas3D);
canvas3D.setFocusable(true);
canvas3D.requestFocus();
su = new SimpleUniverse(canvas3D);
createSceneGraph();
prepareViewPoint(be);
su.addBranchGraph( sceneBG );
} // end of WrapMaze3D()
void createSceneGraph()
// initilise the scene
{
sceneBG = new BranchGroup();
bounds = new BoundingSphere(new Point3d(0,0,0), BOUNDSIZE);
lightScene(); // add the lights
TexturedFloor floor = new TexturedFloor();
sceneBG.addChild( floor.getBG() );
sceneBG.addChild( mazeMan.getMaze() ); // add maze, using MazeManager
sceneBG.addChild( camera2TG ); // add second camera
createStarBackground(sceneBG);
sceneBG.compile(); // fix the scene
} // end of createSceneGraph()
private void lightScene()
// *No* ambient light, 2 directional lights
{
Color3f white = new Color3f(0.3f, 0.3f, 0.3f);
// Set up the ambient light
AmbientLight ambientLightNode = new AmbientLight(white);
ambientLightNode.setInfluencingBounds(bounds);
// sceneBG.addChild(ambientLightNode); // ambient commented out
// Set up the directional lights
Vector3f light1Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
// left, down, backwards
Vector3f light2Direction = new Vector3f(1.0f, -1.0f, 1.0f);
// right, down, forwards
DirectionalLight light1 =
new DirectionalLight(white, light1Direction);
light1.setInfluencingBounds(bounds);
sceneBG.addChild(light1);
DirectionalLight light2 =
new DirectionalLight(white, light2Direction);
light2.setInfluencingBounds(bounds);
sceneBG.addChild(light2);
} // end of lightScene()
private void prepareViewPoint(BirdsEye be)
{
// adjust viewpoint parameters
View userView = su.getViewer().getView();
userView.setFieldOfView( Math.toRadians(90.0)); // wider FOV
// 10 and 0.1; keep ratio between 100-1000
userView.setBackClipDistance(60); // can see a long way
userView.setFrontClipDistance(0.05); // can see close things
ViewingPlatform vp = su.getViewingPlatform();
// add a spotlight and avatar to viewpoint
PlatformGeometry pg = new PlatformGeometry();
pg.addChild( makeSpot() );
pg.addChild(makeAvatar()); // avatar not used here
vp.setPlatformGeometry( pg );
// fix starting position and orientation of viewpoint
TransformGroup steerTG = vp.getViewPlatformTransform();
initViewPosition(steerTG);
// set up keyboard controls
KeyBehavior keybeh = new KeyBehavior(mazeMan, be, camera2TG,b1,b2,b3,b4);
keybeh.setSchedulingBounds(bounds);
vp.setViewPlatformBehavior(keybeh);
} // end of prepareViewPoint()
private void initViewPosition(TransformGroup steerTG)
// rotate and move the viewpoint
{
Transform3D t3d = new Transform3D();
steerTG.getTransform(t3d);
Transform3D toRot = new Transform3D();
toRot.rotY(-Math.PI);
// rotate 180 degrees around Y-axis, so facing along positive z-axis
t3d.mul(toRot);
t3d.setTranslation( mazeMan.getMazeStartPosn() ); // place at maze start
steerTG.setTransform(t3d);
} // end of initViewPosition()
private SpotLight makeSpot()
// a spotlight to help the user see in the (relative) darkness
{
SpotLight spot = new SpotLight();
spot.setPosition(0.0f, 0.5f, 0.0f); // a bit above the user
spot.setAttenuation(0.0f, 1.2f, 0.0f); // linear attentuation
spot.setSpreadAngle( (float)Math.toRadians(30.0)); // smaller angle
spot.setConcentration(5.0f); // reduce strength quicker
spot.setInfluencingBounds(bounds);
return spot;
} // end of makeSpot()
private TransformGroup makeAvatar()
{
TransformGroup U = new TransformGroup();
Apperance SphereApp = new Appearance();
Appearance SphereApp = new Appearance();
SphereApp.setTexture((new TextureLoader("F:/Rayman/BonusGame_1/Sky.jpg",null)).getTexture());
SphereApp.setTextureAttributes(new TextureAttributes(TextureAttributes.MODULATE,new Transform3D(),new Color4f(),TextureAttributes.FASTEST));
if(bb1=true)
{U.addChild(new Sphere(1.0f,Sphere.GENERATE_NORMALS_INWARD,80,SphereApp);}
return U;
}
}