⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 393-395.html

📁 java game programming e-book
💻 HTML
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1571690433"><META name=vstitle content="Black Art of Java Game Programming"><META name=vsauthor content="Joel Fan"><META name=vsimprint content="Sams"><META name=vspublisher content="Macmillan Computer Publishing"><META name=vspubdate content="11/01/96"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Black Art of Java Game Programming:Into the Third Dimension</TITLE>
<!-- HEADER --><STYLE type="text/css">  <!-- A:hover  { 	color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><script><!--function displayWindow(url, width, height) {         var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></script><SCRIPT><!--function popUp(url) {        var Win = window.open(url,"displayWindow",'width=400,height=300,resizable=1,scrollbars=yes');	if (Win) {		Win.focus();	}}//--></SCRIPT><script language="JavaScript1.2"><!--function checkForQuery(fm) {  /* get the query value */  var i = escape(fm.query.value);  if (i == "") {      alert('Please enter a search word or phrase');      return false;  }                  /* query is blank, dont run the .jsp file */  else return true;  /* execute the .jsp file */}//--></script></HEAD><BODY> 
<TABLE border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=75 valign=top>
<img src="../1571690433.gif" width=60 height=73 alt="Black Art of Java Game Programming" border="1">
</td>
<td align="left">
    <font face="arial, helvetica" size="-1" color="#336633"><b>Black Art of Java Game Programming</b></font>
    <br>
    <font face="arial, helvetica" size="-1"><i>by Joel Fan</i>
    <br>
    Sams,&nbsp;Macmillan Computer Publishing
    <br>
    <b>ISBN:</b>&nbsp;1571690433<b>&nbsp;&nbsp;&nbsp;Pub Date:</b>&nbsp;11/01/96</font>&nbsp;&nbsp;
</td>
</tr>
</table>
<P>

<!--ISBN=1571690433//-->
<!--TITLE=Black Art of Java Game Programming//-->
<!--AUTHOR=Joel Fan//-->
<!--AUTHOR=Eric Ries//-->
<!--AUTHOR=Calin Tenitchi//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=11//-->
<!--PAGES=393-395//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="387-393.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="395-399.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">Vertices</FONT></H4>
<P>Taking a closer look at Figure 11-2, you should notice that polygons share vertices with each other. This means that a vertex is used in more than a single polygon, because all the polygons are connected to each other. Since a vertex is used in at least three polygons, it would be a waste of memory and calculations to let every polygon have its own 3D coordinates. Instead of eight different coordinates, we would end up with 24, out of which two-thirds are identical. Rotating the polyhedron would mean rotating three times as many vertices. That is a deadly sin. Rule #1 in the list of things that you shouldn&#146;t do is this: Don&#146;t ever make the same calculation twice.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading9"></A><FONT COLOR="#000077">Understanding the Structure of a Polyhedron</FONT></H4>
<P>The structure of a polyhedron is described as a list of vertices and a list of polygons. How the polygons are defined will be described later. The vertices in Figure 11-2 can, however, be comfortably expressed using vectors. The vectors contain the coordinates of each vertex.
</P>
<!-- CODE SNIP //-->
<PRE>
//- vertex # 0  1  2  3  4  5  6  7
double x[]=&#123;-1,-1, 1, 1,-1,-1, 1, 1&#125;
double y[]=&#123; 1, 1, 1, 1,-1,-1,-1,-1&#125;
double z[]=&#123; 1,-1,-1, 1, 1,-1,-1, 1&#125;
</PRE>
<!-- END CODE SNIP //-->
<P>To make life easier, we will implement a support class that encapsulates an array of 3D coordinates using three vectors of doubles. The data encapsulation is not very strict, and it can be accessed by any class in the module. The reason for this somewhat repulsive implementation is that the vertices of a polyhedron are treated and looked upon as an entity and not a bunch of independent 3D coordinates. All transforms will be applied on all vertices every time.
</P>
<P>We will use a class with three vectors of doubles instead of an array of 3D points because such a class is effectively handled by Java. It is an implementation decision and will not affect the overall design.</P>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">Implementing an Array of 3D Points</FONT></H4>
<P>Parts of the code shown in Listing 11-1 include reading and writing to streams, which will not be covered in this chapter. Except for the use of streams, the implementation is pretty much straightforward without any unpleasant surprises.
</P>
<P><B>Listing 11-1</B> An array of 3D points</P>
<!-- CODE //-->
<PRE>
/**
 * A class that encapsulates an array of 3D points.
 */
class fArrayOf3dPoints extends Object &#123;
   double x[],y[],z[];
   int npoints;
   /**
    * Constructs an array of 3d points with the supplied vectors.
    */
   fArrayOf3dPoints(double x0[],double y0[],double z0[],int n)&#123;
      x=x0; y=y0; z=z0; npoints=n;
   &#125;
   /**
    * Constructs an empty array of 3d points with size "n"
    */
   fArrayOf3dPoints(int n)&#123;
      npoints=n;
      x=new double[n]; y=new double[n]; z=new double[n];
   &#125;
   /**
    * construct an array of 3d points from a stream
    */
   fArrayOf3dPoints(InputStream is) throws IOException&#123;
      fromString(is);
   &#125;
   /**
    * ovrrides the Object method
    */
   public String toString()&#123;
      String str=new String();
      //-- the number of vertices
      str=" "&#43;npoints&#43;"\n";
      //-- concat the coordinates to the string
      for(int n=0;n&lt;npoints;n&#43;&#43;)&#123;
         str=str&#43;x[n]&#43;" "&#43;y[n]&#43;" "&#43;z[n]&#43;"\n";
      &#125;
      return str;
   &#125;
   /**
    * Returns a clone.
    */
   fArrayOf3dPoints makeClone()&#123;
      double xnew[],ynew[],znew[];
      System.arraycopy(x,0,xnew=new double[npoints],0,npoints);
      System.arraycopy(y,0,ynew=new double[npoints],0,npoints);
      System.arraycopy(z,0,znew=new double[npoints],0,npoints);
      return new
fArrayOf3dPoints(xnew,ynew,znew,npoints);
   &#125;
   /**
    * Reads an array from a stream
    */
   void fromString(InputStream is) throws IOException &#123;
      //-- make a stream tokenizer
      StreamTokenizer stream = new StreamTokenizer (is);
              stream.commentChar('#');

      //-- get the # points
      stream.nextToken(); npoints=(int)stream.nval;

      //-- create the vectors
      x=new double[npoints];
      y=new double[npoints];
      z=new double[npoints];

      //-- read the coordinates
      for(int n=0;n&lt;npoints;n&#43;&#43;)&#123;
          stream.nextToken(); x[n]=(double)stream.nval;
          stream.nextToken(); y[n]=(double)stream.nval;
          stream.nextToken(); z[n]=(double)stream.nval;
      &#125;
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">Polygons</FONT></H4>
<P>Each face in a polyhedron is defined by specifying the vertices it contains in a certain order. The order is either clockwise (CW) or counterclockwise (CCW). We will use the CCW standard. The easiest way to understand what CW and CCW means is to look at Figure 11-2. Polygon #4 is the front face of the cube and it is turned toward the camera. The vertices it uses are 3, 0, 4, and 7. The numbers don&#146;t seem to have any apparent order, but there is a logic behind this arrangement. If the polyhedron is rotated in such a way that the polygon we are looking at is facing us, then its vertices will always go in a CCW fashion. Figure 11-3 illustrates this for polygon #4.
</P>
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/11-03.jpg',462,364 )"><IMG SRC="images/11-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/11-03.jpg',462,364)"><FONT COLOR="#000077"><B>Figure 11-3</B></FONT></A>&nbsp;&nbsp;Polygon with its vertices defined CCW</P>
<P>It takes a little bit of training to be able to rotate polyhedrons in your head and visualize vertices moving around, so don&#146;t worry if it feels awkward. Also notice that we indirectly use the two-dimensional representation of the polyhedron to decide if a polygon is CW or CCW.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="387-393.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="395-399.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -