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