/*
* File : TheScene3D.java
*
* Copyright (C) 2008 Steliana Vatau <steliana.vatau@jquadrobot.org>
*
* www.jquadrobot.org
*
* This file is part of JQuadRobot.
*
* JQuadRobot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JQuadRobot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with JQuadRobot. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.jquadrobot.simulator;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GraphicsConfiguration;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Material;
import javax.media.j3d.Texture;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
/**
* This class is the 3D scene for JQuadRobot Simulator.
*
* @version 1.1
* @author Steliana Vatau <steliana.vatau@jquadrobot.org>
*/
public class TheScene3D extends JFrame{
Canvas3D canvas3d;
private MouseRotate mouseRotate = new MouseRotate();
private MouseTranslate mouseTranslate = new MouseTranslate();
private MouseZoom mouseZoom = new MouseZoom();
private TransformGroup objTransform = new TransformGroup();
private SimpleUniverse simpleU;
public TheScene3D(){
super("JQuadRobot - simulator");
setSize(1024, 768); //the desktop size
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
canvas3d = new Canvas3D(config);
add(canvas3d, BorderLayout.CENTER);
JPanel panelRight = new JPanel();
PanelRight rightPanel = new PanelRight();
panelRight.add(rightPanel);
add(panelRight,BorderLayout.EAST) ;
JPanel test = new JPanel();
test.add(new PanelDown());
add(test, BorderLayout.SOUTH);
simpleU = new SimpleUniverse(canvas3d);
BranchGroup scene = createSceneGraph();
simpleU.addBranchGraph(scene);
simpleU.getViewingPlatform().setNominalViewingTransform();
}
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
objTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTransform.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
objTransform.setCapability(TransformGroup.ALLOW_CHILDREN_READ);
objTransform.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
Appearance groundApp = new Appearance();
Texture textureGround = new TextureLoader("img/groundimage3.jpg", this).getTexture();
groundApp.setTexture(textureGround);
Box ground = new Box(35f,0.1f,35f, groundApp);
objTransform.addChild(ground); // add ground;
objTransform.addChild(makeTheRobot()); // add the robot;
objRoot.addChild(objTransform);
// mouse action: rotate, translate and zoom
mouseRotate.setTransformGroup(objTransform);
mouseRotate.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(mouseRotate);
mouseTranslate.setTransformGroup(objTransform);
mouseTranslate.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(mouseTranslate);
mouseZoom.setTransformGroup(objTransform);
mouseZoom.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(mouseZoom);
BoundingSphere bounds = new BoundingSphere();
bounds.setRadius(1000d);
Background bg = new Background(new Color3f(Color.white));
bg.setApplicationBounds(bounds);
objRoot.addChild(bg);
// set the ambient light;
Color3f ambientColor = new Color3f(0.5f, 0.5f, 0.5f);
AmbientLight ambientLight = new AmbientLight(ambientColor);
ambientLight.setInfluencingBounds(bounds);
objRoot.addChild(ambientLight);
return objRoot;
}
BranchGroup makeTheRobot(){
BranchGroup base = new BranchGroup();
// appearance platform
Appearance appP = new Appearance();
Material matP = new Material();
matP.setEmissiveColor(0, 0, 0);
//matP.setEmissiveColor(0.8f,0.8f,0.8f);
appP.setMaterial(matP);
//platform
Transform3D trPlat = new Transform3D();
Vector3f vPlat = new Vector3f(0f,4.5f,0f);
trPlat.set(vPlat);
TransformGroup tgPlat = new TransformGroup(trPlat);
tgPlat.addChild(new Box(3f,0.15f,3f,appP));
//leg1
Transform3D trleg1 = new Transform3D();
Vector3f vLeg1 = new Vector3f(2f,3.5f,2f);
trleg1.set(vLeg1);
TransformGroup tgLeg1 = new TransformGroup(trleg1);
tgLeg1.addChild(aLeg(0,30));
//leg2
Transform3D trleg2 = new Transform3D();
Vector3f vLeg2 = new Vector3f(2f,3.5f,-2f);
trleg2.set(vLeg2);
TransformGroup tgLeg2 = new TransformGroup(trleg2);
tgLeg2.addChild(aLeg(0,70));
//leg3
Transform3D trleg3 = new Transform3D();
Vector3f vLeg3 = new Vector3f(-2f,3.5f,2f);
trleg3.set(vLeg3);
TransformGroup tgLeg3 = new TransformGroup(trleg3);
tgLeg3.addChild(aLeg(0,30));
//leg4
Transform3D trleg4 = new Transform3D();
Vector3f vLeg4 = new Vector3f(-2f,3.5f,-2f);
trleg4.set(vLeg4);
TransformGroup tgLeg4 = new TransformGroup(trleg4);
tgLeg4.addChild(aLeg(-30,30));
base.addChild(tgPlat);
base.addChild(tgLeg1);
base.addChild(tgLeg2);
base.addChild(tgLeg3);
base.addChild(tgLeg4);
//obstacol
// appearance platform
Appearance appObs = new Appearance();
Material matObs = new Material();
matObs.setEmissiveColor(0.9f,0.9f,0.0f);
appObs.setMaterial(matObs);
/*
Transform3D trObs = new Transform3D();
Vector3f vObs = new Vector3f(0f,0.6f,-3.5f);
trObs.set(vObs);
TransformGroup tgObs = new TransformGroup(trObs);
tgObs.addChild(new Box(3f,0.6f,1f,appObs));
//base.addChild(tgObs); // obstacol box;
*/
Transform3D tr1bs0 = new Transform3D();
Transform3D tr1bs01 = new Transform3D();
Vector3f v1bs0 = new Vector3f(0f,-10f,1f);
tr1bs01.set(v1bs0);
tr1bs0.rotX(Math.PI/2);
tr1bs0.mul(tr1bs01);
TransformGroup tg1bs0 = new TransformGroup(tr1bs0);
tg1bs0.addChild(new Cylinder(5f,10f,appObs));
base.addChild(tg1bs0); // obstacol cylinder;
return base;
}
BranchGroup aLeg(int rot2, int rot3){
BranchGroup leg = new BranchGroup();
// appearance box
Appearance app = new Appearance();
Material mat = new Material();
app.setMaterial(mat);
// appearance cylinder
Appearance appCyl = new Appearance();
Material matCyl = new Material();
//matCyl.setDiffuseColor(0,1,0);
matCyl.setEmissiveColor(0f,1f,0f);
appCyl.setMaterial(matCyl);
// appearance sphere
Appearance appSphere = new Appearance();
Material matSphere = new Material();
matSphere.setEmissiveColor(1f,1f,0f);
appSphere.setMaterial(matSphere);
TransformGroup trG1 = new TransformGroup();
// Cylinder 1
Transform3D trCyl1 = new Transform3D();
trCyl1.set(new Vector3f(0.f,0.8f,0f));
Transform3D trCyl1rot = new Transform3D();
trCyl1rot.rotX(90*Math.PI/180);
trCyl1.mul(trCyl1rot);
TransformGroup trGcyl1 = new TransformGroup(trCyl1);
Cylinder cyl1 = new Cylinder(0.2f,0.5f,appCyl);
trGcyl1.addChild(cyl1);
// Cylinder 2
Transform3D trCyl2 = new Transform3D();
trCyl2.set(new Vector3f(0f,0.5f,0f));
Transform3D trCyl2rot = new Transform3D();
trCyl2rot.rotZ(90*Math.PI/180);
trCyl2.mul(trCyl2rot);
TransformGroup trGcyl2 = new TransformGroup(trCyl2);
Cylinder cyl2 = new Cylinder(0.2f,0.5f,appCyl);
trGcyl2.addChild(cyl2);
// box1
Transform3D tr1 = new Transform3D();
Transform3D trRot2 = new Transform3D();
trRot2.rotX(rot2*Math.PI/180);
tr1.mul(trRot2);
Transform3D tr11 = new Transform3D();
tr11.set(new Vector3f(0f,-0.5f,0f));
tr1.mul(tr11);
//tr1.set(new Vector3f(0f,-0.5f,0f));
TransformGroup trGbox1 = new TransformGroup(tr1);
Box box1 = new Box(0.2f,1f,0.2f,app);
trGbox1.addChild(box1);
Transform3D tr2 = new Transform3D();
tr2.set(new Vector3f(0f,-0.5f,0.3f));
tr1.mul(tr2);
Transform3D tr3 = new Transform3D();
//tr3.rotX(30*Math.PI/180);
tr3.rotX(rot3*Math.PI/180);
// Cylinder 3
Transform3D trCyl3 = new Transform3D();
//trCyl3.set(new Vector3f(0f,-1.5f,0f));
Transform3D trCyl33 = new Transform3D();
trCyl33.set(new Vector3f(0f,-0.5f,-0.2f));
trCyl3.mul(tr1);
trCyl3.mul(trCyl33);
Transform3D trCyl3rot = new Transform3D();
trCyl3rot.rotZ(90*Math.PI/180);
trCyl3.mul(trCyl3rot);
TransformGroup trGcyl3 = new TransformGroup(trCyl3);
Cylinder cyl3 = new Cylinder(0.2f,0.5f,appCyl);
trGcyl3.addChild(cyl3);
Transform3D tr4 = new Transform3D();
tr4.set(new Vector3f(0f,-1.5f,0f));
tr3.mul(tr4);
tr1.mul(tr3);
// box2
TransformGroup trGbox2 = new TransformGroup(tr1);
Box box2= new Box(0.2f,1f,0.2f,app);
trGbox2.addChild(box2);
// transform for sphere
Transform3D tr5 = new Transform3D();
tr5.set(new Vector3f(0f,-1f,0f));
tr1.mul(tr5);
TransformGroup trGsphere = new TransformGroup(tr1);
Sphere sphere= new Sphere(0.2f,appSphere);
trGsphere.addChild(sphere);
trG1.addChild(trGcyl1);
trG1.addChild(trGcyl2);
trG1.addChild(trGbox1);
trG1.addChild(trGbox2);
trG1.addChild(trGcyl3);
trG1.addChild(trGsphere);
leg.addChild(trG1);
return leg;
}
}