First BSP Example
You'll encapsulate the BSP drawing in the SimpleBSPRenderer class, which is a subclass of ShadedSurfacePolygonRenderer. This new class has one method for drawing polygons in a tree and also is a BSPTraverseListener, as shown here:
/** Draws the visible polygons in a BSP tree based on the camera location. The polygons are drawn front to back. */ public void draw(Graphics2D g, BSPTree tree) { currentGraphics2D = g; traverser.traverse(tree, camera.getLocation()); } // from the BSPTreeTraverseListener interface public boolean visitPolygon(BSPPolygon poly, boolean isBack) { draw(currentGraphics2D, poly); return !((SortedScanConverter)scanConverter).isFilled(); }
Next, create the basic demo in a class called BSPTest3D. The BSPTest3D class is similar to BSPTest2D, except that it draws polygons using the SimpleBSPRenderer:
public void draw(Graphics2D g) { // draw polygons polygonRenderer.startFrame(g); ((SimpleBSPRenderer)polygonRenderer).draw(g, bspTree); polygonRenderer.endFrame(g); super.drawText(g); }
That's it for the demo. Finally, you can see your example floor plan in 3D. Check out the screenshot in Screenshot.
Screenshot Screenshot of BSPTest3D.
This demo works fine, but there are a couple problems:
- You can draw 3D objects such as monsters and treasures, but they won't be merged correctly with the scene because object drawing relies on z-buffering.
- Creating polygons in code will quickly become a drag. You need an easier way.
So, let's work on solving these issues, shall we?