In immediate mode, I'm getting a NullPointerException when doing
GraphicsContext3D.setFog(null).  According to the documentation, a null
parameter is legal and should disable any fog.  Is this a bug?  I'm using
Java 1.3.0 and Java3D 1.2 (not 1.2.1) on Solaris 5.7.

See the attached program for a demonstration of this problem.  When run,
it first displays a white X, with no fog.  Then successive clicks in the
window enable and disable green-colored fog.

If this is not a bug, is there some official way of disabling fog, short
of creating a new Canvas3D?

 --Young
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.universe.*;

public class SetFogBug
    extends Canvas3D
{
    public static void main(String[] args)
    {
        GraphicsConfiguration config =
            SimpleUniverse.getPreferredConfiguration();

        final SetFogBug canvas = new SetFogBug(config);
        canvas.stopRenderer();
        canvas.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e)
                {
                    canvas.toggleDepthCueing();
                }
            });

        SimpleUniverse univ = new SimpleUniverse(canvas);
        univ.getViewingPlatform().setNominalViewingTransform();

        Frame frame = new Frame();
        frame.setSize(500, 500);
        frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            });
        frame.add(canvas, BorderLayout.CENTER);
        frame.show();
    }

    ///////////////////////////////////////////////////////////////////////

    public SetFogBug(GraphicsConfiguration config)
    {
        super(config);
        m_geometry = createGeometry();
        m_appearance = createAppearance();
    }

    public void toggleDepthCueing()
    {
        if (m_depthCueing == null)
        {
            System.out.println("Enabling depth cueing...");
            m_depthCueing = new LinearFog(0.0f, 1.0f, 0.0f);
            m_depthCueing.setFrontDistance(1.0f);
            m_depthCueing.setBackDistance(3.0f);

            // Not needed in immediate mode, but here for completeness.
            m_depthCueing.setInfluencingBounds
                (new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0));
        }
        else
        {
            System.out.println("Disabling depth cueing...");
            m_depthCueing = null;
        }

        m_depthCueingChanged = true;
        render();
    }

    public void paint(Graphics g)
    {
        super.paint(g);
        render();
    }

    private void render()
    {
        GraphicsContext3D gc = getGraphicsContext3D();
        if (m_depthCueingChanged)
        {
            m_depthCueingChanged = false;
            gc.setFog(m_depthCueing);
        }
        gc.clear();
        gc.setAppearance(m_appearance);
        gc.draw(m_geometry);
        swap();
    }

    private Geometry createGeometry()
    {
        double r = 1.0;

        LineArray axisGeometry = new LineArray(8, LineArray.COORDINATES);
        Point3d[] coordinates = new Point3d[8];
        coordinates[0] = new Point3d(-r, r, 0.0);
        coordinates[1] = new Point3d(0.0, 0.0, 1.0);
        coordinates[2] = new Point3d(r, r, 0.0);
        coordinates[3] = new Point3d(0.0, 0.0, 1.0);
        coordinates[4] = new Point3d(r, -r, 0.0);
        coordinates[5] = new Point3d(0.0, 0.0, 1.0);
        coordinates[6] = new Point3d(-r, -r, 0.0);
        coordinates[7] = new Point3d(0.0, 0.0, 1.0);
        axisGeometry.setCoordinates(0, coordinates);
        return axisGeometry;
    }

    private Appearance createAppearance()
    {
        LineAttributes lineAttributes = new LineAttributes();   
        lineAttributes.setLineWidth(50.0f);

        ColoringAttributes coloringAttributes =
            new ColoringAttributes(1.0f, 1.0f, 1.0f,
                                   ColoringAttributes.FASTEST);
      
        Appearance appearance = new Appearance();
        appearance.setColoringAttributes(coloringAttributes);
        appearance.setLineAttributes(lineAttributes);
        return appearance;
    }

    private Geometry m_geometry;
    private Appearance m_appearance;
    private boolean m_depthCueingChanged;
    private LinearFog m_depthCueing;
}

Reply via email to