view examples/java3d/util.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents 5df24c91468d
children
line wrap: on
line source
import samer.core.*;
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.*;
//import com.sun.j3d.utils.universe.*; 
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 init() { new samer.core.shells.AppShell(); }
	public static void expose(java.awt.Component c,String title) {
		Shell.Window win;
		win=Shell.getWindow(title);
		win.addWindowListener(Shell.exitListener());
		win.container().setLayout(new BorderLayout());
		win.container().add(c);
		win.expose();
	}

	public static void setBounds(Bounds b) { bounds=b; }

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

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

	public static TransformGroup mouseZoom(TransformGroup tg)
	{
        MouseZoom m = new MouseZoom(readwrite(tg));
        // mr.setTransformGroup(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 void addKeyNavigator(Component c, Group g, TransformGroup tg)
	{
		// readwrite(tg);
        KeyNavigatorBehavior keynav = new KeyNavigatorBehavior(c,tg);
        keynav.setSchedulingBounds(bounds);
        g.addChild(keynav);
	}

	public static GeometryInfo yoyoGeometryInfo(int N)
	{
		GeometryInfo gi=new GeometryInfo(GeometryInfo.TRIANGLE_FAN_ARRAY);

		int     totalN = 4*(N+1);
		Point3f coords[] = new Point3f[totalN];
		int     stripCounts[] = {N+1, N+1, N+1, N+1};
		float   r = 0.5f;
		float   w = 0.5f;
		int     n;
		double  a;
		float   x, y;

		// set the central points for four triangle fan strips
		coords[0*(N+1)] = new Point3f(0.0f, 0.0f, w);
		coords[1*(N+1)] = new Point3f(0.0f, 0.0f, 0.0f);
		coords[2*(N+1)] = new Point3f(0.0f, 0.0f, 0.0f);
		coords[3*(N+1)] = new Point3f(0.0f, 0.0f, -w);

		for(a = 0,n = 0; n < N; a = 2.0*Math.PI/(N-1) * ++n){
			x = (float) (r * Math.cos(a));
			y = (float) (r * Math.sin(a));
			coords[0*(N+1)+n+1] = new Point3f(x, y, w);
			coords[1*(N+1)+N-n] = new Point3f(x, y, w);
			coords[2*(N+1)+n+1] = new Point3f(x, y, -w);
			coords[3*(N+1)+N-n] = new Point3f(x, y, -w);
		}

		gi.setCoordinates(coords);
		gi.setStripCounts(stripCounts);

		return gi;

	} 

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

	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 void addBackground(Group g, Background bg)
	{	// background
		bg.setApplicationBounds(bounds);
		g.addChild(bg);
	}

	public static View setViewPolicy(View v)
	{
		v.setWindowEyepointPolicy(View.RELATIVE_TO_WINDOW);
		//v.setWindowMovementPolicy(View.VIRTUAL_WORLD);
		v.setWindowResizePolicy(View.VIRTUAL_WORLD);
		//v.repaint();
		return v;
	}

	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; 
	}

	public static Group yoyoGroup() { 
		return yoyoGroup(yoyoGeometryInfo(17).getGeometryArray());
	}
	public static Group yoyoGroup(Geometry geom) {
		return yoyoGroup(geom,wireframe2(),translucent());
	}
		
	public static Group yoyoGroup(Geometry geom, Appearance a1, Appearance a2)
	{
		Shape3D		faces=new Shape3D();
		Shape3D		edges=new Shape3D();

		faces.setGeometry(geom);
		faces.setAppearance(a1);
		edges.setGeometry(geom);
		edges.setAppearance(a2);
		
		BranchGroup bg = new BranchGroup();
		bg.addChild(faces);
		bg.addChild(edges);

		return bg;
	}

	public static GraphicsConfiguration getGraphicsConfiguration()
	{
/*
        return GraphicsEnvironment.getLocalGraphicsEnvironment().
                getDefaultScreenDevice().getDefaultConfiguration();
		
*/		
		// Canvas default seems to be better than above
		return null;
		
	}

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

        Material material = new Material();
        material.setDiffuseColor(new Color3f(.6f, 0.5f, 0.7f));
        material.setSpecularColor(new Color3f(.6f, 0.5f, 0.7f));
		material.setShininess(48);
        a.setMaterial(material);
        return a;
    }


    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;
    }


	static Appearance translucent() 
	{		
		Appearance a = new Appearance();
		PolygonAttributes polyAttrib = new PolygonAttributes();
		ColoringAttributes colorAttrib=new ColoringAttributes();
		a.setPolygonAttributes(polyAttrib);

		{
			boolean ft=Shell.getBoolean("yoyo.facetransparency",true);
			if (ft) {
				a.setTransparencyAttributes(
					new TransparencyAttributes(TransparencyAttributes.BLENDED,0.2f)
				);
				colorAttrib.setColor(0.45f,0.4f,.5f);
			} else {
				polyAttrib.setPolygonOffset(2f);
				colorAttrib.setColor(0.5f,0.4f,.5f);
			}
		}
		a.setColoringAttributes(colorAttrib);
		return a;
	}

    static Appearance wireframe1()
	{

        Appearance a = new Appearance();
        PolygonAttributes polyAttrib = new PolygonAttributes();
        polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE);
		if (!Shell.getBoolean("yoyo.bfacecull",false)) {
			polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
		}
		a.setLineAttributes(
			new LineAttributes((float)Shell.getDouble("yoyo.linewidth",1),
						LineAttributes.PATTERN_SOLID,true));
        a.setPolygonAttributes(polyAttrib);
        ColoringAttributes redColoring = new ColoringAttributes();
        redColoring.setColor(.6f, 0.4f, 0.5f);
        a.setColoringAttributes(redColoring);
		a.setTransparencyAttributes(
			new TransparencyAttributes(TransparencyAttributes.BLENDED,0.4f));

        return a;
    }

	static Appearance wireframe2()
	{
		Appearance appearance1 = new Appearance();
		PolygonAttributes polyAttrib = new PolygonAttributes();
		polyAttrib.setPolygonMode(
			Shell.getBoolean("yoyo.points",true) ?
				PolygonAttributes.POLYGON_POINT :
				PolygonAttributes.POLYGON_LINE

		);
		if (!Shell.getBoolean("yoyo.bfacecull",false)) {
			polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
		}
	
		appearance1.setPolygonAttributes(polyAttrib);
		appearance1.setPointAttributes(
			new PointAttributes((float)Shell.getDouble("yoyo.pointsize",2), false));
		appearance1.setLineAttributes(
			new LineAttributes((float)Shell.getDouble("yoyo.linewidth",1),
						LineAttributes.PATTERN_SOLID,true));
		appearance1.setTransparencyAttributes(
			new TransparencyAttributes(TransparencyAttributes.BLENDED,0.7f));
		ColoringAttributes colorAttrib=new ColoringAttributes();
		colorAttrib.setColor(0.8f,0.6f,1f);
		appearance1.setColoringAttributes(colorAttrib);
		return appearance1;
	}

	static Appearance wireframe3()
	{
		Appearance appearance1 = new Appearance();
		PolygonAttributes polyAttrib = new PolygonAttributes();
		polyAttrib.setPolygonMode(
			Shell.getBoolean("yoyo.points",true) ?
				PolygonAttributes.POLYGON_POINT :
				PolygonAttributes.POLYGON_LINE

		);
		if (!Shell.getBoolean("yoyo.bfacecull",false)) {
			polyAttrib.setCullFace(PolygonAttributes.CULL_NONE);
		}
	
		appearance1.setPolygonAttributes(polyAttrib);
		appearance1.setPointAttributes(
			new PointAttributes((float)Shell.getDouble("yoyo.pointsize",2), false));
		appearance1.setLineAttributes(
			new LineAttributes((float)Shell.getDouble("yoyo.linewidth",1),
						LineAttributes.PATTERN_SOLID,true));
		appearance1.setTransparencyAttributes(
			new TransparencyAttributes(TransparencyAttributes.BLENDED,0.7f));

        Material material = new Material();
        material.setDiffuseColor(new Color3f(.8f, 0.6f, 1f));
        appearance1.setMaterial(material);
		return appearance1;
	}

	public static Appearance pointy()
	{
		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(5,false));
		a.setTransparencyAttributes(
		new TransparencyAttributes(TransparencyAttributes.BLENDED,0.8f));
/*
			new TransparencyAttributes(
				TransparencyAttributes.BLENDED,0.9f,
				TransparencyAttributes.BLEND_SRC_ALPHA,
				TransparencyAttributes.BLEND_ONE));
*/
		ColoringAttributes colorAttrib=new ColoringAttributes();
		colorAttrib.setColor(0.8f,0.6f,1f);
		a.setColoringAttributes(colorAttrib);
		return a;
	}

	public static Appearance pointy2()
	{
		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(3,false));
		a.setTransparencyAttributes(
			new TransparencyAttributes(TransparencyAttributes.BLENDED,0.7f));

		ColoringAttributes colorAttrib=new ColoringAttributes();
		colorAttrib.setColor(0.8f,0.6f,1f);
		a.setColoringAttributes(colorAttrib);
		return a;
	}

	public static Appearance pointy3()
	{
		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(3,false));

		ColoringAttributes colorAttrib=new ColoringAttributes();
		colorAttrib.setColor(0.8f,0.6f,1f);
		a.setColoringAttributes(colorAttrib);
		return a;
	}
}