import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
import javax.vecmath.Vector3d;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import org.newdawn.xith3d.loaders.md2.MD2Loader;
import org.newdawn.xith3d.loaders.md2.MD2Model;
import org.newdawn.xith3d.loaders.md2.MD2ModelInstance;
import org.newdawn.xith3d.loaders.ModelLoadingException;
import com.xith3d.scenegraph.BranchGroup;
import com.xith3d.scenegraph.VirtualUniverse;
import com.xith3d.scenegraph.View;
import com.xith3d.scenegraph.Locale;
import com.xith3d.scenegraph.PolygonAttributes;
import com.xith3d.scenegraph.Appearance;
import com.xith3d.scenegraph.Shape3D;
import com.xith3d.scenegraph.Canvas3D;
import com.xith3d.scenegraph.TransformGroup;
import com.xith3d.scenegraph.Transform3D;
import com.xith3d.scenegraph.DirectionalLight;
import com.xith3d.scenegraph.AmbientLight;
import com.xith3d.scenegraph.QuadArray;
import com.xith3d.scenegraph.Material;
import com.xith3d.render.CanvasPeer;
import com.xith3d.render.RenderPeer;
import com.xith3d.render.jogl.RenderPeerImpl;
import com.xith3d.loaders.texture.TextureLoader;
import com.xith3d.render.RenderOptions;
import com.xith3d.render.Option;
import com.xith3d.userinterface.UIWindowManager;
import com.xith3d.userinterface.UIWindow;
import com.xith3d.userinterface.UIEventAdapter;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* A simple test example to load up an MD2 model
*
* @author Keivn Glass
*/
public class MD2Test implements Runnable {
/** The model to be displayed */
private String modelName;
/** The skin to be displayed */
private String skin;
/** The model instance being displayed */
private MD2ModelInstance alien;
/** current animation */
private String anim = "stand";
/** A light to swim round the model */
private DirectionalLight light = new DirectionalLight();
/** The angle of light */
private double lightAngle = 0;
private View view;
private UIWindowManager windowMgr;
private Canvas3D canvas;
private JLabel fpsLabel;
private TestWindow w;
/**
* Creates new First
*
* @param modelName The file name of the model to display
* @param skin The file name of the skin to apply
*/
public MD2Test(String modelName,String skin) {
this.modelName = modelName;
this.skin = skin;
VirtualUniverse universe = new VirtualUniverse();
view = new View();
universe.addView(view);
Locale locale = new Locale();
universe.addLocale(locale);
// setup view
view.getTransform().lookAt(
new Vector3f(2.41f, 0, 0), // location of eye
new Vector3f( 0, 0, 0), // center of view
new Vector3f( 0, 1, 0)); // vector pointing up
RenderPeer rp = new RenderPeerImpl();
CanvasPeer cp = rp.makeCanvas(null, 640, 480, 32, false);
canvas = new Canvas3D();
canvas.set3DPeer(cp);
view.addCanvas3D(canvas);
RenderOptions options = new RenderOptions();
options.setOption(Option.USE_SHADOWS,true);
options.setOption(Option.USE_LIGHTING,true);
cp.setRenderOptions(options);
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
TransformGroup tGroup = new TransformGroup();
BranchGroup model = loadModel();
tGroup.addChild(model);
objRoot.addChild(tGroup);
int format = QuadArray.COORDINATES;
QuadArray array = new QuadArray(4,format);
array.setCoordinate(0,new Point3f(-5f,-2f,-5f));
array.setCoordinate(1,new Point3f(5f,-2f,-5f));
array.setCoordinate(2,new Point3f(5f,-2f,5f));
array.setCoordinate(3,new Point3f(-5f,-2f,5f));
Appearance app = new Appearance();
Material mat = new Material();
mat.setAmbientColor(0.75f,0.75f,0.75f);
mat.setDiffuseColor(0.75f,0.75f,0.75f);
mat.setLightingEnable(true);
app.setMaterial(mat);
PolygonAttributes poly = new PolygonAttributes();
poly.setCullFace(PolygonAttributes.CULL_NONE);
app.setPolygonAttributes(poly);
objRoot.addChild(new Shape3D(array,app));
// Set up the ambient light
Color3f ambientColor = new Color3f(0.5f, 0.5f, 0.5f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
objRoot.addChild(ambientLightNode);
light.setEnable(true);
light.setColor(new Color3f(1.0f,0.0f,1.0f));
objRoot.addChild(light);
objRoot.compile();
locale.addBranchGraph(objRoot);
initGUI();
(new Thread(this)).start();
}
/**
* A quick running thread
*/
public void run() {
int count = 0;
while (true) {
try { Thread.sleep(10); } catch (Exception e) {};
view.renderOnce();
lightAngle += 0.05;
Vector3f dir = new Vector3f((float) Math.cos(lightAngle),(float) Math.sin(lightAngle),-1f);
light.setDirection(dir);
//alien.nextFrame();
alien.interpolateAnim(0.1f);
count++;
if (count == 500) {
count = 0;
if (anim.equals("run")) {
alien.setAnimation("stand");
anim = "stand";
} else {
alien.setAnimation("run");
anim = "run";
}
}
fpsLabel.setText("FPS: BLAH"+0);
fpsLabel.repaint(0);
}
}
/**
* Load up the example model
*/
private BranchGroup loadModel() {
try {
MD2Loader loader = new MD2Loader();
MD2Model model = loader.load(new FileInputStream(modelName),skin);
alien = model.getInstance();
alien.setAnimation(anim);
BranchGroup temp = new BranchGroup();
temp.addChild(alien);
return temp;
} catch (ModelLoadingException e) {
System.err.println(e);
System.exit(1);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
return null;
}
public void initGUI() {
windowMgr = new UIWindowManager(canvas);
w = new TestWindow(200,30);
windowMgr.addOverlay(w);
windowMgr.setPosition(w,10,10);
windowMgr.setVisible(w,true);
UIEventAdapter eventAdapter = new UIEventAdapter(windowMgr);
canvas.get3DPeer().getComponent().addKeyListener(eventAdapter);
canvas.get3DPeer().getComponent().addMouseListener(eventAdapter);
canvas.get3DPeer().getComponent().addMouseMotionListener(eventAdapter);
canvas.get3DPeer().getComponent().setFocusable(true);
}
private class TestWindow extends UIWindow {
public TestWindow(int width, int height) {
super(width, height, false, false );
setRoot(buildGUI(width,height));
}
private JComponent buildGUI (int width, int height) {
JPanel p = new JPanel();
p.setDoubleBuffered(true);
p.setSize(new Dimension(width,height));
p.setLocation(0,0);
p.setBackground(new Color(0.5f,0.5f,0.5f,1.0f));
// add a button for disabling lighting
fpsLabel = new JLabel();
fpsLabel.setText("FPS:");
fpsLabel.setPreferredSize(new Dimension(150,25));
p.add(fpsLabel);
return p;
}
}
/**
* The entry point into this example
*
* @param argv The arguments passed in
*/
public static void main(String argv[]) {
String model = "model/tris.md2";
String skin = "model/marvin.pcx";
if (argv.length > 0) {
if (argv.length == 2) {
model = argv[0];
skin = argv[1];
} else {
System.out.println("Usage: md2test [<model> <skin>]");
System.exit(0);
}
}
TextureLoader.tf.registerPath(".");
new MD2Test(model,skin);
}
}