Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java3D add transformgroup while app running  (Read 4811 times)
0 Members and 1 Guest are viewing this topic.
Offline click_24

Junior Newbie





« Posted 2014-05-22 22:18:28 »

Hello,

I have issues with my Java3D application.I load my class and XYZ axis show in the canvas.Then I want to load the method LoadPointClouds() while program is running which will show a circle of point clouds,but this doesn`t show up and I can`t figure it out why.

I get no exceptions and I added the TransformGroup from the method to the BranchGroup and I have the capabilities set.

The point clouds must show up when I click the jButton.Tryed different things but did not succeed.I can confirm the method LoadPointClouds() gets called when I click the button because I put a message to be printed in the console as confirmation that is calling it,but still nothing is showing up in the scene.

Please help me :|


Here is my code:

//------------------------------------------------------------------------------------------

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Others;

import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Text2D;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GraphicsConfiguration;
import static java.lang.Math.cos;
import static java.lang.Math.hypot;
import static java.lang.Math.sin;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.PointArray;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

/**
 *
 * @author Me
 */
public class Java3D extends JPanel {

    public static Java3D instance;

    public static Java3D getInstance() {

        if (instance == null) {

            instance = new Java3D();

        }

        return instance;

    }

    BranchGroup branch;
    SimpleUniverse universe;
    BranchGroup b;

    public Java3D() {

        setLayout(new BorderLayout());

        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

        Canvas3D canvas = new Canvas3D(config);

        add("Center", canvas);

        universe = new SimpleUniverse(canvas);

        branch = new BranchGroup();
        branch.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
        branch.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);

        branch.addChild(setLights());
        branch.addChild(setAxis());

        universe.getViewingPlatform().setNominalViewingTransform();

        universe.addBranchGraph(branch);

    }
    Shape3D plShape;

    public TransformGroup LoadPointCloud() {

        Appearance app = new Appearance();

        Point3f[] plaPts = new Point3f[361];
        Color3f[] colPts = new Color3f[361]; //parallel to coordinates, colors.

        plaPts[0] = new Point3f(0.0f, 0.0f, 0.0f);
        colPts[0] = new Color3f(3.0f, 3.0f, 3.0f);

        float j = 0;
        for (int i = 0; i < 360; i++) {

            Point3f p3f1 = calc(0.2f, 0.0f, 0.2f, j++);
            plaPts[i + 1] = new Point3f(p3f1.x, p3f1.y, p3f1.z);
            colPts[i + 1] = new Color3f(0.8f, 1.0f, 5.0f);

        }

        PointArray pla = new PointArray(361, GeometryArray.COORDINATES | GeometryArray.COLOR_3);
        pla.setColors(0, colPts); //this is the color-array setting
        pla.setCoordinates(0, plaPts);

        plShape = new Shape3D(pla, app);

        TransformGroup transfPointCloud = new TransformGroup();
        transfPointCloud.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        transfPointCloud.addChild(plShape);

        MouseRotate mouseRotate = new MouseRotate();
        mouseRotate.setTransformGroup(transfPointCloud);
        mouseRotate.setSchedulingBounds(new BoundingSphere());
        transfPointCloud.addChild(mouseRotate);

        System.out.println("Works");

        return transfPointCloud;

    }

    public Point3f calc(float x, float y, float z, float deg) {

        float r = (float) hypot(x, y);//radical (x2+y2)

        float xx = (float) (r * sin(deg));
        float yy = (float) y;
        float zz = (float) (r * cos(deg));

        Point3f p3 = new Point3f(xx, yy, zz);

        return p3;

    }

    public TransformGroup setAxis() {

        TransformGroup tgg = new TransformGroup();

        float x;
        TransformGroup tgX = null;
        float y;
        TransformGroup tgY = null;
        float z;
        TransformGroup tgZ = null;

        for (x = -1.0f; x <= 1.0f; x = x + 0.01f) {

            Sphere sphere = new Sphere(0.01f);

            tgX = new TransformGroup();

            Transform3D transform = new Transform3D();

            Vector3f vector = new Vector3f(x, .0f, .0f);

            transform.setTranslation(vector);

            tgX.setTransform(transform);

            tgX.addChild(sphere);
            tgg.addChild(tgX);

        }

        Text2D textX = new Text2D("X", new Color3f(0.9f, 1.0f, 1.0f), "Helvetica", 24, Font.BOLD);
        Transform3D tX = new Transform3D();
        tX.set(2, new Vector3f(x - 1.0f, 0.0f, 0.0f));
        TransformGroup tgTemp = new TransformGroup(tX);
        tgTemp.addChild(textX);
        tgX.addChild(tgTemp);

        for (y = -1.0f; y <= 1.0f; y = y + 0.01f) {

            Sphere sphere = new Sphere(0.01f);

            tgY = new TransformGroup();

            Transform3D transform = new Transform3D();

            Vector3f vector = new Vector3f(.0f, y, .0f);

            transform.setTranslation(vector);

            tgY.setTransform(transform);

            tgY.addChild(sphere);
            tgg.addChild(tgY);

        }
        Text2D textY = new Text2D("Y", new Color3f(0.9f, 1.0f, 1.0f), "Helvetica", 24, Font.BOLD);
        Transform3D tY = new Transform3D();
        tY.set(2, new Vector3f(0.0f, y - 1.0f, 0.0f));
        TransformGroup tgTempY = new TransformGroup(tY);
        tgTempY.addChild(textY);
        tgY.addChild(tgTempY);

        for (z = -1.0f; z <= 1.0f; z = z + 0.01f) {

            Sphere sphere = new Sphere(0.01f);

            tgZ = new TransformGroup();

            Transform3D transform = new Transform3D();

            Vector3f vector = new Vector3f(.0f, .0f, z);

            transform.setTranslation(vector);

            tgZ.setTransform(transform);

            tgZ.addChild(sphere);
            tgg.addChild(tgZ);

        }

        Text2D textZ = new Text2D("Z", new Color3f(0.9f, 1.0f, 1.0f), "Helvetica", 24, Font.BOLD);
        Transform3D tZ = new Transform3D();
        tZ.set(2, new Vector3f(0.0f, 0.0f, z - 1.0f));
        TransformGroup tgTempZ = new TransformGroup(tZ);
        tgTempZ.addChild(textZ);
        tgZ.addChild(tgTempZ);

        tgg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

        MouseRotate mouseRotate = new MouseRotate();
        mouseRotate.setTransformGroup(tgg);
        mouseRotate.setSchedulingBounds(new BoundingSphere());
        tgg.addChild(mouseRotate);

        MouseZoom mouseDepth = new MouseZoom();
        mouseDepth.setTransformGroup(tgg);
        mouseDepth.setSchedulingBounds(new BoundingSphere());
        tgg.addChild(mouseDepth);

        MouseTranslate mouseTranslate = new MouseTranslate();
        mouseTranslate.setTransformGroup(tgg);
        mouseTranslate.setSchedulingBounds(new BoundingSphere());
        tgg.addChild(mouseTranslate);

        return tgg;

    }

    public DirectionalLight setLights() {

        Color3f light1Color = new Color3f(.1f, 1.4f, .1f);

        BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);

        Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);

        DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);

        light1.setInfluencingBounds(bounds);

        return light1;

    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        Java3D j = new Java3D();
        frame.add(new JScrollPane(j));
        frame.setSize(800, 800);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Button b = new Button();
        b.setVisible(true);
    }

}

and here is the method I use to call and set the pointclouds from  another JForm  class

//----------------------------------------------------------------------------------
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:

        Java3D j3d = Java3D.getInstance();
        BranchGroup b = new BranchGroup();
        b.addChild(j3d.LoadPointCloud());
        j3d.universe.addBranchGraph(b);
         
       

    }           

Offline Riven
Administrator

« JGO Overlord »


Medals: 1341
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2014-05-23 06:30:37 »

1  
2  
3  
4  
-   setCapabilities(ALLOW_CHILDREN_EXTEND);
-   setCapabilities(ALLOW_CHILDREN_WRITE);

+   setCapabilities(ALLOW_CHILDREN_EXTEND | ALLOW_CHILDREN_WRITE);


you set the capabilities with value A, then immediately discard it with value B.




Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline click_24

Junior Newbie





« Reply #2 - Posted 2014-05-23 09:42:08 »

The same issue if I do that :|


Well if I add the PointCloudMethod to the branch before running the program it shows up,but it does not show up if I put it on the branch while the program is running :|(button click to add it to the branchgroup)

Don`t know what to do :|

Please help me
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline temporary9

Innocent Bystander





« Reply #3 - Posted 2014-07-13 20:12:33 »

Maybe try in the following order:

1. detach BranchGroup:

branch.detach();

2. add TransformGroup and whatever objects you need to `branch'

3. add BranchGraph to your SimpleUniverse:

... universe.addBranchGraph(branch);
Offline gouessej
« Reply #4 - Posted 2014-08-06 09:32:10 »

Hi

If your problem isn't solved, maybe it will be worth posting on the official Java3D forum.

Julien Gouesse | Personal blog | Website | Jogamp
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (78 views)
2018-06-10 19:43:48

EgonOlsen (58 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (260 views)
2018-05-13 18:23:11

nelsongames (158 views)
2018-04-24 18:15:36

nelsongames (157 views)
2018-04-24 18:14:32

ivj94 (898 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (811 views)
2018-03-24 14:43:53

Solater (175 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!