Voici un petit exemple compatible avec la version 0.2.4 de ODEJava (2004) liée à Xith3D pour le rendu. A noter que le chapitre 5 du livre du Dr. Andrew Davison intitulé "Pro Java 6 3D game development: Java 3D, JOGL, JInput, and JOAL APIs" (Apress 2007), dont des extraits sont mis à disposition par Google Books, traite de cette version de la bibliothèque et donc offre un exemple plus complet que celui-ci, lié cependant à Java3D pour le rendu.
package com.cosyhut.jblocks.client.display;
import javax.vecmath.*;
import java.util.Observable;
import java.util.Observer;
import java.util.List;
// xith
import com.cosyhut.jblocks.server.ode.Engine;
import com.xith3d.scenegraph.*;
import com.xith3d.test.*;
import com.xith3d.render.*;
import com.xith3d.render.jsr231.*;
// Import odejava to xith binder
import org.odejava.xith3d.OdejavaToXith3D;
import org.odejava.xith3d.Xith3DDisplayObject;
import org.odejava.Body;
import org.odejava.display.*;
public class XithRender implements Observer{
// -- Attributes
// Odejava world
Engine _engine;
View _view;
Locale _locale;
BranchGroup _scene;
Canvas3D _canvas;
// Odejava to xith helper class
OdejavaToXith3D odejavaToXith;
// Each TransformGroup contains xith shapes, these are constantly updated
// by Odejava. TransformGroup's userData contains object Odejava.body
DisplayBin _boundObjects = new DisplayBin();
// Store car chassis body locally because it is frequently accessed
Body _chassisBody;
TransformGroup[] _wheelTransformGroup = new TransformGroup[4];
// -- constructor
public XithRender() {
_engine = Engine.getEngine();
_engine.addObserver(this);
odejavaToXith = new OdejavaToXith3D(_boundObjects);
// create the virtual universe
VirtualUniverse universe = new VirtualUniverse();
// add view to the universe
_view = new View();
universe.addView(_view);
// add locale
_locale = new Locale();
universe.addLocale(_locale);
// create scene
_scene = new BranchGroup();
_locale.addBranchGraph(_scene);
// create canvas for our graphics
RenderPeer rp = new RenderPeerImpl();
CanvasPeer cp = rp.makeCanvas(null, 640, 360, 32, false);
_canvas = new Canvas3D();
_canvas.set3DPeer(cp);
// Set view
_view.addCanvas3D(_canvas);
// Create xith lights
initLights();
// Create xith ground
initGround();
// Create xith objects based on Odejava
initXithFromOdejava();
}
/**
* Initialize some lighting to the scene.
*
*/
private void initLights() {
Color3f col1 = new Color3f(0.5f, 0.5f, 0.5f);
Transform3D t = new Transform3D();
Vector3f pos1 = new Vector3f(0.0f, 0.0f, 30.0f);
t.set(pos1);
Vector3f dir1 = new Vector3f(pos1);
dir1.negate();
dir1.normalize();
DirectionalLight light1 = new DirectionalLight(col1, dir1);
_scene.addChild(light1);
light1.setEnable(true);
}
/**
* Initialize ground manually.
*
*/
private void initGround() {
float k = 100f; // number of texture tiles per narrow side
float w = 2000f; // Width of grass line, length = 128*w
float x1 = -1000f;
float x2 = 1000f;
float y = 0f;
float z1 = -64f * w;
float z2 = 64f * w;
float tsx = k * 1f;
float tsy = k * 128f;
Shape3D shape = new Shape3D();
Geometry geom =TestUtils.createQuad(
new Point3f(x1, z1, y),
new Point3f(x1, z2, y),
new Point3f(x2, z2, y),
new Point3f(x2, z1, y),
tsx,
tsy);
// -- set to color...
shape.setGeometry(geom);
Appearance app = new Appearance();
ColoringAttributes appca = new ColoringAttributes();
appca.setColor(1.0f, 1.0f, 0.9f);
app.setColoringAttributes(appca);
shape.setAppearance(app);
_scene.addChild(shape);
}
/**
* Initialize xith transformgroups with shapes. Use a helper class
* OdejavaToXith for creating xith objects from Odejava objects. Set
* appearances for chassis, ramp and box objects. Set own transformgroup
* (tire and rim shape) for wheel objects.
*
*/
private void initXithFromOdejava() {
Appearance cylinderApp = new Appearance();
ColoringAttributes cylinderAppca = new ColoringAttributes();
cylinderAppca.setColor(0.0f, 0.0f, 0.1f);
cylinderApp.setColoringAttributes(cylinderAppca);
/*
LineAttributes cylinderAppla = new LineAttributes();
cylinderAppla.setLineWidth(10);
cylinderAppla.setLinePattern(LineAttributes.PATTERN_SOLID);
cylinderApp.setLineAttributes(cylinderAppla);
*/
PolygonAttributes cylinderApppa = new PolygonAttributes();
cylinderApppa.setPolygonMode(PolygonAttributes.POLYGON_LINE);
cylinderApp.setPolygonAttributes(cylinderApppa);
odejavaToXith.addUserAppearance("cylinder", cylinderApp);
// Create transform groups based on given Odejava object
odejavaToXith.createTransformGroups(_engine.getGeoms());
Xith3DDisplayObject.addToScene(_boundObjects, _scene);
}
// -- Observer
/*
* a real display should ave its on thread so it's refresh rate won't depend on
* the engine's rate. later.
*/
public void update(Observable o, Object arg) {
// update xith objects based on Odejava bodies
_boundObjects.updateAll();
}
/**
* Render xith scene after Odejava simulation is step ahead and transform
* groups are updated.
*
*/
public void displayRender() {
_view.renderOnce();
}
public View getView() {
return _view;
}
}
