Forum Stats

  • 3,875,873 Users
  • 2,266,977 Discussions
  • 7,912,362 Comments

Discussions

Drawing ColorCubes

843799
843799 Member Posts: 49,999
edited Dec 22, 2009 11:10PM in Java 3D
I want to draw a cubes of size 25 and in many number at different co-ordinates.These are the steps i followed.

1. Declared and initialised Virtual Universe.
2. Declared and set x,y,z for HiResCoord.
3. Declaring and passing hirescoord and virtualuniverse obj to Locale.
4. Declaring and BranchGroup(scenegraph) and adding an array of ColorCube to the branchgroup.
5. Declaring another BranchGroup(View graph).
6. Declaring an array of TransformGroup and Transform3D.
7. Inside the loop, translating to new position. The x,y values are incremented by loop.(-250,250,-1000 and -215,215,-1000). and transform3d object is added to transformgroup object. And transform3d object is inverted.
8. Declared and initialised ViewPlatform and added view graph.
9. Declared , initialised view class and add PhysicalEnvironment and PhysicalBody to it.
10. set backClipDistance for 200 and and frontClipDistance for 2.
11. And added canvas3d .
12. And added canvas3d to applet.

I tried to add two cubes at different coordinates.But they are getting overlapped.I couldn't able to trace out.Please help me solving the issue.

Comments

  • 843799
    843799 Member Posts: 49,999
    post your code
  • 843799
    843799 Member Posts: 49,999
    Here is my code..
    public class Test2  extends JApplet
    {
        BoundingSphere m_ApplicationBounds;
        private ViewPlatform [] m_ViewPlatformArray = new ViewPlatform[2];
        private final long m_TranslateHouseZ = (long) 10.0e+2;
        Appearance app = new Appearance();
        @SuppressWarnings("static-access")
        
        public Test2()
        {
            VirtualUniverse vir_universe    = new VirtualUniverse();
            Locale locale = createLocale(vir_universe);
            
                    /********Code for Transparency************/
            
            TransparencyAttributes ta = new TransparencyAttributes();
            ta.setTransparencyMode(ta.NICEST);
            ta.setTransparency(0.5f); // Level of transparency increases as the value increase.
            app.setTransparencyAttributes(ta);
                    /****************************************/
            
            BranchGroup scenebranch         = createSceneBranchGroupHouse();
            TransformGroup tg = new TransformGroup();
            
            scenebranch.addChild(createBackground());
            m_ViewPlatformArray = createViewPlatform();
            
            BranchGroup viewBranchGroup = createViewBranchGroup(getViewTransformGroupArray(),m_ViewPlatformArray);
            
            locale.addBranchGraph(scenebranch);
            locale.addBranchGraph(viewBranchGroup);
            createView(m_ViewPlatformArray);
        }
        
        protected Bounds createApplicationBounds() {
            m_ApplicationBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),100.0);
            return m_ApplicationBounds;
        }
        protected Locale createLocaleHouse(VirtualUniverse u) 
        {
            int[] xPos = { 5, 0, 0, 4, 300, 0, 0, 0 };
            int[] yPos = { 5, 0, 0, 2, 600, 0, 0, 0 };
            int[] zPos = { 5, 0, 0, 2, 500, 0, 0, 0 };
    
            HiResCoord hiResCoord = new HiResCoord(xPos, yPos, zPos);
            return new Locale(u, hiResCoord);
        }
        protected BranchGroup createViewBranchGroup(TransformGroup[] tgArray,ViewPlatform[] vp) 
        {
            BranchGroup vpBranchGroup = new BranchGroup();
            
            
            if (tgArray != null ) 
            {
                Group parentGroup = vpBranchGroup;
                TransformGroup curTg = null;
    
                for (int n = 0; n < tgArray.length; n++) 
                {
                    parentGroup.addChild(tgArray[n]);
                    tgArray[n].addChild(vp[n]);
                }
            }
            return vpBranchGroup;
        }
        
        public TransformGroup[] getViewTransformGroupArray() 
        {
            double x = -250;
            double y = 250;
            TransformGroup [] tgArray = new TransformGroup[2];
            Transform3D []  t3d  = new Transform3D[2];
            for(int i=0;i<tgArray.length;i++)
            {
                tgArray[i] = new TransformGroup();
                Vector3d vector = new Vector3d(x,y,-m_TranslateHouseZ);
                t3d[i]  = new Transform3D();
                t3d<em>.setTranslation(vector);<br />
    t3d[i].invert(); <br />
    tgArray[i].setTransform(t3d[i]);<br />
    <br />
    x += 35;<br />
    y -= 105;<br />
    }<br />
    <br />
    return tgArray;<br />
    }<br />
    protected ViewPlatform[] createViewPlatform() <br />
    {<br />
    ViewPlatform [] vp = new ViewPlatform[2];<br />
    <br />
    for(int i=0;i<vp.length;i++)<br />
    {<br />
    vp[i] = new ViewPlatform();<br />
    vp[i].setViewAttachPolicy(View.RELATIVE_TO_FIELD_OF_VIEW);//ELATIVE_TO_FIELD_OF_VIEW);<br />
    vp[i].setActivationRadius(400);<br />
    }<br />
    return vp;<br />
    }<br />
    protected Background createBackground() <br />
    {<br />
    Background back = new Background(new Color3f(1.9f, 1.9f, .9f));<br />
    back.setApplicationBounds(createApplicationBounds());<br />
    return back;<br />
    }<br />
    protected BranchGroup createSceneBranchGroupHouse() <br />
    {<br />
    BranchGroup objRoot = new BranchGroup();<br />
    ColorCube cube = null;<br />
    TransformGroup[] subgroup = new TransformGroup[2];<br />
    for(int i=0;i<subgroup.length;i++)<br />
    { <br />
    subgroup[i] = new TransformGroup();<br />
    Transform3D t3dTilt = new Transform3D();<br />
    if(i==1)<br />
    {<br />
    cube = new ColorCube(30);<br />
    cube.setAppearance(app);<br />
    t3dTilt.rotX(0.6);<br />
    subgroup[i].setTransform(t3dTilt);<br />
    subgroup[i].addChild(cube);<br />
    }<br />
    else<br />
    {<br />
    cube = new ColorCube(25);<br />
    subgroup[i].addChild(cube);<br />
    }<br />
    <br />
    objRoot.addChild(subgroup[i]);<br />
    }<br />
    <br />
    return objRoot;<br />
    }<br />
    protected View createView(ViewPlatform [] vp) <br />
    {<br />
    View view = new View();<br />
    <br />
    PhysicalBody pb = new PhysicalBody();<br />
    PhysicalEnvironment pe = new PhysicalEnvironment();<br />
    <br />
    AudioDevice audioDevice = createAudioDevice(pe);<br />
    <br />
    if (audioDevice != null) {<br />
    pe.setAudioDevice(audioDevice);<br />
    audioDevice.initialize();<br />
    }<br />
    <br />
    view.setPhysicalEnvironment(pe);<br />
    view.setPhysicalBody(pb);<br />
    for(int i=0;i<vp.length;i++)<br />
    {<br />
    if (vp != null)<br />
    view.attachViewPlatform(vp[i]);<br />
    <br />
    <br />
    }<br />
    view.setBackClipDistance(200);<br />
    view.setFrontClipDistance(2.0);<br />
    <br />
    Canvas3D c3d = createCanvas3D();<br />
    view.addCanvas3D(c3d);<br />
    addCanvas3D(c3d); <br />
    return view;<br />
    }<br />
    protected void addCanvas3D(Canvas3D c3d) <br />
    {<br />
    setLayout(new BorderLayout());<br />
    add(c3d, BorderLayout.CENTER);<br />
    doLayout();<br />
    }<br />
    protected AudioDevice createAudioDevice(PhysicalEnvironment pe) <br />
    {<br />
    JavaSoundMixer javaSoundMixer = new JavaSoundMixer(pe);<br />
    <br />
    if (javaSoundMixer == null)<br />
    System.out.println("create of audiodevice failed");<br />
    <br />
    return javaSoundMixer;<br />
    }<br />
    /*protected void addViewBranchGroup(Locale locale, BranchGroup bg)<br />
    {<br />
    locale.addBranchGraph(bg);<br />
    }*/<br />
    protected BranchGroup createSceneGraph()<br />
    {<br />
    BranchGroup objRoot = new BranchGroup();<br />
    Transform3D t3dTilt = new Transform3D();<br />
    t3dTilt.rotX(0.3);<br />
    <br />
    TransformGroup subTg = new TransformGroup(t3dTilt);<br />
    //subTg.addChild(new ColorCube(25));<br />
    objRoot.addChild(subTg);<br />
    return objRoot;<br />
    }<br />
    protected Locale createLocale(VirtualUniverse u)<br />
    {<br />
    int[] xPos = { 0, 0, 0, 2, 200, 0, 0, 0 };<br />
    int[] yPos = { 0, 0, 0, 2, 500, 0, 0, 0 };<br />
    int[] zPos = { 0, 0, 0, 2, 500, 0, 0, 0 };<br />
    <br />
    HiResCoord hiResCoord = new HiResCoord(xPos, yPos, zPos);<br />
    return new Locale(u, hiResCoord);<br />
    }<br />
    <br />
    protected Canvas3D createCanvas3D() <br />
    {<br />
    GraphicsConfigTemplate3D gc3D = new GraphicsConfigTemplate3D();<br />
    gc3D.setSceneAntialiasing(GraphicsConfigTemplate.PREFERRED);<br />
    GraphicsDevice gd[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();<br />
    <br />
    Canvas3D c3d = new Canvas3D(gd[0].getBestConfiguration(gc3D));<br />
    c3d.setSize(300,300);<br />
    <br />
    return c3d;<br />
    }<br />
    <br />
    public static void main(String [] args)<br />
    {<br />
    MainFrame Frame = new MainFrame(new Test2(),300,300);<br />
    }<br />
    }<br />
    
    <pre class="jive-pre"><code class="jive-code">                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
This discussion has been closed.