Use the default line shader

From JReality Wiki
Jump to: navigation, search

Source file: LineShaderExample

JavaDoc: DefaultLineShader

Run as Java webstart

This example demonstrates many of the features of the default line shader provided in the jReality scene graph. It is best studied in conjunction with the tutorial example featuring line set factories.

The underlying scene graph in this example consists of a cylindrical stack of circles, each represented by an indexed line set with twenty vertices. Each layer of the stack is attached to a different instance of the default line shader. The underlying geometric line sets are identical to each other, with the exception of some optional vertex and edge attributes (colors and relative radii) which interact with the settings of the line shader to produce different effects. In the center of each circle is a text label containing a number from 1 to 6, which can be matched to the index used in the source code to generate the scene.

The shader attributes for the line shader are created using the shader interfaces, rather than directly setting Appearance attributes. (See Introductory tutorial for more details about this distinction.)

The first two examples demonstrate the display of lines in the simplest form. (In the case of the JOGL backend, this is an edge drawn using an anti-aliased Bresenham algorithm). The rest of the examples render each line as a 3D geometric tube centered on the actual 1D line. In the latter case, many of the rendering parameters are derived from the polygon shader contained in the line shader. In the case of non-tube rendering, the size of the representation is set with the method setLineWidth which is a specified in pixels, while with tube rendering the size is specified with setTubeRadius.

To see the difference between these two coordinate systems, use the click wheel camera zoom tool provided in the example, to zoom in on the scene. The edges drawn in the first two example (non-tube) do not get larger (since pixels remain the same size) but the other points, drawn as tubes, do get larger when one zooms in.

The features demonstrated here include:

  • Simple (non-tube) rendering, using diffuse color and size from point shader.
  • Same as previous, but using a line-stipple pattern for rendering in the JOGL backend, plus activate lighting (indexed line set must have vertex normals attached).
  • Rendering using tubes, single color, single tube radius, opaque.
  • Same as previous, but transparency enabled.
  • Colors from edge colors in the point set
  • Radius of tubes modulated by values appearing in point set vertex attribute Attribute.RELATIVE_RADII.
  • Last two examples same as previous two, but indexed line sets have a single long edge so the tubing is smoother and without gaps between adjacent edges.

Note that when tubes are drawn, they will be drawn with smooth-shading or flat-shading, depending on the value of getSmoothShading() in the polygon shader interface associated to the line shader. The following code will guarantee that tubes are always drawn smooth, although other polygonal geometry would be drawn flat shaded (all attributes from the class CommonAttributes)::

ap.setAttribute(POLYGON_SHADER+"."+SMOOTH_SHADING, false);

Note: The attribute LINE_LIGHTING_ENABLED (shader interface method setLineLighting) is not demonstrated in the example, but can be used if all following conditions are satisfied:

  • JOGL backend is being used,
  • setLineLighting(true) is called, and
  • the line set has vertex normals.

In this case, the lines will be rendered according to the current state of the lights and the vertex normals of the line set. [This feature should be packaged up into a non-default line shader!]

Conclusion: The final diffuse color attached to lines is a result of several factors:

  • tube/non-tube rendering?
  • availability of vertex and edge colors from the indexed line set?
  • the value of the attribute VERTEX_COLOR_ENABLED (alternatively the method getVertexColor)?
  • smooth/flat shading set in the polygon shader attached to the line shader?

Some of the features of the line shader (particularly those related to rendering hints, such as the rhs.setOpaqueTubesAndSpheres(), are only implemented at the current time for the JOGL backend.

Previous: Use the default point shader Developer Tutorial: Contents Next: Use the default polygon shader