<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nklein software &#187; opengl</title>
	<atom:link href="http://nklein.com/tags/opengl/feed/" rel="self" type="application/rss+xml" />
	<link>http://nklein.com</link>
	<description>software development and consulting</description>
	<lastBuildDate>Tue, 22 May 2012 03:48:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>NeHe Tutorial 06: Textured solids</title>
		<link>http://nklein.com/2010/06/nehe-tutorial-06-textured-solids/</link>
		<comments>http://nklein.com/2010/06/nehe-tutorial-06-textured-solids/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 22:57:52 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[NeHe Tutorials]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[nehe]]></category>
		<category><![CDATA[opengl]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1447</guid>
		<description><![CDATA[Introduction The texture Loading the texture Initializing our texture Rotation state Preparing the tick function Drawing textured cubes Drawing the cube Introduction In the previous tutorial, we drew a rotating pyramid and a rotating cube. The next NeHe tutorial renders a textured cube rotating at different speeds around each axis. Again, we&#8217;re going to start [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://nklein.com/wp-content/uploads/2010/06/tut06.png"><img src="http://nklein.com/wp-content/uploads/2010/06/tut06-300x241.png" alt="" title="tut06.png" width="300" height="241" class="alignright size-medium wp-image-1452" /></a></p>
<ul>
<li><a href="#tut06-toc1">Introduction</a></li>
<li><a href="#tut06-toc2">The texture</a>
<ul>
<li><a href="#tut06-toc3">Loading the texture</a></li>
<li><a href="#tut06-toc4">Initializing our texture</a></li>
</ul>
</li>
<li><a href="#tut06-toc5">Rotation state</a></li>
<li><a href="#tut06-toc6">Preparing the <code class="codecolorer text default"><span class="text">tick</span></code> function</a></li>
<li><a href="#tut06-toc7">Drawing textured cubes</a>
<ul>
<li><a href="#tut06-toc8">Drawing the cube</a></li>
</ul>
</li>
</ul>
<h3><a name="tut06-toc1">Introduction</a></h3>
<p>In the <a href="http://nklein.com/2010/06/nehe-tutorial-05-solids/">previous tutorial</a>, we drew a rotating pyramid and a rotating cube. The <a href="http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=06">next NeHe tutorial</a> renders a textured cube rotating at different speeds around each axis. </p>
<p>Again, we&#8217;re going to start with our simple-tutorial base. <a name="*.lisp"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; *.lisp</span>
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;simple-tutorial.lisp&quot;</span><span style="color: #66cc66;">&gt;</span></pre></div>
<p></a></p>
<p>Here is the resulting <a href="http://nklein.com/wp-content/uploads/2010/06/tut06.lisp">tut06.lisp</a>.</p>
<p><a name="window-title"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; window title</span>
<span style="color: #ff0000;">&quot;tut06: UV-textured objects&quot;</span></pre></div>
<p></a></p>
<h3><a name="tut06-toc2">The texture</a></h3>
<p>We&#8217;re going to use a slot in our window class to store the texture.  Note: it might be nice some day to break the cube out into its own class which could store its own position, rotation, and texture.  For now though, we&#8217;re just going to keep piling stuff into our window class. <a name="extra-slots"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra slots</span>
<span style="color: #66cc66;">&#40;</span>texture-id <span style="color: #66cc66;">:</span><span style="color: #555;">initform</span> <span style="color: #b1b100;">nil</span> <span style="color: #66cc66;">:</span><span style="color: #555;">accessor</span> texture-id<span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<h4><a name="tut06-toc3">Loading the texture</a></h4>
<p>To load the texture, I&#8217;m going to use the CL-PNG wrapper around the PNG library.  So, let&#8217;s get it loaded. <a name="extra-decls"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra decls</span>
<span style="color: #66cc66;">&#40;</span>asdf<span style="color: #66cc66;">:</span><span style="color: #555;">load-system</span> <span style="color: #66cc66;">:</span><span style="color: #555;">png</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>Then, I&#8217;m going to need some function that reads in a PNG and creates an OpenGL texture from it.  I&#8217;m going to make my function take a filename for the PNG image and an optional texture id to use for the texture.  (If you don&#8217;t pass in a texture id, one is created using <code class="codecolorer text default"><span class="text">gl:gen-textures</span></code>.  The argument to <code class="codecolorer text default"><span class="text">gl:gen-textures</span></code> tells OpenGL how many textures you want to reserve.  You can call <code class="codecolorer text default"><span class="text">gl:gen-textures</span></code> multiple times.  I&#8217;m not sure what benefit, if any, you get from allocating several of them simultaneously.) </p>
<p>So, we&#8217;re going to open the file and decode the PNG.  Then, we&#8217;re going to try to turn it into a texture.  If we succeed, then we&#8217;re going to  <a name="extra-decls-1"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra decls (cont.)</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> load-png <span style="color: #66cc66;">&#40;</span> filename <span style="color: #66cc66;">&amp;</span>optional <span style="color: #66cc66;">&#40;</span>texture-id <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">gen-textures</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                                                 texture-id-p<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>flet <span style="color: #66cc66;">&#40;</span>#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;load-png: load-and-decode image&quot;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>handler-<span style="color: #b1b100;">case</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>png <span style="color: #66cc66;">&#40;</span>load-and-decode filename<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span>assert png<span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; make sure we got the png</span>
          #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;load-png: turn png into a texture&quot;</span><span style="color: #66cc66;">&gt;</span>
          texture-id<span style="color: #66cc66;">&#41;</span>           <span style="color: #808080; font-style: italic;">; return the texture-id on success</span>
&nbsp;
        #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;load-png: handle errors&quot;</span><span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>To load the image, we&#8217;re going to open the file and decode it. We have to make sure to open the file for binary input. <a name="load-png:-load-and-decode-image"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; load-png: load-and-decode image</span>
<span style="color: #66cc66;">&#40;</span>load-and-decode <span style="color: #66cc66;">&#40;</span>filename<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>with-open-file <span style="color: #66cc66;">&#40;</span>in filename
                      <span style="color: #66cc66;">:</span><span style="color: #555;">element-type</span> '<span style="color: #66cc66;">&#40;</span>unsigned-byte <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>png<span style="color: #66cc66;">:</span><span style="color: #555;">decode</span> in<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>To turn the PNG into a texture, we first have to make sure that OpenGL knows that we&#8217;re going to start tweaking this particular texture.  To do that, we use <code class="codecolorer text default"><span class="text">bind-texture</span></code> and tell it we&#8217;re working with a two-dimensional texture here.  (OpenGL supports 1-, 2-, and 3-dimensional textures.) <a name="load-png:-turn-png-into-a-texture"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; load-png: turn png into a texture</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">bind-texture</span> <span style="color: #66cc66;">:</span><span style="color: #555;">texture-2d</span> texture-id<span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>Now, we&#8217;re going to need to hand OpenGL our texture data. The CL-PNG library keeps our data in a three-dimensional array (width, height, channels).  We need to get this down to a one-dimensional array for OpenGL.  Fortunately, we can take advantage of the fact that Common Lisp arrays are stored contiguously.  We&#8217;ll create an array called <code class="codecolorer text default"><span class="text">data</span></code> that is a one-dimensional view into our three-dimensional array and let OpenGL copy from it. <a name="load-png:-turn-png-into-a-texture-1"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; load-png: turn png into a texture (cont.)</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>ww <span style="color: #66cc66;">&#40;</span>png<span style="color: #66cc66;">:</span><span style="color: #555;">image-width</span> png<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>hh <span style="color: #66cc66;">&#40;</span>png<span style="color: #66cc66;">:</span><span style="color: #555;">image-height</span> png<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>cc <span style="color: #66cc66;">&#40;</span>png<span style="color: #66cc66;">:</span><span style="color: #555;">image-channels</span> png<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>data <span style="color: #66cc66;">&#40;</span>make-<span style="color: #b1b100;">array</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>* ww hh cc<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                          <span style="color: #66cc66;">:</span><span style="color: #555;">element-type</span> <span style="color: #66cc66;">&#40;</span>array-element-type png<span style="color: #66cc66;">&#41;</span>
                          <span style="color: #66cc66;">:</span><span style="color: #555;">displaced-to</span> png<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;load-png: copy data to texture&quot;</span><span style="color: #66cc66;">&gt;</span>
    #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;load-png: set up texture filters&quot;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>To copy the data into the texture, we need to tell OpenGL how the data is laid out. <a name="load-png:-copy-data-to-texture"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; load-png: copy data to texture</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>level-of-detail <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>internal-format #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;load-png: determine internal-format&quot;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>border <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>format #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;load-png: determine format&quot;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>data-type #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;load-png: determine data-type&quot;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">tex-image-2d</span> <span style="color: #66cc66;">:</span><span style="color: #555;">texture-2d</span>
                   level-of-detail
                   internal-format
                   ww
                   hh
                   border
                   format
                   data-type
                   data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a><br />
 The <code class="codecolorer text default"><span class="text">level-of-detail</span></code> is used if we&#8217;re going to manually specify what this image looks like at different resolutions.  For our purposes in this tutorial, we&#8217;re just going to let OpenGL handle all of the scaling for our texture so we&#8217;ll stick with the default level of detail. </p>
<p>The <code class="codecolorer text default"><span class="text">internal-format</span></code> tells OpenGL what type of texture this is going to be.  We&#8217;re going to use the number of bits per sample and the number image channels to figure out what format this texture should be inside OpenGL. <a name="load-png:-determine-internal-format"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; load-png: determine internal-format</span>
<span style="color: #66cc66;">&#40;</span>ecase <span style="color: #66cc66;">&#40;</span>png<span style="color: #66cc66;">:</span><span style="color: #555;">image-bit-depth</span> png<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span>  <span style="color: #66cc66;">&#40;</span>ecase cc
        <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">:</span><span style="color: #555;">luminance8</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">:</span><span style="color: #555;">luminance8-alpha8</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">:</span><span style="color: #555;">rgb8</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">:</span><span style="color: #555;">rgba8</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span> <span style="color: #66cc66;">&#40;</span>ecase cc
        <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">:</span><span style="color: #555;">luminance16</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">:</span><span style="color: #555;">luminance16-alpha16</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">:</span><span style="color: #555;">rgb16</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">:</span><span style="color: #555;">rgba16</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>The <code class="codecolorer text default"><span class="text">border</span></code> parameter can be either zero or one.  If it is zero, then the image width and height must be a power of two.  If it is one, then the image width and height must be two plus a power of two.  For our purposes, we&#8217;re just going to assume that the image is a power of two in width and height. </p>
<p>The <code class="codecolorer text default"><span class="text">format</span></code> parameter declares what kind of data we have in our array.  We&#8217;re going to use the number of image channels to come up with the right value here.  With the internal format, we were able to blend both the size of the samples and the meaning of the samples into one parameter.  For our input data, we give both <code class="codecolorer text default"><span class="text">format</span></code> and <code class="codecolorer text default"><span class="text">data-type</span></code>. <a name="load-png:-determine-format"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; load-png: determine format</span>
<span style="color: #66cc66;">&#40;</span>ecase cc
  <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">:</span><span style="color: #555;">luminance</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">:</span><span style="color: #555;">luminance-alpha</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">:</span><span style="color: #555;">rgb</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">:</span><span style="color: #555;">rgba</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>For the data type, we work from the number of bits per sample. <a name="load-png:-determine-data-type"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; load-png: determine data-type</span>
<span style="color: #66cc66;">&#40;</span>ecase <span style="color: #66cc66;">&#40;</span>png<span style="color: #66cc66;">:</span><span style="color: #555;">image-bit-depth</span> png<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span>  <span style="color: #66cc66;">:</span><span style="color: #555;">unsigned-byte</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span> <span style="color: #66cc66;">:</span><span style="color: #555;">unsigned-short</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>After we have the texture data loaded, we tell OpenGL how to scale our texture when it needs it in a smaller or larger size.  We are going to tell it to use linear filtering whether it needs to minimize or magnify our texture. <a name="load-png:-set-up-texture-filters"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; load-png: set up texture filters</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">tex-parameter</span> <span style="color: #66cc66;">:</span><span style="color: #555;">texture-2d</span> <span style="color: #66cc66;">:</span><span style="color: #555;">texture-min-filter</span> <span style="color: #66cc66;">:</span><span style="color: #555;">linear</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">tex-parameter</span> <span style="color: #66cc66;">:</span><span style="color: #555;">texture-2d</span> <span style="color: #66cc66;">:</span><span style="color: #555;">texture-mag-filter</span> <span style="color: #66cc66;">:</span><span style="color: #555;">linear</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>That wraps up making the texture.  If we ran into an error somewhere along the line of turning the png into a texture, we&#8217;re going to delete the texture if we allocated it and return nil. <a name="load-png:-handle-errors"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; load-png: handle errors</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">error</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>unless texture-id-p
         <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">delete-textures</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> texture-id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<h4><a name="tut06-toc4">Initializing our texture</a></h4>
<p>To initialize our texture, we&#8217;re going to load it with the function above.  Assuming that it loaded okay, we&#8217;re going to go ahead and enable texturing. <a name="display-window-extra-code"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; display-window extra code</span>
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;display-window: make sure texture is loaded&quot;</span><span style="color: #66cc66;">&gt;</span>
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;display-window: enable texturing&quot;</span><span style="color: #66cc66;">&gt;</span></pre></div>
<p></a></p>
<p><a name="display-window:-make-sure-texture-is-loaded"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; display-window: make sure texture is loaded</span>
<span style="color: #66cc66;">&#40;</span>unless <span style="color: #66cc66;">&#40;</span>texture-id win<span style="color: #66cc66;">&#41;</span>     <span style="color: #808080; font-style: italic;">; load texture if needed</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span>texture-id win<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>load-png #P<span style="color: #ff0000;">&quot;./images/cube-texture.png&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p><a name="display-window:-enable-texturing"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; display-window: enable texturing</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span>texture-id win<span style="color: #66cc66;">&#41;</span>       <span style="color: #808080; font-style: italic;">; enable texturing if we have one</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">enable</span> <span style="color: #66cc66;">:</span><span style="color: #555;">texture-2d</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<h3><a name="tut06-toc5">Rotation state</a></h3>
<p>For this tutorial, our rotation state is going to consist of three angles, one for the rotation around the x-axis, one for the rotation around the y-axis, and one for the rotation around the z-axis.  Each of these will initially be zero. <a name="extra-decls-2"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra decls (cont.)</span>
<span style="color: #66cc66;">&#40;</span>defclass rotation-state <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>x-angle <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">x-angle</span> <span style="color: #66cc66;">:</span><span style="color: #555;">reader</span> x-angle<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>y-angle <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">y-angle</span> <span style="color: #66cc66;">:</span><span style="color: #555;">reader</span> y-angle<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>z-angle <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">z-angle</span> <span style="color: #66cc66;">:</span><span style="color: #555;">reader</span> z-angle<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">default-initargs</span> <span style="color: #66cc66;">:</span><span style="color: #555;">x-angle</span> <span style="color: #cc66cc;">0.0</span>
                     <span style="color: #66cc66;">:</span><span style="color: #555;">y-angle</span> <span style="color: #cc66cc;">0.0</span>
                     <span style="color: #66cc66;">:</span><span style="color: #555;">z-angle</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>We&#8217;re also going to add the rotation state into our window class. <a name="extra-slots-1"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra slots (cont.)</span>
<span style="color: #66cc66;">&#40;</span>rotation-state <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">rotation-state</span> <span style="color: #66cc66;">:</span><span style="color: #555;">accessor</span> rotation-state<span style="color: #66cc66;">&#41;</span></pre></div>
<p></a><br />
 And, make sure we initialize our rotation state. <a name="extra-initargs"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra initargs</span>
<span style="color: #66cc66;">:</span><span style="color: #555;">rotation-state</span> <span style="color: #66cc66;">&#40;</span>make-instance 'rotation-state<span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<h3><a name="tut06-toc6">Preparing the <code class="codecolorer text default"><span class="text">tick</span></code> function</a></h3>
<p>Again, we&#8217;re going to try to stay near 60 frames per second. Recall that the tick interval is specified in milliseconds per tick. <a name="extra-initargs-1"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra initargs (cont.)</span>
<span style="color: #66cc66;">:</span><span style="color: #555;">tick-interval</span> <span style="color: #66cc66;">&#40;</span>round <span style="color: #cc66cc;">1000</span> <span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">; milliseconds per tick</span></pre></div>
<p></a></p>
<p>We&#8217;re going to use a different rotation speed for each axis.  We&#8217;ll update all three at once in the <code class="codecolorer text default"><span class="text">tick</span></code> method. <a name="extra-code"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra code</span>
<span style="color: #66cc66;">&#40;</span>defmethod glut<span style="color: #66cc66;">:</span><span style="color: #555;">tick</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>win my-window<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                                <span style="color: #808080; font-style: italic;">; retrieve the current rotation</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>cur <span style="color: #66cc66;">&#40;</span>rotation-state win<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                                <span style="color: #808080; font-style: italic;">; retrieve the current angles</span>
         <span style="color: #66cc66;">&#40;</span>x-angle <span style="color: #66cc66;">&#40;</span>x-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>y-angle <span style="color: #66cc66;">&#40;</span>y-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>z-angle <span style="color: #66cc66;">&#40;</span>z-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span>rotation-state win<span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">; replace the rotation state</span>
          <span style="color: #66cc66;">&#40;</span>make-instance 'rotation-state
                         <span style="color: #66cc66;">:</span><span style="color: #555;">x-angle</span> <span style="color: #66cc66;">&#40;</span>+ x-angle <span style="color: #cc66cc;">0.3</span><span style="color: #66cc66;">&#41;</span>
                         <span style="color: #66cc66;">:</span><span style="color: #555;">y-angle</span> <span style="color: #66cc66;">&#40;</span>+ y-angle <span style="color: #cc66cc;">0.2</span><span style="color: #66cc66;">&#41;</span>
                         <span style="color: #66cc66;">:</span><span style="color: #555;">z-angle</span> <span style="color: #66cc66;">&#40;</span>+ z-angle <span style="color: #cc66cc;">0.4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span>glut<span style="color: #66cc66;">:</span><span style="color: #555;">post-redisplay</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; tell GLUT to redraw</span></pre></div>
<p></a></p>
<h3><a name="tut06-toc7">Drawing textured cubes</a></h3>
<p>In the base code, we already cleared the color buffer and the depth buffer and reset the modelview matrix.  Now, retrieve our rotation angles, move back into the screen, rotate through each of our angles, and draw the cube with textures. <a name="display-extra-code"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; display extra code</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>cur <span style="color: #66cc66;">&#40;</span>rotation-state win<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>x-angle <span style="color: #66cc66;">&#40;</span>x-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>y-angle <span style="color: #66cc66;">&#40;</span>y-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>z-angle <span style="color: #66cc66;">&#40;</span>z-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">translate</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> -<span style="color: #cc66cc;">5.0</span><span style="color: #66cc66;">&#41;</span>   <span style="color: #808080; font-style: italic;">; move and rotate</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">rotate</span> x-angle <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">rotate</span> y-angle <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">rotate</span> z-angle <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;draw textured-cube&quot;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span>       <span style="color: #808080; font-style: italic;">; draw the cube</span></pre></div>
<p></a></p>
<h4><a name="tut06-toc8">Drawing the cube</a></h4>
<p>To draw the cube, we first want to make sure that we have the right texture selected.  Then we are going to draw each face of the cube as a textured quad. <a name="draw-textured-cube"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw textured-cube</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span>texture-id win<span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; bind the texture if we have it</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">bind-texture</span> <span style="color: #66cc66;">:</span><span style="color: #555;">texture-2d</span> <span style="color: #66cc66;">&#40;</span>texture-id win<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">with-primitives</span> <span style="color: #66cc66;">:</span><span style="color: #555;">quads</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;draw textured cube faces&quot;</span><span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>The texured cube faces are going to be like our colored faces. Before each vertex though, instead of specifying a color, we&#8217;re going to specify the texture coordinates for that vertex.  The coordinates in the texture range from <code class="codecolorer text default"><span class="text">0.0</span></code> to <code class="codecolorer text default"><span class="text">1.0</span></code>. The point (0,0) is at the top left of the texture and the point (1,1) is at the bottom right of the texure. </p>
<p>This isn&#8217;t the same coordinate system mentioned in the original NeHe document.  The reason for that is that he is loading a Windows Bitmap.  Windows Bitmaps are stored with the image from bottom to top as you proceed through the file. </p>
<p>Here is the front face.  Note how we are going counterclockwise in both the texture coordinates and the spatial coordinates. (Note: It is traditional to show the texture coordinates and vertex coordinates as sort of two columns of source code.) <a name="draw-textured-cube-faces"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw textured cube faces</span>
<span style="color: #808080; font-style: italic;">;; front face</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">tex-coord</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">tex-coord</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">tex-coord</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">tex-coord</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>The same sort of logic continues around to the remaining five faces.  I&#8217;m going to write a little function though to hopefully speed this along.  Hopefully, if I use constants and an inline function, most of the calculation herein will get optimized into constants, too. <a name="extra-decls-3"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra decls (cont.)</span>
<span style="color: #66cc66;">&#40;</span>declaim <span style="color: #66cc66;">&#40;</span>inline cube-face<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> cube-face <span style="color: #66cc66;">&#40;</span>left up forw<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">tex-coord</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; bottom-left</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>elt left <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>elt up <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>elt forw <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>elt left <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>elt up <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>elt forw <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>elt left <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>elt up <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>elt forw <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">tex-coord</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; bottom-right</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>elt left <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>elt up <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>elt forw <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>elt left <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>elt up <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>elt forw <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>elt left <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>elt up <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>elt forw <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">tex-coord</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; top-right</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>elt left <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>elt up <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>elt forw <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>elt left <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>elt up <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>elt forw <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>elt left <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>elt up <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>elt forw <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">tex-coord</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; top-left</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>elt left <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>elt up <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>elt forw <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>elt left <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>elt up <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>elt forw <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>elt left <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>elt up <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>elt forw <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>Now, I can whip through the faces just saying which way is left, which way is up, and which way is forward for that face. <a name="draw-textured-cube-faces-1"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw textured cube faces (cont.)</span>
<span style="color: #808080; font-style: italic;">;; back face</span>
<span style="color: #66cc66;">&#40;</span>cube-face #<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>  #<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span> #<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; top face</span>
<span style="color: #66cc66;">&#40;</span>cube-face #<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>  #<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0</span>  <span style="color: #cc66cc;">0.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span> #<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; bottom face</span>
<span style="color: #66cc66;">&#40;</span>cube-face #<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>  #<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0</span>  <span style="color: #cc66cc;">0.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span> #<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0</span> -<span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; right face</span>
<span style="color: #66cc66;">&#40;</span>cube-face #<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span> #<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span> #<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; left face</span>
<span style="color: #66cc66;">&#40;</span>cube-face #<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span> #<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span> #<span style="color: #66cc66;">&#40;</span>-<span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>And, now we have a textured cube.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2010/06/nehe-tutorial-06-textured-solids/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NeHe Tutorial 05: Solids</title>
		<link>http://nklein.com/2010/06/nehe-tutorial-05-solids/</link>
		<comments>http://nklein.com/2010/06/nehe-tutorial-05-solids/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 16:14:25 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[NeHe Tutorials]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[nehe]]></category>
		<category><![CDATA[opengl]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1443</guid>
		<description><![CDATA[Introduction Rotation state Preparing the tick function Drawing rotated pyramids and cubes Drawing the pyramid Drawing the cube Important note Introduction In the previous tutorial, we drew a rotating triangle and a rotating square on the screen. The next NeHe tutorial fleshes out these polygons into solid shapes: a (square-bottomed) pyramid and a cube. Again, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://nklein.com/wp-content/uploads/2010/06/tut05.png"><img src="http://nklein.com/wp-content/uploads/2010/06/tut05-300x241.png" alt="" title="tut05.png" width="300" height="241" class="alignright size-medium wp-image-1455" /></a></p>
<ul>
<li><a href="#tut05-toc1">Introduction</a></li>
<li><a href="#tut05-toc2">Rotation state</a></li>
<li><a href="#tut05-toc3">Preparing the <code class="codecolorer text default"><span class="text">tick</span></code> function</a></li>
<li><a href="#tut05-toc4">Drawing rotated pyramids and cubes</a>
<ul>
<li><a href="#tut05-toc5">Drawing the pyramid</a></li>
<li><a href="#tut05-toc6">Drawing the cube</a></li>
<li><a href="#tut05-toc7">Important note</a></li>
</ul>
</li>
</ul>
<h3><a name="tut05-toc1">Introduction</a></h3>
<p>In the <a href="http://nklein.com/2010/06/nehe-tutorial-04-rotation/">previous tutorial</a>, we drew a rotating triangle and a rotating square on the screen.  The <a href="http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=05">next NeHe tutorial</a> fleshes out these polygons into solid shapes: a (square-bottomed) pyramid and a cube. </p>
<p>Again, we&#8217;re going to start with our simple-tutorial base. <a name="*.lisp"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; *.lisp</span>
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;simple-tutorial.lisp&quot;</span><span style="color: #66cc66;">&gt;</span></pre></div>
<p></a></p>
<p>Here is the resulting <a href="http://nklein.com/wp-content/uploads/2010/06/tut05.lisp">tut05.lisp</a>.</p>
<p><a name="window-title"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; window title</span>
<span style="color: #ff0000;">&quot;tut05: solid shapes&quot;</span></pre></div>
<p></a></p>
<p>This tutorial is almost identical to the previous one.  For variety, I am going to use what I called the <q>alternate version</q> of managing the modelview matrix in the previous tutorial. </p>
<h3><a name="tut05-toc2">Rotation state</a></h3>
<p>Again, our rotation state is just going to be two angles: one for the pyramid and one for the cube.  They are both going to default to zero. <a name="extra-decls"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra decls</span>
<span style="color: #66cc66;">&#40;</span>defclass rotation-state <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>pyramid-angle <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">pyramid-angle</span> <span style="color: #66cc66;">:</span><span style="color: #555;">reader</span> pyramid-angle<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>cube-angle <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">cube-angle</span> <span style="color: #66cc66;">:</span><span style="color: #555;">reader</span> cube-angle<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">default-initargs</span> <span style="color: #66cc66;">:</span><span style="color: #555;">pyramid-angle</span> <span style="color: #cc66cc;">0.0</span>
                     <span style="color: #66cc66;">:</span><span style="color: #555;">cube-angle</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>We&#8217;re also going to add the rotation state into our window class. <a name="extra-slots"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra slots</span>
<span style="color: #66cc66;">&#40;</span>rotation-state <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">rotation-state</span> <span style="color: #66cc66;">:</span><span style="color: #555;">accessor</span> rotation-state<span style="color: #66cc66;">&#41;</span></pre></div>
<p></a><br />
 And, make sure we initialize our rotation state. <a name="extra-initargs"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra initargs</span>
<span style="color: #66cc66;">:</span><span style="color: #555;">rotation-state</span> <span style="color: #66cc66;">&#40;</span>make-instance 'rotation-state<span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<h3><a name="tut05-toc3">Preparing the <code class="codecolorer text default"><span class="text">tick</span></code> function</a></h3>
<p>Again, we&#8217;re going to try to stay near 60 frames per second. </p>
<p><a name="extra-initargs-1"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra initargs (cont.)</span>
<span style="color: #66cc66;">:</span><span style="color: #555;">tick-interval</span> <span style="color: #66cc66;">&#40;</span>round <span style="color: #cc66cc;">1000</span> <span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">; milliseconds per tick</span></pre></div>
<p></a></p>
<p>And, our tick method is unchanged from the previous tutorial except that we now have a pyramid and cube instead of a triangle and quad. <a name="extra-code"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra code</span>
<span style="color: #66cc66;">&#40;</span>defmethod glut<span style="color: #66cc66;">:</span><span style="color: #555;">tick</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>win my-window<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                                <span style="color: #808080; font-style: italic;">; retrieve the current rotation</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>cur <span style="color: #66cc66;">&#40;</span>rotation-state win<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                                <span style="color: #808080; font-style: italic;">; retrieve the current angles</span>
         <span style="color: #66cc66;">&#40;</span>pyramid <span style="color: #66cc66;">&#40;</span>pyramid-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>cube <span style="color: #66cc66;">&#40;</span>cube-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span>rotation-state win<span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">; replace the rotation state</span>
          <span style="color: #66cc66;">&#40;</span>make-instance 'rotation-state
                         <span style="color: #66cc66;">:</span><span style="color: #555;">pyramid-angle</span> <span style="color: #66cc66;">&#40;</span>+ pyramid <span style="color: #cc66cc;">0.2</span><span style="color: #66cc66;">&#41;</span>
                         <span style="color: #66cc66;">:</span><span style="color: #555;">cube-angle</span> <span style="color: #66cc66;">&#40;</span>+ cube <span style="color: #cc66cc;">0.15</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span>glut<span style="color: #66cc66;">:</span><span style="color: #555;">post-redisplay</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; tell GLUT to redraw</span></pre></div>
<p></a></p>
<h3><a name="tut05-toc4">Drawing rotated pyramids and cubes</a></h3>
<p>In the base code, we already cleared the color buffer and the depth buffer and reset the modelview matrix.  Now, we&#8217;re going to retrieve our rotations, position our pyramid, save our modelview matrix, rotate for it, draw it, and restore our saved modelview matrix.  Then, we&#8217;re going to position our cube, save our modelview matrix, rotate for it, draw it, and restore our saved modelview matrix. <a name="display-extra-code"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; display extra code</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>cur <span style="color: #66cc66;">&#40;</span>rotation-state win<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>pyramid-angle <span style="color: #66cc66;">&#40;</span>pyramid-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>cube-angle <span style="color: #66cc66;">&#40;</span>cube-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;position pyramid&quot;</span><span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">with-pushed-matrix</span>
      #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;rotate pyramid&quot;</span><span style="color: #66cc66;">&gt;</span>
      #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;draw pyramid&quot;</span><span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&#41;</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;position cube&quot;</span><span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">with-pushed-matrix</span>
      #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;rotate cube&quot;</span><span style="color: #66cc66;">&gt;</span>
      #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;draw cube&quot;</span><span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<h4><a name="tut05-toc5">Drawing the pyramid</a></h4>
<p> For the pyramid, we&#8217;re going to slide to the left and back into the screen. <a name="position-pyramid"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; position pyramid</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">translate</span> -<span style="color: #cc66cc;">1.5</span> <span style="color: #cc66cc;">0.0</span> -<span style="color: #cc66cc;">6.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; translate left and into the screen</span></pre></div>
<p></a></p>
<p>Then, we&#8217;re going to rotate the coordinate system around the Y-axis. <a name="rotate-pyramid"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; rotate pyramid</span>
                                <span style="color: #808080; font-style: italic;">; rotate around the y-axis</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">rotate</span> pyramid-angle <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a><br />
 Again, the first parameter to rotate is an angle (in degrees). The remaining parameters are the axis about which to rotate. </p>
<p>Now, we&#8217;re going to draw the pyramid.  We&#8217;re going to draw each of the four triangles that make up the pyramid.  We&#8217;re going to keep each vertexes colored the same way regardless of which face the vertex is being drawn on at the moment. <a name="draw-pyramid"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw pyramid</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">with-primitives</span> <span style="color: #66cc66;">:</span><span style="color: #555;">triangles</span>  <span style="color: #808080; font-style: italic;">; start drawing triangles</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;draw pyramid faces&quot;</span><span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>The front face is going to be just about the same as our triangle from the previous tutorials.  We&#8217;re just going to kick the bottom forward a bit. <a name="draw-pyramid-faces"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw pyramid faces</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to red</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>         <span style="color: #808080; font-style: italic;">; top vertex (front)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to green</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>       <span style="color: #808080; font-style: italic;">; bottom-left vertex (front)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to blue</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; bottom-right vertex (front)</span></pre></div>
<p></a></p>
<p>The right face is going to share two vertexes with our front face and introduce a third. <a name="draw-pyramid-faces-1"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw pyramid faces (cont.)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to red</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>         <span style="color: #808080; font-style: italic;">; top vertex (right)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to blue</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; bottom-left vertex (right)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to green</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>       <span style="color: #808080; font-style: italic;">; bottom-left vertex (right)</span></pre></div>
<p></a></p>
<p>The back face is going to share two points with the right face and one point with the front face. <a name="draw-pyramid-faces-2"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw pyramid faces (cont.)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to red</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>         <span style="color: #808080; font-style: italic;">; top vertex (back)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to green</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>       <span style="color: #808080; font-style: italic;">; bottom-left vertex (back)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to blue</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; bottom-left vertex (back)</span></pre></div>
<p></a></p>
<p>The left face is going to share two points with the back face and two points with the front face (and, of course, the apex with the right face). <a name="draw-pyramid-faces-3"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw pyramid faces (cont.)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to red</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>         <span style="color: #808080; font-style: italic;">; top vertex (left)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to blue</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; bottom-left vertex (left)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to green</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>       <span style="color: #808080; font-style: italic;">; bottom-left vertex (left)</span></pre></div>
<p></a></p>
<p>This completes the four sides of our pyramid.  The NeHe tutorial doesn&#8217;t bother drawing a bottom for the pyramid.  It won&#8217;t ever be seen with the way the rest of this code is organized, but I am going to include it here for completeness. <a name="draw-pyramid-1"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw pyramid (cont.)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">with-primitives</span> <span style="color: #66cc66;">:</span><span style="color: #555;">quads</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; set the color to blue</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; front-right corner</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; set the color to green</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>     <span style="color: #808080; font-style: italic;">; back-right corner</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; set the color to blue</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; back-left corner</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; set the color to green</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; front-left corner</span></pre></div>
<p></a></p>
<h4><a name="tut05-toc6">Drawing the cube</a></h4>
<p>At the point we need to position the cube, we&#8217;re sitting at the point where the triangle was drawn.  So, we need to slide to the right before drawing the cube. <a name="position-cube"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; position cube</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">translate</span>  <span style="color: #cc66cc;">3.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>     <span style="color: #808080; font-style: italic;">; translate right</span></pre></div>
<p></a></p>
<p>Now, we&#8217;re going to rotate the coordinate system around the x-axis. <a name="rotate-cube"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; rotate cube</span>
                                <span style="color: #808080; font-style: italic;">; rotate around the x-axis</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">rotate</span> cube-angle <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>Now, we&#8217;re going to draw the cube with each face a different color. <a name="draw-cube"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw cube</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">with-primitives</span> <span style="color: #66cc66;">:</span><span style="color: #555;">quads</span>      <span style="color: #808080; font-style: italic;">; start drawing quadrilaterals</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;draw cube faces&quot;</span><span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>The top face is going to be green.  We are taking care here to draw the vertexes in counter-clockwise order when viewed from above the cube. <a name="draw-cube-faces"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw cube faces</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to green</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; right top back</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; left top back</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; left top front</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; right top front</span></pre></div>
<p></a></p>
<p>The bottom face is going to be orange.  We are still taking care to draw the vertexes in counter-clockwise order when looking at this face from outside the cube.  For the bottom face, that would be looking at the cube from below.  For consistency, should we later want to texture map the cube, we&#8217;re going to start working from the front of the cube this time instead of the back as if we just flipped the cube 180 degrees forward and are now looking at the bottom. <a name="draw-cube-faces-1"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw cube faces (cont.)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.5</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to orange</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; right bottom front</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; left bottom front</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; left bottom back</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; right bottom back</span></pre></div>
<p></a></p>
<p>Next, we&#8217;re going to draw the front face.  We are going to make it red.  Again, we&#8217;re going to keep our vertexes counter clockwise and we&#8217;re going to start with the one that&#8217;s in the upper right when we&#8217;re looking at the face. <a name="draw-cube-faces-2"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw cube faces (cont.)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to red</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; right top front</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; left top front</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; left bottom front</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; right bottom front</span></pre></div>
<p></a></p>
<p>Next, we&#8217;re going to draw the back face.  We are going to make it yellow.  Again, we&#8217;re going to keep our vertexes counter clockwise and we&#8217;re going to start with the one that&#8217;s in the upper right when we&#8217;re looking at the face (as if we&#8217;ve rotated the cube forward 180 degrees so that what was back is now front). <a name="draw-cube-faces-3"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw cube faces (cont.)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to yellow</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; right bottom back</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; left bottom back</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; left top back</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; right top back</span></pre></div>
<p></a></p>
<p>We&#8217;re going to draw the left side in blue. <a name="draw-cube-faces-4"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw cube faces (cont.)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to blue</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; left top front</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; left top back</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; left bottom back</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; left bottom front</span></pre></div>
<p></a></p>
<p>For this tutorial, we&#8217;re never going to see the right side of the cube, but we&#8217;re going to draw it anyway for completeness.  It will be magenta. <a name="draw-cube-faces-5"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw cube faces (cont.)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to magenta</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; right top back</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; right top front</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; right bottom front</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; right bottom back</span></pre></div>
<p></a></p>
<p>And, now we have a cube. </p>
<h4><a name="tut05-toc7">Important note</a></h4>
<p>In all of the above examples, we have only used <code class="codecolorer text default"><span class="text">vertex</span></code> inside a <code class="codecolorer text default"><span class="text">with-primitives</span></code> call.  There is good reason for this.  We cannot just make a vertex whenever we want.  It has to be a part of a shape. The <code class="codecolorer text default"><span class="text">with-primitives</span></code> call starts building a shape (so far, we&#8217;ve only used triangles or quads) and then ends the shape at the end of the form.  In C, we would need to do something like this to explicitly begin and end the shape. <a name="example-triangle.c"></p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/* example-triangle.c */</span>
glBegin<span style="color: #009900;">&#40;</span>GL_TRIANGLES<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   glVertex3f<span style="color: #009900;">&#40;</span>  <span style="color:#800080;">0.0</span><span style="color: #339933;">,</span>  <span style="color:#800080;">1.0</span><span style="color: #339933;">,</span>  <span style="color:#800080;">0.0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   glVertex3f<span style="color: #009900;">&#40;</span> <span style="color: #339933;">-</span><span style="color:#800080;">1.0</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">1.0</span><span style="color: #339933;">,</span>  <span style="color:#800080;">0.0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   glVertex3f<span style="color: #009900;">&#40;</span>  <span style="color:#800080;">1.0</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color:#800080;">1.0</span><span style="color: #339933;">,</span>  <span style="color:#800080;">0.0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
glEnd<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div>
<p></a></p>
<p>If you try to make a vertex that isn&#8217;t part of a shape, things get corrupted.  In C, you can probably still limp along and never notice. Unless you explicitly check the OpenGL error state on a regular basis, you&#8217;ll never notice that OpenGL is screaming quietly to itself. </p>
<p>CL-OpenGL checks the OpenGL error state for us though.  It notices right away that something has gone wrong if we try to make a vertex outside of a <code class="codecolorer text default"><span class="text">with-primitives</span></code> call.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2010/06/nehe-tutorial-05-solids/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NeHe Tutorial 04: Rotation</title>
		<link>http://nklein.com/2010/06/nehe-tutorial-04-rotation/</link>
		<comments>http://nklein.com/2010/06/nehe-tutorial-04-rotation/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 14:18:21 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[NeHe Tutorials]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[nehe]]></category>
		<category><![CDATA[opengl]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1436</guid>
		<description><![CDATA[Introduction Rotation state Preparing the tick function Drawing rotated triangles and quadrilaterals Drawing the triangle Resetting the modelview transform Drawing the quadrilateral Drawing rotated triangles and quadrilaterals (alternate version) Introduction In the previous tutorial, we drew a colored triangle and quadrilateral on the screen. The next NeHe tutorial rotates these polygons on the screen. This [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://nklein.com/wp-content/uploads/2010/06/tut04.png"><img src="http://nklein.com/wp-content/uploads/2010/06/tut04-300x241.png" alt="" title="tut04.png" width="300" height="241" class="alignright size-medium wp-image-1458" /></a></p>
<ul>
<li><a href="#tut04-toc1">Introduction</a></li>
<li><a href="#tut04-toc2">Rotation state</a></li>
<li><a href="#tut04-toc3">Preparing the <code class="codecolorer text default"><span class="text">tick</span></code> function</a></li>
<li><a href="#tut04-toc4">Drawing rotated triangles and quadrilaterals</a>
<ul>
<li><a href="#tut04-toc5">Drawing the triangle</a></li>
<li><a href="#tut04-toc6">Resetting the modelview transform</a></li>
<li><a href="#tut04-toc7">Drawing the quadrilateral</a></li>
</ul>
</li>
<li><a href="#tut04-toc8">Drawing rotated triangles and quadrilaterals (alternate version)</a></li>
</ul>
<h3><a name="tut04-toc1">Introduction</a></h3>
<p>In the <a href="http://nklein.com/2010/06/nehe-tutorial-03-color/">previous tutorial</a>, we drew a colored triangle and quadrilateral on the screen.  The <a href="http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=04">next NeHe tutorial</a> rotates these polygons on the screen. </p>
<p>This tutorial will mark my first significant departures from the original NeHe tutorials.  In this NeHe tutorial, he updates the polygons&#8217; angles inside the display function.  I&#8217;m going to move those out into GLUT&#8217;s <code class="codecolorer text default"><span class="text">tick</span></code> function.  The NeHe tutorial also keeps those angles in some global variables.  I am going to tuck them inside my window class. </p>
<p>Again, we&#8217;re going to start with our simple-tutorial base. <a name="*.lisp"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; *.lisp</span>
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;simple-tutorial.lisp&quot;</span><span style="color: #66cc66;">&gt;</span></pre></div>
<p></a></p>
<p>Here is the resulting <a href="http://nklein.com/wp-content/uploads/2010/06/tut04.lisp">tut04.lisp</a>.</p>
<p><a name="window-title"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; window title</span>
<span style="color: #ff0000;">&quot;tut04: rotation&quot;</span></pre></div>
<p></a></p>
<h3><a name="tut04-toc2">Rotation state</a></h3>
<p>Our rotation state is just going to be two angles: one for the triangle and one for the quad.  They are both going to default to zero. <a name="extra-decls"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra decls</span>
<span style="color: #66cc66;">&#40;</span>defclass rotation-state <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>triangle-angle <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">triangle-angle</span> <span style="color: #66cc66;">:</span><span style="color: #555;">reader</span> triangle-angle<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>quad-angle <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">quad-angle</span> <span style="color: #66cc66;">:</span><span style="color: #555;">reader</span> quad-angle<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">default-initargs</span> <span style="color: #66cc66;">:</span><span style="color: #555;">triangle-angle</span> <span style="color: #cc66cc;">0.0</span>
                     <span style="color: #66cc66;">:</span><span style="color: #555;">quad-angle</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a><br />
 I have opted here to make the rotation-state immutable (unless you side-step and act on the slots directly).  I&#8217;m doing this largely as a personal experiment.  Below, you will see that rather than update the members of the rotation state in the window, I simply replace the whole rotation state for the window.  You may not wish to do this yourself, especially for something so simple as a pair of angles. </p>
<p>We&#8217;re also going to add the rotation state into our window class. <a name="extra-slots"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra slots</span>
<span style="color: #66cc66;">&#40;</span>rotation-state <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">rotation-state</span> <span style="color: #66cc66;">:</span><span style="color: #555;">accessor</span> rotation-state<span style="color: #66cc66;">&#41;</span></pre></div>
<p></a><br />
 <a name="extra-initargs"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra initargs</span>
<span style="color: #66cc66;">:</span><span style="color: #555;">rotation-state</span> <span style="color: #66cc66;">&#40;</span>make-instance 'rotation-state<span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<h3><a name="tut04-toc3">Preparing the <code class="codecolorer text default"><span class="text">tick</span></code> function</a></h3>
<p>CL-GLUT provides us with an easy mechanism to get a callback at a regular interval.  First, we need to add another initarg when we create our window to tell it how often we&#8217;d like a callback.  We&#8217;re going to try to stay near 60 frames per second.  The tick interval is specified in milliseconds. <a name="extra-initargs-1"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra initargs (cont.)</span>
<span style="color: #66cc66;">:</span><span style="color: #555;">tick-interval</span> <span style="color: #66cc66;">&#40;</span>round <span style="color: #cc66cc;">1000</span> <span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">; milliseconds per tick</span></pre></div>
<p></a></p>
<p>Then, we need to fill in the body of the callback.  In the callback, we&#8217;re going to update the rotation state and then let GLUT know we need to redraw the screen. <a name="extra-code"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra code</span>
<span style="color: #66cc66;">&#40;</span>defmethod glut<span style="color: #66cc66;">:</span><span style="color: #555;">tick</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>win my-window<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                                <span style="color: #808080; font-style: italic;">; retrieve the current rotation</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>cur <span style="color: #66cc66;">&#40;</span>rotation-state win<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                                <span style="color: #808080; font-style: italic;">; retrieve the current angles</span>
         <span style="color: #66cc66;">&#40;</span>tri <span style="color: #66cc66;">&#40;</span>triangle-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#40;</span>quad <span style="color: #66cc66;">&#40;</span>quad-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span>rotation-state win<span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">; replace the rotation state</span>
          <span style="color: #66cc66;">&#40;</span>make-instance 'rotation-state
                         <span style="color: #66cc66;">:</span><span style="color: #555;">triangle-angle</span> <span style="color: #66cc66;">&#40;</span>+ tri <span style="color: #cc66cc;">0.2</span><span style="color: #66cc66;">&#41;</span>
                         <span style="color: #66cc66;">:</span><span style="color: #555;">quad-angle</span> <span style="color: #66cc66;">&#40;</span>+ quad <span style="color: #cc66cc;">0.15</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
  <span style="color: #66cc66;">&#40;</span>glut<span style="color: #66cc66;">:</span><span style="color: #555;">post-redisplay</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; tell GLUT to redraw</span></pre></div>
<p></a></p>
<h3><a name="tut04-toc4">Drawing rotated triangles and quadrilaterals</a></h3>
<p>In the base code, we already cleared the color buffer and the depth buffer and reset the modelview matrix. In our previous two tutorials, we positioned the triangle, drew it, then moved from there over to where we were going to draw the quadrilateral. </p>
<p>Now though, we&#8217;re going to have to be more careful.  We&#8217;re going to move over to where the triangle is to be drawn, rotate the coordinate system, and draw the triangle.  If we then tried to translate over to where we want to draw the quadrilateral, we&#8217;d have to figure out how to do it in the rotated coordinate system.  Rather than do that, we are just going to reset the transformation altogether before positioning the quad. <a name="display-extra-code"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; display extra code</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>cur <span style="color: #66cc66;">&#40;</span>rotation-state win<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>triangle-angle <span style="color: #66cc66;">&#40;</span>triangle-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>quad-angle <span style="color: #66cc66;">&#40;</span>quad-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;position triangle&quot;</span><span style="color: #66cc66;">&gt;</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;rotate triangle&quot;</span><span style="color: #66cc66;">&gt;</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;draw triangle&quot;</span><span style="color: #66cc66;">&gt;</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;reset transformation&quot;</span><span style="color: #66cc66;">&gt;</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;position quad&quot;</span><span style="color: #66cc66;">&gt;</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;rotate quad&quot;</span><span style="color: #66cc66;">&gt;</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;draw quad&quot;</span><span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<h4><a name="tut04-toc5">Drawing the triangle</a></h4>
<p> For the triangle, we&#8217;re going to slide to the left and back into the screen. <a name="position-triangle"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; position triangle</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">translate</span> -<span style="color: #cc66cc;">1.5</span> <span style="color: #cc66cc;">0.0</span> -<span style="color: #cc66cc;">6.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; translate left and into the screen</span></pre></div>
<p></a></p>
<p>Then, we&#8217;re going to rotate the coordinate system around the Y-axis. </p>
<p>Imagine you&#8217;re riding on your bicycle.  If your front wheel were centered at the origin, it would be rotating around the X-axis. </p>
<p>If a revolving door is centered at the origin, it would be rotating around the Y-axis. </p>
<p>If your car steering wheel is centered at the origin, you rotate it around the Z-axis to turn the car. </p>
<p>So, to draw the triangle rotated, we&#8217;re going to rotate the coordinate system around the Y-axis. <a name="rotate-triangle"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; rotate triangle</span>
                                <span style="color: #808080; font-style: italic;">; rotate around the y-axis</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">rotate</span> triangle-angle <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a><br />
 The first parameter to rotate is an angle (in degrees).  The remaining parameters are the axis about which to rotate. </p>
<p>Now, we&#8217;re just going to draw the triangle like we did in the previous tutorial. <a name="draw-triangle"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw triangle</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">with-primitives</span> <span style="color: #66cc66;">:</span><span style="color: #555;">triangles</span>  <span style="color: #808080; font-style: italic;">; start drawing triangles</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; set the color to red</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>       <span style="color: #808080; font-style: italic;">; top vertex</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; set the color to green</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>     <span style="color: #808080; font-style: italic;">; bottom-left vertex</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; set the color to blue</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>     <span style="color: #808080; font-style: italic;">; bottom-right vertex</span></pre></div>
<p></a></p>
<h4><a name="tut04-toc6">Resetting the modelview transform</a></h4>
<p>To reset the transformation, we&#8217;re just going to load the identity transformation again.  Below, we will show an alternate way to write this code that doesn&#8217;t involve going the whole way back to a blank slate. <a name="reset-transformation"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; reset transformation</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">load-</span><span style="color: #b1b100;">identity</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<h4><a name="tut04-toc7">Drawing the quadrilateral</a></h4>
<p>In the previous tutorials, we translated <code class="codecolorer text default"><span class="text">3.0 0.0 0.0</span></code> to get from where the triangle was drawn to where the quadrilateral will be drawn. This time, though, we have already gone back to center.  We will need to translate to the right and back into the screen. <a name="position-quad"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; position quad</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">translate</span>  <span style="color: #cc66cc;">1.5</span> <span style="color: #cc66cc;">0.0</span> -<span style="color: #cc66cc;">6.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; translate right and into the screen</span></pre></div>
<p></a></p>
<p>Now, we&#8217;re going to rotate the coordinate system around the x-axis. <a name="rotate-quad"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; rotate quad</span>
                                <span style="color: #808080; font-style: italic;">; rotate around the x-axis</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">rotate</span> quad-angle <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>Then, we&#8217;ll just draw the quadrilateral exactly as we did in the previous tutorial. <a name="draw-quad"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw quad</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.5</span> <span style="color: #cc66cc;">0.5</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to light blue</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">with-primitives</span> <span style="color: #66cc66;">:</span><span style="color: #555;">quads</span>      <span style="color: #808080; font-style: italic;">; start drawing quadrilaterals</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; top-left vertex</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; top-right vertex</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; bottom-right vertex</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>   <span style="color: #808080; font-style: italic;">; bottom-left vertex</span></pre></div>
<p></a></p>
<p>In the previous tutorial, we mentioned that the color is now set to light blue until we explicitly change it again.  Similarly, the modelview matrix is set to be shifted to the right and into the screen and then rotated around the x-axis.  It will be like this until we explicitly reset it.  Fortunately, our template code resets the modelview matrix to the identity matrix at the very beginning of our <code class="codecolorer text default"><span class="text">display</span></code> routine. </p>
<h3><a name="tut04-toc8">Drawing rotated triangles and quadrilaterals (alternate version)</a></h3>
<p>In the previous section, we positioned, rotated, and drew the triangle.  Then, we reset the modelview matrix and positioned, rotated, and drew the quadrilateral. </p>
<p>Sometimes, you don&#8217;t want to have to reset everything back to the the identity matrix before continuing on.  For that, we can take advantage of the <code class="codecolorer text default"><span class="text">with-pushed-matrix</span></code> macro provided by CL-OpenGL. </p>
<p>OpenGL maintains a (finite) stack on which you can push the modelview matrix (and a smaller stack on which you can push the projection matrix).  In C, you have to explicitly push and pop the matrix: <a name="example-of-push-matrix.c"></p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/* example-of-push-matrix.c */</span>
glPushMatrix<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #666666; font-style: italic;">// do something here</span>
glPopMatrix<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div>
<p></a><br />
 With CL-OpenGL, you can take advantage of the <code class="codecolorer text default"><span class="text">with-</span></code> pattern to avoid having to remember to keep your pushes and pops paired up.  The <code class="codecolorer text default"><span class="text">with-pushed-matrix</span></code> effectively remembers the current transformation and restores it at the end of the form. <a name="display-extra-code-(alternate)-.lisp"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; display extra code (alternate) .lisp</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>cur <span style="color: #66cc66;">&#40;</span>rotation-state win<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>triangle-angle <span style="color: #66cc66;">&#40;</span>triangle-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>quad-angle <span style="color: #66cc66;">&#40;</span>quad-angle cur<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;position triangle&quot;</span><span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">with-pushed-matrix</span>
      #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;rotate triangle&quot;</span><span style="color: #66cc66;">&gt;</span>
      #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;draw triangle&quot;</span><span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&#41;</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;position quad (original) .lisp&quot;</span><span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">with-pushed-matrix</span>
      #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;rotate quad&quot;</span><span style="color: #66cc66;">&gt;</span>
      #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;draw quad&quot;</span><span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>Here, we can go back to the original positioning for the quadrilateral because at the time we&#8217;re going to move, we&#8217;re back to using the original matrix from where we positioned the triangle.  We don&#8217;t have to move back into the screen, but we have to move twice as far to the right. <a name="position-quad-(original)-.lisp"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; position quad (original) .lisp</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">translate</span> <span style="color: #cc66cc;">3.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; translate right</span></pre></div>
<p></a><br />
 Everything else is the same as in the previous section. </p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2010/06/nehe-tutorial-04-rotation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NeHe Tutorial 03: Color</title>
		<link>http://nklein.com/2010/06/nehe-tutorial-03-color/</link>
		<comments>http://nklein.com/2010/06/nehe-tutorial-03-color/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 23:07:37 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[NeHe Tutorials]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[nehe]]></category>
		<category><![CDATA[opengl]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1432</guid>
		<description><![CDATA[Introduction Drawing colored triangles and quadrilaterals Drawing with vertex coloring Drawing with flat coloring Introduction In the previous tutorial, we drew a plain triangle and quadrilateral on the screen. The next NeHe tutorial colors this triangle and quadrilateral. We&#8217;re going to start with our simple-tutorial base. ;;; *.lisp #&#60;:use &#34;simple-tutorial.lisp&#34;&#62; ;;; window title &#34;tut03: color&#34; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://nklein.com/wp-content/uploads/2010/06/tut03.png"><img src="http://nklein.com/wp-content/uploads/2010/06/tut03-300x241.png" alt="" title="tut03.png" width="300" height="241" class="alignright size-medium wp-image-1460" /></a></p>
<ul>
<li><a href="#toc1-03">Introduction</a></li>
<li><a href="#toc2-03">Drawing colored triangles and quadrilaterals</a>
<ul>
<li><a href="#toc3-03">Drawing with vertex coloring</a></li>
<li><a href="#toc4-03">Drawing with flat coloring</a></li>
</ul>
</li>
</ul>
<h3><a name=toc1-03>Introduction</a></h3>
<p>In the <a href="http://nklein.com/2010/06/nehe-tutorial-02-drawing-triangles-and-quadrilaterals/">previous tutorial</a>, we drew a plain triangle and quadrilateral on the screen.  The <a href="http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=03">next NeHe tutorial</a> colors this triangle and quadrilateral. </p>
<p>We&#8217;re going to start with our simple-tutorial base. <a name="*.lisp"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; *.lisp</span>
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;simple-tutorial.lisp&quot;</span><span style="color: #66cc66;">&gt;</span></pre></div>
<p></a></p>
<p><a name="window-title"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; window title</span>
<span style="color: #ff0000;">&quot;tut03: color&quot;</span></pre></div>
<p></a></p>
<h3><a name=toc2-03>Drawing colored triangles and quadrilaterals</a></h3>
<p>In the base display code, we already cleared the color buffer and the depth buffer and reset the modelview matrix.  Now, we&#8217;re going to translate the modelview matrix so that when we draw our triangle, it is going to be in front of our viewpoint and off to our left.  Then, we&#8217;ll draw the triangle, translate over toward the right, and draw the quadrilateral. <a name="display-extra-code"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; display extra code</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">translate</span> -<span style="color: #cc66cc;">1.5</span> <span style="color: #cc66cc;">0.0</span> -<span style="color: #cc66cc;">6.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; translate left and into the screen</span>
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;draw triangle&quot;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">translate</span> <span style="color: #cc66cc;">3.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; translate right</span>
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;draw quadrilateral&quot;</span><span style="color: #66cc66;">&gt;</span></pre></div>
<p></a><br />
 The above is untouched from the previous tutorial. </p>
<h4><a name=toc3-03>Drawing with vertex coloring</a></h4>
<p> Now that we&#8217;ve moved over to the side a little bit and back a ways, we&#8217;re going to draw a triangle.  We open with the <code class="codecolorer text default"><span class="text">with-primitives</span></code> call and then specify the vertexes. <a name="draw-triangle"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw triangle</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">with-primitives</span> <span style="color: #66cc66;">:</span><span style="color: #555;">triangles</span>  <span style="color: #808080; font-style: italic;">; start drawing triangles</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;draw triangle vertexes&quot;</span><span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>Before, we simply listed the vertexes.  Here, we are going to specify a color before each vertex. <a name="draw-triangle-vertexes"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw triangle vertexes</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; set the color to red</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>       <span style="color: #808080; font-style: italic;">; top vertex</span></pre></div>
<p></a><br />
 The arguments to <code class="codecolorer text default"><span class="text">color</span></code> are the red, green, and blue values (respectively).  The values range from zero (for the darkest) to one (for the brightest).  I have omitted here the optional fourth argument for the alpha channel.  It defaults to <code class="codecolorer text default"><span class="text">1.0</span></code>. </p>
<p>It is important to note that we have set the global color to red.  This vertex will be red because the global color was red at the time we created the vertex.  If we failed to ever set the color again, everything would be red. </p>
<p>Here, however, we&#8217;re going to make the next vertex green. <a name="draw-triangle-vertexes-1"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw triangle vertexes (cont.)</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; set the color to green</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>     <span style="color: #808080; font-style: italic;">; bottom-left vertex</span></pre></div>
<p></a></p>
<p>We are going to make the final vertex blue for this triangle. <a name="draw-triangle-vertexes-2"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw triangle vertexes (cont.)</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; set the color to blue</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; bottom-right vertex</span></pre></div>
<p></a></p>
<p>Note: the global color is now blue.  We could leave it blue and it would be blue until we set it to some other color. </p>
<h4><a name=toc4-03>Drawing with flat coloring</a></h4>
<p>Drawing quadrilaterals is much like drawing triangles.  Here, of course, we need four vertexes.  In this case, however, we&#8217;re going to color the whole quadrilateral the same color.  So, we are just going to set the global color to a light blue and then draw the quadrilateral exactly as we did in the previous tutorial. <a name="draw-quadrilateral"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw quadrilateral</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">color</span> <span style="color: #cc66cc;">0.5</span> <span style="color: #cc66cc;">0.5</span> <span style="color: #cc66cc;">1.0</span><span style="color: #66cc66;">&#41;</span>          <span style="color: #808080; font-style: italic;">; set the color to light blue</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">with-primitives</span> <span style="color: #66cc66;">:</span><span style="color: #555;">quads</span>      <span style="color: #808080; font-style: italic;">; start drawing quadrilaterals</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; top-left vertex</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; top-right vertex</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; bottom-right vertex</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>   <span style="color: #808080; font-style: italic;">; bottom-left vertex</span></pre></div>
<p></a></p>
<p>Now, the color is still this light blue.  It will remain so until we reset the color to red when drawing the triangle during the next time our screen is redrawn. </p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2010/06/nehe-tutorial-03-color/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NeHe Tutorial 02: Drawing Triangles and Quadrilaterals</title>
		<link>http://nklein.com/2010/06/nehe-tutorial-02-drawing-triangles-and-quadrilaterals/</link>
		<comments>http://nklein.com/2010/06/nehe-tutorial-02-drawing-triangles-and-quadrilaterals/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 21:18:42 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[NeHe Tutorials]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[nehe]]></category>
		<category><![CDATA[opengl]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1419</guid>
		<description><![CDATA[Introduction Drawing triangles and quadrilaterals Drawing triangles Drawing quadrilaterals Toggling Fullscreen mode Switching based on keyboard event Introduction In the previous tutorial, we made a basic shell of a CL-OpenGL application. I have slightly modified it for this tutorial so that it has some hooks where we can add in code specific to this tutorial. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://nklein.com/wp-content/uploads/2010/06/tut02.png"><img src="http://nklein.com/wp-content/uploads/2010/06/tut02-300x241.png" alt="" title="tut02.png" width="300" height="241" class="alignright size-medium wp-image-1463" /></a></p>
<ul>
<li><a href="#toc1-02">Introduction</a></li>
<li><a href="#toc2-02">Drawing triangles and quadrilaterals</a>
<ul>
<li><a href="#toc3-02">Drawing triangles</a></li>
<li><a href="#toc4-02">Drawing quadrilaterals</a></li>
</ul>
</li>
<li><a href="#toc5-02">Toggling Fullscreen mode</a>
<ul>
<li><a href="#toc6-02">Switching based on keyboard event</a></li>
</ul>
</li>
</ul>
<h3><a name=toc1-02>Introduction</a></h3>
<p>In the <a href="http://nklein.com/2010/06/nehe-tutorials-for-cl-opengl/">previous tutorial</a>, we made a basic shell of a CL-OpenGL application.  I have slightly modified it for this tutorial so that it has some hooks where we can add in code specific to this tutorial. </p>
<p>In this tutorial, we&#8217;re going to draw a triangle and a quadrilateral in our window.  We&#8217;re going to start with our simple-tutorial base. <a name="*.lisp"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; *.lisp</span>
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;simple-tutorial.lisp&quot;</span><span style="color: #66cc66;">&gt;</span></pre></div>
<p></a></p>
<p>Here is the whole <a href="http://nklein.com/wp-content/uploads/2010/06/tut02.lisp">tut02.lisp</a>.</p>
<p><a name="window-title"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; window title</span>
<span style="color: #ff0000;">&quot;tut02: triangles and quads&quot;</span></pre></div>
<p></a></p>
<h3><a name=toc2-02>Drawing triangles and quadrilaterals</a></h3>
<p>In the base display code, we already cleared the color buffer and the depth buffer and reset the modelview matrix.  Now, we&#8217;re going to translate the modelview matrix so that when we draw our triangle, it is going to be in front of our viewpoint and off to our left.  Then, we&#8217;ll draw the triangle, translate over toward the right, and draw the quadrilateral. <a name="display-extra-code"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; display extra code</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">translate</span> -<span style="color: #cc66cc;">1.5</span> <span style="color: #cc66cc;">0.0</span> -<span style="color: #cc66cc;">6.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; translate left and into the screen</span>
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;draw triangle&quot;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">translate</span> <span style="color: #cc66cc;">3.0</span> <span style="color: #cc66cc;">0.0</span> <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; translate right</span>
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;draw quadrilateral&quot;</span><span style="color: #66cc66;">&gt;</span></pre></div>
<p></a><br />
 The parameters to <code class="codecolorer text default"><span class="text">gl:translate</span></code> are x, y, and z (respectively). After the <code class="codecolorer text default"><span class="text">gl:load-identity</span></code>, the modelview matrix is centered at the origin with the positive x axis pointing to the right of your screen, the positive y axis pointing up your screen, and the positive z-axis pointing out of your screen. </p>
<p>With the way that we set up the projection matrix in the <code class="codecolorer text default"><span class="text">reshape</span></code> method, the origin of the modelview space should be dead-center in our window. </p>
<h4><a name=toc3-02>Drawing triangles</a></h4>
<p> Now that we&#8217;ve moved over to the side a little bit and back a ways, we&#8217;re going to draw a triangle.  The CL-OpenGL code looks like this: <a name="draw-triangle"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw triangle</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">with-primitives</span> <span style="color: #66cc66;">:</span><span style="color: #555;">triangles</span>  <span style="color: #808080; font-style: italic;">; start drawing triangles</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">0.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; top vertex</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; bottom-left vertex</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>   <span style="color: #808080; font-style: italic;">; bottom-right vertex</span></pre></div>
<p></a><br />
 The <code class="codecolorer text default"><span class="text">with-primitives</span></code> form lets OpenGL know how to use the vertexes we&#8217;re going to make.  In this case, it&#8217;s going to make a triangle out of each set of three vertexes.  If we had six vertexes there, we&#8217;d end up with two triangles. </p>
<p>Here, we drew the vertexes in clockwise order.  By default, OpenGL considers this triangle to be facing away from us, then.  With our current OpenGL settings, this does not make a difference since OpenGL will draw both front and back faces. </p>
<p>Each call to <code class="codecolorer text default"><span class="text">vertex</span></code> gives the x, y, and z (respectively) coordinates in the modelview projection for the vertex.  You will note that I used floating-point numbers here.  I could have easily written them as integers like <code class="codecolorer text default"><span class="text">(gl:vertex 1 -1 0)</span></code>.  CL-OpenGL would convert them to floating point numbers for me on the fly.  I tend to use floating point constants when possible to try to save it the extra work.  I should check, sometime, to be sure though that I don&#8217;t pay a boxing/unboxing penalty that negates the benefit. </p>
<h4><a name=toc4-02>Drawing quadrilaterals</a></h4>
<p>Drawing quadrilaterals is much like drawing triangles.  Here, of course, we need four vertexes. <a name="draw-quadrilateral"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; draw quadrilateral</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">with-primitives</span> <span style="color: #66cc66;">:</span><span style="color: #555;">quads</span>      <span style="color: #808080; font-style: italic;">; start drawing quadrilaterals</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; top-left vertex</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; top-right vertex</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span>  <span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; bottom-right vertex</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">vertex</span> -<span style="color: #cc66cc;">1.0</span> -<span style="color: #cc66cc;">1.0</span>  <span style="color: #cc66cc;">0.0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>   <span style="color: #808080; font-style: italic;">; bottom-left vertex</span></pre></div>
<p></a><br />
 In this case, we drew a square.  We could draw any convex quadrilateral. </p>
<p>Again, we drew the vertexes in clockwise order.  By default, OpenGL considers this triangle to be facing away from us, then.  With our current OpenGL settings, this does not make a difference since OpenGL will draw both front and back faces. </p>
<h3><a name=toc5-02>Toggling Fullscreen mode</a></h3>
<p>We&#8217;re also going to add a slot that keeps track of whether or not our window is full screen. <a name="extra-slots"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra slots</span>
<span style="color: #66cc66;">&#40;</span>fullscreen <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">fullscreen</span> <span style="color: #66cc66;">:</span><span style="color: #555;">reader</span> fullscreen-p<span style="color: #66cc66;">&#41;</span></pre></div>
<p></a><br />
 <a name="extra-initargs"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; extra initargs</span>
<span style="color: #66cc66;">:</span><span style="color: #555;">fullscreen</span> <span style="color: #b1b100;">nil</span></pre></div>
<p></a></p>
<p>Then, before we display our window, we&#8217;re going to switch to fullscreen mode if this is true. <a name="display-window-extra-code"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; display-window extra code</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span>fullscreen-p win<span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; check to see if fullscreen needed</span>
  <span style="color: #66cc66;">&#40;</span>glut<span style="color: #66cc66;">:</span><span style="color: #555;">full-screen</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>           <span style="color: #808080; font-style: italic;">; if so, then tell GLUT</span></pre></div>
<p></a></p>
<h4><a name=toc6-02>Switching based on keyboard event</a></h4>
<p>Here, we add an extra case to the keypress handler.  We destroy our window and create a new one with the fullscreen property toggled if we get an <code class="codecolorer text default"><span class="text">'f'</span></code> on the keyboard. <a name="keyboard-extra-cases"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; keyboard extra cases</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>#\f #\F<span style="color: #66cc66;">&#41;</span>                      <span style="color: #808080; font-style: italic;">; when we get an 'f'</span>
                                <span style="color: #808080; font-style: italic;">; save whether we're in fullscreen</span>
     <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>full <span style="color: #66cc66;">&#40;</span>fullscreen-p win<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>glut<span style="color: #66cc66;">:</span><span style="color: #555;">close</span> win<span style="color: #66cc66;">&#41;</span>         <span style="color: #808080; font-style: italic;">; close the current window</span>
       <span style="color: #66cc66;">&#40;</span>glut<span style="color: #66cc66;">:</span><span style="color: #555;">display-window</span>     <span style="color: #808080; font-style: italic;">; open a new window with fullscreen toggled</span>
           <span style="color: #66cc66;">&#40;</span>make-instance 'my-window
                          <span style="color: #66cc66;">:</span><span style="color: #555;">fullscreen</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">not</span> full<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2010/06/nehe-tutorial-02-drawing-triangles-and-quadrilaterals/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>NeHe Tutorials for CL-OpenGL</title>
		<link>http://nklein.com/2010/06/nehe-tutorials-for-cl-opengl/</link>
		<comments>http://nklein.com/2010/06/nehe-tutorials-for-cl-opengl/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 16:23:42 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[NeHe Tutorials]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[nehe]]></category>
		<category><![CDATA[opengl]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1407</guid>
		<description><![CDATA[Introduction Setting Up CL-OpenGL Basic Template Loading OpenGL Setting up OpenGL Display function Resizing the window Creating and displaying our window Introduction The Neon Helium Productions (NeHe) online tutorials (http://nehe.gamedev.net/) are the best resources available for coders trying to learn specific OpenGL rendering techniques in C/C++. The CL-OpenGL library (http://common-lisp.net/project/cl-opengl/) is, to my mind, the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://nklein.com/wp-content/uploads/2010/06/intro.png"><img src="http://nklein.com/wp-content/uploads/2010/06/intro-300x241.png" alt="" title="intro.png" width="300" height="241" class="alignright size-medium wp-image-1465" /></a></p>
<ul>
<li><a href="#toc3">Introduction</a></li>
<li><a href="#toc4">Setting Up CL-OpenGL</a></li>
<li><a href="#toc5">Basic Template</a>
<ul>
<li><a href="#toc6">Loading OpenGL</a></li>
<li><a href="#toc7">Setting up OpenGL</a></li>
<li><a href="#toc8">Display function</a></li>
<li><a href="#toc9">Resizing the window</a></li>
<li><a href="#toc10">Creating and displaying our window</a></li>
</ul>
</li>
</ul>
<h3><a name=toc3>Introduction</a></h3>
<p>The Neon Helium Productions (NeHe) online tutorials (<a href="http://nehe.gamedev.net/">http://nehe.gamedev.net/</a>) are the best resources available for coders trying to learn specific OpenGL rendering techniques in C/C++.  The CL-OpenGL library (<a href="http://common-lisp.net/project/cl-opengl/">http://common-lisp.net/project/cl-opengl/</a>) is, to my mind, the most straightforward mapping of the OpenGL, GLU, and GLUT APIs into Common Lisp.  In this series, I hope to combine the best of both so that the aspiring Lisp coder can quickly access these OpenGL techniques. </p>
<p>I am aware that someone else reworked the first six tutorials for CL-OpenGL.  However, I can&#8217;t track those down any longer. The original website is gone.  I am also aware that I won&#8217;t have a great deal of time for this sort of coding in the near future, but I hope to tackle a bunch of these this summer.  Most of the tutorials are fairly short. </p>
<p>In this first tutorial, we&#8217;re going to generate a simple template file that opens an OpenGL window. <a name="*.lisp"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; *.lisp</span>
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;glut-template.lisp&quot;</span><span style="color: #66cc66;">&gt;</span></pre></div>
<p></a></p>
<p>Here is the resulting: <a href="http://nklein.com/wp-content/uploads/2010/06/intro.lisp">intro.lisp</a>.</p>
<h3><a name=toc4>Setting Up CL-OpenGL</a></h3>
<p>To get started with CL-OpenGL, you will need a Lisp implementation that supports ASDF and CFFI, a git client, and OpenGL libraries.  I will probably flesh this section out at some later date.  For now, I am just barely going to touch on the prerequisites. </p>
<p><b>Lisp implementation.</b> </p>
<p><b>CFFI.</b> </p>
<p><b>git client.</b> </p>
<p><b>OpenGL libraries.</b>  (Under Windows, you may need FreeGlut.dylib.) </p>
<p>Once you have all of the above, you will need to clone the CL-OpenGL repository so that you have the sources on your machine.  The CL-OpenGL git repository is <a href="http://github.com/3b/cl-opengl.git">http://github.com/3b/cl-opengl.git</a>. </p>
<p><a name="clone-repository.sh"></p>
<div class="codecolorer-container bash blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># clone-repository.sh</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>where<span style="color: #000000; font-weight: bold;">/</span>you<span style="color: #000000; font-weight: bold;">/</span>want<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>put<span style="color: #000000; font-weight: bold;">/</span>the<span style="color: #000000; font-weight: bold;">/</span>cl-opengl<span style="color: #000000; font-weight: bold;">/</span>sources
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">git</span> clone http:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>3b<span style="color: #000000; font-weight: bold;">/</span>cl-opengl.git</pre></div>
<p></a></p>
<p>You need to ensure that the <code class="codecolorer text default"><span class="text">cl-opengl/</span></code> directory that you just created is included in your <code class="codecolorer text default"><span class="text">asdf:*central-registry*</span></code> list.  For example, I have this in my <code class="codecolorer text default"><span class="text">~/.sbclrc</span></code> file <a name="asdf-prep.lisp"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; asdf-prep.lisp</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">dolist</span> <span style="color: #66cc66;">&#40;</span>subdir <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> <span style="color: #808080; font-style: italic;">;; ... some other packages ...</span>
                      #P<span style="color: #ff0000;">&quot;cl-opengl/&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>push <span style="color: #66cc66;">&#40;</span>merge-pathnames subdir #P<span style="color: #ff0000;">&quot;/usr/local/asdf-install/site/&quot;</span><span style="color: #66cc66;">&#41;</span>
        asdf<span style="color: #66cc66;">:</span>*central-registry*<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<h3><a name=toc5>Basic Template</a></h3>
<p> Once you have CL-OpenGL installed, you&#8217;ll be able to use the following template. <a name="glut-template.lisp"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; glut-template.lisp</span>
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;load opengl&quot;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defclass my-window <span style="color: #66cc66;">&#40;</span>glut<span style="color: #66cc66;">:</span><span style="color: #555;">window</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">default-initargs</span> <span style="color: #66cc66;">:</span><span style="color: #555;">width</span> <span style="color: #cc66cc;">400</span> <span style="color: #66cc66;">:</span><span style="color: #555;">height</span> <span style="color: #cc66cc;">300</span>
                     <span style="color: #66cc66;">:</span><span style="color: #555;">title</span> <span style="color: #ff0000;">&quot;My Window Title&quot;</span>
                     <span style="color: #66cc66;">:</span><span style="color: #555;">x</span> <span style="color: #cc66cc;">100</span> <span style="color: #66cc66;">:</span><span style="color: #555;">y</span> <span style="color: #cc66cc;">100</span>
                     <span style="color: #66cc66;">:</span><span style="color: #555;">mode</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">double</span> <span style="color: #66cc66;">:</span><span style="color: #555;">rgb</span> <span style="color: #66cc66;">:</span><span style="color: #555;">depth</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;initialization method&quot;</span><span style="color: #66cc66;">&gt;</span>
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;additional glut methods&quot;</span><span style="color: #66cc66;">&gt;</span>
&nbsp;
#<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;create an instance of our window&quot;</span><span style="color: #66cc66;">&gt;</span></pre></div>
<p></a></p>
<h4><a name=toc6>Loading OpenGL</a></h4>
<p> Usually, we&#8217;re going to load OpenGL, GLU, and GLUT. <a name="load-opengl"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; load opengl</span>
<span style="color: #66cc66;">&#40;</span>require <span style="color: #66cc66;">:</span><span style="color: #555;">asdf</span><span style="color: #66cc66;">&#41;</span>                 <span style="color: #808080; font-style: italic;">; need ASDF to load other things</span>
<span style="color: #66cc66;">&#40;</span>asdf<span style="color: #66cc66;">:</span><span style="color: #555;">load-system</span> <span style="color: #66cc66;">:</span><span style="color: #555;">cl-opengl</span><span style="color: #66cc66;">&#41;</span>   <span style="color: #808080; font-style: italic;">; load OpenGL bindings</span>
<span style="color: #66cc66;">&#40;</span>asdf<span style="color: #66cc66;">:</span><span style="color: #555;">load-system</span> <span style="color: #66cc66;">:</span><span style="color: #555;">cl-glu</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">; load GLU bindings</span>
<span style="color: #66cc66;">&#40;</span>asdf<span style="color: #66cc66;">:</span><span style="color: #555;">load-system</span> <span style="color: #66cc66;">:</span><span style="color: #555;">cl-glut</span><span style="color: #66cc66;">&#41;</span>     <span style="color: #808080; font-style: italic;">; load GLUT bindings</span></pre></div>
<p></a></p>
<h4><a name=toc7>Setting up OpenGL</a></h4>
<p>Our initialization method can do anything we need to do in terms of loading textures or fonts or what-have-you.  We could do some with the normal CLOS <code class="codecolorer text default"><span class="text">initialize-object</span></code> method if it is stuff we can do before OpenGL is initialized.  For our purposes though, we need to wait until after OpenGL is ready but before our window is displayed so we make sure to go before the <code class="codecolorer text default"><span class="text">glut:display-window</span></code> call. <a name="initialization-method"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; initialization method</span>
<span style="color: #66cc66;">&#40;</span>defmethod glut<span style="color: #66cc66;">:</span><span style="color: #555;">display-window</span> <span style="color: #66cc66;">:</span><span style="color: #555;">before</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>win my-window<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;prepare opengl&quot;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>To prepare the default OpenGL environment that we&#8217;re going to use, we&#8217;re first going to turn on smooth shading.  This allows colors to blend across our polygons.  Later tutorials will go into more detail about smooth shading. <a name="prepare-opengl"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; prepare opengl</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">shade-model</span> <span style="color: #66cc66;">:</span><span style="color: #555;">smooth</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; enables smooth shading</span></pre></div>
<p></a></p>
<p>The next line here sets the color used to clear the screen.  OpenGL color values range from zero to one with zero being the darkest and one being the brightest.  The parameters here are (in order) the red, green, blue, and alpha channels.  The alpha channel doesn&#8217;t really come into play when clearing the screen, so it doesn&#8217;t much matter in this instance.  We&#8217;re going to use a black background. <a name="prepare-opengl-1"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; prepare opengl (cont.)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">clear-color</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>        <span style="color: #808080; font-style: italic;">; background will be black</span></pre></div>
<p></a></p>
<p>The next several lines prepare the depth buffer.  OpenGL keeps a variety of buffers that are the same dimensions as your window. You were probably expecting the color buffer that stores the actual pixel values that are rendered on the screen.  The depth can be used to keep track of the depth of the last item drawn to the screen or to prevent an object from drawing if it doesn&#8217;t have a depth thats less than the current value of the depth buffer for the current pixel. <a name="prepare-opengl-2"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; prepare opengl (cont.)</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">clear-depth</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>              <span style="color: #808080; font-style: italic;">; clear buffer to maximum depth</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">enable</span> <span style="color: #66cc66;">:</span><span style="color: #555;">depth-test</span><span style="color: #66cc66;">&#41;</span>         <span style="color: #808080; font-style: italic;">; enable depth testing</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">depth-func</span> <span style="color: #66cc66;">:</span><span style="color: #555;">lequal</span><span style="color: #66cc66;">&#41;</span>         <span style="color: #808080; font-style: italic;">; okay to write pixel if its depth</span>
                                <span style="color: #808080; font-style: italic;">; is less-than-or-equal to the</span>
                                <span style="color: #808080; font-style: italic;">; depth currently written</span></pre></div>
<p></a></p>
<p>We are also going to tell OpenGL that we&#8217;d like it to make things in perspective look as nice as possible. <a name="prepare-opengl-3"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; prepare opengl (cont.)</span>
                                <span style="color: #808080; font-style: italic;">; really nice perspective correction</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">hint</span> <span style="color: #66cc66;">:</span><span style="color: #555;">perspective-correction-hint</span> <span style="color: #66cc66;">:</span><span style="color: #555;">nicest</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<h4><a name=toc8>Display function</a></h4>
<p>Almost all applications will need at least a GLUT display function.  Usually, they will do more than this, but this will get us started. <a name="additional-glut-methods"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; additional glut methods</span>
<span style="color: #66cc66;">&#40;</span>defmethod glut<span style="color: #66cc66;">:</span><span style="color: #555;">display</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>win my-window<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">clear</span> <span style="color: #66cc66;">:</span><span style="color: #555;">color-buffer-bit</span> <span style="color: #66cc66;">:</span><span style="color: #555;">depth-buffer-bit</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">load-</span><span style="color: #b1b100;">identity</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<h4><a name=toc9>Resizing the window</a></h4>
<p> The following method gets called when your window is first created and any time your window is resized.  We will use it to prepare our projection matrix. <a name="additional-glut-methods-1"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; additional glut methods (cont.)</span>
<span style="color: #66cc66;">&#40;</span>defmethod glut<span style="color: #66cc66;">:</span><span style="color: #555;">reshape</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>win my-window<span style="color: #66cc66;">&#41;</span> width height<span style="color: #66cc66;">&#41;</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;glut reshape -- prepare viewport&quot;</span><span style="color: #66cc66;">&gt;</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;glut reshape -- prepare projection&quot;</span><span style="color: #66cc66;">&gt;</span>
  #<span style="color: #66cc66;">&lt;:</span><span style="color: #555;">use</span> <span style="color: #ff0000;">&quot;glut reshape -- switch to model view&quot;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>To initialize the viewport, we simply take the given width and height and use them as the horizontal and vertical extents of our coordinate system. <a name="glut-reshape----prepare-viewport"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; glut reshape -- prepare viewport</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">viewport</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> width height<span style="color: #66cc66;">&#41;</span>  <span style="color: #808080; font-style: italic;">; reset the current viewport</span></pre></div>
<p></a></p>
<p>Next, we&#8217;re going to prepare the projection matrix.  We&#8217;re going to set things up for a perspective view so that distant objects appear smaller than closer objects.  We&#8217;re going to assume that the window accounts for a 45-degree field of view from left to right.  We&#8217;re going to assume that objects in our scene can be anywhere from 1/10 to 100 units in front of our viewpoint. </p>
<p>First, we switch into the mode where matrix commands will change the projection matrix.  Then, we make sure we&#8217;re starting from the identity matrix.  Then, we prepare our perspective transformation assuming 45-degrees from left-to-right and a proportional amount from top-to-bottom (taking care not to divide by zero in the proportion). <a name="glut-reshape----prepare-projection"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; glut reshape -- prepare projection</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">matrix-mode</span> <span style="color: #66cc66;">:</span><span style="color: #555;">projection</span><span style="color: #66cc66;">&#41;</span>    <span style="color: #808080; font-style: italic;">; select the projection matrix</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">load-</span><span style="color: #b1b100;">identity</span><span style="color: #66cc66;">&#41;</span>              <span style="color: #808080; font-style: italic;">; reset the matrix</span>
&nbsp;
<span style="color: #808080; font-style: italic;">;; set perspective based on window aspect ratio</span>
<span style="color: #66cc66;">&#40;</span>glu<span style="color: #66cc66;">:</span><span style="color: #555;">perspective</span> <span style="color: #cc66cc;">45</span> <span style="color: #66cc66;">&#40;</span>/ width <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">max</span> height <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span>/<span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
<p>Once we are done setting up the projection matrix, we need to switch back to the model-view matrix so that further transforms will affect the space we&#8217;re viewing rather than where we are viewing things from. <a name="glut-reshape----switch-to-model-view"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; glut reshape -- switch to model view</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">matrix-mode</span> <span style="color: #66cc66;">:</span><span style="color: #555;">modelview</span><span style="color: #66cc66;">&#41;</span>     <span style="color: #808080; font-style: italic;">; select the modelview matrix</span>
<span style="color: #66cc66;">&#40;</span>gl<span style="color: #66cc66;">:</span><span style="color: #555;">load-</span><span style="color: #b1b100;">identity</span><span style="color: #66cc66;">&#41;</span>              <span style="color: #808080; font-style: italic;">; reset the matrix</span></pre></div>
<p></a></p>
<h4><a name=toc10>Creating and displaying our window</a></h4>
<p> To create and display an instance of our window, we simply go ahead and create and instance and pass it to <code class="codecolorer text default"><span class="text">glut:display-window</span></code>. <a name="create-an-instance-of-our-window"></p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;; create an instance of our window</span>
<span style="color: #66cc66;">&#40;</span>glut<span style="color: #66cc66;">:</span><span style="color: #555;">display-window</span> <span style="color: #66cc66;">&#40;</span>make-instance 'my-window<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p></a></p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2010/06/nehe-tutorials-for-cl-opengl/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Fourier Transforms in JavaScript</title>
		<link>http://nklein.com/2009/09/fourier-transforms-in-javascript/</link>
		<comments>http://nklein.com/2009/09/fourier-transforms-in-javascript/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 17:07:52 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[imgui]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[opengl]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=789</guid>
		<description><![CDATA[In preparation for a study group on Numeric Photography, I started toying with what language to use for the coding projects. Feature shopping My inclination, of course, was to go with Lisp. Part of the goal of the assignments, however, was to make interactive web applications for each assignment and a gallery at the end [...]]]></description>
			<content:encoded><![CDATA[<p>In preparation for <a href="https://www.ibm.com/developerworks/mydeveloperworks/blogs/algomusic/entry/numeric_photography2">a study group</a> on <a href="http://ocw.mit.edu/OcwWeb/Media-Arts-and-Sciences/MAS-961Fall1998/CourseHome/index.htm">Numeric Photography</a>, I started toying with what language to use for the coding projects.</p>
<h3>Feature shopping</h3>
<p>My inclination, of course, was to go with Lisp.  Part of the goal of the assignments, however, was to make interactive web applications for each assignment and a gallery at the end of the completed assignments.  I experimented a bit with <a href="http://common-lisp.net/project/armedbear/">Armed Bear Common Lisp</a>.  The hurdles that I would need a user to go through to use my application safely were too great.  The user would have to edit their own java.policy file to give my applet permission.  If I wanted to let the user do that with a single click or something, I would have to spring for a $200/year code-signing cert.</p>
<p>My next choice then was stand-alone Lisp applications.  Rather than online, interactive applications, I would have downloadable ones.  Not ideal, but doable.  The big hurdle here is crafting a GUI to run the application.  The GUI wouldn&#8217;t be too terrible thanks to an interesting concept that Dirk Gerrits pointed out:  <a href="http://mollyrocket.com/861">Immediate Mode GUIs</a>.  I have already implemented the buttons from <a href="http://sol.gfxile.net/imgui/">this tutorial</a> in OpenGL under SBCL.</p>
<p>Late one night, I remembered <a href="http://common-lisp.net/project/parenscript/">Parenscript</a>.  It is a Lisp-like language that compiles to Javascript.  Parenscript was a little bit more its own thing and a little bit less Lisp than I had wanted.  But, it got me thinking.  What can you do in Javascript these days?</p>
<p>I searched for <a href="http://www.google.com/search?client=safari&#038;rls=en&#038;q=image+processing+javascript&#038;ie=UTF-8&#038;oe=UTF-8">image processing javascript</a>.  The first hit was <a href="http://www.pixastic.com/">Pixastic</a>.  It is a library of basic image processing functions that you can run on any image on your page.  At its heart, it uses the Canvas element that is available on Safari, Firefox, and Opera.  So, I figured, why not?  If I can get an FFT working in Javascript, then there is nothing keeping me from implementing all of the assignments in Javascript.  Yes, Javascript is awful, but it&#8217;s a dream compared to Java.  The new Javascript debugging features in Safari are quite useful, too.</p>
<h3>Success</h3>
<p>Below is a simple example of what I have working.  When you first load the page, you see an image of my son and my dad at the Pittsburgh Zoo.  This image is actually loaded in a hidden image and copied into a visible canvas.  You are seeing the canvas.</p>
<p>If you press the <q>FFT</q> button and wait for it, you will see a representation of the 2-D Discrete Fourier Transform of the image.  Really, the DFT output is complex numbers.  The representation is just the magnitude of the complex numbers.  The actual complex numbers are kept around in a variable so that when you hit the <q>IFFT</q> button, it has enough information to actually reconstruct the image.  So, the <q>FFT</q> button works from the pixels in the canvas.  The <q>IFFT</q> button works from the data generated with the last <q>FFT</q>.  So, unfortunately, you cannot meaningfully FFT-FFT-IFFT-IFFT here.  It is interesting, but not meaningful.</p>
<p>Also, I was trying to add a text box here so that you could use your own image, but I am not getting it right.  You&#8217;re going to have to wait until I finish the whole assignment.</p>
<p><script type="text/javascript" src="http://nklein.com/wp-content/uploads/2009/09/fft.js"></script><br />
<script type="text/javascript">
var _fftData;
</script><br />
<canvas id="the_canvas" width="256" height="256"><br />
      You need a browser that supports the Canvas tag.<br />
      Firefox, Safari, and Opera should all work.<br />
</canvas></p>
<div style="visibility: hidden; width: 0; height: 0;"><img id="the_original" style="visibility: collapse;" src="http://nklein.com/wp-content/uploads/2009/09/ppz.jpg" onload="return prepImageForFFT('the_original','the_canvas');"></img></div>
<form method="post" action="#" onsubmit="loadNewImage('the_original'); return false;">
<input type="button" name="do_fft" value="FFT" onclick="_fftData = FFT( 'the_canvas' ); return true;" />
<input type="button" name="do_ifft" value="IFFT" onclick="IFFT( _fftData, 'the_canvas' ); return true;" />
</form>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/09/fourier-transforms-in-javascript/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>A Different Look at the Mandelbrot Set</title>
		<link>http://nklein.com/2009/05/a-different-look-at-the-mandelbrot-set/</link>
		<comments>http://nklein.com/2009/05/a-different-look-at-the-mandelbrot-set/#comments</comments>
		<pubDate>Thu, 14 May 2009 22:28:40 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[opengl]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=365</guid>
		<description><![CDATA[When you see pictures of the Mandelbrot set you are seeing the results of iterating a function over and over. For the Mandelbrot set, you take a complex number . You square it and add the original number. You take that, square it, and add the original. You do this a number of times. If [...]]]></description>
			<content:encoded><![CDATA[<p>When you see <a href="http://images.google.com/images?client=safari&#038;rls=en-us&#038;q=Mandelbrot&#038;oe=UTF-8&#038;um=1&#038;ie=UTF-8&#038;ei=XpkMSvTGEoTEtAOG5bj3Ag&#038;sa=X&#038;oi=image_result_group&#038;resnum=4&#038;ct=title">pictures</a> of the <a href="http://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot set</a> you are seeing the results of iterating a function over and over.</p>
<p>For the Mandelbrot set, you take a complex number <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_08c1898a88651cdd0a9fdb2d6ce6e8a1.png" title="c" style="vertical-align:-20%;" class="tex" alt="c" />.  You square it and add the original number.  You take that, square it, and add the original.  You do this a number of times.  If the value stays relatively close to zero, the number is in the Mandelbrot set.  If the value takes off away from zero, the number is not in the Mandelbrot set.  Typically, one colors in the parts of the picture that aren&#8217;t in the Mandelbrot set with some color that indicates just how fast it hightailed it out of there.</p>
<p>I wanted to try something different.  I wanted to see what the iteration process looks like.  So, I threw together some code using Lisp with <a href="http://common-lisp.net/project/cl-opengl/">cl-opengl</a>.</p>
<p><a href="http://nklein.com/wp-content/uploads/2009/05/fibers.png"><img src="http://nklein.com/wp-content/uploads/2009/05/fibers-300x235.png" alt="fibers" title="fibers" width="300" height="235" class="alignright size-medium wp-image-368" /></a>The first visualization that I coded was to start with tiny patches of the complex plane.  From each patch, I would extrude it up out of the plane and over to its next iteration.  The picture at the right is (IIRC) a patch with side-length one half centered at <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_5477186a84cc2c889974ca6fd01ca96f.png" title="i" style="vertical-align:-20%;" class="tex" alt="i" /> with either 25 or 36 patches tiled in that area.  As you can see, this isn&#8217;t a wholly satisfying picture of what&#8217;s going on.</p>
<p>The other idea that I had was to show the progress through iterations as an animation.  This is a bit more interesting to look at until some of the patches start overflowing my floating point numbers or get too big for my clipping region.  This is an area of side-length one centered at the origin with four-hundred patches.  Here is a <a href="http://nklein.com/wp-content/uploads/2009/05/flow.mov" >movie of that flow</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2009/05/a-different-look-at-the-mandelbrot-set/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://nklein.com/wp-content/uploads/2009/05/flow.mov" length="17735079" type="video/quicktime" />
		</item>
	</channel>
</rss>

