Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java3D上创建自己的自定义球体_Java_Java 3d - Fatal编程技术网

如何在Java3D上创建自己的自定义球体

如何在Java3D上创建自己的自定义球体,java,java-3d,Java,Java 3d,各位。 我想用Java3D建模一个热气球,但我不知道如何绘制一个自定义球体,如何在球体底部打一个洞,或者让它看起来像一个热气球。我知道多边形,但我不知道如何编辑、切片或删除它 这是我的代码: package HotAirBallon; import java.applet.Applet; import java.awt.*; import com.sun.j3d.utils.geometry.Cylinder; import com.sun.j3d.utils.universe.*; impo

各位。 我想用Java3D建模一个热气球,但我不知道如何绘制一个自定义球体,如何在球体底部打一个洞,或者让它看起来像一个热气球。我知道多边形,但我不知道如何编辑、切片或删除它

这是我的代码:

package HotAirBallon;

import java.applet.Applet;
import java.awt.*;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.Sphere;
import javax.swing.Timer;
/**
 *
 * @author pc
 */
public class HotAirBallon extends Applet{

    /**
     * Initialization method that will be called after the applet is loaded
     * into the browser.
     */
//    private Button go = new Button("Go");
      private TransformGroup objTrans;
      private TransformGroup objCube;
      private TransformGroup objtest1;
      private TransformGroup objtest2;
      private Transform3D trans = new Transform3D();
      private float height=0.0f;
      private float sign = 1.0f; // going up or down
      private Timer timer;
      private float xloc=0.0f;
      public BranchGroup createSceneGraph() {
              // Create the root of the branch graph
              BranchGroup objRoot = new BranchGroup();
              objTrans = new TransformGroup();
              objCube = new TransformGroup();
              objtest1 = new TransformGroup();
              objtest2 = new TransformGroup();
              objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
              objCube.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
              objtest1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
              objtest2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

              objRoot.addChild(objTrans);
              objRoot.addChild(objCube);
              objRoot.addChild(objtest1);


         Appearance app = new Appearance();
         Color3f ambientColour = new Color3f(1.0f, 0.0f, 0.0f);
         Color3f diffuseColour = new Color3f(1.0f, 0.0f, 0.0f);
        Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
        Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
        float shininess = 20.0f;
        app.setMaterial(new Material(ambientColour, emissiveColour,
            diffuseColour, specularColour, shininess));

        // Create a simple shape leaf node, add it to the scene graph.
        Sphere sphere = new Sphere(0.6f, Sphere.GENERATE_NORMALS,120,app);
        Cylinder cylinder = new Cylinder(0.2f, 0.2f,app);
        Cylinder tali1 = new Cylinder(0.01f, 0.6f,app);
        Cylinder tali2 = new Cylinder(0.01f, 0.6f,app);

//        Appearance ap = new Appearance();
//        Color3f col = new Color3f(0.0f, 0.0f, 1.0f);
//        ColoringAttributes ca = new ColoringAttributes
//        (col, ColoringAttributes.SHADE_GOURAUD); 
//        ap.setColoringAttributes(ca);
//        sphere.setAppearance(ap);



         objTrans = new TransformGroup();
         objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
         objCube = new TransformGroup();
         objCube.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
         objtest1 = new TransformGroup();
         objtest1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
         objtest2 = new TransformGroup();
         objtest2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);



        Transform3D pos1 = new Transform3D();
        Transform3D pos2 = new Transform3D();
        Transform3D pos3 = new Transform3D();
        Transform3D pos4 = new Transform3D();
        pos1.setTranslation(new Vector3f(0.0f,-0.5f,0.0f));
        pos2.setTranslation(new Vector3f(0.0f,0.3f,0.0f));
        pos3.setTranslation(new Vector3f(0.2f,-0.3f,0.0f));
        pos3.setRotation(new AxisAngle4d(1.0,1.0,0.0,Math.PI/5.0)) ;
        pos4.setTranslation(new Vector3f(-0.2f,-0.3f,0.0f));
        pos4.setRotation(new AxisAngle4d(1.0,-1.0,0.0,Math.PI/5.0)) ;

        objTrans.setTransform(pos1);
        objTrans.addChild(cylinder);
        objtest1.setTransform(pos3);
        objtest1.addChild(tali1);
        objtest2.setTransform(pos4);
        objtest2.addChild(tali2);
        objCube.setTransform(pos2);
        objCube.addChild(sphere);
        objRoot.addChild(objTrans);
        objRoot.addChild(objCube);
        objRoot.addChild(objtest1);
        objRoot.addChild(objtest2);
        BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

        addLights(objRoot);
//        Color3f light1Color = new Color3f(1.0f, 0.0f, 0.2f);
//        Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
//        DirectionalLight light1
//        = new DirectionalLight(light1Color, light1Direction);
//        light1.setInfluencingBounds(bounds);
//        objRoot.addChild(light1);
//
//        // Set up the ambient light
//        Color3f ambientColor = new Color3f(1.0f, 1.0f, 1.0f);
//        AmbientLight ambientLightNode = new AmbientLight(ambientColor);
//        ambientLightNode.setInfluencingBounds(bounds);
//        objRoot.addChild(ambientLightNode);
        return objRoot;
}

 protected void addLights(BranchGroup b) {
    // Create a bounds for the lights
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
        100.0);
    // Set up the global lights
    Color3f lightColour1 = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f lightDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
    Color3f lightColour2 = new Color3f(1.0f, 1.0f, 1.0f);
    Point3f lightPosition2 = new Point3f(3.0f, 3.0f, 3.0f);
    Point3f lightAtten2 = new Point3f(0.0f, 0.0f, 1.0f);
    Vector3f lightDir2 = new Vector3f(-1.0f, -1.0f, -1.0f);
    Color3f ambientColour = new Color3f(0.2f, 0.2f, 0.2f);
    AmbientLight ambientLight1 = new AmbientLight(ambientColour);
    ambientLight1.setInfluencingBounds(bounds);
    DirectionalLight light1 = new DirectionalLight(lightColour1, lightDir1);
    light1.setInfluencingBounds(bounds);
    PointLight light2 = new PointLight(lightColour2, lightPosition2,
        lightAtten2);
    light2.setInfluencingBounds(bounds);
    b.addChild(ambientLight1);
    b.addChild(light1);
    //b.addChild(light2);
  }

/*
    @Override
    public void init() {
        // TODO start asynchronous download of heavy resources
    }
*/

    // TODO overwrite start(), stop() and destroy() methods
public HotAirBallon() {
        setLayout(new BorderLayout());
        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
        Canvas3D c = new Canvas3D(config);
        add("Center", c);
        BranchGroup scene = createSceneGraph();
        SimpleUniverse u = new SimpleUniverse(c);
        u.getViewingPlatform().setNominalViewingTransform();
        u.addBranchGraph(scene);
}

}
谢谢所有帮助我的人。。。
注意。

使用Java3D对球体进行切片或创建自定义网格:这可能与主题无关,但为什么要使用Java3D?为什么不使用OpenGL?或者至少是GL4Java。我大学的教授想让我在学习OpenGLI之前学习java3d,我不确定答案应该走多远。您可以以
IndexedQuadArray
IndexedTriangelArray
的形式创建自定义几何图形。不过,计算这样一个“球体”的顶点和索引可能有点麻烦。。。。