1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| public class TerrainRenderer2 { Transform3D testRotateY = new Transform3D(); TransformGroup testRotateYGroup = new TransformGroup(); Material material = new Material(); boolean play = true; boolean pause = false; TerrainData terrainMap = null; View view = new View();
public static void main(String[] args) { <load image in map> final TerrainRenderer2 test = new TerrainRenderer2(map, panel); ... test.start(); }
public TerrainRenderer2(TerrainData terrainMap, Component ccc) throws Exception { this.terrainMap = terrainMap;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
VirtualUniverse universe = new VirtualUniverse();
view.setBackClipDistance(100f); view.setProjectionPolicy(View.PERSPECTIVE_PROJECTION); new Vector3f(0, 1, 0)); view.getTransform().lookAt( new Vector3f(0, 3.5f, 3), new Vector3f(0, 0, 0), Locale locale = new Locale(); universe.addLocale(locale); universe.addView(view);
RenderPeer renderPeer = new RenderPeerImpl(); CanvasPeer canvasPeer = renderPeer.makeCanvas( ccc, screenSize.width - 200, screenSize.height - 200, 16, true); Canvas3D canvas = new Canvas3D(); canvas.set3DPeer(canvasPeer); view.addCanvas3D(canvas);
BranchGroup scene = this.createSceneGraph(); locale.addBranchGraph(scene);
class KeyboardEventListener implements AWTEventListener{...} Toolkit.getDefaultToolkit().addAWTEventListener( new KeyboardEventListener(), AWTEvent.KEY_EVENT_MASK); }
public void start() { <classical loop with view.renderOnce()> }
public void stop() { play = false; }
protected BranchGroup createSceneGraph() throws Exception { BranchGroup objRoot = new BranchGroup();
AmbientLight light = new AmbientLight(true, new Color3f(0.25f, 0.25f, 0.25f)); DirectionalLight dlight = new DirectionalLight( true, new Color3f(0.75f, 0.75f, 0.75f), new Vector3f(-1, -1, -1)); objRoot.addChild(light); objRoot.addChild(dlight);
testRotateYGroup.setTransform(testRotateY); objRoot.addChild(testRotateYGroup);
TransformGroup sceneRootTransform = new TransformGroup(); Transform3D t = new Transform3D(); t.setIdentity(); t.setTranslation(new Vector3f(-2, 0, -2)); sceneRootTransform.setTransform(t); testRotateYGroup.addChild(sceneRootTransform);
material.setEmissiveColor(0.0f, 0f, 0f); material.setSpecularColor(0.5f, 0.5f, 0.5f); material.setAmbientColor(10.0f, 0.0f, 0.0f); material.setDiffuseColor(0.75f, 0.0f, 0.0f); material.setLightingEnable(true);
int width = terrainMap.getSize().width; int height = terrainMap.getSize().height; int cellHeight = 0;
float[][] ihf = new float[width - 1][height - 1];
for (int i = 0; i < ihf.length; i++) { for (int j = 0; j < ihf[0].length; j++) { <much much (too much ?) lines to generate the triangles > <looks like xith examples> } }
int flags = GeometryArray.COORDINATES; flags |= GeometryArray.NORMALS;
TriangleArray qa = new TriangleArray(coords.length, flags); qa.setCoordinates(0, coords); qa.setNormals(0, normals);
Appearance a = new Appearance(); a.setPolygonAttributes( new PolygonAttributes( PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0)); a.setColoringAttributes( new ColoringAttributes( new Color3f(1, 0, 0), ColoringAttributes.SHADE_GOURAUD)); a.setMaterial(material);
Shape3D shape = new Shape3D(); shape.setAppearance(a); shape.setGeometry(qa); sceneRootTransform.addChild(shape); } }
return objRoot; } } |