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

📄 appendix-e.html

📁 java game programming e-book
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<P>Just to see that this is actually working, Figure E-6 and Listing E-3 show an applet that spins a number of random 3D points around.
</P>
<P><B>Listing E-3</B> Rotating points</P>
<!-- CODE //-->
<PRE>
import java.awt.*;
import java.applet.*;

public class RotatingPoints extends Applet implements Runnable&#123;
   //-- # points
   int pts;
   //-- the source points
   double x[],y[],z[];
   //-- the transformed points
   double xt[],yt[],zt[];
   //-- the angles
   double Ax,Ay,Az;
   //-- angular velocity
   double da;
   //-- the thread
   Thread myThread;

   public void init()&#123;
      pts=10;
      x =new double[pts]; y =new double[pts]; z =new double[pts];
      xt=new double[pts]; yt=new double[pts]; zt=new double[pts];
      //-- create some random 3d points
      for(int n=0;n&lt;pts;n&#43;&#43;)&#123;
         x[n]=Math.random()*100-50;
         y[n]=Math.random()*100-50;
         z[n]=Math.random()*100-50;
      &#125;

      //-- set the angular velocity
      da=Math.PI/10;
      //-- start the thread
      myThread=new Thread(this);
      myThread.start();
   &#125;

   public void run()&#123;
      while(myThread!=null)&#123;
         try &#123; myThread.sleep(100);
         &#125; catch ( InterruptedException e) &#123;&#125;
         rotatePoints(x,y,z,xt,yt,zt,pts,Ax,Ay,Az);
         Az&#43;=da;
         repaint();
      &#125;
   &#125;

   public void start()&#123;
      if(myThread==null)&#123;
         myThread=new Thread(this);myThread.start();
      &#125;
   &#125;

   public void stop()&#123;
      if(myThread!=null)&#123;
         myThread.stop(); myThread=null;
      &#125;
   &#125;

   public void paint(Graphics g)&#123;
      int Xo=size().width&gt;&gt;1,Yo=size().height&gt;&gt;1;
      //-- paint all the points
      for(int n=0;n&lt;pts;n&#43;&#43;)&#123;
         int x=(int)xt[n],y=(int)yt[n];
         //-- set the different colors
         g.setColor(new Color(n&lt;&lt;3,0,0));
         //-- draw a line from origot to center
         g.drawLine(Xo,Yo,Xo&#43;x,Yo&#43;y);
         //-- draw a little circle
         g.fillOval(Xo&#43;x-4,Yo&#43;y-4,8,8);
         &#125;
      &#125;
      private void rotatePoints(double xs[],double ys[],double zs[],
                                 double xd[],double yd[],double zd[],
                                 int pts,double a,double b,double c)&#123;
         for(int n=0;n&lt;pts;n&#43;&#43;)&#123;
             double Xa,Ya,Za,Xb,Yb,Zb;


             //-- rotate the dource in x-y-plane
             Xa = xs[n]*Math.cos(a)-ys[n]*Math.sin(a);
             Ya = xs[n]*Math.sin(a)&#43;ys[n]*Math.cos(a);
             Za = zs[n];

              //-- rotate the resulting point in the y-z-plane
              Xb = Xa;
              Yb = Ya*Math.cos(b)-Za*Math.sin(b);
              Zb = Ya*Math.sin(b)&#43;Za*Math.cos(b);

              //-- rotate the resulting point in the z-x-plane
              xd[n] = Xb*Math.cos(c)&#43;Zb*Math.sin(c);
              yd[n] = Yb;
              zd[n] = -Xb*Math.sin(c)&#43;Zb*Math.cos(c);
         &#125;
      &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P><A NAME="Fig6"></A><A HREF="javascript:displayWindow('images/ape-06.jpg',509,347 )"><IMG SRC="images/ape-06t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/ape-06.jpg',509,347)"><FONT COLOR="#000077"><B>Figure E-6</B></FONT></A>&nbsp;&nbsp;The Rotating Points applet</P>
<P><FONT SIZE="+1"><B>Linear Algebra Basics</B></FONT></P>
<P>Computer graphics algorithms make use of many mathematical concepts and techniques. This section will provide a description of some of the basic notions of linear algebra. Since almost all 3D transformations are done using linear algebra, it is essential to at least understand the basic definitions in this field. If you have worked with matrixes and vectors before but have not used them in 3D graphics, you should browse this section to acquaint yourself with their use in this context.
</P>
<P><FONT SIZE="+1"><B><I>Orthogonal Normalized Coordinate System</I></B></FONT></P>
<P>We are all used to the simple and intuitive coordinate system consisting of an x-, y-, and possibly z-axis in which points are placed by specifying coordinates; for example, (x,y)=(2,3). But what do those numbers mean? It&#146;s fairly obvious that 2 means the number of scale units on the x-axis and 3 means the number of scale units on the y-axis. Figure E-7 demonstrates.
</P>
<P><A NAME="Fig7"></A><A HREF="javascript:displayWindow('images/ape-07.jpg',510,348 )"><IMG SRC="images/ape-07t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/ape-07.jpg',510,348)"><FONT COLOR="#000077"><B>Figure E-7</B></FONT></A>&nbsp;&nbsp;Orthogonal normalized coordinate system with two axes</P>
<P>But what do they stand for? Is it 2 inches to the right and 3 meters upward? Or possibly miles? If nothing is specified on the axis, the numbers stand for units. In 3D graphics we will use the right-handed orthogonal normalized (O.N.) coordinate system, which is mathematically correct (see Figure E-8).
</P>
<P><A NAME="Fig8"></A><A HREF="javascript:displayWindow('images/ape-08.jpg',206,270 )"><IMG SRC="images/ape-08t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/ape-08.jpg',206,270)"><FONT COLOR="#000077"><B>Figure E-8</B></FONT></A>&nbsp;&nbsp;Right- and left-handed O.N. system with three axes</P>
<P>This is not as complicated as it sounds. It simply means that all axes are at a right angle to each other (orthogonal) and have the same length. This length equals one (normalized). In linear algebra you can use any sort of coordinate system, even weird ones, like that shown in Figure E-9. But the O.N. system in Figure E-5 simplifies a lot of linear algebra operations, and since this is the system that we will use when dealing with 3D math, we simply give thanks and move on.
</P>
<P><A NAME="Fig9"></A><A HREF="javascript:displayWindow('images/ape-09.jpg',347,275 )"><IMG SRC="images/ape-09t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/ape-09.jpg',347,275)"><FONT COLOR="#000077"><B>Figure E-9</B></FONT></A>&nbsp;&nbsp;Weird left-handed, unorthogonal, unnormalized coordinate system with three axes</P>
<P><FONT SIZE="+1"><B><I>Vectors</I></B></FONT></P>
<P>There is a fundamental difference between a 3D point and a vector, although these bounds get a little bit fuzzy when dealing with 3D graphics. A point is merely a position in a three-dimensional space, while a vector is the difference between two points. This is shown in Figure E-10.
</P>
<P><A NAME="Fig10"></A><A HREF="javascript:displayWindow('images/ape-10.jpg',508,347 )"><IMG SRC="images/ape-10t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/ape-10.jpg',508,347)"><FONT COLOR="#000077"><B>Figure E-10</B></FONT></A>&nbsp;&nbsp;A vector is defined as the difference between two points</P>
<P>A vector can be described as a directed line segment that has two properties: magnitude and direction.
</P>
<P>Magnitude is the length of the vector, and it is calculated as <IMG SRC="images/ape-01i.jpg"> in 2D and <IMG SRC="images/ape-02i.jpg"> in 3D.</P>
<P>As you can see, there is consistency between the 2D and 3D cases. This is typical when it comes to vector and matrix operations.</P>
<P>The direction of a vector is defined indirectly by its components. In Figure E-10, the components are Vx and Vy. In the 3D case there would also be Vz.</P>
<P><B>Addition and Subtraction with Vectors</B></P>
<P>Addition (shown in Figure E-11) is done by simply adding the vectors&#146; components as follows:
</P>
<P ALIGN="CENTER"><IMG SRC="images/ape-03d.jpg"></P>
<P><A NAME="Fig11"></A><A HREF="javascript:displayWindow('images/ape-11.jpg',381,299 )"><IMG SRC="images/ape-11t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/ape-11.jpg',381,299)"><FONT COLOR="#000077"><B>Figure E-11</B></FONT></A>&nbsp;&nbsp;Addition between two-dimensional vectors</P>
<P>Subtraction (shown in Figure E-12) is done by making b a negative number. The expression will barely change, but the graphical representation is totally different, as you might expect.
</P>
<P ALIGN="CENTER"><IMG SRC="images/ape-04d.jpg"></P>
<P><A NAME="Fig12"></A><A HREF="javascript:displayWindow('images/ape-12.jpg',89,33 )"><IMG SRC="images/ape-12t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/ape-12.jpg',89,33)"><FONT COLOR="#000077"><B>Figure E-12</B></FONT></A>&nbsp;&nbsp;Subtraction between two-dimensional vectors</P>
<P>What we do here is turn the vector b so that it points in the &#147;opposite&#148; direction.
</P>
<P><B>Dot Product</B></P>
<P>Dot product is very powerful and is the foundation for many linear algebra operations.
</P>

<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>Dot Product, the Definition</B></FONT>
<P ALIGN="CENTER"><IMG SRC="images/ape-05d.jpg"></P>
</TABLE>

<P>Another way of calculating the dot product is by multiplying the components of the vectors as follows:
</P>
<P ALIGN="CENTER"><IMG SRC="images/ape-06d.jpg"></P>
<P>This only works in an O.N. system. The result of this operation will be a scalar (a number) that can be interpreted as the product of the parallel components of the two vectors. This interpretation is very abstract, not very intuitive. Let&#146;s look at the behavior of dot product to get a better idea of what it does.
</P>
<P><B>Characteristics of Dot Product</B></P>
<DL>
<DD><B>&#149;</B>&nbsp;&nbsp;The result gets smaller as the angle comes closer to 90 degrees and larger as the angle gets closer to zero. The largest value is obtained when the two vectors are parallel or &#147;on top&#148; of each other, because the angle is 0 and cos 0=1. This is shown in Figure E-13.
<P><A NAME="Fig13"></A><A HREF="javascript:displayWindow('images/ape-13.jpg',112,33 )"><IMG SRC="images/ape-13t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/ape-13.jpg',112,33)"><FONT COLOR="#000077"><B>Figure E-13</B></FONT></A>&nbsp;&nbsp;The result of dot product depends on the angle between the vectors</P>
<DD><B>&#149;</B>&nbsp;&nbsp;The dot product can be used to determine if a vector is at a right angle to another vector. At this angle the result will be zero, because <IMG SRC="images/ape-03i.jpg">, as shown in Figure E-14.<P><A NAME="Fig14"></A><A HREF="javascript:displayWindow('images/ape-14.jpg',380,300 )"><IMG SRC="images/ape-14t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/ape-14.jpg',380,300)"><FONT COLOR="#000077"><B>Figure E-14</B></FONT></A>&nbsp;&nbsp;If the vectors are at a right angle to each other, the result of the dot product will be 0</P>
<DD><B>&#149;</B>&nbsp;&nbsp;The result will be negative when the angle is larger than 90 degrees (see Figure E-15). This can be used to decide if the vectors point in &#147;opposite&#148; directions.
<P><A NAME="Fig15"></A><A HREF="javascript:displayWindow('images/ape-15.jpg',174,73 )"><IMG SRC="images/ape-15t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/ape-15.jpg',174,73)"><FONT COLOR="#000077"><B>Figure E-15</B></FONT></A>&nbsp;&nbsp;If the angle is larger than 90 degrees, the result will be negative</P>
<DD><B>&#149;</B>&nbsp;&nbsp;When both vectors have magnitude (length) 1, the result of a dot product will be between -1 and 1. This result is actually the cosine value of the angle between the vectors. Figure E-16 shows this. In other words, we could say that cos<I>a</I>=V<SUB>1x</SUB>&#215;V<SUB>2x</SUB>&#215;V<SUB>1y</SUB>V<SUB>2y</SUB>&#43;V<SUB>1x</SUB>&#215;V<SUB>2z</SUB> as long as both V1 and V2 are normalized (length=1).
<P><A NAME="Fig16"></A><A HREF="javascript:displayWindow('images/ape-16.jpg',174,73 )"><IMG SRC="images/ape-16t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/ape-16.jpg',174,73)"><FONT COLOR="#000077"><B>Figure E-16</B></FONT></A>&nbsp;&nbsp;Calculating the angle between the vectors</P>
</DL>
<P><B>The Value of Dot Product</B></P>
<P>Dot product is a powerful operation that can be used in many ways. As will be shown later, a variation of the dot product between two 2D vectors can tell the orientation of a polygon. In 3D the dot product will be heavily used to determine if a point is in front of or behind a plane. The definition of the dot product in combination with other calculations will be used to determine the distance from a point to a line or a plane. It is also used to determine the shading of a polygon, depending on the normal of the polygon and the light vector.
</P>
<P><FONT SIZE="+1"><B><I>Cross Product</I></B></FONT></P>
<P>The cross product between two vectors is related to the dot product but produces completely different results. It is linear algebra&#146;s way of multiplying two vectors. The result will be another vector that is at a right angle to both of the operands. Let&#146;s look at the definition:
</P>

<TABLE BORDER="2" BORDERCOLOR="#0000" WIDTH="100%" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>Cross Product, the Definition</B></FONT>
<P ALIGN="CENTER"><B>V<SUB><I>1</I></SUB>xV<SUB><I>2</I></SUB>=u|V<SUB><I>1</I></SUB>|V<SUB><I>2</I></SUB>|sin<I>a</I></B></P>
</TABLE>

<P>Another way of calculating the vector product in an O.N. system is using the components of the operands as follows:
</P>
<P ALIGN="CENTER"><B><I>v</I></B><SUB>1</SUB><B>x<I>v</I></B><SUB>2</SUB><B>=(<I>v</I></B><SUB>1y</SUB><B>&#183;<I>v</I></B><SUB>2z</SUB><B>&#150;<I>v</I></B><SUB>1z</SUB><B>&#183;<I>v</I></B><SUB>2y</SUB>,<B><I>v</I></B><SUB>1z</SUB><B>&#183;<I>v</I></B><SUB>2x</SUB><B>&#150;<I>v</I></B><SUB>1x</SUB><B>&#183;<I>v</I></B><SUB>2z</SUB>,<B><I>v</I></B><SUB>1x</SUB><B>&#183;<I>v</I></B><SUB>2y</SUB><B>&#150;<I>v</I></B><SUB>1y</SUB><B>&#183;<I>v</I></B><SUB>2x</SUB><B>)</B></P>
<P>The magnitude of the resulting vector is the same as the area of the parallelogram that the two vectors make. This can be used to calculate the area of a triangle, for example.</P>

⌨️ 快捷键说明

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