home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 April / comcd0402.iso / homepage / javaspecial / 03_01 / kubik / FixedCamera.java < prev    next >
Encoding:
Java Source  |  1999-05-18  |  6.8 KB  |  356 lines

  1. import java.awt.*;
  2. import java.util.*;
  3.  
  4. /**
  5.  * This class transforms the 3D coordinates of Wireframe objects into 2D
  6.  * coordinates suitable for screen display. In a sense, this class makes
  7.  * up a 3D world. The name 'Camera' derives from a real camera that puts
  8.  * the real world on a flat film. This camera is 'Fixed' because it always
  9.  * looks in the same direction (it's not really fixed as you can move it
  10.  * around, but its angle is fixed).
  11.  * <P>
  12.  * If you want to set up and display a 3D world, you have to do the following
  13.  * steps:
  14.  * <OL>
  15.  * <LI> create a FixedCamera object
  16.  * <LI> create some Wireframe objects
  17.  * <LI> add the Wireframe objects to the FixedCamera using addWireframe
  18.  * <LI> draw the 3D world within any Graphics context using draw
  19.  * <LI> after that, you can change the camera's attributes using its
  20.  *      setXXX/getXXX/move methods and draw the 3D world again.
  21.  * <LI> you can also change the Wireframe objects' attributes using its
  22.  *      setXXX/getXXX/move/rotate methods and draw the 3D world again.
  23.  * </OL>
  24.  * <P>
  25.  * author: Michael Kraus<BR>
  26.  * version: 1.2<BR>
  27.  * date: 1999/5/17<BR>
  28.  * environment: JDK 1.0.2<BR>
  29.  * email: <A href="michael.kraus@informatik.uni-muenchen.de">michael.kraus@informatik.uni-muenchen.de</A><BR>
  30.  * homepage: <A href="www.informatik.uni-muenchen.de/~michael.kraus">www.informatik.uni-muenchen.de/~michael.kraus</A>
  31.  */
  32.  
  33. public class FixedCamera
  34. {
  35.     /**
  36.      * index of the x coordinate in both the world and view
  37.      * coordinate array
  38.      */
  39.  
  40.     private static final int X=0;
  41.  
  42.     /**
  43.      * index of the y coordinate in both the world and view
  44.      * coordinate array
  45.      */
  46.  
  47.     private static final int Y=1;
  48.  
  49.     /**
  50.      * index of the z coordinate in world coordinate array
  51.      */
  52.  
  53.     private static final int Z=2;
  54.  
  55.     /**
  56.      * x coordinate of the camera's position in world coordinates
  57.      */
  58.  
  59.     private int xposition;
  60.  
  61.     /**
  62.      * y coordinate of the camera's position in world coordinates
  63.      */
  64.  
  65.     private int yposition;
  66.  
  67.     /**
  68.      * z coordinate of the camera's position in world coordinates
  69.      */
  70.  
  71.     private int zposition;
  72.  
  73.     /**
  74.      * distance of the projection screen of the camera from its
  75.      * position in world coordinates
  76.      */
  77.  
  78.     private int screendistance;
  79.  
  80.     /**
  81.      * width of the camera's projection screen in world coordinates.
  82.      */
  83.  
  84.     private int screenwidth;
  85.  
  86.     /**
  87.      * height of the camera's projection screen in world coordinates
  88.      */
  89.  
  90.     private int screenheight;
  91.  
  92.     /**
  93.      * list of all Wireframe objects associated with this camera
  94.      */
  95.  
  96.     private Vector memberVector;
  97.  
  98.     /**
  99.      * true if the view coords of all members are valid
  100.      */
  101.  
  102.     private boolean isValid;
  103.  
  104.     /**
  105.      * construct a new FixedCamera. Parameters: position of the camera
  106.      * in world coordinates, distance of the projection screen from
  107.      * the position in world coordinates, size of the projection
  108.      * screen in world coordinates.
  109.      */
  110.  
  111.     public FixedCamera(int xposition,int yposition,int zposition,int screenwidth,int screenheight,int screendistance)
  112.     {
  113.         memberVector=new Vector();
  114.  
  115.         setXPosition(xposition);
  116.         setYPosition(yposition);
  117.         setZPosition(zposition);
  118.  
  119.         setScreenwidth(screenwidth);
  120.         setScreenheight(screenheight);
  121.         setScreendistance(screendistance);
  122.     }
  123.  
  124.     /**
  125.      * convert an array of world coordinates into view coordinates.
  126.      * Both the argument and the result array are two-dimensional.
  127.      * the first dimension is for the multiple vertices, the second
  128.      * one for the coordinate. 0=x, 1=y, 2=z coordinate. This method
  129.      * should not be called by the user.
  130.      */
  131.  
  132.     public int[][] viewcoords(int world[][])
  133.     {
  134.         int n;
  135.  
  136.         int x1;
  137.         int y1;
  138.         int z1;
  139.  
  140.         int view[][]=new int[world.length][2];
  141.  
  142.         for(n=0;n<world.length;n++)
  143.         {
  144.             x1=world[n][X]-xposition;
  145.             y1=world[n][Y]-yposition;
  146.             z1=world[n][Z]-zposition;
  147.  
  148.             view[n][X]=screenwidth/2+screendistance*x1/z1;
  149.             view[n][Y]=screenheight/2+screendistance*y1/z1;
  150.         }
  151.  
  152.         return view;
  153.     }
  154.  
  155.     /**
  156.      * adds a Wireframe object to the member list of the camera
  157.      */
  158.  
  159.     public void addWireframe(Wireframe wireframe)
  160.     {
  161.         memberVector.addElement(wireframe);
  162.     }
  163.  
  164.     /**
  165.      * removes a Wireframe object from the member list of the camera
  166.      */
  167.  
  168.     public void removeWireframe(Wireframe wireframe)
  169.     {
  170.         memberVector.removeElement(wireframe);
  171.     }
  172.  
  173.     /**
  174.      * invalidate the camera. This means, that before the members
  175.      * are drawn, their view coordinates will be recalculated.
  176.      */
  177.  
  178.     private void invalidate()
  179.     {
  180.         isValid=false;
  181.     }
  182.  
  183.     /**
  184.      * validate the camera. This is, calculate the view
  185.      * coordinates of all member Wireframe objects.
  186.      */
  187.  
  188.     private void validate()
  189.     {
  190.         if(isValid==false)
  191.         {
  192.             for(int n=0;n<memberVector.size();n++)
  193.                 ((Wireframe)memberVector.elementAt(n)).viewcoords(this);
  194.  
  195.             isValid=true;
  196.         }
  197.         else
  198.         {
  199.             Wireframe wireframe;
  200.  
  201.             for(int n=0;n<memberVector.size();n++)
  202.                 if(!(wireframe=(Wireframe)memberVector.elementAt(n)).isValid())
  203.                     wireframe.viewcoords(this);
  204.         }
  205.     }
  206.  
  207.     /**
  208.      * draw all member Wireframe objects from this camera's
  209.      * point of view
  210.      */
  211.  
  212.     public void draw(Graphics graphics)
  213.     {
  214.         validate();
  215.  
  216.         for(int n=0;n<memberVector.size();n++)
  217.             ((Wireframe)memberVector.elementAt(n)).draw(graphics);
  218.     }
  219.  
  220.     /**
  221.      * get the x position of the camera
  222.      */
  223.  
  224.     public int getXPosition()
  225.     {
  226.         return xposition;
  227.     }
  228.  
  229.     /**
  230.      * set the x position of the camera
  231.      */
  232.  
  233.     public void setXPosition(int xposition)
  234.     {
  235.         this.xposition=xposition;
  236.  
  237.         invalidate();
  238.     }
  239.  
  240.     /**
  241.      * get the y position of the camera
  242.      */
  243.  
  244.     public int getYPosition()
  245.     {
  246.         return yposition;
  247.     }
  248.  
  249.     /**
  250.      * set the y position of the camera
  251.      */
  252.  
  253.     public void setYPosition(int yposition)
  254.     {
  255.         this.yposition=yposition;
  256.  
  257.         invalidate();
  258.     }
  259.  
  260.     /**
  261.      * get the z position of the camera
  262.      */
  263.  
  264.     public int getZPosition()
  265.     {
  266.         return zposition;
  267.     }
  268.  
  269.     /**
  270.      * set the z position of the camera
  271.      */
  272.  
  273.     public void setZPosition(int zposition)
  274.     {
  275.         this.zposition=zposition;
  276.  
  277.         invalidate();
  278.     }
  279.  
  280.     /**
  281.      * move the camera's position
  282.      */
  283.  
  284.     public void move(int dx,int dy,int dz)
  285.     {
  286.         xposition+=dx;
  287.         yposition+=dy;
  288.         zposition+=dz;
  289.  
  290.         invalidate();
  291.     }
  292.  
  293.     /**
  294.      * get the width of the camera's projection screen
  295.      */
  296.  
  297.     public int getScreenwidth()
  298.     {
  299.         return screenwidth;
  300.     }
  301.  
  302.     /**
  303.      * set the width of the camera's projection screen
  304.      */
  305.  
  306.     public void setScreenwidth(int screenwidth)
  307.     {
  308.         this.screenwidth=screenwidth;
  309.  
  310.         invalidate();
  311.     }
  312.  
  313.     /**
  314.      * get the height of the camera's projection screen
  315.      */
  316.  
  317.     public int getScreenheight()
  318.     {
  319.         return screenheight;
  320.     }
  321.  
  322.     /**
  323.      * set the height of the camera's projection screen
  324.      */
  325.  
  326.     public void setScreenheight(int screenheight)
  327.     {
  328.         this.screenheight=screenheight;
  329.  
  330.         invalidate();
  331.     }
  332.  
  333.     /**
  334.      * get the distance of the camera's projection screen
  335.      * from the position of the camera
  336.      */
  337.  
  338.     public int getScreendistance()
  339.     {
  340.         return screendistance;
  341.     }
  342.  
  343.     /**
  344.      * set the distance of the camera's projection screen
  345.      * from the position of the camera
  346.      */
  347.  
  348.     public void setScreendistance(int screendistance)
  349.     {
  350.         this.screendistance=screendistance;
  351.  
  352.         invalidate();
  353.     }
  354. }
  355.  
  356.