Computer Science Canada

3D Terrain(lighting included)

Author:  Homer_simpson [ Fri Apr 30, 2004 3:39 pm ]
Post subject:  3D Terrain(lighting included)

code:
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Dimension;

public class Terrain
{
    public static void main (String [] args)
    {
        // initialised with the title for the title bar
        JFrame frame = new JFrame ("3D Engine");
        Dimension screenSize = frame.getToolkit ().getScreenSize ();
        frame.setBounds (0, 0,
                800, 600);
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        int w = frame.getBounds ().width;
        int h = frame.getBounds ().height;

        frame.getContentPane ().add (new DrawView (800, 600));
        frame.show ();
    }
}


class DrawView extends Canvas
{
    private int mx, my;
    public static Obj3D obj = new Obj3D ();
    public static Obj3D obj2 = new Obj3D ();
    private Image bImage;
    private Graphics bg;
    private Graphics2D g2d;
    private int w, h;



    public DrawView (int width, int height)
    {
        super ();
        // addMouseListener (new MouseAdapter ()
        // {
        //     public void mouseClicked (MouseEvent e)
        //     {
        //         mx = e.getX ();
        //         my = e.getY ();
        //     }
        // }
        // );
        obj.initTerrain ();
        //obj.Scale (100, 100, 100);
        obj.zoom = -2500;
        obj.Camera [2] = -100;
        obj.Camera [1] = 0;
        obj.RotYZ (45);
        obj.MoveX (0);
        obj.MoveY (0);
        obj.MoveZ (0);
        obj.light1 [0] = 0;
        obj.light1 [1] = 10;
        obj.light1 [2] = -100;

        setBackground (Color.black);
        w = width;
        h = height;

    }





    private double cosd (double degrees)
    {
        return Math.cos ((3.141592653 * degrees) / 180);
    }


    private double sind (double degrees)
    {
        return Math.sin ((3.141592653 * degrees) / 180);
    }


    public void paint (Graphics g)
    {

        g2d = (Graphics2D) g;
        bImage = createImage (w, h);
        bg = bImage.getGraphics ();
        //bg.setXORMode (Color.white);
        for (double i = 0 ; i < 720 ; i += 1)
        {
            bg.drawOval (mx, my, 10, 10);
            //obj.RotXY (1);
            obj.RotXZ (3);
            obj.RotYZ (4);
            obj.light1 [0] += sind (i * 5) * 1;
            obj.light1 [2] += sind (i * 5) * 10;
            obj.light1 [1] -= sind (i * 5) * 1;
            //obj.moveTerrain ();
            //obj.light1[0]-=Math.cos(i);
            //obj.light1[1]-=Math.cos(i);
            //obj.light1[2]-=Math.cos(i);
            //System.out.println (obj.xz + "--->" + sind (obj.xz));
            obj.Render (bg);

            updateScreen ();
            bg.setColor (Color.black);
            bg.fillRect (0, 0, w, h);

        }
        System.out.print ("GOH!");
        bg.dispose ();
        bImage.flush ();
    }


    private void updateScreen ()
    {
        g2d.drawImage (bImage, 0, 0, this);
    }
}

class Obj3D
{
    public static double zlist [];
    public static double [] light1 = new double [3];
    public static Color colors [] = new Color [255];
    public static double polylist [] [] [];
    public static double polynormals [] [];
    public static int polyindex [];
    public static int polycolor [];
    public static double center [];
    public static int maxPoly = 0;
    public static double zoom = 10000;
    public static double xz = 0, yz = 0, xy = 0;
    public static double [] Camera = new double [6];

    private double cosd (double degrees)
    {
        return Math.cos ((3.141592653 * degrees) / 180);
    }


    private double sind (double degrees)
    {
        return Math.sin ((3.141592653 * degrees) / 180);
    }




    private void rotateXY (int index, int polyNumber, double Rotaion)
    {
        double OriginX = center [0];
        double OriginY = center [1];

        double secondpartX = polylist [index] [polyNumber] [0];
        double secondpartY = polylist [index] [polyNumber] [1];
        double tempx = (((OriginX - secondpartX) * cosd (Rotaion)) + ((OriginY - secondpartY) * sind (Rotaion)));
        double tempy = (((OriginY - secondpartY) * cosd (Rotaion)) - ((OriginX - secondpartX) * sind (Rotaion)));
        secondpartY = OriginY - tempy;
        secondpartX = OriginX - tempx;
        polylist [index] [polyNumber] [0] = secondpartX;
        polylist [index] [polyNumber] [1] = secondpartY;
    } //*/


    private void rotateXZ (int index, int polyNumber, double Rotaion)
    {
        double OriginX = center [0];
        double OriginY = center [2];
        double secondpartX = polylist [index] [polyNumber] [0];
        double secondpartY = polylist [index] [polyNumber] [2];
        double tempx = (((OriginX - secondpartX) * cosd (Rotaion)) + ((OriginY - secondpartY) * sind (Rotaion)));
        double tempy = (((OriginY - secondpartY) * cosd (Rotaion)) - ((OriginX - secondpartX) * sind (Rotaion)));
        secondpartY = OriginY - tempy;
        secondpartX = OriginX - tempx;
        polylist [index] [polyNumber] [0] = secondpartX;
        polylist [index] [polyNumber] [2] = secondpartY;
    }


    private void rotateYZ (int index, int polyNumber, double Rotaion)
    {
        double OriginX = center [1];
        double OriginY = center [2];
        double secondpartX = polylist [index] [polyNumber] [1];
        double secondpartY = polylist [index] [polyNumber] [2];
        double tempx = (((OriginX - secondpartX) * cosd (Rotaion)) + ((OriginY - secondpartY) * sind (Rotaion)));
        double tempy = (((OriginY - secondpartY) * cosd (Rotaion)) - ((OriginX - secondpartX) * sind (Rotaion)));
        secondpartY = OriginY - tempy;
        secondpartX = OriginX - tempx;
        polylist [index] [polyNumber] [1] = secondpartX;
        polylist [index] [polyNumber] [2] = secondpartY;
    }




    private void cameraXZ (int index, int polyNumber, double Rotaion)
    {
        double OriginX = Camera [0];
        double OriginY = Camera [2];
        double secondpartX = polylist [index] [polyNumber] [0];
        double secondpartY = polylist [index] [polyNumber] [2];
        double tempx = (((OriginX - secondpartX) * cosd (Rotaion)) + ((OriginY - secondpartY) * sind (Rotaion)));
        double tempy = (((OriginY - secondpartY) * cosd (Rotaion)) - ((OriginX - secondpartX) * sind (Rotaion)));
        secondpartY = OriginY - tempy;
        secondpartX = OriginX - tempx;
        polylist [index] [polyNumber] [0] = secondpartX;
        polylist [index] [polyNumber] [2] = secondpartY;
    }


    private void cameraYZ (int index, int polyNumber, double Rotaion)
    {
        double OriginX = Camera [1];
        double OriginY = Camera [2];
        double secondpartX = polylist [index] [polyNumber] [1];
        double secondpartY = polylist [index] [polyNumber] [2];
        double tempx = (((OriginX - secondpartX) * cosd (Rotaion)) + ((OriginY - secondpartY) * sind (Rotaion)));
        double tempy = (((OriginY - secondpartY) * cosd (Rotaion)) - ((OriginX - secondpartX) * sind (Rotaion)));
        secondpartY = OriginY - tempy;
        secondpartX = OriginX - tempx;
        polylist [index] [polyNumber] [1] = secondpartX;
        polylist [index] [polyNumber] [2] = secondpartY;
    }


    private void cameraXY (int index, int polyNumber, double Rotaion)
    {
        double OriginX = Camera [0];
        double OriginY = Camera [1];
        double secondpartX = polylist [index] [polyNumber] [0];
        double secondpartY = polylist [index] [polyNumber] [1];
        double tempx = (((OriginX - secondpartX) * cosd (Rotaion)) + ((OriginY - secondpartY) * sind (Rotaion)));
        double tempy = (((OriginY - secondpartY) * cosd (Rotaion)) - ((OriginX - secondpartX) * sind (Rotaion)));
        secondpartY = OriginY - tempy;
        secondpartX = OriginX - tempx;
        polylist [index] [polyNumber] [0] = secondpartX;
        polylist [index] [polyNumber] [1] = secondpartY;
    } //*/


    public void RotXZ (double rot)
    {
        xz += rot;
        for (int i = 0 ; i < maxPoly ; i++)
        {

            for (int j = 0 ; j < 3 ; j++)
            {
                rotateXZ (i, j, rot);
            }
        }
    }


    public void RotYZ (double rot)
    {
        yz += rot;
        for (int i = 0 ; i < maxPoly ; i++)
        {

            for (int j = 0 ; j < 3 ; j++)
            {
                rotateYZ (i, j, rot);
            }
        }
    }


    public void RotXY (double rot)
    {
        for (int i = 0 ; i < maxPoly ; i++)
        {

            for (int j = 0 ; j < 3 ; j++)
            {
                rotateXY (i, j, rot);
            }
        }
    }


    public void MoveX (double rot)
    {
        xy += rot;
        for (int i = 0 ; i < maxPoly ; i++)
        {

            for (int j = 0 ; j < 3 ; j++)
            {
                polylist [i] [j] [0] += rot;
            }
        }
        center [0] += rot;
    }


    public void MoveY (double rot)
    {
        for (int i = 0 ; i < maxPoly ; i++)
        {

            for (int j = 0 ; j < 3 ; j++)
            {
                polylist [i] [j] [1] += rot;
            }
        }
        center [1] += rot;
    }


    public void MoveZ (double rot)
    {
        for (int i = 0 ; i < maxPoly ; i++)
        {

            for (int j = 0 ; j < 3 ; j++)
            {
                polylist [i] [j] [2] += rot;
            }
        }
        center [2] += rot;
    }


    public void CamXZ (double rot)
    {
        for (int i = 0 ; i < maxPoly ; i++)
        {

            for (int j = 0 ; j < 3 ; j++)
            {
                cameraXZ (i, j, rot);
            }
        }
    }


    public void CamYZ (double rot)
    {
        for (int i = 0 ; i < maxPoly ; i++)
        {

            for (int j = 0 ; j < 3 ; j++)
            {
                cameraYZ (i, j, rot);
            }
        }
    }


    public void CamXY (double rot)
    {
        for (int i = 0 ; i < maxPoly ; i++)
        {

            for (int j = 0 ; j < 3 ; j++)
            {
                cameraXY (i, j, rot);
            }
        }
    }


    public void Scale (double xx, double yy, double zz)
    {
        for (int i = 0 ; i < maxPoly ; i++)
        {

            for (int j = 0 ; j < 3 ; j++)
            {
                polylist [i] [j] [2] = center [2] + ((polylist [i] [j] [2] - center [2]) * zz);
                polylist [i] [j] [1] = center [1] + ((polylist [i] [j] [1] - center [1]) * yy);
                polylist [i] [j] [0] = center [0] + ((polylist [i] [j] [0] - center [0]) * xx);
            }
        }
    }


    public void zsort ()
    {
        double tempa [] = new double [maxPoly];
        for (int i = 0 ; i < maxPoly ; i++)
        {
            polyindex [i] = i;
            tempa [i] = (polylist [i] [0] [2] + polylist [i] [1] [2] + polylist [i] [2] [2]) / 3;
        }
        sort (tempa);

    }




    private void draw3ddot (double x, double y, double z, Graphics g)
    {
        g.drawOval ((int) ((x + Camera [0]) / ((z + Camera [2]) / zoom)) + 320, (int) ((y + Camera [1]) / ((z + Camera [2]) / zoom)) + 200, 20, 20);
    }


    private void draw3dline (double x, double y, double z, double x2, double y2, double z2, Graphics g)
    {
        g.drawLine ((int) ((x + Camera [0]) / ((z + Camera [2]) / zoom)) + 320, (int) ((y + Camera [1]) / ((z + Camera [2]) / zoom)) + 200, (int) ((x2 + Camera [0]) / ((z2 + Camera [2]) / zoom)) + 320, (int) ((y2 + Camera [1]) / ((z2 + Camera [2]) / zoom)) + 200);
    }


    public void Render (Graphics g)
    {
        int xPoly [] = new int [3];
        int yPoly [] = new int [3];
        zsort ();
        for (int i = 0 ; i < maxPoly ; i++)
        {
            Normal (polylist [i], i);
            //System.out.println(polynormals [i][0]+"//"+polynormals [i][1]+"//"+polynormals [i][2]);
        }
        CalculateLights ();

        for (int i = 0 ; i < maxPoly ; i++)
        {

            for (int j = 0 ; j < 3 ; j++)
            {
                xPoly [j] = (int) ((polylist [polyindex [i]] [j] [0] + Camera [0]) / ((polylist [polyindex [i]] [j] [2] + Camera [2]) / zoom)) + 320;
                yPoly [j] = (int) ((polylist [polyindex [i]] [j] [1] + Camera [1]) / ((polylist [polyindex [i]] [j] [2] + Camera [2]) / zoom)) + 200;
            }
            //g.setColor (colors [Math.abs ((int) ((polylist [polyindex [i]] [0] [2] + polylist [polyindex [i]] [1] [2] + polylist [polyindex [i]] [2] [2] / 100000))) % 255]);
            g.setColor (colors [polycolor [polyindex [i]]]);
            //g.setColor (Color.gray);
            g.fillPolygon (xPoly, yPoly, 3);
            g.setColor (Color.green);
            //          g.drawOval((int) ((polynormals [polyindex [i]] [0] + Camera [0]) / ((polynormals [polyindex [i]][2] + Camera [2]) / zoom)) + 320,(int) ((polynormals [polyindex [i]] [1] + Camera [1]) / ((polynormals [polyindex [i]]  [2] + Camera [2]) / zoom)) + 200,1,1);
            double xx = (polylist [polyindex [i]] [0] [0] + polylist [polyindex [i]] [1] [0] + polylist [polyindex [i]] [2] [0]) / 3;
            double yy = (polylist [polyindex [i]] [0] [1] + polylist [polyindex [i]] [1] [1] + polylist [polyindex [i]] [2] [1]) / 3;
            double zz = (polylist [polyindex [i]] [0] [2] + polylist [polyindex [i]] [1] [2] + polylist [polyindex [i]] [2] [2]) / 3;
            //draw3dline (xx * 3, yy * 3, zz * 3, xx + polynormals [polyindex [i]] [0], yy + polynormals [polyindex [i]] [1], zz + polynormals [polyindex [i]] [2], g);

            //draw3dline (center[0],center[1],center[2],light1[0],light1[1],light1[2], g);
            //draw3ddot (light1[0],light1[1],light1[2], g);

            //            g.setColor (Color.green);
            //            g.drawPolygon (xPoly, yPoly, 3);

            //g.fillOval ((int) (center [0] / center [2]) + 320, (int) (center [1] / center [2]) + 200, 10, 10);

        }
        draw3ddot (light1 [0], light1 [1], light1 [2], g);
    }


    public static void CalculateLights ()
    {
        double lightv [] = new double [3];
        double tmagnitude;
        double dotproduct;
        double angle;
        for (int i = 0 ; i < maxPoly ; i++)
        {
            lightv [0] = light1 [0] - polynormals [i] [0];
            lightv [1] = light1 [1] - polynormals [i] [1];
            lightv [2] = light1 [2] - polynormals [i] [2];
            tmagnitude = Math.sqrt ((lightv [0] * lightv [0]) + (lightv [1] * lightv [1]) + (lightv [2] * lightv [2]));
            lightv [0] /= tmagnitude;
            lightv [1] /= tmagnitude;
            lightv [2] /= tmagnitude;
            dotproduct = (lightv [0] * polynormals [i] [0]) + (lightv [1] * polynormals [i] [1]) + (lightv [2] * polynormals [i] [2]);
            angle = Math.acos (dotproduct) / (3.141592653 / 180);
            polycolor [i] = (int) ((((angle) * 255) / 360));
        }
    }


    public static void initfile (String filename)
    {

        String thisLine;
        double tempd;
        String temps = "";
        int polys, sd, line;
        polys = 0;
        sd = 0;
        line = 0;
        Camera [0] = 0;
        Camera [1] = 0;
        Camera [2] = 0;
        Camera [3] = 0;
        Camera [4] = 0;
        Camera [5] = 0;
        for (int i = 0 ; i < 255 ; i++)
        {
            colors [i] = new Color (i, i, i);
        }
        //System.out.print ("Reading...");
        try
        {
            FileInputStream fin = new FileInputStream (filename);

            try
            {
                DataInputStream myInput = new DataInputStream (fin);

                try
                {
                    while ((thisLine = myInput.readLine ()) != null)
                    {
                        //System.out.println (thisLine);
                        polys++;
                    }
                    //System.out.println (polys);
                }
                catch (Exception e)
                {
                    System.out.println ("Error: " + e);
                }
            }
            catch (Exception e)
            {
                System.out.println ("Error: " + e);
            }

        }
        catch (Exception e)
        {
            System.out.println ("failed to open file " + filename);
            System.out.println ("Error: " + e);
        }
        maxPoly = polys;
        Double cube [] [] [] = new Double [maxPoly] [3] [3];
        polyindex = new int [maxPoly];
        polycolor = new int [maxPoly];
        polylist = new double [maxPoly] [3] [3];
        center = new double [3];
        polynormals = new double [maxPoly] [3];

        try
        {
            FileInputStream fin = new FileInputStream (filename);

            try
            {
                DataInputStream myInput = new DataInputStream (fin);

                try
                {
                    //System.out.println ("initializing...");
                    while ((thisLine = myInput.readLine ()) != null)
                    {
                        for (int i = 0 ; i < thisLine.length () ; i++)
                        {
                            if (thisLine.charAt (i) != ' ')
                            {
                                temps += thisLine.charAt (i);
                            }
                            else
                            {

                                cube [line] [(sd / 3)] [sd % 3] = Double.valueOf (temps);
                                temps = "";
                                sd++;
                            }
                        }
                        sd = 0;
                        line++;

                        //System.out.println (thisLine);
                    }
                }
                catch (Exception e)
                {
                    System.out.println ("Error: " + e);
                }
            }
            catch (Exception e)
            {
                System.out.println ("Error: " + e);
            }

        }
        catch (Exception e)
        {
            System.out.println ("failed to open file " + filename);
            System.out.println ("Error: " + e);
        }

        //System.out.println ("Done!");
        //        System.out.println("Index : "+maxPoly);
        for (int i = 0 ; i < maxPoly ; i++)
        {
            for (int j = 0 ; j < 3 ; j++)
            {
                for (int k = 0 ; k < 3 ; k++)
                {
                    polylist [i] [j] [k] = cube [i] [j] [k].doubleValue ();
                    //System.out.println("Index : "+i);
                }
            }
        }

        center [0] = 0;
        center [1] = 0;
        center [2] = 0;
        for (int i = 0 ; i < maxPoly ; i++)
        {
            center [0] += polylist [i] [0] [0];
            center [0] += polylist [i] [1] [0];
            center [0] += polylist [i] [2] [0];

            center [1] += polylist [i] [0] [1];
            center [1] += polylist [i] [1] [1];
            center [1] += polylist [i] [2] [1];

            center [2] += polylist [i] [0] [2];
            center [2] += polylist [i] [1] [2];
            center [2] += polylist [i] [2] [2];
        }

        center [0] /= maxPoly * 3;
        center [1] /= maxPoly * 3;
        center [2] /= maxPoly * 3;
        for (int i = 0 ; i < maxPoly ; i++)
        {
            Normal (polylist [i], i);
            //System.out.println(polynormals [i][0]+"//"+polynormals [i][1]+"//"+polynormals [i][2]);
        }
    }



    public static void moveTerrain ()
    {
        int tw = 10, th = 10;
        int line2 = 0;

        for (int ct = 0 ; ct < tw ; ct++)
        {
            for (int ct2 = 0 ; ct2 < th ; ct2++)
            {
                zlist [line2] = Math.random ();
                line2++;
            }
        }

        /*for (int ct2 = 0 ; ct2 < th ; ct2++)
        {
            zlist [line2] = Math.random ();
            line2++;
        }*/

        int line = 0;
        for (int ct = 0 ; ct < tw ; ct++)
        {
            for (int ct2 = 0 ; ct2 < th ; ct2++)
            {

                polylist [line] [0] [0] = ct;
                polylist [line] [0] [1] = zlist [line];
                polylist [line] [0] [2] = ct2;

                polylist [line] [1] [0] = ct + 1;
                polylist [line] [1] [1] = zlist [((ct + 1) * 10) + ct2];
                polylist [line] [1] [2] = ct2;

                polylist [line] [2] [0] = ct;
                polylist [line] [2] [1] = zlist [((ct) * 10) + ct2 + 1];
                polylist [line] [2] [2] = ct2 + 1;



                /*polylist [line + (int) ((Math.sqrt (maxPoly)))] [0] [0] = ct;
                polylist [line + (int) ((Math.sqrt (maxPoly)))] [0] [1] = zlist [((ct) * 10) + ct2 + 1];
                polylist [line + (int) ((Math.sqrt (maxPoly)))] [0] [2] = ct2 + 1;

                polylist [line + (int) ((Math.sqrt (maxPoly)))] [1] [0] = ct + 1;
                polylist [line + (int) ((Math.sqrt (maxPoly)))] [1] [1] = zlist [((ct + 1) * 10) + ct2];
                polylist [line + (int) ((Math.sqrt (maxPoly)))] [1] [2] = ct2;

                polylist [line + (int) ((Math.sqrt (maxPoly)))] [2] [0] = ct + 1;
                polylist [line + (int) ((Math.sqrt (maxPoly)))] [2] [1] = zlist [((ct + 1) * 10) + ct2 + 1];
                polylist [line + (int) ((Math.sqrt (maxPoly)))] [2] [2] = ct2 + 1;//*/

                polylist [line + (int) (maxPoly / 2)] [0] [0] = ct;
                polylist [line + (int) (maxPoly / 2)] [0] [1] = zlist [((ct) * 10) + ct2 + 1];
                polylist [line + (int) (maxPoly / 2)] [0] [2] = ct2 + 1;

                polylist [line + (int) (maxPoly / 2)] [1] [0] = ct + 1;
                polylist [line + (int) (maxPoly / 2)] [1] [1] = zlist [((ct + 1) * 10) + ct2];
                polylist [line + (int) (maxPoly / 2)] [1] [2] = ct2;

                polylist [line + (int) (maxPoly / 2)] [2] [0] = ct + 1;
                polylist [line + (int) (maxPoly / 2)] [2] [1] = zlist [((ct + 1) * 10) + ct2 + 1];
                polylist [line + (int) (maxPoly / 2)] [2] [2] = ct2 + 1;

                line++;
            }
        }

        center [0] = 0;
        center [1] = 0;
        center [2] = 0;
        for (int i = 0 ; i < maxPoly ; i++)
        {
            center [0] += polylist [i] [0] [0];
            center [0] += polylist [i] [1] [0];
            center [0] += polylist [i] [2] [0];

            center [1] += polylist [i] [0] [1];
            center [1] += polylist [i] [1] [1];
            center [1] += polylist [i] [2] [1];

            center [2] += polylist [i] [0] [2];
            center [2] += polylist [i] [1] [2];
            center [2] += polylist [i] [2] [2];
        }

        center [0] /= maxPoly * 3;
        center [1] /= maxPoly * 3;
        center [2] /= maxPoly * 3;
        for (int i = 0 ; i < maxPoly ; i++)
        {
            Normal (polylist [i], i);
            //System.out.println(polynormals [i][0]+"//"+polynormals [i][1]+"//"+polynormals [i][2]);
        }

    }


    public static void initTerrain ()
    {

        String thisLine;
        double tempd;
        String temps = "";
        int polys, sd, line;
        polys = 0;
        sd = 0;
        line = 0;
        Camera [0] = 0;
        Camera [1] = 0;
        Camera [2] = 0;
        Camera [3] = 0;
        Camera [4] = 0;
        Camera [5] = 0;
        for (int i = 0 ; i < 255 ; i++)
        {
            colors [i] = new Color (0, 128 + (i / 2), 0);
        }
        int tw = 10, th = 10;
        maxPoly = (tw * th) * 2;
        Double cube [] [] [] = new Double [maxPoly] [3] [3];
        polyindex = new int [maxPoly];
        polycolor = new int [maxPoly];
        polylist = new double [maxPoly] [3] [3];
        center = new double [3];
        zlist = new double [maxPoly];
        polynormals = new double [maxPoly] [3];
        int line2 = 0;
        for (int ct = 0 ; ct < tw ; ct++)
        {
            for (int ct2 = 0 ; ct2 < th ; ct2++)
            {
                zlist [line2] = Math.cos (ct) * Math.sin (ct2);
                line2++;
            }
        }

        for (int ct = 0 ; ct < tw ; ct++)
        {
            for (int ct2 = 0 ; ct2 < th ; ct2++)
            {

                polylist [line] [0] [0] = ct;
                polylist [line] [0] [1] = zlist [line];
                polylist [line] [0] [2] = ct2;

                polylist [line] [1] [0] = ct + 1;
                polylist [line] [1] [1] = zlist [((ct + 1) * 10) + ct2];
                polylist [line] [1] [2] = ct2;

                polylist [line] [2] [0] = ct;
                polylist [line] [2] [1] = zlist [((ct) * 10) + ct2 + 1];
                polylist [line] [2] [2] = ct2 + 1;



                /*polylist [line + (int) ((Math.sqrt (maxPoly)))] [0] [0] = ct;
                polylist [line + (int) ((Math.sqrt (maxPoly)))] [0] [1] = zlist [((ct) * 10) + ct2 + 1];
                polylist [line + (int) ((Math.sqrt (maxPoly)))] [0] [2] = ct2 + 1;

                polylist [line + (int) ((Math.sqrt (maxPoly)))] [1] [0] = ct + 1;
                polylist [line + (int) ((Math.sqrt (maxPoly)))] [1] [1] = zlist [((ct + 1) * 10) + ct2];
                polylist [line + (int) ((Math.sqrt (maxPoly)))] [1] [2] = ct2;

                polylist [line + (int) ((Math.sqrt (maxPoly)))] [2] [0] = ct + 1;
                polylist [line + (int) ((Math.sqrt (maxPoly)))] [2] [1] = zlist [((ct + 1) * 10) + ct2 + 1];
                polylist [line + (int) ((Math.sqrt (maxPoly)))] [2] [2] = ct2 + 1;//*/

                polylist [line + (int) (maxPoly / 2)] [0] [0] = ct;
                polylist [line + (int) (maxPoly / 2)] [0] [1] = zlist [((ct) * 10) + ct2 + 1];
                polylist [line + (int) (maxPoly / 2)] [0] [2] = ct2 + 1;

                polylist [line + (int) (maxPoly / 2)] [1] [0] = ct + 1;
                polylist [line + (int) (maxPoly / 2)] [1] [1] = zlist [((ct + 1) * 10) + ct2];
                polylist [line + (int) (maxPoly / 2)] [1] [2] = ct2;

                polylist [line + (int) (maxPoly / 2)] [2] [0] = ct + 1;
                polylist [line + (int) (maxPoly / 2)] [2] [1] = zlist [((ct + 1) * 10) + ct2 + 1];
                polylist [line + (int) (maxPoly / 2)] [2] [2] = ct2 + 1;

                line++;
            }
        }


        center [0] = 0;
        center [1] = 0;
        center [2] = 0;
        for (int i = 0 ; i < maxPoly ; i++)
        {
            center [0] += polylist [i] [0] [0];
            center [0] += polylist [i] [1] [0];
            center [0] += polylist [i] [2] [0];

            center [1] += polylist [i] [0] [1];
            center [1] += polylist [i] [1] [1];
            center [1] += polylist [i] [2] [1];

            center [2] += polylist [i] [0] [2];
            center [2] += polylist [i] [1] [2];
            center [2] += polylist [i] [2] [2];
        }

        center [0] /= maxPoly * 3;
        center [1] /= maxPoly * 3;
        center [2] /= maxPoly * 3;
        for (int i = 0 ; i < maxPoly ; i++)
        {
            Normal (polylist [i], i);
            //System.out.println(polynormals [i][0]+"//"+polynormals [i][1]+"//"+polynormals [i][2]);
        }
    }












    //////////////////////////////////////Sorting stuffs///////////////////////////////////
    private static void QuickSort (double a [], int l, int r)
    {
        int M = 4;
        int i;
        int j;
        double v;

        if ((r - l) > M)
        {
            i = (r + l) / 2;
            if (a [l] > a [i])
                swap (a, l, i);                 // Tri-Median Methode!
            if (a [l] > a [r])
                swap (a, l, r);
            if (a [i] > a [r])
                swap (a, i, r);

            j = r - 1;
            swap (a, i, j);
            i = l;
            v = a [j];
            for (;;)
            {
                while (a [++i] < v)
                    ;
                while (a [--j] > v)
                    ;
                if (j < i)
                    break;
                swap (a, i, j);
            }
            swap (a, i, r - 1);
            QuickSort (a, l, j);
            QuickSort (a, i + 1, r);
        }
    }


    private static void swap (double a [], int i, int j)
    {
        double T;
        int T2;
        T = a [i];
        a [i] = a [j];
        a [j] = T;
        T2 = polyindex [i];
        polyindex [i] = polyindex [j];
        polyindex [j] = T2;
    }


    private static void InsertionSort (double a [], int lo0, int hi0)
    {
        int i;
        int j;
        double v;

        for (i = lo0 + 1 ; i <= hi0 ; i++)
        {
            v = a [i];
            j = i;
            while ((j > lo0) && (a [j - 1] > v))
            {
                a [j] = a [j - 1];
                j--;
            }
            a [j] = v;
        }
    }


    public static void sort (double a [])
    {
        QuickSort (a, 0, a.length - 1);
        InsertionSort (a, 0, a.length - 1);
    }


    ///////////////////////////////////////Sorting stuffs///////////////////////////////////


    /////////////////////////////////// Normal Calculations ////////////////////////////////


    public static void Normal (double vTriangle [] [], int index)
    {
        double vVector1 [] = new double [3];
        double vVector2 [] = new double [3];
        vVector1 [0] = vVector1 [1] = vVector1 [2] = 0;
        vVector1 [0] = vTriangle [2] [0] - vTriangle [0] [0];
        vVector1 [1] = vTriangle [2] [1] - vTriangle [0] [1];
        vVector1 [2] = vTriangle [2] [2] - vTriangle [0] [2];

        vVector2 [0] = vVector2 [1] = vVector2 [2] = 0;
        vVector2 [0] = vTriangle [1] [0] - vTriangle [0] [0];
        vVector2 [1] = vTriangle [1] [1] - vTriangle [0] [1];
        vVector2 [2] = vTriangle [1] [2] - vTriangle [0] [2];

        double vNormal [] = new double [3];
        vNormal [0] = ((vVector1 [1] * vVector2 [2]) - (vVector1 [2] * vVector2 [1]));
        vNormal [1] = ((vVector1 [2] * vVector2 [0]) - (vVector1 [0] * vVector2 [1]));
        vNormal [2] = ((vVector1 [0] * vVector2 [1]) - (vVector1 [1] * vVector2 [0]));

        double magnitude = Math.sqrt ((vNormal [0] * vNormal [0]) + (vNormal [1] * vNormal [1]) + (vNormal [2] * vNormal [2]));
        vNormal [0] /= magnitude;
        vNormal [1] /= magnitude;
        vNormal [2] /= magnitude;
        polynormals [index] [0] = vNormal [0];
        polynormals [index] [1] = vNormal [1];
        polynormals [index] [2] = vNormal [2];
    }
}

comments are welcome...


: