3 Replies Latest reply: Oct 29, 2010 7:24 AM by 809116 RSS

    J3D, nothing appear...

    843799
      Hello everyone,

      I have recently begun programming with J3D and I already have a problem.

      I just wante to make a sphere appear on the screen, so I wrote that :
      ( This Panel3D class is just added as contentPane of a JFrame )
      package com.dx.d3d;
      
      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.Dimension;
      import java.awt.Graphics;
      import java.awt.GraphicsConfiguration;
      
      import javax.swing.JPanel;
      
      import com.sun.j3d.utils.universe.*;
      import com.sun.j3d.utils.geometry.*;
      
      import javax.media.j3d.*;
      import javax.vecmath.*;
      
      import com.sun.j3d.utils.behaviors.vp.*;
      
      public class Panel3D extends JPanel implements Runnable
      {
           SimpleUniverse universe;
           
           public Panel3D()
           {
                super( new BorderLayout() );
                
                final Color3f WHITE = new Color3f(1.0f, 1.0f, 1.0f); 
                final Color3f RED     = new Color3f(1.0f, 0.0f, 0.0f); 
      
                setPreferredSize(new Dimension(640, 480));
                
                //
                GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
                
                //
                Canvas3D canvas3D = new Canvas3D(config);
                add(canvas3D);
                
                //
                universe = new SimpleUniverse(canvas3D);
                
                //
                BranchGroup group = new BranchGroup();
                
                Appearance appearance = new Appearance();
                ColoringAttributes colorA = new ColoringAttributes(RED, ColoringAttributes.SHADE_FLAT);
                appearance.setColoringAttributes( colorA );
                
                Material mat = new Material();
                appearance.setMaterial(mat);
                
                Sphere s = new Sphere(0.2f, Sphere.GENERATE_NORMALS, appearance);
                group.addChild(s);
                
                AmbientLight light = new AmbientLight( WHITE );
                group.addChild(light);
                
                universe.addBranchGraph(group);
           }
      
      }
      but nothing appears when I launch the program...

      So, I wonder if someone knows where is the problem.

      Thanks in advance.
        • 1. Re: J3D, nothing appear...
          809116
          hello troubled guy,

          i've got a good news for you.
          i've run your program and figured it out where the problem is...
          there are basically two problems...
          first, you didn't specify the viewing platform for the simple universe. just do it -

          universe = new SimpleUniverse(canvas3D);
          universe.getViewingPlatform().setNominalViewingTransform(); //this is the one you missed.

          next the major problem lies in forgetting to specify the Influencing bound for the Ambient light source. Be very careful in this mistake..it is really common programming pitfalls. so correct them as -

          AmbientLight light = new AmbientLight(WHITE);
          group.addChild(light);
          light.setInfluencingBounds(new BoundingSphere()); //this is the line you'v missed
          light.setColor(new Color3f(Color.yellow)); //you can specify the color as well. the default one is white

          I'm sure now that your code must be working....
          if it's still not working, do post your whole code..i'll definitely solve it..
          so till then,
          happy Java3Ding...

          -Neo
          • 2. Re: J3D, nothing appear...
            809116
            And ya do always use the Gourad shading instead of flat shading because the former gives very realistic shading....!
            so replace above line ColoringAttributes colorA = new ColoringAttributes(new Color3f(Color.red), ColoringAttributes.SHADE_FLAT); with
            this ColoringAttributes colorA = new ColoringAttributes(new Color3f(Color.red), ColoringAttributes.SHADE_GOURAUD);

            happy Java3Ding...
            -neo
            • 3. Re: J3D, nothing appear...
              PhHein
              Neo,
              please don't revive old, imported threads. I'm locking up here.

              Moderator action: Locking