package org.stratagem.code;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.vecmath.Vector3f;
import com.xith3d.render.CanvasPeer;
import com.xith3d.render.RenderPeer;
import com.xith3d.render.jsr231.RenderPeerImpl;
import com.xith3d.scenegraph.BranchGroup;
import com.xith3d.scenegraph.Canvas3D;
import com.xith3d.scenegraph.Locale;
import com.xith3d.scenegraph.View;
import com.xith3d.scenegraph.VirtualUniverse;
public class RenderCode {
/*
* 3D
*/
Canvas3D canvas;
VirtualUniverse universe;
View view;
BranchGroup scene;
Locale locale;
public enum DisplayMode {
D320x240,
D640x480,
D800x600,
D1024x768,
DS0,
DS200
};
/**
* @param mode D320x240, D640x480, D800x600, D1024x768, or DS200
*/
public RenderCode(DisplayMode mode) {
// Universe
universe = new VirtualUniverse();
view = new View();
view.setFieldOfView(35f * 3.14f / 180f);
locale = new Locale();
universe.addLocale(locale);
universe.addView(view);
scene = new BranchGroup();
locale.addBranchGraph(scene);
// Canvas
RenderPeer rp = new RenderPeerImpl();
int xSize, ySize;
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
switch(mode) {
case D320x240 :
xSize = 320; ySize = 240;
break;
case D640x480 :
xSize = 640; ySize = 480;
break;
case D800x600 :
xSize = 800; ySize = 600;
break;
case D1024x768 :
xSize = 1024; ySize = 768;
break;
case DS0 :
xSize = d.width; ySize = d.height;
break;
case DS200 :
d = Toolkit.getDefaultToolkit().getScreenSize();
xSize = d.width - 200; ySize = d.height - 200;
break;
default :
return;
}
int xPos = d.width / 2 - xSize / 2;
int yPos = d.height / 2 - ySize / 2;
CanvasPeer cp = rp.makeCanvas(null, xSize, ySize, 16,
true);
if(xPos != -1) {
cp.setLocation(xPos, yPos);
}
canvas = new Canvas3D();
canvas.set3DPeer(cp);
canvas.setView(new View());
view.addCanvas3D(canvas);
}
public Canvas3D getCanvas() {
return canvas;
}
public BranchGroup getScene() {
return scene;
}
public View getView() {
return view;
}
public Locale getLocale() {
return locale;
}
public void render() {
view.renderOnce();
}
public void lookAt(Vector3f eye, Vector3f center, Vector3f up) {
getView().getTransform().lookAt(eye, center, up);
}
}