简体   繁体   中英

Java 3d parametric surfaces drawing

i really need your help since that i am fighting with the unknown for some time now.

I am trying to draw a parametric surface on java 3d. The surface is being drawn if ia use a point array. Here is the code :

PointArray lsa=new PointArray(length,    GeometryArray.COLOR_3|GeometryArray.NORMALS|GeometryArray.COORDINATES);


float maxV=(float) ((float) 2*Math.PI);
         float maxU=(float) ((float) Math.PI);
         Vector3f norm = new Vector3f();
         for (float v = 0.01f; v < maxV; v+=0.03)
         {
               for (float u = 0.01f; u < maxU; u+=0.03)
              {
                 vIndex++;
                 Point3f pt = new Point3f();



                 pt.x=(float) (Math.sin(u)*Math.cos(v));
                 pt.y=(float) (2*Math.sin(u)*Math.sin(v));
                 pt.z=(float) Math.cos(u);





                 lsa.setCoordinate(vIndex, pt);
                 lsa.setColor(vIndex, new Color3f(0.9f,0.0f,0.0f));



             } 

         }



     Shape3D shape = new Shape3D(lsa);

The problem that I have is that it's drawing only the points (dots) so it's not a full drawn surface. How can I draw this parametric surface with polygons or any surface? Are there any methods ?

I am searching the Web, bought Books but I still can not make it with java 3d.

Thank you very much.

Here's how I would do it.

I would define a

Point3f[][] points = new Point3f[(int)((umax-umin)/du)][(int)((vmax-vmin)/dv)];

Then use loops similar to the ones you have int i = 0; i<points.length; i++ int i = 0; i<points.length; i++ int i = 0; i<points.length; i++ , int j = 0; j < points[0].length; j++ int j = 0; j < points[0].length; j++ int j = 0; j < points[0].length; j++ . Define u = i * du + umin , v = j * dv + vmin .

and populate this array with Point3f corresponding to (u, v) .

Loop int i = 0; i<points.length - 1; i++ int i = 0; i<points.length - 1; i++ int i = 0; i<points.length - 1; i++ , int j = 0; j < points[0].length - 1; j++ int j = 0; j < points[0].length - 1; j++ int j = 0; j < points[0].length - 1; j++ and get the points at points[i][j] , points[i+1][j] , points[i][j+1] , and points[i+1][j+1] .

Then use the method given in this article to convert these points into a Polygon . Add it to your model / an array that you later add to your model.

Of course, this may not be the best way to do it and I have the feeling that it doesn't handle discontinuities very well, but it should at least make polygons.

Hello here is the solution, it draws a coons surface for example, it should work for any parametric surface x(s, t), y(s, t), z(s, t).

public static Shape3D getShape3D()
    {
        //Coons
        int ns=100;
        int nt=100;
        float param0=1.0f;
        float param1=3.0f;
        float s=0.0f;
        float t=0.0f;
        if (ns>500) ns=500; 
        if (nt>500) nt=500;
        Point3f[][] f=new Point3f[ns][nt];

        int sizeOfVectors=0;

        for (int i=0;i<ns;i++) //t -->s
        {
            for (int j=0;j<nt;j++) //u ---t
            {
                s=((float) i/ns);
                t=((float) j/nt);
                //System.out.println(" i "+ i + " j "+ j  + " s "+ s + " t "+ t);

                f[i][j]=new Point3f();

                //f[i][j].x=s;
                //f[i][j].y=2*t;
                //f[i][j].z=10*t*(1-s);

                f[i][j].x=param0*s;
                f[i][j].y=param1*t;
                f[i][j].z=(float) (0.5*((54*s*Math.sqrt(s)-126*Math.sqrt(s)+72*s-6)*t+(27*Math.sqrt(s)-27*s+6)));



                /*f[i][j].x = (float) (Math.sqrt(s)*Math.cos(t));

                f[i][j].y=(float) (Math.sqrt(s)*Math.sin(t));

                f[i][j].z=s;*/


                sizeOfVectors++;
                sizeOfVectors++;

            }
        }
        System.out.println("Total vectors "+sizeOfVectors);
        Shape3D plShape = new Shape3D();
        int vIndex=-1;
        int k=0;
        for (int i=0;i<(ns-1);i++)
        {
            k=i+1;
            sizeOfVectors=nt*2;
            vIndex=-1;
            TriangleStripArray lsa=new TriangleStripArray(sizeOfVectors, GeometryArray.COLOR_3|GeometryArray.COORDINATES|GeometryArray.NORMALS, new int[] {sizeOfVectors});

            for (int j=0;j<nt;j++)
            {
                vIndex++;
                lsa.setCoordinate(vIndex, f[i][j]);
                lsa.setColor(vIndex, new Color3f(0.9f,0.0f,0.0f));
                vIndex++;
                lsa.setCoordinate(vIndex, f[k][j]);
                lsa.setColor(vIndex, new Color3f(0.9f,0.0f,0.0f));
            }
            plShape.addGeometry(lsa);


        }


        return plShape;

    }

It works like a dream. Your guidance was the catalyst to finally make it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM