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

  1. import java.awt.*;
  2.  
  3. /**
  4.  * This class represents an object in a 3D world. It is made up by an
  5.  * array of vertices and an array of surfaces built on them. Attributes
  6.  * of the object are position and angle, each of them in 3D. The values
  7.  * of the angle attributes are stored in a special fixed point number
  8.  * format, which is used by and described in FastMath. The coordinates
  9.  * of the vertices reside in "local coordinates", i.e. indepentent from
  10.  * the actual position of the object. The transformation according to
  11.  * the position and angle will be done automatically by a FixedCamera.
  12.  * <P>
  13.  * To make things clear, this is an example code for a tetrahedron:
  14.  * <PRE>
  15.  * int[][] vertices=
  16.  * {
  17.  *     {0,-1,0},    // vertex 0 x/y/z coordinates
  18.  *     {-1,1,-1},    // vertex 1 x/y/z coordinates
  19.  *     {1,1,-1},    // vertex 2 x/y/z coordinates
  20.  *     {0,0,1}        // vertex 3 x/y/z coordinates
  21.  * };
  22.  *  
  23.  * int[][] surfaces=
  24.  * {
  25.  *     {0,1,2},    // surface 0 is made up by vertices 0,1 and 2
  26.  *     {0,1,3},    // surface 1 is made up by vertices 0,1 and 3
  27.  *     {0,2,3},    // surface 2 is made up by vertices 0,2 and 3
  28.  *     {1,2,3}     // surface 3 is made up by vertices 1,2 and 3
  29.  * }
  30.  * </PRE>
  31.  * <P>
  32.  * author: Michael Kraus<BR>
  33.  * version: 1.2<BR>
  34.  * date: 1999/5/17<BR>
  35.  * environment: JDK 1.0.2<BR>
  36.  * email: <A href="michael.kraus@informatik.uni-muenchen.de">michael.kraus@informatik.uni-muenchen.de</A><BR>
  37.  * homepage: <A href="www.informatik.uni-muenchen.de/~michael.kraus">www.informatik.uni-muenchen.de/~michael.kraus</A>
  38.  */
  39.  
  40. public class Wireframe
  41. {
  42.     /**
  43.      * index of the x coordinate in the local, world and view
  44.      * coordinate array
  45.      */
  46.  
  47.     private static final int X=0;
  48.  
  49.     /**
  50.      * index of the y coordinate in the local, world and view
  51.      * coordinate array
  52.      */
  53.  
  54.     private static final int Y=1;
  55.  
  56.     /**
  57.      * index of the z coordinate in both the local and world
  58.      * coordinate array
  59.      */
  60.  
  61.     private static final int Z=2;
  62.  
  63.     /**
  64.      * x coordinate of the object's position in world coordinates
  65.      */
  66.  
  67.     private int xPosition;
  68.  
  69.     /**
  70.      * y coordinate of the object's position in world coordinates
  71.      */
  72.  
  73.     private int yPosition;
  74.  
  75.     /**
  76.      * z coordinate of the object's position in world coordinates
  77.      */
  78.  
  79.     private int zPosition;
  80.  
  81.     /**
  82.      * x angle of the object's state in special fixed point
  83.      * number format
  84.      */
  85.  
  86.     private int xAngle;
  87.  
  88.     /**
  89.      * y angle of the object's state in special fixed point
  90.      * number format
  91.      */
  92.  
  93.     private int yAngle;
  94.  
  95.     /**
  96.      * z angle of the object's state in special fixed point
  97.      * number format
  98.      */
  99.  
  100.     private int zAngle;
  101.  
  102.     /**
  103.      * array of local coordinates of the object's vertices. Each
  104.      * vertex is an integer array containing its x, y and z
  105.      * coordinate.
  106.      */
  107.  
  108.     private int local[][];
  109.  
  110.     /**
  111.      * array of world coordinates of the object's vertices. Each
  112.      * vertex is an integer array containing its x, y and z
  113.      * coordinate.
  114.      */
  115.  
  116.     private int world[][];
  117.  
  118.     /**
  119.      * array of view coordinates of the object's vertices. Each
  120.      * vertex is an integer array containing its x and y
  121.      * coordinate.
  122.      */
  123.  
  124.     private int view[][];
  125.  
  126.     /**
  127.      * array of surfaces of the object. Each surface is an array
  128.      * of the indices of the vertices that make up that surface,
  129.      * as used in the local, world and view arrays.
  130.      */
  131.  
  132.     private int surface[][];
  133.  
  134.     /**
  135.      * true if the world coords of this object are valid
  136.      */
  137.  
  138.     private boolean valid;
  139.  
  140.     /**
  141.      * construct a new Wireframe object. Parameter are: local
  142.      * coordinates, surfaces, position and angle of the object.
  143.      */
  144.  
  145.     public Wireframe(int local[][],int surface[][],int xPosition,int yPosition,int zPosition,int xAngle,int yAngle,int zAngle)
  146.     {
  147.         this.local=local;
  148.         this.world=new int[local.length][3];
  149.         this.surface=surface;
  150.  
  151.         setXPosition(xPosition);
  152.         setYPosition(yPosition);
  153.         setZPosition(zPosition);
  154.  
  155.         setXAngle(xAngle);
  156.         setYAngle(yAngle);
  157.         setZAngle(zAngle);
  158.     }
  159.  
  160.     /**
  161.      * convert the object from local coordinates to world
  162.      * coordinates, according to its position and angle
  163.      */
  164.  
  165.     private void worldcoords()
  166.     {
  167.         int x;
  168.         int y;
  169.         int z;
  170.  
  171.         int matrix[][]=new int[3][3];
  172.  
  173.         matrix[0][0]=(FastMath.cos(yAngle-zAngle)+FastMath.cos(yAngle+zAngle))/2;
  174.         matrix[0][1]=((FastMath.cos(xAngle-yAngle-zAngle)+FastMath.cos(xAngle-yAngle+zAngle)-FastMath.cos(xAngle+yAngle-zAngle)-FastMath.cos(xAngle+yAngle+zAngle))/2-FastMath.sin(xAngle-zAngle)+FastMath.sin(xAngle+zAngle))/2;
  175.         matrix[0][2]=((FastMath.sin(xAngle-yAngle+zAngle)+FastMath.sin(xAngle-yAngle-zAngle)-FastMath.sin(xAngle+yAngle-zAngle)-FastMath.sin(xAngle+yAngle+zAngle))/2+FastMath.cos(xAngle-zAngle)-FastMath.cos(xAngle+zAngle))/2;
  176.         matrix[1][0]=(FastMath.sin(yAngle-zAngle)-FastMath.sin(yAngle+zAngle))/2;
  177.         matrix[1][1]=((FastMath.sin(xAngle-yAngle-zAngle)-FastMath.sin(xAngle-yAngle+zAngle)-FastMath.sin(xAngle+yAngle-zAngle)+FastMath.sin(xAngle+yAngle+zAngle))/2+FastMath.cos(xAngle-zAngle)+FastMath.cos(xAngle+zAngle))/2;
  178.         matrix[1][2]=((FastMath.cos(xAngle-yAngle+zAngle)+FastMath.cos(xAngle+yAngle-zAngle)-FastMath.cos(xAngle-yAngle-zAngle)-FastMath.cos(xAngle+yAngle+zAngle))/2+FastMath.sin(xAngle-zAngle)+FastMath.sin(xAngle+zAngle))/2;
  179.         matrix[2][0]=FastMath.sin(yAngle);
  180.         matrix[2][1]=(FastMath.sin(yAngle-xAngle)-FastMath.sin(xAngle+yAngle))/2;
  181.         matrix[2][2]=(FastMath.cos(xAngle-yAngle)+FastMath.cos(xAngle+yAngle))/2;
  182.  
  183.         for(int n=0;n<local.length;n++)
  184.         {
  185.             x=local[n][X];
  186.             y=local[n][Y];
  187.             z=local[n][Z];
  188.  
  189.             world[n][X]=xPosition+((matrix[0][0]*x+matrix[0][1]*y+matrix[0][2]*z)>>FastMath.TWOPI_BITS);
  190.             world[n][Y]=yPosition+((matrix[1][0]*x+matrix[1][1]*y+matrix[1][2]*z)>>FastMath.TWOPI_BITS);
  191.             world[n][Z]=zPosition+((matrix[2][0]*x+matrix[2][1]*y+matrix[2][2]*z)>>FastMath.TWOPI_BITS);
  192.         }
  193.     }
  194.  
  195.     /**
  196.      * convert the object from world coordinates to view
  197.      * coordinates, according to the attributes of the
  198.      * camera argument. This method should never be called
  199.      * by the user.
  200.      */
  201.  
  202.     public void viewcoords(FixedCamera camera)
  203.     {
  204.         validate();
  205.  
  206.         view=camera.viewcoords(world);
  207.     }
  208.  
  209.     /**
  210.      * true if the world coordinates of this Wireframe
  211.      * object are valid. This method should never be called
  212.      * by the user.
  213.      */
  214.  
  215.     public boolean isValid()
  216.     {
  217.         return valid;
  218.     }
  219.  
  220.     /**
  221.      * invalidate the Wireframe object
  222.      */
  223.  
  224.     private void invalidate()
  225.     {
  226.         valid=false;
  227.     }
  228.  
  229.     /**
  230.      * validate the Wireframe object. This is, calculate the
  231.      * world coordinates.
  232.      */
  233.  
  234.     private void validate()
  235.     {
  236.         if(valid==false)
  237.         {
  238.             worldcoords();
  239.  
  240.             valid=true;
  241.         }
  242.     }
  243.  
  244.     /**
  245.      * draw the object. The local coordinates must have been
  246.      * be converted to view coordinates before, so this
  247.      * method should never be called by the user.
  248.      */
  249.  
  250.     public void draw(Graphics graphics)
  251.     {
  252.         int n;
  253.         int m;
  254.  
  255.         for(n=0;n<surface.length;n++)
  256.         {
  257.             Polygon polygon=new Polygon();
  258.  
  259.             for(m=0;m<surface[n].length;m++)
  260.                 polygon.addPoint(view[surface[n][m]][X],view[surface[n][m]][Y]);
  261.  
  262.             graphics.drawPolygon(polygon);
  263.         }
  264.     }
  265.  
  266.     /**
  267.      * get the x position of the Wireframe object
  268.      */
  269.  
  270.     public int getXPosition()
  271.     {
  272.         return xPosition;
  273.     }
  274.  
  275.     /**
  276.      * set the x position of the Wireframe object
  277.      */
  278.  
  279.     public void setXPosition(int xPosition)
  280.     {
  281.         this.xPosition=xPosition;
  282.  
  283.         invalidate();
  284.     }
  285.  
  286.     /**
  287.      * get the y position of the Wireframe object
  288.      */
  289.  
  290.     public int getYPosition()
  291.     {
  292.         return yPosition;
  293.     }
  294.  
  295.     /**
  296.      * set the y position of the Wireframe object
  297.      */
  298.  
  299.     public void setYPosition(int yPosition)
  300.     {
  301.         this.yPosition=yPosition;
  302.  
  303.         invalidate();
  304.     }
  305.  
  306.     /**
  307.      * get the z position of the Wireframe object
  308.      */
  309.  
  310.     public int getZPosition()
  311.     {
  312.         return zPosition;
  313.     }
  314.  
  315.     /**
  316.      * set the z position of the Wireframe object
  317.      */
  318.  
  319.     public void setZPosition(int zPosition)
  320.     {
  321.         this.zPosition=zPosition;
  322.  
  323.         invalidate();
  324.     }
  325.  
  326.     /**
  327.      * move the Wireframe object's position
  328.      */
  329.  
  330.     public void move(int dx,int dy,int dz)
  331.     {
  332.         xPosition+=dx;
  333.         yPosition+=dy;
  334.         zPosition+=dz;
  335.  
  336.         invalidate();
  337.     }
  338.  
  339.     /**
  340.      * get the x angle of the Wireframe object
  341.      */
  342.  
  343.     public int getXAngle()
  344.     {
  345.         return xAngle;
  346.     }
  347.  
  348.     /**
  349.      * set the x angle of the Wireframe object
  350.      */
  351.  
  352.     public void setXAngle(int xAngle)
  353.     {
  354.         this.xAngle=xAngle;
  355.  
  356.         invalidate();
  357.     }
  358.  
  359.     /**
  360.      * get the y angle of the Wireframe object
  361.      */
  362.  
  363.     public int getYAngle()
  364.     {
  365.         return yAngle;
  366.     }
  367.  
  368.     /**
  369.      * set the y angle of the Wireframe object
  370.      */
  371.  
  372.     public void setYAngle(int yAngle)
  373.     {
  374.         this.yAngle=yAngle;
  375.  
  376.         invalidate();
  377.     }
  378.  
  379.     /**
  380.      * get the z angle of the Wireframe object
  381.      */
  382.  
  383.     public int getZAngle()
  384.     {
  385.         return zAngle;
  386.     }
  387.  
  388.     /**
  389.      * set the z angle of the Wireframe object
  390.      */
  391.  
  392.     public void setZAngle(int zAngle)
  393.     {
  394.         this.zAngle=zAngle;
  395.  
  396.         invalidate();
  397.     }
  398.  
  399.     /**
  400.      * rotate the Wireframe object's angle
  401.      */
  402.  
  403.     public void rotate(int dx,int dy,int dz)
  404.     {
  405.         xAngle+=dx;
  406.         yAngle+=dy;
  407.         zAngle+=dz;
  408.  
  409.         invalidate();
  410.     }
  411. }
  412.  
  413.