81 lines
8.5 KiB
HTML
81 lines
8.5 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: 3-D Shading</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=54//-->
|
|
<!--PAGES=1023-1025//-->
|
|
<!--UNASSIGNED1//-->
|
|
<!--UNASSIGNED2//--></HEAD><BODY LINK=#0000FF ALINK=#000099 VLINK=#0000FF BGCOLOR=#FFFFFF>
|
|
|
|
<CENTER>
|
|
<TABLE BORDER>
|
|
<TR>
|
|
<TD><A HREF="54-02.html">Previous</A></TD>
|
|
<TD><A HREF="index.html">Table of Contents</A></TD>
|
|
<TD><A HREF="54-04.html">Next</A></TD>
|
|
</TR>
|
|
</TABLE>
|
|
</CENTER>
|
|
<P><BR></P>
|
|
<H4 ALIGN="LEFT"><A NAME="Heading4"></A><FONT COLOR="#000077">Shading</FONT></H4>
|
|
<P>So far, the polygons out of which our animated objects have been built have had colors of fixed intensities. For example, a face of a cube might be blue, or green, or white, but whatever color it is, that color never brightens or dims. Fixed colors are easy to implement, but they don’t make for very realistic animation. In the real world, the intensity of the color of a surface varies depending on how brightly it is illuminated. The ability to simulate the illumination of a surface, or shading, is the next feature we’ll add to X-Sharp.
|
|
</P>
|
|
<P>The overall shading of an object is the sum of several types of shading components. <I>Ambient shading</I> is illumination by what you might think of as background light, light that’s coming from all directions; all surfaces are equally illuminated by ambient light, regardless of their orientation. <I>Directed lighting</I>, producing diffuse shading, is illumination from one or more specific light sources. Directed light has a specific direction, and the angle at which it strikes a surface determines how brightly it lights that surface. <I>Specular reflection</I> is the tendency of a surface to reflect light in a mirrorlike fashion. There are other sorts of shading components, including transparency and atmospheric effects, but the ambient and diffuse-shading components are all we’re going to deal with in X-Sharp.</P>
|
|
<H4 ALIGN="CENTER"><A NAME="Heading5"></A><FONT COLOR="#000077">Ambient Shading</FONT></H4>
|
|
<P>The basic model for both ambient and diffuse shading is a simple one. Each surface has a reflectivity between 0 and 1, where 0 means all light is absorbed and 1 means all light is reflected. A certain amount of light energy strikes each surface. The energy (intensity) of the light is expressed such that if light of intensity 1 strikes a surface with reflectivity 1, then the brightest possible shading is displayed for that surface. Complicating this somewhat is the need to support color; we do this by separating reflectance and shading into three components each—red, green, and blue—and calculating the shading for each color component separately for each surface.
|
|
</P>
|
|
<P>Given an ambient-light red intensity of IA<SUB><SMALL>red</SMALL></SUB> and a surface red reflectance R<SUB><SMALL>red</SMALL></SUB>, the displayed red ambient shading for that surface, as a fraction of the maximum red intensity, is simply min(IA<SUB><SMALL>red</SMALL></SUB>× R<SUB><SMALL>red</SMALL></SUB>, 1). The green and blue color components are handled similarly. That’s really all there is to ambient shading, although of course we must design some way to map displayed color components into the available palette of colors; I’ll do that in the next chapter. Ambient shading isn’t the whole shading picture, though. In fact, scenes tend to look pretty bland without diffuse shading.</P>
|
|
<H4 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">Diffuse Shading</FONT></H4>
|
|
<P>Diffuse shading is more complicated than ambient shading, because the effective intensity of directed light falling on a surface depends on the angle at which it strikes the surface. According to Lambert’s law, the light energy from a directed light source striking a surface is proportional to the cosine of the angle at which it strikes the surface, with the angle measured relative to a vector perpendicular to the polygon (a polygon normal), as shown in Figure 54.1. If the red intensity of directed light is ID<SUB><SMALL>red</SMALL></SUB>, the red reflectance of the surface is R<SUB><SMALL>red</SMALL></SUB>, and the angle between the incoming directed light and the surface’s normal is theta, then the displayed red diffuse shading for that surface, as a fraction of the largest possible red intensity, is min (ID<SUB><SMALL>red</SMALL></SUB>×R<SUB><SMALL>red</SMALL></SUB>×cos(θ), 1).</P>
|
|
<P>That’s easy enough to calculate—but seemingly slow. Determining the cosine of an angle can be sped up with a table lookup, but there’s also the task of figuring out the angle, and, all in all, it doesn’t seem that diffuse shading is going to be speedy enough for our purposes. Consider this, however: According to the properties of the dot product (denoted by the operator “•”, as shown in Figure 54.2), cos(q)=(v•w)/ |v| × |w| ), where v and w are vectors, q is the angle between v and w, and |v| is the length of v. Suppose, now, that v and w are unit vectors; that is, vectors exactly one unit long. Then the above equation reduces to cos(q)=v•w. In other words, we can calculate the cosine between N, the unit-normal vector (one-unit-long perpendicular vector) of a polygon, and L', the reverse of a unit vector describing the direction of a light source, with just three multiplies and two adds. (I’ll explain why the light-direction vector must be reversed later.) Once we have that, we can easily calculate the red diffuse shading from a directed light source as min(ID<SUB><SMALL>red</SMALL></SUB>×R<SUB><SMALL>red</SMALL></SUB>×(L'• N), 1) and likewise for the green and blue color components.</P>
|
|
<P><A NAME="Fig1"><!-- </A><A HREF="javascript:displayWindow('images/54-01.jpg',406,116 )"> --><IMG SRC="images/54-01.jpg"><BR><!-- </A>
|
|
<BR><A HREF="javascript:displayWindow('images/54-01.jpg',406,116)"> --><FONT COLOR="#000077"><B>Figure 54.1</B></FONT></A> <I>Illumination by a directed light source</I>
|
|
</P>
|
|
<P><A NAME="Fig2"><!-- </A><A HREF="javascript:displayWindow('images/54-02.jpg',409,86 )"> --><IMG SRC="images/54-02.jpg"><BR><!-- </A>
|
|
<BR><A HREF="javascript:displayWindow('images/54-02.jpg',409,86)"> --><FONT COLOR="#000077"><B>Figure 54.2</B></FONT></A> <I>The dot product of two vectors.</I>
|
|
</P>
|
|
<P>The overall red shading for each polygon can be calculated by summing the ambient-shading red component with the diffuse-shading component from each light source, as in min((IA<SUB><SMALL>red</SMALL></SUB>×R<SUB><SMALL>red</SMALL></SUB>) + (ID<SUB><SMALL>red0</SMALL></SUB>×R<SUB><SMALL>red</SMALL></SUB>×(L<SUB><SMALL>0</SMALL></SUB>' • N)) + (ID<SUB><SMALL>red1</SMALL></SUB>×R<SUB><SMALL>red</SMALL></SUB>×(L<SUB><SMALL>1</SMALL></SUB>' • N)) +..., 1) where ID<SUB><SMALL>red0</SMALL></SUB> and L<SUB><SMALL>0</SMALL></SUB>' are the red intensity and the reversed unit-direction vector, respectively, for spotlight 0. Listing 54.2 shows the X-Sharp module DRAWPOBJ.C, which performs ambient and diffuse shading. Toward the end, you will find the code that performs shading exactly as described by the above equation, first calculating the ambient red, green, and blue shadings, then summing that with the diffuse red, green, and blue shadings generated by each directed light source.</P><P><BR></P>
|
|
<CENTER>
|
|
<TABLE BORDER>
|
|
<TR>
|
|
<TD><A HREF="54-02.html">Previous</A></TD>
|
|
<TD><A HREF="index.html">Table of Contents</A></TD>
|
|
<TD><A HREF="54-04.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 © 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 -->
|
|
|
|
|