abrash-black-book/61-02.html
2013-12-30 12:21:49 +11:00

104 lines
7.8 KiB
HTML

<HTML>
<HEAD>
<META name=vsisbn content="1576101746">
<META name=vstitle content="Michael Abrash's Graphics Programming Black Book, Special Edition">
<META name=vsauthor content="Michael Abrash">
<META name=vspublisher content="The Coriolis Group">
<META name=vspubdate content="07/01/97">
<META name=vscategory content="Web and Software Development: Game Development,Web and Software Development: Graphics and Multimedia Development">
<TITLE>Michael Abrash's Graphics Programming Black Book Special Edition: Frames of Reference</TITLE>
<!-- HEADER -->
<!-- Empty Reference Subhead -->
<!--ISBN=1576101746//-->
<!--TITLE=Michael Abrash's Graphics Programming Black Book Special Edition//-->
<!--AUTHOR=Michael Abrash//-->
<!--PUBLISHER=The Coriolis Group, Inc.//-->
<!--CHAPTER=61//-->
<!--PAGES=1135-1138//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//--></HEAD><BODY LINK=#0000FF ALINK=#000099 VLINK=#0000FF BGCOLOR=#FFFFFF>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="61-01.html">Previous</A></TD>
<TD><A HREF="index.html">Table of Contents</A></TD>
<TD><A HREF="61-03.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>I&#146;ll be working in a left-handed coordinate system, whereby if you wrap the fingers of your left hand around the z axis with your thumb pointing in the positive z direction, your fingers will curl from the positive x axis to the positive y axis. The positive x axis runs left to right across the screen, the positive y axis runs bottom to top across the screen, and the positive z axis runs into the screen.
</P>
<P>For our purposes, <I>projection</I> is the process of mapping coordinates onto a line or surface. <I>Perspective projection</I> projects 3-D coordinates onto a viewplane, scaling coordinates according to their z distance from the viewpoint in order to provide proper perspective. <I>Objectspace</I> is the coordinate space in which an object is defined, independent of other objects and the world itself. <I>Worldspace</I> is the absolute frame of reference for a 3-D world; all objects&#146; locations and orientations are with respect to worldspace, and this is the frame of reference around which the viewpoint and view direction move. <I>Viewspace</I> is worldspace as seen from the viewpoint, looking in the view direction. <I>Screenspace</I> is viewspace after perspective projection and scaling to the screen.</P>
<P>Finally, <I>transformation</I> is the process of converting points from one coordinate space into another; in our case, that&#146;ll mean rotating and translating (moving) points from objectspace or worldspace to viewspace.</P>
<P>For additional information, you might want to check out Foley &amp van Dam&#146;s <I>Computer Graphics</I> (ISBN 0-201-12110-7), or the chapters in this book dealing with my X-Sharp 3-D graphics library.</P>
<H3><A NAME="Heading5"></A><FONT COLOR="#000077">The Dot Product</FONT></H3>
<P>Now we&#146;re ready to move on to the dot product. Given two vectors <B>U</B> = [u<SUB>1</SUB> u<SUB>2</SUB> u<SUB>3</SUB>] and <B>V</B> = [v<SUB>1</SUB> v<SUB>2</SUB> v<SUB>3</SUB>], their dot product, denoted by the symbol &#149;, is calculated as:</P>
<P ALIGN="LEFT"><P ALIGN="CENTER"><IMG SRC="images/61-02d.jpg"></P>
</P>
<P ALIGN="RIGHT">(eq. 2)</P>
<P>As you can see, the result is a scalar value (a single real-valued number), <I>not</I> another vector.</P>
<P>Now that we know how to calculate a dot product, what does that get us? Not much. The dot product isn&#146;t of much use for graphics until you start thinking of it this way</P>
<P ALIGN="LEFT"><P ALIGN="CENTER"><IMG SRC="images/61-03d.jpg"></P>
</P>
<P ALIGN="RIGHT">(eq. 3)</P>
<P>where q is the angle between the two vectors, and the other two terms are the lengths of the vectors, as shown in Figure 61.1. Although it&#146;s not immediately obvious, equation 3 has a wide variety of applications in 3-D graphics.</P>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Dot Products of Unit Vectors</FONT></H4>
<P>The simplest case of the dot product is when both vectors are <I>unit vectors</I>; that is, when their lengths are both one, as calculated as in Equation 1. In this case, equation 3 simplifies to:</P>
<P ALIGN="LEFT"><P ALIGN="CENTER"><IMG SRC="images/61-04d.jpg"></P>
</P>
<P ALIGN="RIGHT">(eq. 4)</P>
<P>In other words, the dot product of two unit vectors is the cosine of the angle between them.</P>
<P>One obvious use of this is to find angles between unit vectors, in conjunction with an inverse cosine function or lookup table. A more useful application in 3-D graphics lies in lighting surfaces, where the cosine of the angle between incident light and the normal (perpendicular vector) of a surface determines the fraction of the light&#146;s full intensity at which the surface is illuminated, as in</P>
<P ALIGN="RIGHT"><P ALIGN="CENTER"><IMG SRC="images/61-05d.jpg"></P>
</P>
<P ALIGN="RIGHT">(eq. 5)</P>
<P><A NAME="Fig1"><!-- </A><A HREF="javascript:displayWindow('images/61-01.jpg',406,234 )"> --><IMG SRC="images/61-01.jpg"><BR><!-- </A>
<BR><A HREF="javascript:displayWindow('images/61-01.jpg',406,234)"> --><FONT COLOR="#000077"><B>Figure 61.1</B></FONT></A>&nbsp;&nbsp;<I>The dot product.</I>
</P>
<P>where <B>I</B><SUB>s</SUB> is the intensity of illumination of the surface, <B>I</B><SUB>l</SUB> is the intensity of the light, and q is the angle between <B>-D</B><SUB>l</SUB> (where <B>D</B><SUB>l</SUB> is the light direction vector) and the surface normal. If the inverse light vector and the surface normal are both unit vectors, then this calculation can be performed with four multiplies and three additions&#151;and no explicit cosine calculations&#151;as</P>
<P ALIGN="LEFT"><P ALIGN="CENTER"><IMG SRC="images/61-06d.jpg"></P>
</P>
<P ALIGN="RIGHT">(eq. 6)</P>
<P>where <B>N</B><SUB>s</SUB> is the surface unit normal and <B>D</B><SUB>l</SUB> is the light unit direction vector, as shown in Figure 61.2.</P>
<H3><A NAME="Heading7"></A><FONT COLOR="#000077">Cross Products and the Generation of Polygon Normals</FONT></H3>
<P>One question equation 6 begs is where the surface unit normal comes from. One approach is to store the end of a surface normal as an extra data point with each polygon (with the start being some point that&#146;s already in the polygon), and transform it along with the rest of the points. This has the advantage that if the normal starts out as a unit normal, it will end up that way too, if only rotations and translations (but not scaling and shears) are performed.
</P>
<P>The problem with having an explicit normal is that it will remain a normal&#151;that is, perpendicular to the surface&#151;only through viewspace. Rotation, translation, and scaling preserve right angles, which is why normals are still normals in viewspace, but perspective projection does not preserve angles, so vectors that were surface normals in viewspace are no longer normals in screenspace.</P>
<P><A NAME="Fig2"><!-- </A><A HREF="javascript:displayWindow('images/61-02.jpg',406,158 )"> --><IMG SRC="images/61-02.jpg"><BR><!-- </A>
<BR><A HREF="javascript:displayWindow('images/61-02.jpg',406,158)"> --><FONT COLOR="#000077"><B>Figure 61.2</B></FONT></A>&nbsp;&nbsp;<I>The dot product as used in calculating lighting intensity.</I>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="61-01.html">Previous</A></TD>
<TD><A HREF="index.html">Table of Contents</A></TD>
<TD><A HREF="61-03.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade>
<div align="center">
<font face="Verdana,sans-serif" size="1">Graphics Programming Black Book &copy; 2001 Michael Abrash</font>
</div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed -->
<!-- reference_subfoot = footer -->
<!-- reference_footer = subfoot -->
<!-- BEGIN SUB FOOTER -->
</BODY>
</HTML>
<!-- END FOOTER -->