view src/samer/j3d/Util.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
line wrap: on
line source
package samer.j3d;

import samer.core.*;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.keyboard.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.geometry.*;

public class Util
{
	private static Bounds bounds=new BoundingSphere(new Point3d(0,0,0),1000);
	public static void setBounds(Bounds b) { bounds=b; }
	public static Bounds getBounds() { return bounds; }


	// -------------- TransformGroup behviours ------------------

	public static TransformGroup mouseRotate(TransformGroup tg)
	{
        MouseRotate m = new MouseRotate(readwrite(tg));
        m.setSchedulingBounds(bounds);
        tg.addChild(m);
		return tg;
	}

	public static TransformGroup mouseTranslate(TransformGroup tg)
	{
        MouseTranslate m = new MouseTranslate(readwrite(tg));
        m.setSchedulingBounds(bounds);
        tg.addChild(m);
		return tg;
	}

	public static TransformGroup mouseZoom(TransformGroup tg)
	{
        MouseZoom m = new MouseZoom(readwrite(tg));
        m.setSchedulingBounds(bounds);
        tg.addChild(m);
		return tg;
	}

	public static void addKeyNavigator(Group g, TransformGroup tg)
	{
		// readwrite(tg);
        KeyNavigatorBehavior keynav = new KeyNavigatorBehavior(tg);
        keynav.setSchedulingBounds(bounds);
        g.addChild(keynav);
	}

	public static TransformGroup addRotator(int period, TransformGroup tg)
	{
		Alpha alpha = new Alpha(-1, period);
		Behavior rotator = new RotationInterpolator(alpha, writable(tg));
		rotator.setSchedulingBounds(bounds);
		tg.addChild(rotator);
		return tg;
	}

	// -------------- Lighting ------------------

	public static Light color(Light l, Color3f c) { l.setColor(c); return l; }
	public static Light directionalLight(double x, double y, double z)
	{
        DirectionalLight l = new DirectionalLight();
		l.setDirection(-(float)x,-(float)y,-(float)z);
        l.setInfluencingBounds(bounds);
		return l;
	}

	public static Light ambientLight() 
	{
        AmbientLight a = new AmbientLight();
        a.setInfluencingBounds(bounds);
		return a;
	}

	public static Light pointLight(double x, double y, double z)
	{
        PointLight l = new PointLight();
        l.setInfluencingBounds(bounds);
		l.setPosition((float)x,(float)y,(float)z);
		l.setAttenuation(0,0,1);
		return l;
	}

	public static Background background(double r, double g, double b)
	{
		Background bg=new Background((float)r,(float)g,(float)b);
		bg.setApplicationBounds(bounds);
		return bg;
	}


	public static ViewPlatform	 writable(ViewPlatform vp) {
        vp.setCapability(ViewPlatform.ALLOW_POLICY_WRITE); return vp; 
	}
	public static TransformGroup writable(TransformGroup tg) {
        tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); return tg; 
	}
	public static TransformGroup readwrite(TransformGroup tg) {
        tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
        tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 
		return tg; 
	}

	// ----------------- Appearances ------------------

	// basic appearances
	
	public static Appearance points()
	{
		Appearance a = new Appearance();
		PolygonAttributes pa = new PolygonAttributes();
		pa.setPolygonMode(PolygonAttributes.POLYGON_POINT);
		pa.setCullFace(PolygonAttributes.CULL_NONE);
		a.setPolygonAttributes(pa);
		a.setPointAttributes(
			new PointAttributes((float)Shell.getDouble("pointsize",3), false));
		return a;
	}

	public static Appearance wireframe()
	{
		Appearance a = new Appearance();
		PolygonAttributes polyAttrib = new PolygonAttributes();
		polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
		if (!Shell.getBoolean("backfacecull",false)) {
			polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
		}
		a.setPolygonAttributes(polyAttrib);
		a.setLineAttributes(
			new LineAttributes((float)Shell.getDouble("linewidth",2),
						LineAttributes.PATTERN_SOLID,true));

		return a;
    }

    public static Appearance shadedwireframe() 
	{
        Appearance a = new Appearance();
        PolygonAttributes polyAttrib = new PolygonAttributes();
        polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
        a.setPolygonAttributes(polyAttrib);
		a.setLineAttributes(new LineAttributes(2f,LineAttributes.PATTERN_SOLID,false));

        Material material = new Material();
        material.setDiffuseColor(new Color3f(.7f, 0.6f, 0.9f));
		material.setShininess(128);
        a.setMaterial(material);
        return a;
    }

	public static Appearance offsetPolygon() 
	{		
		Appearance a = new Appearance();
		PolygonAttributes polyAttrib = new PolygonAttributes();
		polyAttrib.setPolygonOffset(2f);
		a.setPolygonAttributes(polyAttrib);

		return a;
	}
	
	public static Appearance patches()
	{
		Appearance a = new Appearance();
		PolygonAttributes polyAttrib = new PolygonAttributes();
		polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
		a.setPolygonAttributes(polyAttrib);

		// I think this is for transparent polygons
		// see depthBufferFreezeTransparent in view
		RenderingAttributes	ra=new RenderingAttributes();
		ra.setDepthBufferEnable(false);
		ra.setIgnoreVertexColors(false);
		a.setRenderingAttributes(ra);
		
		return a;
	}

	// modifiers
	
	public static Appearance color(Color3f c, Appearance a)
	{
		ColoringAttributes colorAttrib=new ColoringAttributes();
		colorAttrib.setColor(c);
		a.setColoringAttributes(colorAttrib);
		return a;
	}

	public static Appearance flat(Appearance a)
	{
		ColoringAttributes colorAttrib=new ColoringAttributes();
		colorAttrib.setShadeModel(ColoringAttributes.SHADE_FLAT);
		a.setColoringAttributes(colorAttrib);
		return a;
	}

	public static Appearance flat(Color3f c, Appearance a)
	{
		ColoringAttributes colorAttrib=new ColoringAttributes();
		colorAttrib.setShadeModel(ColoringAttributes.SHADE_FLAT);
		colorAttrib.setColor(c);
		a.setColoringAttributes(colorAttrib);
		return a;
	}

	public static Appearance alpha(double t, Appearance a)
	{
		a.setTransparencyAttributes(
			new TransparencyAttributes(TransparencyAttributes.BLENDED,(float)t));
		return a;
	}

    public static Appearance material(Color3f diff, Color3f spec, int shin, Appearance a) 
	{
        Material material = new Material();
        material.setDiffuseColor(diff);
        material.setSpecularColor(spec);
		material.setShininess(shin);
        a.setMaterial(material);
        return a;
    }
}