/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* View3D.java
*
* Created on 30.11.2011, 17:42:04
*/
package at.alphagate.ui.pages;
import at.alphagate.mod.MainFrame;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
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.universe.SimpleUniverse;
import java.awt.Color;
import java.awt.Dialog.ModalityType;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Enumeration;
import javax.media.j3d.*;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
/**
*
* @author AlphaBanane
*/
public class View3D extends javax.swing.JPanel
{
private final JDialog loadingScreen = new JDialog();
private BranchGroup rootBranchGroup;
private TransformGroup rootTransformGroup;
private BoundingSphere boundingSphere;
private BranchGroup objectBranchGroup;
/** Creates new form View3D */
public View3D()
{
initComponents();
init3D();
loadingScreen.setTitle("Loading...");
loadingScreen.add(new JLabel(new ImageIcon(getClass().getResource("/at/alphagate/res/images/icons/loading.gif"))));
loadingScreen.setSize(200, 150);
loadingScreen.setUndecorated(true);
loadingScreen.setModalityType(ModalityType.APPLICATION_MODAL);
}
private void init3D()
{
Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
canvas.setSize(getPreferredSize());
canvas.setDoubleBufferEnable(true);
SimpleUniverse simpleUniverse = new SimpleUniverse(canvas);
simpleUniverse.getViewingPlatform().setNominalViewingTransform();
rootBranchGroup = new BranchGroup();
rootTransformGroup = new TransformGroup();
objectBranchGroup = new BranchGroup();
boundingSphere = new BoundingSphere(new Point3d(), 200);
// Create a Transformgroup to scale all objects so they
// appear in the scene.
// Transform3D t3d = new Transform3D();
// t3d.setScale(0.5);
// rootTransformGroup.setTransform(t3d);
// Enable the TRANSFORM_WRITE capability so that
// our behavior code can modify it at runtime. Add it to the
// root of the subgraph.
rootTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objectBranchGroup.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
objectBranchGroup.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
createSceneGraph();
createBehaviors();
rootTransformGroup.addChild(objectBranchGroup);
rootBranchGroup.addChild(rootTransformGroup);
rootBranchGroup.compile();
simpleUniverse.addBranchGraph(rootBranchGroup);
add(canvas);
}
public void loadObject(final File object)
{
loadingScreen.setLocation(MainFrame.frame.getX() + ((MainFrame.frame.getWidth() - loadingScreen.getWidth()) / 2),
MainFrame.frame.getY() + ((MainFrame.frame.getHeight() - loadingScreen.getHeight()) / 2));
new Thread()
{
@Override
public void run()
{
try
{
ObjectFile loader = new ObjectFile(ObjectFile.RESIZE);
BranchGroup bg = loader.load(object.getAbsolutePath()).getSceneGroup();
bg.setCapability(BranchGroup.ALLOW_DETACH); // allows removing this group
objectBranchGroup.addChild(bg);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
loadingScreen.setVisible(false);
}
}
}.start();
loadingScreen.setVisible(true);
}
public void clear()
{
objectBranchGroup.removeAllChildren();
}
/**
*
* @return
*/
private void createSceneGraph()
{
// Let there be light
Color3f lightColor = new Color3f(new Color(150, 150, 150));
DirectionalLight light1 = new DirectionalLight(lightColor, new Vector3f( 1, 1, 1));
DirectionalLight light2 = new DirectionalLight(lightColor, new Vector3f(-1, -1, -1));
DirectionalLight light3 = new DirectionalLight(lightColor, new Vector3f(-1, 1, 1));
DirectionalLight light4 = new DirectionalLight(lightColor, new Vector3f( 1, -1, -1));
light1.setInfluencingBounds(boundingSphere);
light2.setInfluencingBounds(boundingSphere);
light3.setInfluencingBounds(boundingSphere);
light4.setInfluencingBounds(boundingSphere);
rootBranchGroup.addChild(light1);
rootBranchGroup.addChild(light2);
rootBranchGroup.addChild(light3);
rootBranchGroup.addChild(light4);
}
private void createBehaviors()
{
MouseRotate mouseRotate = new MouseRotate();
mouseRotate.setTransformGroup(rootTransformGroup);
mouseRotate.setSchedulingBounds(boundingSphere);
rootBranchGroup.addChild(mouseRotate);
MouseTranslate mouseTranslate = new MouseTranslate();
mouseTranslate.setTransformGroup(rootTransformGroup);
mouseTranslate.setSchedulingBounds(boundingSphere);
rootBranchGroup.addChild(mouseTranslate);
MouseZoom mouseZoom = new MouseZoom();
mouseZoom.setTransformGroup(rootTransformGroup);
mouseZoom.setSchedulingBounds(boundingSphere);
rootBranchGroup.addChild(mouseZoom);
KeyNavigatorBehavior keyNavigatorBehavior = new KeyNavigatorBehavior(rootTransformGroup);
keyNavigatorBehavior.setSchedulingBounds(boundingSphere);
rootBranchGroup.addChild(keyNavigatorBehavior);
Behavior resetBehavior = new Behavior()
{
private final WakeupCriterion criterion = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
private Transform3D originT3D = new Transform3D();
@Override
public void initialize()
{
rootTransformGroup.getTransform(originT3D);
wakeupOn(criterion);
}
@Override
public void processStimulus(Enumeration criteria)
{
WakeupOnAWTEvent event = (WakeupOnAWTEvent) criteria.nextElement();
KeyEvent key = (KeyEvent) event.getAWTEvent()[0];
if (key.getKeyCode() == KeyEvent.VK_SPACE)
{
rootTransformGroup.setTransform(originT3D);
}
wakeupOn(criterion);
}
};
resetBehavior.setSchedulingBounds(boundingSphere);
rootBranchGroup.addChild(resetBehavior);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 690, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 423, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}