用QuardArray,你要画得应该是个面吧

解决方案 »

  1.   

    给个例子好吗??呵呵……没学过JAVA3D啊!
    呵呵……
    谢谢!急急急!!!
      

  2.   

    // 生成函数曲面
    import com.sun.j3d.utils.geometry.*;
    import javax.media.j3d.*;
    import javax.vecmath.*;
    public class Surface extends Shape3D 
    {
    Surface() 
    {
    this.setGeometry(createGeometry());
    this.setAppearance(createAppearance());

    // 具体函数,更改这一函数即可显示不同函数的图像
    float  calz(float x,float y)
    {
    return (float)(-1f*Math.sqrt(2-x*x-y*y));
    }
    Geometry createGeometry()
    {
    int MAXX=200;
    int MAXY=200;
    QuadArray surf = new QuadArray(
    MAXX*MAXY*4, GeometryArray.COORDINATES
    | GeometryArray.NORMALS);
    for(int i=0;i<MAXX;i++)
    {
    for(int j=0;j<MAXY*4;j+=4)
    {
    int jb=j/4;
    float x=(i-100f)/200.0f;
    float y=(jb-100f)/200.0f;
    // 计算一个平面上的4个点
    Point3f A=new Point3f(x,y,calz(x,y));
    Point3f B=
    new Point3f(x+0.005f,y,calz(x+0.005f,y));
    Point3f C=new Point3f(x+0.005f,
    y+0.005f,calz(x+0.005f,y+0.005f));
    Point3f D=new Point3f(x,y+0.005f,
    calz(x,y+0.005f));
    // 计算四个点的法向量
    Vector3f a = new Vector3f(
    A.x - B.x, A.y - B.y, A.z - B.z);
    Vector3f b = new Vector3f(
    C.x - B.x, C.y - B.y, C.z - B.z);
    Vector3f n = new Vector3f();
    n.cross(b, a);
    n.normalize();
    // 设置点坐标
    surf.setCoordinate(i*MAXY*4+j, A);
    surf.setCoordinate(i*MAXY*4+j+1, B);
    surf.setCoordinate(i*MAXY*4+j+2, C);
    surf.setCoordinate(i*MAXY*4+j+3, D);
    // 设置点法向量
    surf.setNormal(i*MAXY*4+j, n);
    surf.setNormal(i*MAXY*4+j+1, n);
    surf.setNormal(i*MAXY*4+j+2, n);
    surf.setNormal(i*MAXY*4+j+3, n);
    }
    }
    return surf;
    }
    Appearance createAppearance() 
    {
    // 指定外观,这样才有明暗效果
    Appearance appear = new Appearance();
    Material material = new Material();
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f red   = new Color3f(1.0f, 0.0f, 0.0f);
    material.setDiffuseColor(red);
            material.setSpecularColor(white);
             material.setShininess(2.0f);
    appear.setMaterial(material);
    return appear;
    }
    }// end of Surface.java
    // 生成函数曲面
    import com.sun.j3d.utils.geometry.*;
    import javax.media.j3d.*;
    import javax.vecmath.*;
    public class Surface extends Shape3D 
    {
    Surface() 
    {
    this.setGeometry(createGeometry());
    this.setAppearance(createAppearance());

    // 具体函数,更改这一函数即可显示不同函数的图像
    float  calz(float x,float y)
    {
    return (float)(-1f*Math.sqrt(2-x*x-y*y));
    }
    Geometry createGeometry()
    {
    int MAXX=200;
    int MAXY=200;
    QuadArray surf = new QuadArray(
    MAXX*MAXY*4, GeometryArray.COORDINATES
    | GeometryArray.NORMALS);
    for(int i=0;i<MAXX;i++)
    {
    for(int j=0;j<MAXY*4;j+=4)
    {
    int jb=j/4;
    float x=(i-100f)/200.0f;
    float y=(jb-100f)/200.0f;
    // 计算一个平面上的4个点
    Point3f A=new Point3f(x,y,calz(x,y));
    Point3f B=
    new Point3f(x+0.005f,y,calz(x+0.005f,y));
    Point3f C=new Point3f(x+0.005f,
    y+0.005f,calz(x+0.005f,y+0.005f));
    Point3f D=new Point3f(x,y+0.005f,
    calz(x,y+0.005f));
    // 计算四个点的法向量
    Vector3f a = new Vector3f(
    A.x - B.x, A.y - B.y, A.z - B.z);
    Vector3f b = new Vector3f(
    C.x - B.x, C.y - B.y, C.z - B.z);
    Vector3f n = new Vector3f();
    n.cross(b, a);
    n.normalize();
    // 设置点坐标
    surf.setCoordinate(i*MAXY*4+j, A);
    surf.setCoordinate(i*MAXY*4+j+1, B);
    surf.setCoordinate(i*MAXY*4+j+2, C);
    surf.setCoordinate(i*MAXY*4+j+3, D);
    // 设置点法向量
    surf.setNormal(i*MAXY*4+j, n);
    surf.setNormal(i*MAXY*4+j+1, n);
    surf.setNormal(i*MAXY*4+j+2, n);
    surf.setNormal(i*MAXY*4+j+3, n);
    }
    }
    return surf;
    }
    Appearance createAppearance() 
    {
    // 指定外观,这样才有明暗效果
    Appearance appear = new Appearance();
    Material material = new Material();
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f red   = new Color3f(1.0f, 0.0f, 0.0f);
    material.setDiffuseColor(red);
            material.setSpecularColor(white);
             material.setShininess(2.0f);
    appear.setMaterial(material);
    return appear;
    }
    }// end of Surface.java