Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Homer's java 3D Engine!(updated: ADDED REAL LIGHTING!!!)
Index -> Programming, Java -> Java Submissions
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Homer_simpson




PostPosted: 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!!!



engine3.zip
 Description:

Download
 Filename:  engine3.zip
 Filesize:  4.5 KB
 Downloaded:  333 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Dan




PostPosted: Thu Mar 04, 2004 7:06 pm   Post subject: (No subject)

i just got an error when i tryed to run this. is it an appliaction or applet?
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Homer_simpson




PostPosted: Thu Mar 04, 2004 7:16 pm   Post subject: (No subject)

fixed!!!

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




PostPosted: Thu Mar 04, 2004 7:59 pm   Post subject: (No subject)

doesnt work for me,
says it cant find DrawView
Homer_simpson




PostPosted: Thu Mar 04, 2004 8:13 pm   Post subject: (No subject)

shaznats!
Catalyst




PostPosted: Thu Mar 04, 2004 9:37 pm   Post subject: (No subject)

i like it
Very Happy

i wonder how fast java can go....
wtd




PostPosted: Thu Mar 04, 2004 9:57 pm   Post subject: (No 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.
Homer_simpson




PostPosted: Thu Mar 04, 2004 10:05 pm   Post subject: (No 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...
Catalyst




PostPosted: Thu Mar 04, 2004 10:24 pm   Post subject: (No subject)

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




PostPosted: Thu Mar 04, 2004 10:27 pm   Post subject: (No subject)

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




PostPosted: Thu Mar 04, 2004 10:49 pm   Post subject: (No subject)

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




PostPosted: Thu Mar 04, 2004 10:59 pm   Post subject: (No 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
Catalyst




PostPosted: Thu Mar 04, 2004 11:03 pm   Post subject: (No 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)
Homer_simpson




PostPosted: Thu Mar 04, 2004 11:10 pm   Post subject: (No 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...
Homer_simpson




PostPosted: Fri Mar 05, 2004 8:17 pm   Post subject: (No 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///////////////////////////////////
}
Display posts from previous:   
   Index -> Programming, Java -> Java Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: