📄 433-434.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=433-434//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="427-432.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="435-440.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading48"></A><FONT COLOR="#000077">Implementing the Polyhedron Instance</FONT></H4>
<P>The implementation (see Listing 11-12) will turn out to be simpler than you might think, but since it might contain unpleasant surprises, there are a few comments to be made.
</P>
<P><B>Listing 11-12</B> The fPolyhedronInstance</P>
<!-- CODE //-->
<PRE>
import java.awt.*;
/**
* Class that represents an instance of a polyhedron.
*/
public class fPolyhedronInstance extends Object {
//-- the transformed vertices
protected fArrayOf3dPoints transformedVertices;
//-- the matrix used for transformations
protected fMatrix3d myTransformMatrix;
//-- the polyhedron
protected fPolyhedron thePolyhedron;
//-- position in WCS
protected fPoint3d myPosition;
//-- the angle in WCS
protected fAngle3d myAngle;
//--
protected boolean positionIsDirty,angleIsDirty;
/**
* construct an instance of the supplied polyhedron.
*/
public fPolyhedronInstance(fPolyhedron poly){
//-- the polyhedron that this instance is using
thePolyhedron=poly;
//-- create the vertices to be used for storing transformations
try{
transformedVertices=(fArrayOf3dPoints)thePolyhedron.getVertices().makeClo
ne();
} catch(Exception e){e.printStackTrace();}
myPosition=new fPoint3d();
myAngle=new fAngle3d();
myTransformMatrix=new fMatrix3d();
}
/**
* set the position and angle for this polyhedron instance.
*/
public void setOrientation(fPoint3d pos,fAngle3d agl){
if(myPosition.equals(pos)==false){
//-- if position has changed then mark the matrix
//-- as "dirty" meaning that the transformed points
//-- need to be updated.
myPosition.set(pos);
positionIsDirty=true;
}
if(myAngle.equals(agl)==false){
myAngle.set(agl);
angleIsDirty=true;
}
}
/**
* paint the polyhedron instance.
*/
public void paint(Graphics g,fGenericCamera camera){
if(positionIsDirty || angleIsDirty){
//-- position or angle has changed and the transformed
//-- vertices need to be updated.
myTransformMatrix.makeMCStoWCStransform(myPosition,myAngle);
//-- transform the polyhedron model coordinates to world coords.
myTransformMatrix.transform(thePolyhedron.getVertices(),transformedVertic
es);
//--
positionIsDirty=angleIsDirty=false;
}
//-- project the WCS to the screen with the supplied camera
//-- and then call the paint method of the polyhedron with
//-- the returned 2d array
thePolyhedron.paint(g,camera.project(transformedVertices));
}
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="CENTER"><A NAME="Heading49"></A><FONT COLOR="#000077">The Array transformedVertices</FONT></H4>
<P>This array is used for storing the world coordinates of the polyhedron’s vertices. Since many objects are static, it is a good idea to save this information to eliminate redundant recalculations.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading50"></A><FONT COLOR="#000077">Understanding the paint() Method</FONT></H4>
<P>The paint() method first checks to see if the world coordinates are up-to-date. If an object’s orientation has changed in any way, then the world coordinates will be “dirty,” meaning that they have to be recalculated. This transform only needs to be done on objects that will be rendered and are actually visible to the camera. Notice that a static object never needs to be updated, since the position and angle never change.
</P>
<P>The paint() method is supplied with a camera. Once the world coordinates are up-to-date, the method project() in camera is called with them as an argument. This method will return an array of 2D coordinates that are fed to the polyhedron’s paint() method. The rest is taken care of by the fPolyhedron.</P>
<H3><A NAME="Heading51"></A><FONT COLOR="#000077">Putting It All Together</FONT></H3>
<P>We have now designed a couple of classes that can be used to construct dynamic 3D scenes viewed through arbitrary cameras. This is actually part of a 3D engine, a very small part. It is now time to put these classes to work.
</P>
<P>The applet shown in Figure 11-25 will only scratch the surface of what can be done with these classes. As you will see, it will be a quick and dirty implementation just to show you how we can put them to work. In a complete 3D engine we would use another layer of classes representing scenes and actors.</P>
<P><A NAME="Fig27"></A><A HREF="javascript:displayWindow('images/11-27.jpg',290,371 )"><IMG SRC="images/11-27t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/11-27.jpg',290,371)"><FONT COLOR="#000077"><B>Figure 11-25</B></FONT></A> The rotating cubes applet</P>
<P>The applet will construct nine instances of a polyhedron and a camera. The polyhedrons will be rotated around while the camera moves backward.
</P>
<P>To avoid flickering, we will use an offscreen image to do the rendering and then blit it to the screen. The implementation of the no-flicker applet is not a 3D issue, so it will be presented as a black-box class. Use it in good health. It can be found on the CD.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="427-432.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="435-440.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -