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

155 lines
7.9 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: Bit-Plane Animation</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=43//-->
<!--PAGES=799-801//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//--></HEAD><BODY LINK=#0000FF ALINK=#000099 VLINK=#0000FF BGCOLOR=#FFFFFF>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="43-01.html">Previous</A></TD>
<TD><A HREF="index.html">Table of Contents</A></TD>
<TD><A HREF="43-03.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
</P>
<H4 ALIGN="LEFT"><A NAME="Heading4"></A><FONT COLOR="#000077">Stacking the Palette Registers</FONT></H4>
<P>Suppose that instead of viewing the four bits per pixel coming out of display memory as selecting one of sixteen colors,we view those bits as selecting one of <I>four</I> colors. If the bit from plane 0 is 1, that would select color 0 (say, red). The bit from plane 1 would select color 1 (say, green), the bit from plane 2 would select color 2 (say, blue), and the bit from plane 3 would select color 3 (say, white). Whenever more than 1 bit is 1, the 1 bit from the lowest-numbered plane would determine the color, and 1 bits from all other planes would be ignored. Finally, the absence of any 1 bits at all would select the background color (say, black).</P>
<P>That would give us four colors and the background color. It would also give us nifty image precedence, with images in plane 0 appearing to be in front of images from the other planes, images in plane 1 appearing to be in front of images from planes 2 and 3, and so on. It would even give us transparency, where rearward images would show through holes within and around the edges of images in forward planes. Finally, and most importantly, it would meet all the criteria needed to allow us to store each image in a single plane, letting us manipulate the images very quickly and with no reprogramming of the VGA&#146;s hardware other than the few <B>OUT</B> instructions required to select the plane we want to write to.</P>
<P>Which leaves only one question: How do we get this magical pixel-precedence scheme to work? As it turns out, all we need to do is reprogram the palette registers so that the 1 bit from the plane with the highest precedence determines the color. The palette RAM settings for the colors described above are summarized in Table 43.1.</P>
<P>Remember that the 4-bit values coming from display memory select which palette register provides the actual pixel color. Given that, it&#146;s easy to see that the rightmost 1-bit of the four bits coming from display memory in Table 43.1 selects the pixel color. If the bit from plane 0 is 1, then the color is red, no matter what the other bits are, as shown in Figure 43.4. If the bit from plane 0 is 0, then if the bit from plane 1 is 1 the color is green, and so on for planes 2 and 3. In other words, with the palette register settings we instantly have exactly what we want, which is an approach that keeps images in one plane from interfering with images in other planes while providing precedence and transparency.</P>
<TABLE width="100%"><TR>
<TH COLSPAN="3"><HR>
<TR>
<TH VALIGN="BOTTOM" ALIGN="LEFT" WIDTH="15%">Bit Value For Plane<BR>3 2 1 0
<TH VALIGN="BOTTOM" ALIGN="LEFT" WIDTH="25%">Palette Register
<TH VALIGN="BOTTOM" ALIGN="LEFT" WIDTH="60%">Register setting
<TR>
<TH COLSPAN="3"><HR>
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">0 0 0 0
<TD VALIGN="TOP" ALIGN="LEFT">0
<TD VALIGN="TOP" ALIGN="LEFT">00H (black)
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">0 0 0 1
<TD VALIGN="TOP" ALIGN="LEFT">1
<TD VALIGN="TOP" ALIGN="LEFT">3CH (red)
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">0 0 1 0
<TD VALIGN="TOP" ALIGN="LEFT">2
<TD VALIGN="TOP" ALIGN="LEFT">3AH (green)
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">0 0 1 1
<TD VALIGN="TOP" ALIGN="LEFT">3
<TD VALIGN="TOP" ALIGN="LEFT">3CH (red)
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">0 1 0 0
<TD VALIGN="TOP" ALIGN="LEFT">4
<TD VALIGN="TOP" ALIGN="LEFT">39H (blue)
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">0 1 0 1
<TD VALIGN="TOP" ALIGN="LEFT">5
<TD VALIGN="TOP" ALIGN="LEFT">3CH (red)
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">0 1 1 0
<TD VALIGN="TOP" ALIGN="LEFT">6
<TD VALIGN="TOP" ALIGN="LEFT">3AH (green)
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">0 1 1 1
<TD VALIGN="TOP" ALIGN="LEFT">7
<TD VALIGN="TOP" ALIGN="LEFT">3CH (red)
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">1 0 0 0
<TD VALIGN="TOP" ALIGN="LEFT">8
<TD VALIGN="TOP" ALIGN="LEFT">3FH (white)
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">1 0 0 1
<TD VALIGN="TOP" ALIGN="LEFT">9
<TD VALIGN="TOP" ALIGN="LEFT">3CH (red)
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">1 0 1 0
<TD VALIGN="TOP" ALIGN="LEFT">10
<TD VALIGN="TOP" ALIGN="LEFT">3AH (green)
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">1 0 1 1
<TD VALIGN="TOP" ALIGN="LEFT">11
<TD VALIGN="TOP" ALIGN="LEFT">3CH (red)
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">1 1 0 0
<TD VALIGN="TOP" ALIGN="LEFT">12
<TD VALIGN="TOP" ALIGN="LEFT">39H (blue)
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">1 1 0 1
<TD VALIGN="TOP" ALIGN="LEFT">13
<TD VALIGN="TOP" ALIGN="LEFT">3CH (red)
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">1 1 1 0
<TD VALIGN="TOP" ALIGN="LEFT">14
<TD VALIGN="TOP" ALIGN="LEFT">3AH (green)
<TR>
<TD VALIGN="TOP" ALIGN="LEFT">1 1 1 1
<TD VALIGN="TOP" ALIGN="LEFT">15
<TD VALIGN="TOP" ALIGN="LEFT">3CH (red)
<TR>
<TH COLSPAN="3"><HR>
<TR>
<TH CAPTION ALIGN="LEFT" COLSPAN="3">Table 43.1 Palette RAM settings for bit-plane animation.
<TR>
<TH COLSPAN="3"><HR>
</TABLE>
<P><A NAME="Fig4"><!-- </A><A HREF="javascript:displayWindow('images/43-04.jpg',405,198 )"> --><IMG SRC="images/43-04.jpg"><BR><!-- </A>
<BR><A HREF="javascript:displayWindow('images/43-04.jpg',405,198)"> --><FONT COLOR="#000077"><B>Figure 43.4</B></FONT></A>&nbsp;&nbsp;<I>How pixel precedence works.</I>
</P>
<P>Seems almost too easy, doesn&#146;t it? Nonetheless, it works beautifully, as we&#146;ll see very shortly. First, though, I&#146;d like to point out that there&#146;s nothing sacred about plane 0 having precedence. We could rearrange the palette register settings so that any plane had the highest precedence, followed by the other planes in any order. I&#146;ve chosen to make plane 0 the highest precedence only because it seems simplest to think of plane 0 as appearing in front of plane 1, which is in front of plane 2, which is in front of plane 3.
</P>
<H3><A NAME="Heading5"></A><FONT COLOR="#000077">Bit-Plane Animation in Action</FONT></H3>
<P>Without further ado, Listing 43.1 shows bit-plane animation in action. Listing 43.1 animates 13 rather large images (each 32 pixels on a side) over a complex background at a good clip <I>even on a primordial 8088-based PC</I>. Five of the images move very quickly, while the other 8 bounce back and forth at a steady pace.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="43-01.html">Previous</A></TD>
<TD><A HREF="index.html">Table of Contents</A></TD>
<TD><A HREF="43-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 -->