<?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>Last Stop &#187; video</title>
	<atom:link href="http://laststop.spaceislimited.org/tag/video/feed/" rel="self" type="application/rss+xml" />
	<link>http://laststop.spaceislimited.org</link>
	<description>it&#039;s going to be a long ride</description>
	<lastBuildDate>Wed, 28 Jan 2009 13:21:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Programming Pong in C and OpenGL &#8211; Part IV</title>
		<link>http://laststop.spaceislimited.org/2008/07/01/programming-pong-in-c-and-opengl-part-iv/</link>
		<comments>http://laststop.spaceislimited.org/2008/07/01/programming-pong-in-c-and-opengl-part-iv/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 13:00:29 +0000</pubDate>
		<dc:creator>Timothy M. Rodriguez</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[glut]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[pong]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://laststop.spaceislimited.org/?p=21</guid>
		<description><![CDATA[OpenGL OpenGL is the Open Graphics Library and it is maintained by the Kronos Group, which is a consortium of top-tier companies that include board members such as ATI nVidia, Apple, IBM, and more. Microsoft used to be on the consortium, but left and decided to make DirectX, a competing closed graphics library that only [...]]]></description>
			<content:encoded><![CDATA[<h2>OpenGL</h2>
<p>OpenGL is the Open Graphics Library and it is maintained by the Kronos Group, which is a consortium of top-tier companies that include board members such as ATI nVidia, Apple, IBM, and more.  Microsoft used to be on the consortium, but left and decided to make DirectX, a competing closed graphics library that only runs on Windows (at least without some sort of emulation layer or wrapper like Wine or Cedega).</p>
<p><span id="more-21"></span></p>
<p>First off, OpenGL is a huge thing to master (and I am nowhere near mastery).  I can’t explain everything about it in one post (or even in hundreds), but I strongly encourage you to get the OpenGL “<a type="amzn">Red Book</a>” to start out.  The “Red Book” is the official and definitive guide to OpenGL and is a must have for any graphics programmer.  I also recommend the <a type="amzn">K&amp;R C Programming book</a> and, if you’re interested in the back-end math, the <a type="amzn">Real-Time Rendering book</a>.</p>
<p>That being said, in our pong clone, we only use a few OpenGL functions.  I’m going to  list them and tell you what they do, but it may take until the next post to really understand how they work.</p>
<p>First, a word about primitives.  OpenGL has only a few key primitives that you can draw with, but that&#8217;s okay, because you can use the primitives to draw more complex shapes!</p>
<p>The OpenGL primitives are:</p>
<ul>
<li>Points &#8211; a simple point in space</li>
<li>Lines &#8211; a 2-dimension line (although it can occupy 3-dimensional space)</li>
<li>Polygons &#8211; a polygon is a simple 2-dimensional shape comprised of 3 or more points.  Triangles, or 3-point polygons, deserve special attention, because OpenGL will break up any other polygon into a combination of triangles.  For example, a square can be broken up into two triangles along its diagonal.  Just about every object you see in OpenGL is comprised of triangles!</li>
</ul>
<p>So let&#8217;s draw a simple quadrilateral!  In OpenGL, you draw polygons by calling a function whenever you want to draw a polygon and then creating a bunch of points in space to create that polygon.  Once you&#8217;re done, you call another function to say you&#8217;re done.</p>
<p>Here&#8217;s an example for drawing a rectangle (our pong paddle):</p>
<blockquote><p><span>glBegin</span><span>(</span>GL_QUADS<span>);</span></p>
<p><span><span> </span></span>glVertex2f<span>(-</span><span>10</span><span>, -</span><span>50</span><span>);</span></p>
<p><span><span> </span></span>glVertex2f<span>(-</span><span>10</span><span>, </span><span>50</span><span>);</span></p>
<p><span><span> </span></span>glVertex2f<span>(</span><span>10</span><span>, </span><span>50</span><span>);</span></p>
<p><span><span> </span></span>glVertex2f<span>(</span><span>10</span><span>, -</span><span>50</span><span>);</span></p>
<p><span><span> </span></span>glEnd<span>();</span></p></blockquote>
<p>We start by calling glBegin() and sending it GL_QUADS to indicate we want to draw a quadrilateral.  This basically tells OpenGL take every four points I send you and make a quad out of it.  We then call glVertex2f() and send it the coordinates of a two-dimensional point.  the 2f indicates that this is the glVertex function which takes 2 floating point values as its arguments (or inputs).  We then call glVertex2f() once for all four of each of our points and then call glEnd() when we&#8217;re done.</p>
<p>We could have optionally set a color before we began to draw that quadrilateral (the default is black on black) by calling this function:</p>
<blockquote><p>glColor3ub<span>(</span><span>255</span><span>, </span><span>0</span><span>, </span><span>0</span><span>);<span> </span></span><span>//red</span></p></blockquote>
<p>Again we see the OpenGL naming convention of starting off a function with gl and then ending it with letters and numbers that indicate its arguments.  In this case the 3ub means 3 unsigned bytes.  Each unsigned byte is positive (hence unsigned) and can go from 0 to 255.  The order of the bytes indicate the amount of Red, Green, and Blue respectively that will mix to give you your final color.  (Color theory is another big subject matter I can post more about in another post, so let me know in the comments!)</p>
<p>Two other functions that are usually called at the beginning of every display function are:</p>
<blockquote><p><span>glClear</span><span>(</span>GL_COLOR_BUFFER_BIT<span> | </span>GL_DEPTH_BUFFER_BIT<span>);</span></p>
<p><span><span> </span></span>glShadeModel<span>(</span><span>GL_FLAT</span><span>);</span></p></blockquote>
<p>glClear(), quite simply is a function that clears the screen and uses the values specified as its argument for doing so.</p>
<p>glShadeModel() specifies the shading model for how vertexes (points) are shaded or lit.  In this case, we&#8217;re going for a simple flat colored 2-d look so we use GL_FLAT.  This paints everything on screen with whatever colors specified and doesn&#8217;t do anything fancy.  It&#8217;s sort of like painting in mspaint on Windows.</p>
<blockquote><p>glPushMatrix<span>();</span></p>
<p>glPopMatrix();</p>
<p><span><span> </span></span>glTranslatef<span>(</span><span>30.0f</span><span>, 10.</span><span>0f, 0.0f</span><span>);</span></p></blockquote>
<p>glPush and Pop Matrix() are important functions.  They allow you to draw something as if you are starting from scratch with a new coordinate system.  Think about our pong paddle.  When you want to draw it some place else, do you really want to respecify the coordinates for how it&#8217;s drawn?  You could instead <em>push</em> a matrix (a set of points) on the stack, then <em>translate</em> (or move), to a new location and draw the object as you would anywhere else, and then finally <em>pop</em> the matrix which will draw the set of points translated (moved) by the glTranslatef() function.  This is a little hard to understand (and explain), so I&#8217;ll give an example.</p>
<p>In Part II we talked about drawing a box that moved across the screen.  We could draw our box as a point in a 2-dimensional plane.  For example, let&#8217;s make a square that is 4 across centered at the origin (0,0).  So it&#8217;s points are, going counter-clockwise, (1,1), (-1,1), (-1,-1), (1,-1).  Rather than figuring out how to move it along for every frame.  We can do this.</p>
<blockquote><p>glPushMatrix<span>();</span></p>
<p><span> </span></p>
<p><span><span> </span></span>glColor3ub<span>(</span><span>255</span><span>, </span><span>0</span><span>, </span><span>0</span><span>);<span> </span></span><span>//red</span></p>
<p><span> </span></p>
<p><span><span> </span></span>glTranslatef<span>(</span><span>x, 0.0f, </span><span>0.0f</span><span>);</span></p>
<p><span> </span></p>
<p><span><span> </span></span><span>glBegin</span><span>(</span>GL_QUADS<span>);</span></p>
<p><span><span> </span></span>glVertex2f<span>(1,1</span><span>);</span></p>
<p><span><span> </span></span>glVertex2f<span>(-1,1</span><span>);</span></p>
<p><span><span> </span></span>glVertex2f<span>(</span><span>-1,-1</span><span>);</span></p>
<p><span><span> </span></span>glVertex2f<span>(</span><span>1,-1</span><span>);</span></p>
<p><span><span> </span></span>glEnd<span>();</span></p>
<p><span> </span></p>
<p><span><span> </span></span>glPopMatrix<span>();</span></p></blockquote>
<p>So we push a matrix (a set of points) on the stack, switch the drawing color to red, then Translate x units in the x-direction (horizontal) and then set the drawing mode to GL_QUADS and push the points of our square as it is centered at the origin, and finally pop the matrix!  This has OpenGL do all the work of moving our square x units to the right.</p>
<p>Okay, that&#8217;s enough for now.  I encourage you to scour the web and read up more before we get to our next post, where I&#8217;ll finally post the code and go through it!</p>
]]></content:encoded>
			<wfw:commentRss>http://laststop.spaceislimited.org/2008/07/01/programming-pong-in-c-and-opengl-part-iv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

