## Immediate-Mode Rendering

Java 3D is fundamentally a scene graph-based API. Most of the constructs in the API are biased toward retained mode and compiled-retained mode rendering. However, there are some applications that want both the control and the flexibility that immediate-mode rendering offers.

Immediate-mode applications can either use or ignore Java 3D's scene graph structure. By using immediate mode, end-user applications have more freedom, but this freedom comes at the expense of performance. In immediate mode, Java 3D has no high-level information concerning graphical objects or their composition. Because it has minimal global knowledge, Java 3D can perform only localized optimizations on behalf of the application programmer.

## Two Styles of Immediate-Mode Rendering

Use of Java 3D's immediate mode falls into one of two categories: pure immediate-mode rendering and mixed-mode rendering in which immediate mode and retained or compiled-retained mode interoperate and render to the same canvas. The Java 3D renderer is idle in pure immediate mode, distinguishing it from mixed-mode rendering.

### Pure Immediate-Mode Rendering

Pure immediate-mode rendering provides for those applications and applets that do not want Java 3D to do any automatic rendering of the scene graph. Such applications may not even wish to build a scene graph to represent their graphical data. However, they use Java 3D's attribute objects to set graphics state and Java 3D's geometric objects to render geometry.

Note: Scene antialiasing is not supported in pure immediate mode.

A pure immediate mode application must create a minimal set of Java 3D objects before rendering. In addition to a Canvas3D object, the application must create a View object, with its associated PhysicalBody and PhysicalEnvironment objects, and the following scene graph elements: a VirtualUniverse object, a high-resolution Locale object, a BranchGroup node object, a TransformGroup node object with associated transform, and, finally, a ViewPlatform leaf node object that defines the position and orientation within the virtual universe that generates the view (see Figure 1).

Figure 1 – Minimal Immediate-Mode Structure

Java 3D provides utility functions that create much of this structure on behalf of a pure immediate-mode application, making it less noticeable from the application's perspective-but the structure must exist.

All rendering is done completely under user control. It is necessary for the user to clear the 3D canvas, render all geometry, and swap the buffers. Additionally, rendering the right and left eye for stereo viewing becomes the sole responsibility of the application.

In pure immediate mode, the user must stop the Java 3D renderer, via the Canvas3D object `stopRenderer()` method, prior to adding the Canvas3D object to an active View object (that is, one that is attached to a live ViewPlatform object).

### Mixed-Mode Rendering

Mixing immediate mode and retained or compiled-retained mode requires more structure than pure immediate mode. In mixed mode, the Java 3D renderer is running continuously, rendering the scene graph into the canvas.

The basic Java 3D stereo rendering loop, executed for each Canvas3D, is as follows:

    clear canvas (both eyes)

    call preRender()                           // user-supplied method
    set left eye view
    render opaque scene graph objects
    call renderField(FIELD_LEFT)               // user-supplied method
    render transparent scene graph objects
    set right eye view
    render opaque scene graph objects again
    call renderField(FIELD_RIGHT)              // user-supplied method
    render transparent scene graph objects again
    call postRender()                          // user-supplied method
    synchronize and swap buffers

    call postSwap()                            // user-supplied method

The basic Java 3D monoscopic rendering loop is as follows:

    clear canvas

    call preRender()                            // user-supplied method
    set view
    render opaque scene graph objects
    call renderField(FIELD_ALL)                 // user-supplied method
    render transparent scene graph objects
    call postRender()                           // user-supplied method
    synchronize and swap buffers

    call postSwap()                             // user-supplied method

In both cases, the entire loop, beginning with clearing the canvas and ending with swapping the buffers, defines a frame. The application is given the opportunity to render immediate-mode geometry at any of the clearly identified spots in the rendering loop. A user specifies his or her own rendering methods by extending the Canvas3D class and overriding the `preRender`, `postRender`, `postSwap`, and/or `renderField` methods.