Computer Science Canada

Homer's java 3D Engine!(updated: ADDED REAL LIGHTING!!!)

Author:  Homer_simpson [ Thu Mar 04, 2004 6:55 pm ]
Post subject:  Homer's java 3D Engine!(updated: ADDED REAL LIGHTING!!!)

finally here!
current features:
file import
camera
zsort
fake lights

to be added
quicker zsort
real light
more functional camera
maybe even texturing!

Source code included!!!

Author:  Dan [ Thu Mar 04, 2004 7:06 pm ]
Post subject: 

i just got an error when i tryed to run this. is it an appliaction or applet?

Author:  Homer_simpson [ Thu Mar 04, 2004 7:16 pm ]
Post subject: 

fixed!!!

and download some models from http://compsci.ca/models.zip

Author:  Catalyst [ Thu Mar 04, 2004 7:59 pm ]
Post subject: 

doesnt work for me,
says it cant find DrawView

Author:  Homer_simpson [ Thu Mar 04, 2004 8:13 pm ]
Post subject: 

shaznats!

Author:  Catalyst [ Thu Mar 04, 2004 9:37 pm ]
Post subject: 

i like it
Very Happy

i wonder how fast java can go....

Author:  wtd [ Thu Mar 04, 2004 9:57 pm ]
Post subject: 

Catalyst wrote:
i like it
Very Happy

i wonder how fast java can go....


Extremely fast.

The primary slowdowns are in the AWT and Swing toolkits. Without them, even Java code that hasn't been Just-In-Time compiled to native machine code can run ridiculously fast. Just look at all of the servers out there running Java, and the companies pushing it. IBM, Sun, Apple (maintainers of the largest multimedia site on the internet), etc.

Then there are projects like GCJ which aim to compile Java directly to machine code, and JIT projects like Jikes at IBM that take compiled Java programs and translate them to native code the first time they're run.

Author:  Homer_simpson [ Thu Mar 04, 2004 10:05 pm ]
Post subject: 

catalyst man! with out the sorting and stuff i had it running 10000 polys+ with mad speed!!!
this is all experimental but it makes it easy for me to understand more advanced engines like OGL... so i might make my own polygon filling and therefore my own Z-buffering system...

Author:  Catalyst [ Thu Mar 04, 2004 10:24 pm ]
Post subject: 

nice
with that kind of speed i think i might try my hand at a 3d engine in java...

Author:  Homer_simpson [ Thu Mar 04, 2004 10:27 pm ]
Post subject: 

i wanna try it in c++ it's got better syntax... u ever tried it in c++?(not using OGL ofcourse)

Author:  Catalyst [ Thu Mar 04, 2004 10:49 pm ]
Post subject: 

i havent
im not sure how to access the screen w/o using ogl or windows

Author:  wtd [ Thu Mar 04, 2004 10:59 pm ]
Post subject: 

Why can't you use OpenGL when you're programming in C++?

The following page has tutorials (you may have to scroll down):

http://www.cprogramming.com/tutorial.html

Author:  Catalyst [ Thu Mar 04, 2004 11:03 pm ]
Post subject: 

i can, and do use openGl when im in c++
i just think it would be interesting to write a software renderer (e.g. not using it)

Author:  Homer_simpson [ Thu Mar 04, 2004 11:10 pm ]
Post subject: 

man i know how to draw pixels in assembly... and u can import asm to c++ and pascal easily... and trust me there's no language that runs faster than asembly... after all it's machine language....

and according to my calcs it takes 21811200 clock ticks to render a whole page in a 640x480 rewsolution page! which again according to my calcs is about 1/7741th of my cpu usage...

Author:  Homer_simpson [ Fri Mar 05, 2004 8:17 pm ]
Post subject: 

Updated
Added stuffs:
Scale(x,y,z) command
much faster Z-Sorting

soon to be added poly normal calcualtions and lighting...

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

public class ThreeD
{
    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
{
    public static Obj3D obj = new Obj3D ();
    private Image bImage;
    private Graphics bg;
    private Graphics2D g2d;
    private int w, h;


    DrawView (int width, int height)
    {
 super ();
 obj.initfile ("c:\\sphh2.raw");
 //obj.Scale (.1, .1, .1);
 obj.zoom = 25000;
 obj.Camera [2] = 2000;
 obj.Camera [1] = 10;
 setBackground (Color.black);
 w = width;
 h = height;
    }


    public void paint (Graphics g)
    {
 g2d = (Graphics2D) g;
 bImage = createImage (w, h);
 bg = bImage.getGraphics ();
 //bg.setXORMode (Color.white);
 for (double i = 0 ; i < 360 ; i += 1)
 {
     obj.RotXZ (1);
     obj.RotYZ (1);
     //obj.RotYZ (2);
     obj.Render (bg);

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

 }
 bg.dispose ();
 bImage.flush ();
    }


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

class Obj3D
{
    public static Color colors [] = new Color [255];
    public static double polylist [] [] [];
    public static int polyindex [];
    public static double center [];
    public static int maxPoly = 0;
    public static double zoom = 10000;
    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)
    {
 for (int i = 0 ; i < maxPoly ; i++)
 {

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


    public void RotYZ (double 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)
    {
 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);

    }


    public void Render (Graphics g)
    {
 int xPoly [] = new int [3];
 int yPoly [] = new int [3];
 zsort ();
 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.fillPolygon (xPoly, yPoly, 3);
     //            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);

 }
    }



    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];
 polylist = new double [maxPoly] [3] [3];
 center = new double [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;
    }

    //////////////////////////////////////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);               
     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///////////////////////////////////
}

Author:  Homer_simpson [ Sat Mar 06, 2004 2:43 pm ]
Post subject: 

This is an actuall flat lighting method... still kinda buggy for somereason but i'll fix it when i get back home from work...

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

public class ThreeD
{
    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
{
    public static Obj3D obj = new Obj3D ();
    private Image bImage;
    private Graphics bg;
    private Graphics2D g2d;
    private int w, h;


    DrawView (int width, int height)
    {
        super ();
        obj.initfile ("c:\\engine3.raw");
        //        obj.Scale (.1, .1, .1);
        obj.zoom = -20000;
        obj.Camera [2] = 2000;
        obj.Camera [1] = 0;
        obj.light1[0]=20;
        obj.light1[1]=0;
        obj.light1[2]=20;       
        setBackground (Color.black);
        w = width;
        h = height;
    }


    public void paint (Graphics g)
    {
        g2d = (Graphics2D) g;
        bImage = createImage (w, h);
        bg = bImage.getGraphics ();
        //bg.setXORMode (Color.white);
        for (double i = 0 ; i < 2*3.6 ; i += .01)
        {
            //obj.RotXZ (1);
            //obj.RotYZ (1);
            //obj.RotYZ (2);
            obj.light1[0]-=Math.cos(i);
            obj.light1[2]-=Math.cos(i);
            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 [] 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 [] 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)
    {
        for (int i = 0 ; i < maxPoly ; i++)
        {

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


    public void RotYZ (double 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)
    {
        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);

    }


    public 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 [0] * lightv [0]));
            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+180)*255)/360);
//            if (polycolor[i]==0)
//            System.out.println(angle+"====="+polycolor[i]);
        }
    }


    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, 1, 1);
    }


    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 ();
        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.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, yy, zz, 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);

            //            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);

        }
    }



    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]);
        }
    }













    //////////////////////////////////////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];
    }
}

Author:  Homer_simpson [ Sat Mar 06, 2004 2:47 pm ]
Post subject: 

this is actual flat shading. still kinda buggy but i'll fix it when i get back home.
tell me what u think Razz

removed

Author:  rizzix [ Sat Mar 06, 2004 3:05 pm ]
Post subject: 

nice

Author:  Homer_simpson [ Sun Mar 07, 2004 2:09 am ]
Post subject: 

errors fixed!
to be added:
polygon shading,texturing possibly...


: