# VRML Loader(noob Frage) ^^



## mattn (21. Jun 2010)

hi, ich will einfach via vrml loader eine *.wrl "importieren" 
ich bekomm keine fehlermeldung(applet gibt nur nichts aus), was mich darauf schließen lässt das der fehler in der "logik" des codes liegt...


```
public BranchGroup createSceneGraph(){
		rootBG = new BranchGroup();
		
		VrmlLoader newVrml = new VrmlLoader();
		BranchGroup vrmlBG = null;
				
		try {
			vrmlBG = newVrml.load("earth.wrl").getSceneGroup();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IncorrectFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParsingErrorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		vrmlBG.setCapability( BranchGroup.ALLOW_BOUNDS_READ );
		TransformGroup vrmlTG = new TransformGroup();
		vrmlTG.addChild(vrmlBG);
		// Add the VRML file to the scene
		rootBG.addChild(vrmlTG);
				   
		TransformGroup BoxTG = new TransformGroup();
		Transform3D BoxT3D = new Transform3D();
		BoxT3D.setTranslation(new Vector3f(0f,0f,-10f));
		BoxTG.setTransform(BoxT3D);

	    rootBG.addChild(BoxTG);
		rootBG.compile();
		return rootBG;
	 }

	public void init(){
		setLayout(new BorderLayout());
		config = SimpleUniverse.getPreferredConfiguration();
		c= new Canvas3D(config);
		add("Center", c);
		u = new SimpleUniverse(c);		
		u.getViewingPlatform().setNominalViewingTransform();
		u.addBranchGraph(createSceneGraph());
	}
```


EDIT:
also das laden der 'earth.wrl' scheint zu klappen, da das vrmlBG != null ist
wird komischer weise nur nicht ausgegeben


----------



## truesoul (23. Jun 2010)

Hier habe mal ein alten Code von mir gefunden , womit es klappen sollte.
Funktioniert einwandfrei!!


```
import com.sun.j3d.loaders.*;
import org.jdesktop.j3d.loaders.vrml97.*;
import com.sun.j3d.loaders.ParsingErrorException;
import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.Scene;
import java.applet.Applet;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import java.io.*;
import javax.vecmath.*;
import com.sun.j3d.utils.picking.behaviors.PickRotateBehavior;
import com.sun.j3d.utils.picking.behaviors.PickTranslateBehavior;
import com.sun.j3d.utils.picking.behaviors.PickZoomBehavior;


public class Ladevrml extends JApplet
{
    Canvas3D c;
    BranchGroup objWurzel = new BranchGroup();
    PickRotateBehavior pickDreh = null;
    PickZoomBehavior pickzoom = null;
    PickTranslateBehavior picktrans = null;

    BoundingSphere BigBounds = new BoundingSphere(new Point3d(), 1000000);

    public Ladevrml()
    {
        setLayout(new BorderLayout());
        GraphicsConfiguration config =
        SimpleUniverse.getPreferredConfiguration();
        c = new Canvas3D(config);

        add("Center", c);
        BranchGroup szene = erstelleBranchGroup(c);

        SimpleUniverse u = new SimpleUniverse(c);

        View v = u.getViewer().getView();

        // "Größe" des Universums einstellen
        v.setFrontClipDistance(1.0);
        v.setBackClipDistance(303000.0);
        v.getPhysicalBody().setNominalEyeOffsetFromNominalScreen(1.0);
        u.getViewingPlatform().setNominalViewingTransform();

        // Licht als eigenes BranchGroup
        u.addBranchGraph(erstelleLicht());
        // Hintergrund als eigenes BranchGroup
        u.addBranchGraph(erstelleHintergrund());
        // Scene als eigenes BranchGroup
        u.addBranchGraph(szene);

    }

    public BranchGroup erstelleHintergrund()
   {
        BranchGroup view = new BranchGroup();
        Background back = new Background(new Color3f(0.2f, 0f, 0f));
        back.setApplicationBounds(new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000000));
        view.addChild(back);

        return view;
    }

    public BranchGroup erstelleBranchGroup(Canvas3D can)
    {
        TransformGroup meineTransformGruppe = new TransformGroup();
        meineTransformGruppe.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        meineTransformGruppe.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        meineTransformGruppe.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

        Transform3D meinTransform3D = new Transform3D();
        Scene meineScene = holeScene();

        // Wichtig um Objekt zu Skalieren falls sie zu groß sind um Korrekt im Universum    angezeigt zu werden
        meinTransform3D.setScale(0.1);
        meinTransform3D.setTranslation(new Vector3f(0f, 0f, -700f));
        meineTransformGruppe.setTransform(meinTransform3D);

       
        meineTransformGruppe.addChild(meineScene.getSceneGroup());

        objWurzel.addChild(erstelleBehaviorsGruppe());

        objWurzel.addChild(meineTransformGruppe);

        return objWurzel;
    }

    public Scene holeScene()
    {
        Loader f = new VrmlLoader();
        Scene scene = null;

        try
        {
            scene = f.load("test/erde.wrl");
        }
        catch (FileNotFoundException e)
        {
            System.err.println(e);
            System.exit(1);
        }
        catch (ParsingErrorException e)
        {
            System.err.println(e);
            System.exit(1);
        }
        catch (IncorrectFormatException e)
        {
            System.err.println(e);
            System.exit(1);
        }

        return scene;
    }

    public BranchGroup erstelleLicht()
    {
        BranchGroup branch = new BranchGroup();

        AmbientLight licht = new AmbientLight();
        licht.setInfluencingBounds(BigBounds);
        branch.addChild(licht);

        // Mehrere Lichtquellen möglich!
        return branch;

    }

    private TransformGroup erstelleBehaviorsGruppe()
      {

          /**
           * Picking Behavior werden hinzugefügt
           */
            TransformGroup examineGroup = new TransformGroup();
            examineGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
            examineGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            examineGroup.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

            pickzoom = new PickZoomBehavior(objWurzel, c, new BoundingSphere(new Point3d(), 100));
            pickDreh = new PickRotateBehavior(objWurzel, c, new BoundingSphere(new Point3d(), 100));
            picktrans = new PickTranslateBehavior(objWurzel, c, new BoundingSphere(new Point3d(), 100));
            pickDreh.setTolerance(0f);

            examineGroup.addChild(pickDreh);
            examineGroup.addChild(pickzoom);
            examineGroup.addChild(picktrans);

            return examineGroup;
      }

    

    public static void main(String[] args)
    {
        new MainFrame(new Ladevrml(), 700, 700);
    }
}
```


----------



## mattn (23. Jun 2010)

habe das jetzt anders gelöst.... vllt ein bisschen umständlich aber es geht ... 
(nach gefühlter ewigkeit fummeln)
ich post mal code für die nachwelt....

```
public class myVrmlImport extends JApplet{

	private SimpleUniverse u = null;

	GraphicsConfiguration config;
	Canvas3D c;
	BranchGroup rootBG;
	TransformGroup sceneTG;

	public myVrmlImport(){}

	public BranchGroup createSceneGraph(){
		
		rootBG = new BranchGroup();
		sceneTG = new TransformGroup();
		Scene vrmlScene = this.loadScene("earth.wrl");
		BranchGroup rootObj = null;
		
		
		if (vrmlScene != null && (rootObj = vrmlScene.getSceneGroup()) != null)
			sceneTG.addChild(rootObj);
		//vrmlBG.setCapability(BranchGroup.ALLOW_BOUNDS_READ);
		rootBG.addChild(sceneTG);
		//rootBG.addChild(vrmlBG);	  
		rootBG.compile();
		return rootBG;
	 }

	public void init(){
		
		setLayout(new BorderLayout());
		config = SimpleUniverse.getPreferredConfiguration();
		c= new Canvas3D(config);
		add("Center", c);
		u = new SimpleUniverse(c);		
		OrbitBehavior orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL);
		orbit.setSchedulingBounds(new BoundingSphere());
		u.getViewingPlatform().setViewPlatformBehavior(orbit);
		u.getViewingPlatform().setNominalViewingTransform();
		u.addBranchGraph(createSceneGraph());
	}

	public void destroy(){
		u.cleanup();
	}
	
	
	private Scene loadScene(String relativePath) {
		Scene scene = null;
		try {
			// Alternative: classLoader.getResourceAsStream(relativePath)
			InputStream stream = this.getClass().getResourceAsStream(relativePath);
			if (stream == null) return null;
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
			
//			Some loader could have problems to handle a url based on jar: protocol
//			The syntax of the jar: protocol is jar:<url>!/{entry}. "!/" is called the separator.
//			Examples local/web:
//			jar:file:/ ... /appletVrmlLoader.jar!/com/interactivemesh/j3d/sourcecode/applet/scenes/Fish.wrl
//			jar:[url]http://www.interactivemesh.org/sourcecode/webstart/appletVrmlLoader.jar!/com/interactivemesh/j3d/sourcecode/applet/scenes/Fish.wrl[/url]			

			//Holen der URL aus dem relativePath
			URL sceneUrl = this.getClass().getResource(relativePath);
			if (sceneUrl == null) return null;
			
			//für URL Consolenausgabe vorbereiten
			String sceneUrlString = sceneUrl.toString();
			String baseUrlString = sceneUrlString.substring(0, sceneUrlString.lastIndexOf('/') + 1); // since 1.2: +1
			URL baseUrl = new URL(baseUrlString);
			System.out.println("Scene URL = " + sceneUrlString);
			
			Loader vrmlLoader = new VrmlLoader(); // No flag: Shapes only
			//BaseUrl 
			vrmlLoader.setBaseUrl(baseUrl); 
			System.out.println("Base  URL = " + vrmlLoader.getBaseUrl().toString());
			
			//VRML in die Scene Schreiben(inkl. Abfangen der Exeptions)
			try {
				scene = vrmlLoader.load(bufferedReader);
				
				// Alternative: if Loader is able to handle jar: protocol
				// scene = vrmlLoader.load(sceneUrl);
			}
			catch (FileNotFoundException e) {	
				e.printStackTrace();
			}
			catch (IncorrectFormatException e) {	
				e.printStackTrace();
			}
			catch (ParsingErrorException e) {		
				e.printStackTrace();
			}
			
			stream.close(); 
			bufferedReader.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		return scene;
	}
```


----------



## AlphaBanane (23. Jul 2012)

mattn mit welchem Programm hast du das geschrieben?


----------

