📄 424-427.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, Macmillan Computer Publishing
<br>
<b>ISBN:</b> 1571690433<b> Pub Date:</b> 11/01/96</font>
</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=424-427//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="419-423.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="427-432.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading38"></A><FONT COLOR="#000077">Implementing a Generic Camera (fGenericCamera)</FONT></H4>
<P>A few details in the implementation need to be discussed before we study the code for the fGenericCamera (Listing 11-8).
</P>
<P><B>Listing 11-8</B> A very generic camera</P>
<!-- CODE //-->
<PRE>
/**
* A generic camera.
*/
public class fGenericCamera extends Object {
//-- a temporary buffer used for projection
protected static fArrayOf2dPoints our2dBuffer=
new fArrayOf2dPoints(new int[100],new int[100],100);
//-- a temporary buffer used for WCS to VCS transform
protected static fArrayOf3dPoints our3dBuffer=
new fArrayOf3dPoints(new double[100],new double[100],new
double[100],100);
//-- the screen distance
protected double screendist;
//-- screen origo
protected int x0,y0;
//-- the viewangle
protected double myViewAngle;
//-- the matrix used for the WCS to VCS tranform
fMatrix3d myWCStoVCSmatrix;
//-- mark if the matrix is dirty
boolean matrixIsDirty;
//-- the position and angle of the camera in WCS
fPoint3d myPosition;
fAngle3d myAngle;
/**
* constructs a camera by specifying the width, height and viewangle
*/
public fGenericCamera(int width,int height,double viewAngle){
myViewAngle=viewAngle;
//-- calculate the screen origo
x0=width>>1; y0=height>>1;
//-- calculate the screen distance
screendist=(double)x0/(Math.tan(viewAngle/2));
//-- construct the matrix
myWCStoVCSmatrix=new fMatrix3d();
//--
myPosition=new fPoint3d();
myAngle=new fAngle3d();
matrixIsDirty=true;
}
/**
* sets the position and angle of the camera.
*/
public void setOrientation(fPoint3d pos,fAngle3d agl){
if(myPosition.equals(pos)==false){
myPosition.set(pos);
matrixIsDirty=true;
}
if(myAngle.equals(agl)==false){
myAngle.set(agl);
matrixIsDirty=true;
}
}
/**
* projects an array of 3d points to the temporary 2d buffer
*/
public fArrayOf2dPoints project(fArrayOf3dPoints p3d){
//-- updates the matrix if it needed
updateMatrix();
//-- transform the WCS vertices to VCS storing the results
//-- in a buffer
myWCStoVCSmatrix.transform(p3d,our3dBuffer);
//-- project the VCS coordiantes to SCS storing the results
//-- in a buffer
for(int n=0;n<p3d.npoints;n++){
double z=our3dBuffer.z[n];
our2dBuffer.x[n]=-(int)(screendist*our3dBuffer.x[n]/z)+x0;
our2dBuffer.y[n]= (int)(screendist*our3dBuffer.y[n]/z)+y0;
}
//-- lend the buffer to the caller.
return our2dBuffer;
}
/**
* updates the matrix
*/
private void updateMatrix(){
if(matrixIsDirty==true){
//-- only remake the matrix if it is "dirty"
myWCStoVCSmatrix.makeWCStoVCStransform(myPosition,myAngle);
matrixIsDirty=false;
}
}
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading39"></A><FONT COLOR="#000077">The Static Buffers</FONT></H4>
<P>The static buffer of 3D coordinates is used for storing the VCS vertices before the projection. It is static because only one camera at a time will use it.
</P>
<P>The static buffer of two-dimensional coordinates is used for projection. It will be “borrowed” to the caller so that it can use it for rendering the polyhedrons.</P>
<H4 ALIGN="CENTER"><A NAME="Heading40"></A><FONT COLOR="#000077">Calculating Screen Distance with Respect to the View Angle</FONT></H4>
<P>When constructing a camera, the view angle has to be specified. Using this argument and the width of the screen, we can calculate the screen distance with a trigonometric operation, as in Figure 11-21.
</P>
<P><A NAME="Fig23"></A><A HREF="javascript:displayWindow('images/11-23.jpg',600,406 )"><IMG SRC="images/11-23t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/11-23.jpg',600,406)"><FONT COLOR="#000077"><B>Figure 11-21</B></FONT></A> Calculating the screen distance</P>
<H4 ALIGN="CENTER"><A NAME="Heading41"></A><FONT COLOR="#000077">The Private updateMatrix() Method</FONT></H4>
<P>Every time the position or orientation changes, the transformation matrix is marked as “dirty.” The updateMatrix() method checks to see if the matrix is dirty, and if it is, it is recalculated.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading42"></A><FONT COLOR="#000077">The project() Method</FONT></H4>
<P>The project() method is fed with an array of 3D coordinates from the WCS. The coordinates will be transformed to the VCS and then projected into the 2D buffer. The 2D buffer will be returned from the method so that the caller can use it.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading43"></A><FONT COLOR="#000077">Implementing the 3D Point Class (fPoint3d)</FONT></H4>
<P>To make the handling of 3D coordinates smoother, a 3D point class is introduced in Listing 11-9. There are no surprises in this class, and it needs no further comments.
</P>
<P><B>Listing 11-9</B> The fPoint3d class</P>
<!-- CODE //-->
<PRE>
public class fPoint3d{
public double x;
public double y;
public double z;
public fPoint3d (double x0, double y0, double z0) {
x=x0; y=y0; z=z0;
}
public fPoint3d () {
x=y=z=0;
}
boolean equals (fPoint3d p) {
return (p.x==x)&&(p.y==y)&&(p.z==z);
}
void set (fPoint3d p) {
x=p.x; y=p.y; z=p.z;
}
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="419-423.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="427-432.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -