<?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 Ray of Hope &#187; engine</title>
	<atom:link href="http://www.lastrayofhope.com/tag/engine/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lastrayofhope.com</link>
	<description>Home of Kaluriel Hargrove</description>
	<lastBuildDate>Thu, 02 Feb 2012 00:21:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Athena: Word Wrap</title>
		<link>http://www.lastrayofhope.com/2009/09/19/athena-word-wrap/</link>
		<comments>http://www.lastrayofhope.com/2009/09/19/athena-word-wrap/#comments</comments>
		<pubDate>Sat, 19 Sep 2009 16:31:46 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=2806</guid>
		<description><![CDATA[One thing I hate doing is writing classes that will output text, as well as handle special character sequences that get replaced by images. And while all this is happening, having it word wrap as well. So I wrote a quick word wrap template class today that will do this. It doesn't give line spacing [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2009/09/athena_word_wrap_01.png"><img class="alignleft size-medium wp-image-2807" title="Athena: Word Wrap" src="http://www.lastrayofhope.com/wp-content/uploads/2009/09/athena_word_wrap_01-300x187.png" alt="Athena: Word Wrap" width="180" height="112" /></a>One thing I hate doing is writing classes that will output text, as well as handle special character sequences that get replaced by images. And while all this is happening, having it word wrap as well.</p>
<p>So I wrote a quick word wrap template class today that will do this. It doesn't give line spacing yet, which I'm still unsure whether or not it should be this.</p>
<p>The way it works is when you create the word wrap object, you can pass to it's constructor a value for the maximum width a line can be, this can be a floating point value, or an integer (it defaults to integer).</p>
<p>Then you call Parse() and give it a string, it will go through that string, checking the width of characters until the accumulated character widths are greater than the maximum width, or a new line is found. It will output this line of characters and move onto the next bit.</p>
<p>If it finds a word that is longer than maximum width, it will do one of two things. First if it is at the start of a newline, it will cut the word at the point it goes over. If it is already part way into a line, it will cause a new line and put the word on the next line.</p>
<p>I've included with this blog entry the header for the word wrap class, it comes as part of a test project in Xcode (it uses the <a href="http://www.lipsum.com/" target="_blank">Lorem Ipsum</a> text), but the source and headers should work in another C++ IDE.</p>
Download Source: <a href="http://www.lastrayofhope.com/wp-content/plugins/download-monitor/download.php?id=29" title="Athena Word Wrap Source (Version 1.0.0)">Athena Word Wrap Source (Version 1.0.0)</a>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2009/09/19/athena-word-wrap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Athena: Bezier Curves</title>
		<link>http://www.lastrayofhope.com/2009/09/10/athena-bezier-curves/</link>
		<comments>http://www.lastrayofhope.com/2009/09/10/athena-bezier-curves/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 16:53:22 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=2835</guid>
		<description><![CDATA[When animations are exported from Maya through COLLADA files, they come out in a variety of formats depending on how the animation was implemented, and what is being animated. One format is the bezier curve, a curve made from four points. Two fo the points are the start and end position, and the other two [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2009/09/800px-Bezier_curve.svg.png"><img class="alignleft size-medium wp-image-2836" title="Bezier Curve" src="http://www.lastrayofhope.com/wp-content/uploads/2009/09/800px-Bezier_curve.svg-300x187.png" alt="Bezier Curve" width="180" height="112" /></a>When animations are exported from Maya through COLLADA files, they come out in a variety of formats depending on how the animation was implemented, and what is being animated.</p>
<p>One format is the bezier curve, a curve made from four points. Two fo the points are the start and end position, and the other two are control points that describe how it should curve.</p>
<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2009/09/bezier_curve_01.png"><img class="alignright size-medium wp-image-2844" title="Bezier Curve Application" src="http://www.lastrayofhope.com/wp-content/uploads/2009/09/bezier_curve_01-284x300.png" alt="Bezier Curve Application" width="170" height="180" /></a>Since all three axis can be animated, I made it possible to make a 3-component vector bezier class using the bezier class I created, and just calculate the point coefficients once, passing them to the bezier class to get the position. This of course only works if the time length for curves are the same on all three axis.</p>
<p>I've made a sample, it is an Xcode project that uses GLUT to render a 2D bezier and a 3D bezier. The code should work with other IDEs. One thing I should warn is that this bezier class only works when the time step is constant between the control point and it's respective point.</p>
<blockquote><p>P0 (0, 0) ... keypoint 0<br />
P1 (2, 10) ... keypoint 1<br />
C0 (0.666667, 42.6212) ... control point 0<br />
C1 (1.33333, 10) ... control point 1</p>
<p>T0 = (C0 - P0) -&gt; (2, 42.6212)<br />
T1 = (P1 - C1) -&gt; (2, 0)</p></blockquote>
<p>The above sample is from the <a href="https://collada.org/public_forum/viewtopic.php?f=12&amp;t=1132" target="_blank">COLLADA forum</a>, as you can see the time step is 2 for both T0 and T1. The same post describes how to calculate the S0 if your time step is not constant.</p>
Download Source: <a href="http://www.lastrayofhope.com/wp-content/plugins/download-monitor/download.php?id=30" title="Athena Bezier Source (Version 1.0.0)">Athena Bezier Source (Version 1.0.0)</a>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2009/09/10/athena-bezier-curves/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Athena: Wiimote and Mac OS X</title>
		<link>http://www.lastrayofhope.com/2009/07/01/athena-wiimote-and-mac-os-x/</link>
		<comments>http://www.lastrayofhope.com/2009/07/01/athena-wiimote-and-mac-os-x/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 00:33:23 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Bluetooth]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[HID]]></category>
		<category><![CDATA[MacBook]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Nintendo]]></category>
		<category><![CDATA[Wii]]></category>
		<category><![CDATA[Wiimote]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=2531</guid>
		<description><![CDATA[Since solving the problem of the rumbling on the XBox 360 pad, I've grown bored of it, and decided to branch out trying to get more controllers working in my engine. So a few days ago I started with the Wiimote, I had planned to try to the PS3 controller but I didn't have one, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2009/07/wedisk_wiimote_usb_drive1.jpg"><img class="alignleft size-medium wp-image-2532" title="Wiimote USB Drives" src="http://www.lastrayofhope.com/wp-content/uploads/2009/07/wedisk_wiimote_usb_drive1-300x221.jpg" alt="Wiimote USB Drives" width="180" height="133" /></a>Since solving the problem of the rumbling on the XBox 360 pad, I've grown bored of it, and decided to branch out trying to get more controllers working in my engine.</p>
<p>So a few days ago I started with the Wiimote, I had planned to try to the PS3 controller but I didn't have one, so borrowing one of my housemates Wiimotes I set to work.</p>
<p>There is quite a lot of documentation on the Wiimote at the moment, whether it is the correct way to do things is questionable, since there are cases like "1? - it is set to 1 by the Wii" and "set this to 0x55 at memory address X and set to 0x00 at memory address Y to enable it".</p>
<p>I started with website called <a href="http://wiki.wiimoteproject.com/" target="_blank">The Wiimote Project</a>, but as their documentation grew scarce I found a better site (which it seemed to copy it all from anyways) called <a href="http://wiibrew.org/" target="_blank">WiiBrew</a>. The WiiBrew wiki is quite comprehensive and within a few minutes I already had the LEDs flashing.</p>
<p>I won't go into a lot of detail, I'll just show the basics that I've got to grips with from the documentation on WiiBrew - LEDs + Motor, Buttons, and Battery.</p>
<h4><strong>Connecting the Wiimote</strong></h4>
<p>Connecting the Wiimote to a Mac is far simpler than it is for XBox 360, for a start its a Bluetooth device that operates using the normal protocols and it doesn't require a key to activate. To connect, first turn on Bluetooth on your Mac, and then select "<strong>Set up Bluetooth Device...</strong>".</p>
<p>Follow on the onscreen instructions, and when it gets to the point where it is searching for nearby Bluetooth devices, press <span style="text-decoration: underline;">1 + 2 to soft synchronize</span> if the Wiimote isn't bound to another device, or <span style="text-decoration: underline;">the sync button to hard synchronize</span> (unbinds the Wiimote from anything it remembers).</p>
<p>When it appears, select it and click the "<strong>Passkey Options...</strong>" button. Select the "<strong>Do not use a passkey with this device</strong>" option and then <strong>OK</strong>. Continue on and the device will be setup and connected. You can't actually tell since the LEDs are still blinking, but if you have done it correctly, the Bluetooth icon should be different, or the menu will have a disconnect option for the device just setup.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2009/07/01/athena-wiimote-and-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Athena: XBox 360 Pad and Mac OS X (cont further)</title>
		<link>http://www.lastrayofhope.com/2009/06/27/athena-xbox-360-pad-and-mac-os-x-cont-further/</link>
		<comments>http://www.lastrayofhope.com/2009/06/27/athena-xbox-360-pad-and-mac-os-x-cont-further/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 21:00:14 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Bluetooth]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[HID]]></category>
		<category><![CDATA[MacBook]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Nintendo]]></category>
		<category><![CDATA[Tattiebogle]]></category>
		<category><![CDATA[Wii]]></category>
		<category><![CDATA[Wiimote]]></category>
		<category><![CDATA[XBox]]></category>
		<category><![CDATA[XInput]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=2510</guid>
		<description><![CDATA[The past couple of week's I've really gotten into writing code that communicates with gaming devices (the Mac is limited in this part of itself). One annoyance I had was with getting rumbling working on the XBox 360 pad, eventually after essentially pinging all messages, I found and worked out the format for the rumble [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2009/06/steampunk.jpg"><img class="alignleft size-medium wp-image-2511" title="Steampunk Lincoln" src="http://www.lastrayofhope.com/wp-content/uploads/2009/06/steampunk-240x300.jpg" alt="Steampunk Lincoln" width="144" height="180" /></a>The past couple of week's I've really gotten into writing code that communicates with gaming devices (the Mac is limited in this part of itself).</p>
<p>One annoyance I had was with getting rumbling working on the XBox 360 pad, eventually after essentially pinging all messages, I found and worked out the format for the rumble message, which was annoyingly nothing like the others listed.</p>
<p>Well after that I got kinda bored with the 360 and decided to turn towards another console controller, the Wiimote. And I thought I'd write my own driver.</p>
<p>Long story short, when I was having trouble getting my driver working, I looked at the code for the driver for <a href="http://tattiebogle.net/" target="_blank">Tattiebogle's</a> XBox 360 pad driver, only to find out that it has redefined the rumble message to it's own format, namely the 4 byte one, only to forward on the settings to the 360 pad.</p>
<p>Well I'm glad I figured out why the message was different, kinda annoying that wasn't in his FAQ. Hopefully others will find this post and go "ohhhhhhhh" like I did when seeing the code.</p>
<p>As for the Wiimote, writing a driver for a Bluetooth device I've decided doesn't make sense, the device is already designed to communicate with another bluetooth device, so I made a class instead, using a Bluetooth API and documentation I found on <a href="http://www.wiibrew.org/" target="_blank">WiiBrew</a>.</p>
<p>I'll hopefully post my Xcode project for testing the 360 pad in a few days. And I'll probably write up my Wiimote stuff once I get it working the way I want. I'm really tempted to buy lots of cheap USB and Bluetooth devices on eBay now just to see if I can write drivers for them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2009/06/27/athena-xbox-360-pad-and-mac-os-x-cont-further/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Athena: XBox 360 Pad and Mac OS X (cont)</title>
		<link>http://www.lastrayofhope.com/2009/06/26/athena-xbox-360-pad-and-mac-os-x-cont/</link>
		<comments>http://www.lastrayofhope.com/2009/06/26/athena-xbox-360-pad-and-mac-os-x-cont/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 00:21:40 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[HID]]></category>
		<category><![CDATA[MacBook]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Tattiebogle]]></category>
		<category><![CDATA[XBox]]></category>
		<category><![CDATA[XInput]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=2485</guid>
		<description><![CDATA[In my previous entry, Athena: XBox 360 Pad and Mac OS X, I began work on getting the XBox 360 pad working in my game engine. I also said that I'd post how to change the LEDs and get rumble working when I figured it out. I had no trouble with getting the LEDs flashing, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2009/06/XBox360_Actuator.png"><img class="alignleft size-medium wp-image-2488" title="XBox360 Actuator Responds" src="http://www.lastrayofhope.com/wp-content/uploads/2009/06/XBox360_Actuator-300x187.png" alt="XBox360 Actuator Responds" width="180" height="112" /></a>In my previous entry, <a href="http://www.lastrayofhope.com/2009/06/12/athena-xbox-360-pad-and-mac-os-x/">Athena: XBox 360 Pad and Mac OS X</a>, I began work on getting the XBox 360 pad working in my game engine. I also said that I'd post how to change the LEDs and get rumble working when I figured it out.</p>
<p>I had no trouble with getting the LEDs flashing, but for some reason the rumble actuators just would not respond to my message with success.</p>
<p>I had searched at all other documentation on the pad on the internet, but although I found two variations of rumbling, neither worked.</p>
<p>It has taken my a week or two, but then I got the inspiration to just fire all possible messages to the pad, nullified apart from the header, and see responds with success. So I wrote some code to send 255 different type ids, with 255 different lengths.</p>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> HIDInput<span class="sy4">::</span><span class="me2">TestDevice</span><span class="br0">&#40;</span> IOHIDDeviceRef inDeviceRef <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw4">unsigned</span> <span class="kw4">char</span> reportData<span class="br0">&#91;</span>255<span class="br0">&#93;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Nullify Report</span>
	<span class="kw3">memset</span><span class="br0">&#40;</span> reportData, 0, <span class="kw3">sizeof</span><span class="br0">&#40;</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> <span class="br0">&#41;</span> <span class="sy2">*</span> 255 <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Loop through most possible messages</span>
	<span class="kw1">for</span><span class="br0">&#40;</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> reportType <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span> reportType <span class="sy1">&lt;</span> <span class="nu0">255</span><span class="sy4">;</span> <span class="sy2">++</span>reportType <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw1">for</span><span class="br0">&#40;</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> reportLength <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span> reportLength <span class="sy1">&lt;</span> <span class="nu0">255</span><span class="sy4">;</span> <span class="sy2">++</span>reportLength <span class="br0">&#41;</span>
		<span class="br0">&#123;</span>
			<span class="co1">// Setup Header</span>
			reportData<span class="br0">&#91;</span>0<span class="br0">&#93;</span> <span class="sy1">=</span> reportType<span class="sy4">;</span>
			reportData<span class="br0">&#91;</span>1<span class="br0">&#93;</span> <span class="sy1">=</span> reportLength<span class="sy4">;</span>
&nbsp;
			<span class="co1">// Send Report to Pad</span>
			<span class="kw1">if</span><span class="br0">&#40;</span> DeviceSendReport<span class="br0">&#40;</span> inDeviceRef, reportType, reportData, reportLength <span class="br0">&#41;</span> <span class="br0">&#41;</span>
			<span class="br0">&#123;</span>
				<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;Success - Type(%u), Length(%u)<span class="es1">\n</span>&quot;</span>, reportType, reportLength <span class="br0">&#41;</span><span class="sy4">;</span>
			<span class="br0">&#125;</span>
			<span class="kw1">else</span>
			<span class="br0">&#123;</span>
				<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;Failed - Type(%u), Length(%u)<span class="es1">\n</span>&quot;</span>, reportType, reportLength <span class="br0">&#41;</span><span class="sy4">;</span>
			<span class="br0">&#125;</span>
		<span class="br0">&#125;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;Finished<span class="es1">\n</span>&quot;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>I'll explain show the function body for <strong>DeviceSendReport</strong> in a minute. After running this, only two reports returned with success, one was the LED report, and the other was of Type 0 with length 4.</p>
<p>Since the header take two bytes, I guessed the last two must be Actuator A and Actuator B. And after setting one of them to 0xff, I successfully managed to get one of the actuators rumbling, and then the other actuator with the other byte.</p>
<p>Now for the code for sending reports to the pad.</p>
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">bool</span> HIDInput<span class="sy4">::</span><span class="me2">DeviceSendReport</span><span class="br0">&#40;</span> IOHIDDeviceRef inDeviceRef, <span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> inType, <span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> <span class="sy2">*</span> inReportData, <span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> inReportSize <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw1">return</span> IOHIDDeviceSetReport<span class="br0">&#40;</span> inDeviceRef, kIOHIDReportTypeOutput, inType, inReportData, inReportSize <span class="br0">&#41;</span> <span class="sy1">==</span> kIOReturnSuccess<span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>As you can see it takes a parameter called <strong>inDeviceRef</strong>, this is the value that was passed to <strong>DeviceMatchingCallback</strong> in the previous entry. And now here is the code to get the LED flashing, there are a few status values for it, so I've created an enum with some comments explaining what each do.</p>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw2">enum</span> LEDStatus
<span class="br0">&#123;</span>
	kLED_AllOff <span class="sy1">=</span> <span class="nu12">0x00</span>, <span class="co1">// All LEDs are turned off</span>
	kLED_AllBlink <span class="sy1">=</span> <span class="nu12">0x01</span>, <span class="co1">// All LEDs repeatedly blink on and off</span>
	kLED_P1_FlashOn <span class="sy1">=</span> <span class="nu12">0x02</span>, <span class="co1">// Player 1 LED flashes then stays on</span>
	kLED_P2_FlashOn <span class="sy1">=</span> <span class="nu12">0x03</span>, <span class="co1">// Player 2 LED flashes then stays on</span>
	kLED_P3_FlashOn <span class="sy1">=</span> <span class="nu12">0x04</span>, <span class="co1">// Player 3 LED flashes then stays on</span>
	kLED_P4_FlashOn <span class="sy1">=</span> <span class="nu12">0x05</span>, <span class="co1">// Player 4 LED flashes then stays on</span>
	kLED_P1_On <span class="sy1">=</span> <span class="nu12">0x06</span>, <span class="co1">// Player 1 LED turns on</span>
	kLED_P2_On <span class="sy1">=</span> <span class="nu12">0x07</span>, <span class="co1">// Player 2 LED turns on</span>
	kLED_P3_On <span class="sy1">=</span> <span class="nu12">0x08</span>, <span class="co1">// Player 3 LED turns on</span>
	kLED_P4_On <span class="sy1">=</span> <span class="nu12">0x09</span>, <span class="co1">// Player 4 LED turns on</span>
	kLED_Rotating <span class="sy1">=</span> <span class="nu12">0x0a</span>, <span class="co1">// LEDs flash on in sequence</span>
	kLED_CurrentBlink <span class="sy1">=</span> <span class="nu12">0x0b</span>, <span class="co1">// Currently active LED blinks</span>
	kLED_SlowBlink <span class="sy1">=</span> <span class="nu12">0x0c</span>, <span class="co1">// All LEDs blink slowly</span>
	kLED_Alternating <span class="sy1">=</span> <span class="nu12">0x0d</span>, <span class="co1">// Player 1 &amp; 4 alternate blink with Player 2 &amp; 3</span>
<span class="br0">&#125;</span><span class="sy4">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> HIDInput<span class="sy4">::</span><span class="me2">FlashLED</span><span class="br0">&#40;</span> IOHIDDeviceRef inDeviceRef, <span class="kw4">const</span> LEDStatus inStatus <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	<span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> kReportType <span class="sy1">=</span> <span class="nu12">0x01</span><span class="sy4">;</span>
	<span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> kReportSize <span class="sy1">=</span> <span class="nu12">0x03</span><span class="sy4">;</span>
	<span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> kReportData<span class="br0">&#91;</span>kReportSize<span class="br0">&#93;</span> <span class="sy1">=</span>
	<span class="br0">&#123;</span>
		kReportType, kReportSize, <span class="kw2">static_cast</span><span class="sy1">&lt;</span>Core<span class="sy4">::</span><span class="me2">byte</span><span class="sy1">&gt;</span><span class="br0">&#40;</span> inStatus <span class="br0">&#41;</span>
	<span class="br0">&#125;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	DeviceSendReport<span class="br0">&#40;</span> inDeviceRef, kReportType, kReportData, kReportSize <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>As you can see, it is fairly simple to send a message, all you have to remember is that the first two bytes contain a header (one byte for type, and one type for the size of the report including the header size). And finally for rumbling the motors.</p>
<div id="wpshdo_4" class="wp-synhighlighter-outer"><div id="wpshdt_4" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_4"></a><a id="wpshat_4" class="wp-synhighlighter-title" href="#codesyntax_4"  onClick="javascript:wpsh_toggleBlock(4)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_4" onClick="javascript:wpsh_code(4)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_4" onClick="javascript:wpsh_print(4)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_4" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> HIDInput<span class="sy4">::</span><span class="me2">SetRumble</span><span class="br0">&#40;</span> IOHIDDeviceRef inDeviceRef, <span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> inBigSpeed, <span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> inLittleSpeed <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	<span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> kReportType <span class="sy1">=</span> <span class="nu12">0x00</span><span class="sy4">;</span>
	<span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> kReportSize <span class="sy1">=</span> <span class="nu12">0x04</span><span class="sy4">;</span>
	<span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> kReportData<span class="br0">&#91;</span>kReportSize<span class="br0">&#93;</span> <span class="sy1">=</span>
	<span class="br0">&#123;</span>
		kReportType, kReportSize, inBigSpeed, inLittleSpeed
	<span class="br0">&#125;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	DeviceSendReport<span class="br0">&#40;</span> inDeviceRef, kReportType, kReportData, kReportSize <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>I had some minor problems with the rumble not changing after the first call to SetRumble originally, however it turned out that I had just accidently set the report data variable to be static, so the initialisation was only setup with the first values passed.</p>
<h4>Other Rumble Reports</h4>
<p>As I mentioned before, I've found other message formats for rumbling the motors, I don't know if its a version thing, or something, but here are the other two I've found if you have trouble with mine.</p>
<p><strong>Version 1 (</strong><a href="http://tattiebogle.net/index.php/ProjectRoot/Xbox360Controller/UsbInfo" target="_blank">Tattiebogle</a><strong>)<br />
</strong></p>
<div id="wpshdo_5" class="wp-synhighlighter-outer"><div id="wpshdt_5" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_5"></a><a id="wpshat_5" class="wp-synhighlighter-title" href="#codesyntax_5"  onClick="javascript:wpsh_toggleBlock(5)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_5" onClick="javascript:wpsh_code(5)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_5" onClick="javascript:wpsh_print(5)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_5" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> HIDInput<span class="sy4">::</span><span class="me2">SetRumble</span><span class="br0">&#40;</span> IOHIDDeviceRef inDeviceRef, <span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> inBigSpeed, <span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> inLittleSpeed <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	<span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> kReportType <span class="sy1">=</span> <span class="nu12">0x00</span><span class="sy4">;</span>
	<span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> kReportSize <span class="sy1">=</span> <span class="nu12">0x08</span><span class="sy4">;</span>
	<span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> kReportData<span class="br0">&#91;</span>kReportSize<span class="br0">&#93;</span> <span class="sy1">=</span>
	<span class="br0">&#123;</span>
		kReportType, kReportSize, 0x00, inBigSpeed, inLittleSpeed, 0x00, 0x00, 0x00
	<span class="br0">&#125;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	DeviceSendReport<span class="br0">&#40;</span> inDeviceRef, kReportType, kReportData, kReportSize <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p><strong>Version 2 (</strong><a href="http://euc.jp/periphs/xbox-controller.ja.html" target="_blank">http://euc.jp/periphs/xbox-controller.ja.html</a><strong>)<br />
</strong></p>
<div id="wpshdo_6" class="wp-synhighlighter-outer"><div id="wpshdt_6" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_6"></a><a id="wpshat_6" class="wp-synhighlighter-title" href="#codesyntax_6"  onClick="javascript:wpsh_toggleBlock(6)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_6" onClick="javascript:wpsh_code(6)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_6" onClick="javascript:wpsh_print(6)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_6" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> HIDInput<span class="sy4">::</span><span class="me2">SetRumble</span><span class="br0">&#40;</span> IOHIDDeviceRef inDeviceRef, <span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> inBigSpeed, <span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> inLittleSpeed <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	<span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> kReportType <span class="sy1">=</span> <span class="nu12">0x00</span><span class="sy4">;</span>
	<span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> kReportSize <span class="sy1">=</span> <span class="nu12">0x06</span><span class="sy4">;</span>
	<span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> kReportData<span class="br0">&#91;</span>kReportSize<span class="br0">&#93;</span> <span class="sy1">=</span>
	<span class="br0">&#123;</span>
		kReportType, kReportSize, 0x00, inBigSpeed, 0x00, inLittleSpeed
	<span class="br0">&#125;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	DeviceSendReport<span class="br0">&#40;</span> inDeviceRef, kReportType, kReportData, kReportSize <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2009/06/26/athena-xbox-360-pad-and-mac-os-x-cont/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Athena: XBox 360 Pad and Mac OS X</title>
		<link>http://www.lastrayofhope.com/2009/06/12/athena-xbox-360-pad-and-mac-os-x/</link>
		<comments>http://www.lastrayofhope.com/2009/06/12/athena-xbox-360-pad-and-mac-os-x/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 23:27:42 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[HID]]></category>
		<category><![CDATA[MacBook]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Tattiebogle]]></category>
		<category><![CDATA[XBox]]></category>
		<category><![CDATA[XInput]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=2461</guid>
		<description><![CDATA[Tonight I began the process of writing some using the HID APIs to be able to use an XBox 360 controller on my MacBook Pro for the Athena engine. As usual, my Courage game played host to testing this new method of control. First of all, the XBox 360 pad uses a 0xff device class [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2009/06/XBox_Pad_with_MacOSX.png"><img class="alignleft size-medium wp-image-2464" title="XBox_Pad_with_MacOSX" src="http://www.lastrayofhope.com/wp-content/uploads/2009/06/XBox_Pad_with_MacOSX-300x187.png" alt="XBox_Pad_with_MacOSX" width="180" height="112" /></a>Tonight I began the process of writing some using the HID APIs to be able to use an XBox 360 controller on my MacBook Pro for the Athena engine.</p>
<p>As usual, my Courage game played host to testing this new method of control.</p>
<p>First of all, the XBox 360 pad uses a 0xff device class (Vendor specific), so normal HID drivers (they use a device class of 0x03) will not attach to it, so I needed to download a driver for MacOSX that could handle this. Also I didn't have a Microsoft Wireless Gaming Receiver so I bought a wired pad instead.</p>
<p>I tried the <a href="http://tattiebogle.net/index.php/ProjectRoot/Xbox360Controller/OsxDriver" target="_blank">Tattiebogle</a> driver, and at first it worked, but after I logged into <a href="http://www.vmware.com/products/fusion/" target="_blank">VMWare Fusion</a> and used by pad in Vista it stopped working on MacOSX. Or it could have been that I got the Wacom Bamboo pad installed recently.</p>
<p>So after a bit of searching I found another driver, which although didn't work, sparked Tattiebogle's to life. This one is known as <a href="http://sourceforge.net/project/showfiles.php?group_id=193069" target="_blank">XBox HID Driver for Mac OS X</a>.</p>
<p>Once it was all up and running, getting code to listen for the 360 pad was a breeze, and Apple has a nice section on your website for it <a href="http://developer.apple.com/technotes/tn2007/tn2187.html" target="_blank">Technical Note TN2187: New HID Manager APIs for Mac OS X version 10</a>. I'll summarise the basics needed to connect to the pad.</p>
<p>None of this code cleans up properly, like I said I only just started with the HID manager, so I'll probably post an example App at some point that does clean up properly when shutting down.</p>
<h3>Setting up HID Manager</h3>
<p>The following code is quite straight forward, it just sets up two callbacks, one for when a device matches what we are looking for, and one for when a device is removed.</p>
<p><strong><span style="text-decoration: underline;">The IOKit.framework is required</span>.</strong></p>
<p>I also have two global variables, one for the HID manager, and another the device we going to be reading (this code can only do one device since we only have one buffer variable).</p>
<div id="wpshdo_7" class="wp-synhighlighter-outer"><div id="wpshdt_7" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_7"></a><a id="wpshat_7" class="wp-synhighlighter-title" href="#codesyntax_7"  onClick="javascript:wpsh_toggleBlock(7)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_7" onClick="javascript:wpsh_code(7)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_7" onClick="javascript:wpsh_print(7)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_7" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="co2">#include &lt;IOKit/hid/IOHIDLib.h&gt;</span>
<span class="co2">#include &lt;IOKit/hid/IOHIDUsageTables.h&gt;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw2">namespace</span> HIDInput
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	<span class="kw4">void</span> DeviceMatchingCallback<span class="br0">&#40;</span> <span class="kw4">void</span> <span class="sy2">*</span> inContext, IOReturn inResult, <span class="kw4">void</span> <span class="sy2">*</span> inSender, IOHIDDeviceRef inIOHIDDeviceRef <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw4">void</span> DeviceRemovalCallback<span class="br0">&#40;</span> <span class="kw4">void</span> <span class="sy2">*</span> inContext, IOReturn inResult, <span class="kw4">void</span> <span class="sy2">*</span> inSender, IOHIDDeviceRef inIOHIDDeviceRef <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw4">void</span> DeviceIOHIDReportCallback<span class="br0">&#40;</span> <span class="kw4">void</span> <span class="sy2">*</span> inContext, IOReturn inResult, <span class="kw4">void</span> <span class="sy2">*</span> inSender, IOHIDReportType inType, uint32_t inReportID, uint8_t <span class="sy2">*</span> inReport, CFIndex inReportLength <span class="br0">&#41;</span><span class="sy4">;</span>
	Boolean Device_GetLongProperty<span class="br0">&#40;</span> IOHIDDeviceRef inDeviceRef, CFStringRef inKey, <span class="kw4">long</span> <span class="sy2">*</span> outValue <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw4">long</span> Device_GetVendorID<span class="br0">&#40;</span> IOHIDDeviceRef inIOHIDDeviceRef <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw4">long</span> Device_GetProductID<span class="br0">&#40;</span> IOHIDDeviceRef inIOHIDDeviceRef <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	IOHIDManagerRef l_IOHIDManagerRef<span class="sy4">;</span>
	uint8_t <span class="sy2">*</span> l_pReportBuf <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">int</span> main<span class="br0">&#40;</span> <span class="kw4">int</span> argc, <span class="kw4">char</span> <span class="sy2">*</span> argv<span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">// Create a HID manager with a default allocator</span>
	l_IOHIDManagerRef <span class="sy1">=</span> IOHIDManagerCreate<span class="br0">&#40;</span> kCFAllocatorDefault, kIOHIDOptionsTypeNone <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw3">assert</span><span class="br0">&#40;</span> IOHIDManagerRef <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Look for all device and open the manager</span>
	IOHIDManagerSetDeviceMatching<span class="br0">&#40;</span> l_IOHIDManagerRef, <span class="kw2">NULL</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	IOHIDManagerOpen<span class="br0">&#40;</span> l_IOHIDManagerRef, kIOHIDOptionsTypeNone <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Register Callbacks to be run with application loop</span>
	IOHIDManagerRegisterDeviceMatchingCallback<span class="br0">&#40;</span> l_IOHIDManagerRef, HIDInput<span class="sy4">::</span><span class="me2">DeviceMatchingCallback</span>, <span class="kw2">NULL</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	IOHIDManagerRegisterDeviceRemovalCallback<span class="br0">&#40;</span> l_IOHIDManagerRef, HIDInput<span class="sy4">::</span><span class="me2">DeviceRemovalCallback</span>, <span class="kw2">NULL</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	IOHIDManagerScheduleWithRunLoop<span class="br0">&#40;</span> l_IOHIDManagerRef, CFRunLoopGetCurrent<span class="br0">&#40;</span><span class="br0">&#41;</span>, kCFRunLoopDefaultMode <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">while</span><span class="br0">&#40;</span> <span class="kw2">true</span> <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="co1">// loop application</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="kw1">return</span> <span class="nu0">0</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>The call to <strong>IOHIDManagerSetDeviceMatching()</strong> can have a dictionary passed to it to look only for devices matching certain criteria, but for simplicity, I'll just be checking all devices in this example, so I passed NULL as a second argument.</p>
<h3>Checking for XBox360 Pad</h3>
<p>With the code setup for calling back when a device is found that matches (or all devices in our case), we now need to check the device vendor id and product id to be sure they are what we are looking for.</p>
<div id="wpshdo_8" class="wp-synhighlighter-outer"><div id="wpshdt_8" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_8"></a><a id="wpshat_8" class="wp-synhighlighter-title" href="#codesyntax_8"  onClick="javascript:wpsh_toggleBlock(8)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_8" onClick="javascript:wpsh_code(8)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_8" onClick="javascript:wpsh_print(8)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_8" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
Boolean HIDInput<span class="sy4">::</span><span class="me2">Device_GetLongProperty</span><span class="br0">&#40;</span> IOHIDDeviceRef inDeviceRef, CFStringRef inKey, <span class="kw4">long</span> <span class="sy2">*</span> outValue <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	CFTypeRef tCFTypeRef <span class="sy1">=</span> IOHIDDeviceGetProperty<span class="br0">&#40;</span> inDeviceRef, inKey <span class="br0">&#41;</span><span class="sy4">;</span>
	Boolean result <span class="sy1">=</span> FALSE<span class="sy4">;</span>
&nbsp;
	<span class="kw1">if</span><span class="br0">&#40;</span> tCFTypeRef <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="co1">// If this is a number get its value</span>
		<span class="kw1">if</span><span class="br0">&#40;</span> CFNumberGetTypeID<span class="br0">&#40;</span> <span class="br0">&#41;</span> <span class="sy1">==</span> CFGetTypeID<span class="br0">&#40;</span> tCFTypeRef <span class="br0">&#41;</span> <span class="br0">&#41;</span>
		<span class="br0">&#123;</span>
			result <span class="sy1">=</span> CFNumberGetValue<span class="br0">&#40;</span> <span class="br0">&#40;</span> CFNumberRef <span class="br0">&#41;</span> tCFTypeRef, kCFNumberSInt32Type, outValue <span class="br0">&#41;</span><span class="sy4">;</span>
		<span class="br0">&#125;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="kw1">return</span> result<span class="sy4">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">long</span> HIDInput<span class="sy4">::</span><span class="me2">Device_GetVendorID</span><span class="br0">&#40;</span> IOHIDDeviceRef inIOHIDDeviceRef <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw4">long</span> result <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span>
	Device_GetLongProperty<span class="br0">&#40;</span> inIOHIDDeviceRef, CFSTR<span class="br0">&#40;</span> kIOHIDVendorIDKey <span class="br0">&#41;</span>, <span class="sy3">&amp;</span>result <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw1">return</span> result<span class="sy4">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">long</span> HIDInput<span class="sy4">::</span><span class="me2">Device_GetProductID</span><span class="br0">&#40;</span> IOHIDDeviceRef inIOHIDDeviceRef <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw4">long</span> result <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span>
	Device_GetLongProperty<span class="br0">&#40;</span> inIOHIDDeviceRef, CFSTR<span class="br0">&#40;</span> kIOHIDProductIDKey <span class="br0">&#41;</span>, <span class="sy3">&amp;</span>result <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw1">return</span> result<span class="sy4">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> HIDInput<span class="sy4">::</span><span class="me2">DeviceMatchingCallback</span><span class="br0">&#40;</span> <span class="kw4">void</span> <span class="sy2">*</span> inContext, IOReturn inResult, <span class="kw4">void</span> <span class="sy2">*</span> inSender, IOHIDDeviceRef inIOHIDDeviceRef <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw1">if</span><span class="br0">&#40;</span> <span class="sy3">!</span>inIOHIDDeviceRef <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw1">return</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="kw4">const</span> <span class="kw4">long</span> kVendor_Microsoft <span class="sy1">=</span> <span class="nu12">0x045e</span><span class="sy4">;</span>
	<span class="kw4">const</span> <span class="kw4">long</span> kProduct_XBox360Pad <span class="sy1">=</span> <span class="nu12">0x028e</span><span class="sy4">;</span>
&nbsp;
	<span class="kw4">const</span> <span class="kw4">long</span> kVendorID <span class="sy1">=</span> Device_GetVendorID<span class="br0">&#40;</span> inIOHIDDeviceRef <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw4">const</span> <span class="kw4">long</span> kProductID <span class="sy1">=</span> Device_GetProductID<span class="br0">&#40;</span> inIOHIDDeviceRef <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="kw1">if</span><span class="br0">&#40;</span> kVendorID <span class="sy1">==</span> kVendor_Microsoft <span class="sy3">&amp;&amp;</span> kProductID <span class="sy1">==</span> kProduct_XBox360Pad <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="co1">// Assert if we're trying to create a second XBox 360 pad device</span>
		<span class="kw3">assert</span><span class="br0">&#40;</span> l_pReportBuf <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
		<span class="co1">// Get the size of the report for the device and allocate a buffer. The</span>
		<span class="co1">// report size property should be right, however the packet header</span>
		<span class="co1">// seems to want 20 bytes, so I'm allocating 20 bytes to be safe.</span>
		CFIndex reportSize <span class="sy1">=</span> <span class="nu0">20</span><span class="sy4">;</span>
		<span class="co1">//IOHIDDevice_GetLongProperty( inIOHIDDeviceRef, CFSTR( kIOHIDMaxInputReportSizeKey ), &amp;reportSize );</span>
		l_pReportBuf <span class="sy1">=</span> <span class="kw2">static_cast</span><span class="sy1">&lt;</span>uint8_t <span class="sy2">*</span><span class="sy1">&gt;</span><span class="br0">&#40;</span> <span class="kw3">malloc</span><span class="br0">&#40;</span> reportSize <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
		<span class="co1">// Register a report callback for this device</span>
		IOHIDDeviceRegisterInputReportCallback<span class="br0">&#40;</span> inIOHIDDeviceRef, l_pReportBuf, reportSize, DeviceIOHIDReportCallback, 0 <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
		<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;XBox Pad Connected.<span class="es1">\n</span>&quot;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> HIDInput<span class="sy4">::</span><span class="me2">DeviceRemovalCallback</span><span class="br0">&#40;</span> <span class="kw4">void</span> <span class="sy2">*</span> inContext, IOReturn inResult, <span class="kw4">void</span> <span class="sy2">*</span> inSender, IOHIDDeviceRef inIOHIDDeviceRef <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">// Deallocate report buffer</span>
	<span class="kw3">free</span><span class="br0">&#40;</span> l_pReportBuf <span class="br0">&#41;</span><span class="sy4">;</span>
	l_pReportBuf <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;XBox Pad Disconnected.<span class="es1">\n</span>&quot;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>We have two constants in the device matching callback, one that is the vendor id for Microsoft, and the other which is the product id for the XBox 360 pad.</p>
<p>Since this code only supports the one pad, I've added an assert which will be triggered if a second pad is attempted to be matched that is a 360 pad (it checks to see if the report buffer has already been allocated).</p>
<p>Once a device has been removed, I free the memory for the report buffer. I think I'm suppose to unregister the input report callback by passing null as the callback, but I'm not 100% sure yet.</p>
<p>All the other helper functions were found on the Apple website technical note.</p>
<h3>Reading Pad Status</h3>
<p>XBox 360 pad updates come through on a report type of 0x00 and are 14 bytes long (though I have read on other sites of people getting reports of 20 bytes, this just seems to be padding).</p>
<p>I have skipped the checking of the report type in this code, but it should probably be checked, as well as the report length.</p>
<div id="wpshdo_9" class="wp-synhighlighter-outer"><div id="wpshdt_9" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_9"></a><a id="wpshat_9" class="wp-synhighlighter-title" href="#codesyntax_9"  onClick="javascript:wpsh_toggleBlock(9)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_9" onClick="javascript:wpsh_code(9)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_9" onClick="javascript:wpsh_print(9)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_9" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> HIDInput<span class="sy4">::</span><span class="me2">DeviceIOHIDReportCallback</span><span class="br0">&#40;</span> <span class="kw4">void</span> <span class="sy2">*</span> inContext, IOReturn inResult, <span class="kw4">void</span> <span class="sy2">*</span> inSender, IOHIDReportType inType, uint32_t inReportID, uint8_t <span class="sy2">*</span> inReport, CFIndex inReportLength <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">// Output hex for each byte in the report (always 2 characters long, making up the rest with 0)</span>
	<span class="kw1">for</span><span class="br0">&#40;</span> <span class="kw4">int</span> i <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span> inReportLength<span class="sy4">;</span> <span class="sy2">++</span>i <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;%02x&quot;</span>, inReport<span class="br0">&#91;</span>i<span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;<span class="es1">\n</span>&quot;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>That sourcecode allows you to read just the actual reports being sent from the pad, however if you want to be able to use it as a 360 pad. You'll get a message something like this...</p>
<blockquote><p>0014000000002efcc70792ff9a06</p></blockquote>
<p>The 0014 at the beginning is a packet header, first byte being the command, second being the size of the packet. The other sections are split up as follows...</p>
<blockquote><p>0014<strong>aaaa</strong><em>bb</em><strong>cc</strong><em>dddd</em><strong>eeee</strong><em>ffff</em><strong>gggg</strong></p>
<p>a is an unsigned short of button flags</p>
<p>b is an unsigned char representing the left trigger (LT)</p>
<p>c is an unsigned char representing the right trigger (RT)</p>
<p>d is a signed short representing the Left Axis X</p>
<p>e is a signed short representing the Left Axis Y</p>
<p>f is a signed short representing the Right Axis X</p>
<p>g is a signed short representing the Right Axis Y</p></blockquote>
<p>This can be put into a function like so...</p>
<div id="wpshdo_10" class="wp-synhighlighter-outer"><div id="wpshdt_10" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_10"></a><a id="wpshat_10" class="wp-synhighlighter-title" href="#codesyntax_10"  onClick="javascript:wpsh_toggleBlock(10)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_10" onClick="javascript:wpsh_code(10)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_10" onClick="javascript:wpsh_print(10)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_10" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> HIDInput<span class="sy4">::</span><span class="me2">DeviceIOHIDReportCallback</span><span class="br0">&#40;</span> <span class="kw4">void</span> <span class="sy2">*</span> inContext, IOReturn inResult, <span class="kw4">void</span> <span class="sy2">*</span> inSender, IOHIDReportType inType, uint32_t inReportID, uint8_t <span class="sy2">*</span> inReport, CFIndex inReportLength <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw4">unsigned</span> <span class="kw4">char</span> leftTrigger, rightTrigger<span class="sy4">;</span>
	<span class="kw4">short</span> rightAxisX, rightAxisY<span class="sy4">;</span>
	<span class="kw4">short</span> leftAxisX, leftAxisY<span class="sy4">;</span>
	<span class="kw4">unsigned</span> <span class="kw4">short</span> buttonFlags<span class="sy4">;</span>
&nbsp;
	<span class="co1">// Copy bytes to their respective variables (incase they are not aligned correctly)</span>
	<span class="kw3">memcpy</span><span class="br0">&#40;</span> <span class="sy3">&amp;</span>buttonFlags, <span class="sy3">&amp;</span>inReport<span class="br0">&#91;</span>2<span class="br0">&#93;</span>, <span class="kw3">sizeof</span><span class="br0">&#40;</span> <span class="kw4">unsigned</span> <span class="kw4">short</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw3">memcpy</span><span class="br0">&#40;</span> <span class="sy3">&amp;</span>leftTrigger, <span class="sy3">&amp;</span>inReport<span class="br0">&#91;</span>4<span class="br0">&#93;</span>, <span class="kw3">sizeof</span><span class="br0">&#40;</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw3">memcpy</span><span class="br0">&#40;</span> <span class="sy3">&amp;</span>rightTrigger, <span class="sy3">&amp;</span>inReport<span class="br0">&#91;</span>5<span class="br0">&#93;</span>, <span class="kw3">sizeof</span><span class="br0">&#40;</span> <span class="kw4">unsigned</span> <span class="kw4">char</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw3">memcpy</span><span class="br0">&#40;</span> <span class="sy3">&amp;</span>leftAxisX, <span class="sy3">&amp;</span>inReport<span class="br0">&#91;</span>6<span class="br0">&#93;</span>, <span class="kw3">sizeof</span><span class="br0">&#40;</span> <span class="kw4">short</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw3">memcpy</span><span class="br0">&#40;</span> <span class="sy3">&amp;</span>leftAxisY, <span class="sy3">&amp;</span>inReport<span class="br0">&#91;</span>8<span class="br0">&#93;</span>, <span class="kw3">sizeof</span><span class="br0">&#40;</span> <span class="kw4">short</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw3">memcpy</span><span class="br0">&#40;</span> <span class="sy3">&amp;</span>rightAxisX, <span class="sy3">&amp;</span>inReport<span class="br0">&#91;</span>10<span class="br0">&#93;</span>, <span class="kw3">sizeof</span><span class="br0">&#40;</span> <span class="kw4">short</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw3">memcpy</span><span class="br0">&#40;</span> <span class="sy3">&amp;</span>rightAxisY, <span class="sy3">&amp;</span>inReport<span class="br0">&#91;</span>12<span class="br0">&#93;</span>, <span class="kw3">sizeof</span><span class="br0">&#40;</span> <span class="kw4">short</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Output pad stats</span>
	<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;Buttons %u<span class="es1">\n</span>&quot;</span>, buttonFlags <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;LTrigger: %u<span class="es1">\n</span>&quot;</span>, leftTrigger <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;RTrigger: %u<span class="es1">\n</span>&quot;</span>, rightTrigger <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;LAxis: %d, %d<span class="es1">\n</span>&quot;</span>, leftAxisX, leftAxisY <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;RAxis: %d, %d<span class="es1">\n</span>&quot;</span>, rightAxisX, rightAxisY <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw3">printf</span><span class="br0">&#40;</span><span class="st0">&quot;<span class="es1">\n</span>&quot;</span><span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>I don't know what the 2048 button flag is, it is possibly the synchronization button that is on the wireless controller. So here is a list of all the other button flags and their bit number.</p>
<div id="wpshdo_11" class="wp-synhighlighter-outer"><div id="wpshdt_11" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_11"></a><a id="wpshat_11" class="wp-synhighlighter-title" href="#codesyntax_11"  onClick="javascript:wpsh_toggleBlock(11)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_11" onClick="javascript:wpsh_code(11)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_11" onClick="javascript:wpsh_print(11)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_11" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw2">enum</span> XBox360_ButtonFlags
<span class="br0">&#123;</span>
	XBOX360PAD_DPAD_UP <span class="sy1">=</span> 1,
	XBOX360PAD_DPAD_DOWN <span class="sy1">=</span> 2,
	XBOX360PAD_DPAD_LEFT <span class="sy1">=</span> 4,
	XBOX360PAD_DPAD_RIGHT <span class="sy1">=</span> 8,
	XBOX360PAD_START <span class="sy1">=</span> 16,
	XBOX360PAD_BACK <span class="sy1">=</span> 32,
	XBOX360PAD_LAXIS <span class="sy1">=</span> 64,
	XBOX360PAD_RAXIS <span class="sy1">=</span> 128,
	XBOX360PAD_LB <span class="sy1">=</span> 256,
	XBOX360PAD_RB <span class="sy1">=</span> 512,
	XBOX360PAD_XBOX <span class="sy1">=</span> 1024,
	XBOX360PAD_A <span class="sy1">=</span> 4096,
	XBOX360PAD_B <span class="sy1">=</span> 8192,
	XBOX360PAD_X <span class="sy1">=</span> 16384,
	XBOX360PAD_Y <span class="sy1">=</span> 32768,
<span class="br0">&#125;</span><span class="sy4">;</span></pre></div></div>
<p>&nbsp;</p>
<p>There is still a lot more to cover, so I'll probably make a follow up entry tomorrow or the day after for sending messages to the pad to make it vibrate, and how to clean up after you're finished properly. As well as post an example application.</p>
<p>I also found a few new variables to add to my list of debugging preprocessor. I'll explain on the next page since this one is getting rather long.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2009/06/12/athena-xbox-360-pad-and-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Athena: Cheap(er) Per-Pixel Directional Lighting (cont)</title>
		<link>http://www.lastrayofhope.com/2009/06/01/athena-cheaper-per-pixel-directional-lighting-cont/</link>
		<comments>http://www.lastrayofhope.com/2009/06/01/athena-cheaper-per-pixel-directional-lighting-cont/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 19:23:06 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=2411</guid>
		<description><![CDATA[In my previous blog entry, Athena: Cheap(er) Per-Pixel Directional Lighting, I wrote how I had an idea for post process lighting calculations. Well, like many other things, it has already been done before, in fact a long time ago, 1988. It is known as Deferred Shading. The wiki article also mentions something else that I [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous blog entry, <a href="http://www.lastrayofhope.com/2009/05/31/athena-cheaper-per-pixel-directional-lighting/">Athena: Cheap(er) Per-Pixel Directional Lighting</a>, I wrote how I had an idea for post process lighting calculations. Well, like many other things, it has already been done before, in fact a long time ago, 1988. It is known as <a href="http://www.lastrayofhope.com/2009/05/31/athena-cheaper-per-pixel-directional-lighting/" target="_blank">Deferred Shading</a>.</p>
<p>The wiki article also mentions something else that I didn't take into considering, and that is transparent objects, since there would be more than one lighting equation needed.</p>
<p>On the whole though, I'm glad I got a chance to implement it, one more thing I know how to do.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2009/06/01/athena-cheaper-per-pixel-directional-lighting-cont/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Athena: Cheap(er) Per-Pixel Directional Lighting</title>
		<link>http://www.lastrayofhope.com/2009/05/31/athena-cheaper-per-pixel-directional-lighting/</link>
		<comments>http://www.lastrayofhope.com/2009/05/31/athena-cheaper-per-pixel-directional-lighting/#comments</comments>
		<pubDate>Sun, 31 May 2009 21:40:52 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=2364</guid>
		<description><![CDATA[As mentioned in Athena: More Uses for Framebuffer Objects, I have thought up a way of doing lighting using colour buffers, at the moment it is only directional lighting however. But the great thing is, depending on how many objects are in your scene, and how much overdraw can happen, this method is faster (I [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2009/05/perpixel_directional_postprocess.png"><img class="alignleft size-medium wp-image-2374" title="Per-Pixel Directional Lighting through Post Process" src="http://www.lastrayofhope.com/wp-content/uploads/2009/05/perpixel_directional_postprocess-300x233.png" alt="Per-Pixel Directional Lighting through Post Process" width="180" height="140" /></a>As mentioned in <a href="http://www.lastrayofhope.com/2009/05/29/athena-more-framebuffer-objects/" target="_blank">Athena: More Uses for Framebuffer Objects</a>, I have thought up a way of doing lighting using colour buffers, at the moment it is only directional lighting however.</p>
<p>But the great thing is, depending on how many objects are in your scene, and how much overdraw can happen, this method is faster (I think, I don't know the cost of writing to a second colour attachment).</p>
<h3><span style="text-decoration: underline;">Colour Shader</span></h3>
<p>The colour shader is just a basic shader I've setup to show. First of all the vertex shader...</p>
<div id="wpshdo_12" class="wp-synhighlighter-outer"><div id="wpshdt_12" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_12"></a><a id="wpshat_12" class="wp-synhighlighter-title" href="#codesyntax_12"  onClick="javascript:wpsh_toggleBlock(12)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_12" onClick="javascript:wpsh_code(12)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_12" onClick="javascript:wpsh_print(12)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_12" class="wp-synhighlighter-inner" style="display: block;"><pre class="glsl" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">// Varying Variables</span>
<span class="kw2">varying</span> <span class="kw3">vec3</span> Normal<span class="sy0">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">// Vertex Shader entry point</span>
<span class="kw3">void</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	Normal <span class="sy0">=</span> <span class="kw5">normalize</span><span class="br0">&#40;</span> <span class="kw6">gl_NormalMatrix</span> <span class="sy0">*</span> <span class="kw6">gl_Normal</span> <span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="kw6">gl_Position</span> <span class="sy0">=</span> <span class="kw5">ftransform</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>As you can see this is just a basic vertex shader, transforming a vertex position into screen space and passing the normal onto the fragment shader through a varying variable type.</p>
<p>Now the fragment shader...</p>
<div id="wpshdo_13" class="wp-synhighlighter-outer"><div id="wpshdt_13" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_13"></a><a id="wpshat_13" class="wp-synhighlighter-title" href="#codesyntax_13"  onClick="javascript:wpsh_toggleBlock(13)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_13" onClick="javascript:wpsh_code(13)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_13" onClick="javascript:wpsh_print(13)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_13" class="wp-synhighlighter-inner" style="display: block;"><pre class="glsl" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">// Varying Variables</span>
<span class="kw2">varying</span> <span class="kw3">vec3</span> Normal<span class="sy0">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">// Fragment Shader entry point</span>
<span class="kw3">void</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw3">vec3</span> N <span class="sy0">=</span> <span class="kw5">normalize</span><span class="br0">&#40;</span> Normal <span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="kw3">float</span> kDrawLit <span class="sy0">=</span> <span class="nu0">1.0</span><span class="sy0">;</span>
	<span class="kw6">gl_FragData</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span> <span class="sy0">=</span> <span class="kw3">vec4</span><span class="br0">&#40;</span> 1<span class="sy0">.</span>0<span class="sy0">,</span> 0<span class="sy0">.</span>0<span class="sy0">,</span> 0<span class="sy0">.</span>0<span class="sy0">,</span> 1<span class="sy0">.</span>0 <span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="kw6">gl_FragData</span><span class="br0">&#91;</span>1<span class="br0">&#93;</span> <span class="sy0">=</span> <span class="kw3">vec4</span><span class="br0">&#40;</span> N<span class="sy0">,</span> kDrawLit <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>This is a typical fragment shader as well, using draw buffer zero for the unlit texel colour. However, I've normalised the interpolated normal that was calculated for this texel and I'm outputting it to draw buffer one, along with another value '<em>kDrawLit</em>'.</p>
<p>When the w component of draw buffer 1 for this texel is set to <strong>0.0</strong>, the texel will not have any lighting calculations performed on it in the post process. I came up with this when I started thinking about rendering a HUD before the post processing.</p>
<p>I've chose just to output red as a colour, anything is fine, such as a texture that is mapped to the object in question.</p>
<h3><span style="text-decoration: underline;">Per-Pixel Lighting Shader</span></h3>
<p>Again, another simple vertex shader, passing a transformed vertex and texture coordinate to the fragment shader...</p>
<div id="wpshdo_14" class="wp-synhighlighter-outer"><div id="wpshdt_14" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_14"></a><a id="wpshat_14" class="wp-synhighlighter-title" href="#codesyntax_14"  onClick="javascript:wpsh_toggleBlock(14)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_14" onClick="javascript:wpsh_code(14)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_14" onClick="javascript:wpsh_print(14)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_14" class="wp-synhighlighter-inner" style="display: block;"><pre class="glsl" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">// Varying Variables</span>
<span class="kw2">varying</span> <span class="kw3">vec3</span> v_lightDir<span class="sy0">;</span>
<span class="kw2">varying</span> <span class="kw3">vec3</span> v_halfVec<span class="sy0">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">// Vertex Shader entry point</span>
<span class="kw3">void</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	v_lightDir <span class="sy0">=</span> <span class="kw5">normalize</span><span class="br0">&#40;</span> <span class="kw6">gl_LightSource</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="sy0">.</span><span class="me1">position</span><span class="sy0">.</span><span class="me1">xyz</span> <span class="br0">&#41;</span><span class="sy0">;</span>
	v_halfVec <span class="sy0">=</span> <span class="kw5">normalize</span><span class="br0">&#40;</span> <span class="kw6">gl_LightSource</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="sy0">.</span><span class="me1">halfVector</span><span class="sy0">.</span><span class="me1">xyz</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw6">gl_TexCoord</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="sy0">.</span><span class="me1">xy</span> <span class="sy0">=</span> <span class="kw6">gl_MultiTexCoord0</span><span class="sy0">.</span><span class="me1">xy</span><span class="sy0">;</span>
	<span class="kw6">gl_Position</span> <span class="sy0">=</span> <span class="kw6">gl_Vertex</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>Now for where the magic happens, the fragment shader, it uses two textures. The '<em>baseImage</em>' sampler is for the colour buffer, whilst the '<em>nrmImage</em>' is used for the normal texture we wrote...</p>
<div id="wpshdo_15" class="wp-synhighlighter-outer"><div id="wpshdt_15" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_15"></a><a id="wpshat_15" class="wp-synhighlighter-title" href="#codesyntax_15"  onClick="javascript:wpsh_toggleBlock(15)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_15" onClick="javascript:wpsh_code(15)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_15" onClick="javascript:wpsh_print(15)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_15" class="wp-synhighlighter-inner" style="display: block;"><pre class="glsl" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">// Uniform Variables</span>
<span class="kw2">uniform</span> <span class="kw3">sampler2D</span> baseImage<span class="sy0">;</span>
<span class="kw2">uniform</span> <span class="kw3">sampler2D</span> nrmImage<span class="sy0">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">// Varying Variables</span>
<span class="kw2">varying</span> <span class="kw3">vec3</span> v_lightDir<span class="sy0">;</span>
<span class="kw2">varying</span> <span class="kw3">vec3</span> v_halfVec<span class="sy0">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">// Fragment Shader entry point</span>
<span class="kw3">void</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">// Get texel colour and normal data</span>
	<span class="kw3">vec4</span> kTexelColour <span class="sy0">=</span> <span class="kw5">texture2D</span><span class="br0">&#40;</span> baseImage<span class="sy0">,</span> <span class="kw6">gl_TexCoord</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="sy0">.</span><span class="me1">xy</span> <span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="kw3">vec4</span> kNormalColour <span class="sy0">=</span> <span class="kw5">texture2D</span><span class="br0">&#40;</span> nrmImage<span class="sy0">,</span> <span class="kw6">gl_TexCoord</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="sy0">.</span><span class="me1">xy</span> <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
	<span class="co1">// Don't Perform Lighting on a non-lit texel</span>
	<span class="kw1">if</span><span class="br0">&#40;</span> kNormalColour<span class="sy0">.</span><span class="me1">w</span> <span class="sy0">==</span> 0<span class="sy0">.</span>0 <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw6">gl_FragData</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span> <span class="sy0">=</span> kTexelColour<span class="sy0">;</span>
		<span class="kw1">return</span><span class="sy0">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">// Calculate Ambient</span>
	<span class="kw3">vec4</span> kAmbient <span class="sy0">=</span> <span class="kw6">gl_LightSource</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="sy0">.</span><span class="me1">ambient</span> <span class="sy0">*</span> kTexelColour<span class="sy0">;</span>
&nbsp;
	<span class="co1">// Process Light Direction and Normals</span>
	<span class="kw3">float</span> NdotL <span class="sy0">=</span> <span class="kw5">max</span><span class="br0">&#40;</span> <span class="kw5">dot</span><span class="br0">&#40;</span> kNormalColour<span class="sy0">.</span><span class="me1">xyz</span><span class="sy0">,</span> v_lightDir <span class="br0">&#41;</span><span class="sy0">,</span> 0<span class="sy0">.</span>0 <span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="kw3">vec4</span> kColour <span class="sy0">=</span> kAmbient<span class="sy0">;</span>
&nbsp;
	<span class="co1">// Add Diffuse and Specular is NdotL is greater than zero</span>
	<span class="kw1">if</span><span class="br0">&#40;</span> NdotL <span class="sy0">&gt;</span> <span class="nu0">0.0</span> <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="co1">// Calculate Diffuse</span>
		<span class="kw3">vec4</span> kDiffuse <span class="sy0">=</span> <span class="br0">&#40;</span> 1<span class="sy0">.</span>0 <span class="sy0">-</span> kAmbient <span class="br0">&#41;</span> <span class="sy0">*</span> <span class="br0">&#40;</span> <span class="kw6">gl_LightSource</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="sy0">.</span><span class="me1">diffuse</span> <span class="sy0">*</span> kTexelColour <span class="br0">&#41;</span><span class="sy0">;</span>
		<span class="kw3">vec4</span> kSpecular <span class="sy0">=</span> <span class="kw6">gl_LightSource</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="sy0">.</span><span class="me1">specular</span><span class="sy0">;</span>
		<span class="kw3">float</span> kShininess <span class="sy0">=</span> <span class="nu0">60.0</span><span class="sy0">;</span>
&nbsp;
		<span class="kw3">float</span> NdotHV <span class="sy0">=</span> <span class="kw5">max</span><span class="br0">&#40;</span> <span class="kw5">dot</span><span class="br0">&#40;</span> kNormalColour<span class="sy0">.</span><span class="me1">xyz</span><span class="sy0">,</span> v_halfVec <span class="br0">&#41;</span><span class="sy0">,</span> 0<span class="sy0">.</span>0 <span class="br0">&#41;</span><span class="sy0">;</span>
		kColour <span class="sy0">+=</span> kDiffuse <span class="sy0">*</span> NdotL<span class="sy0">;</span>
		kColour <span class="sy0">+=</span> kSpecular <span class="sy0">*</span> <span class="kw5">pow</span><span class="br0">&#40;</span> NdotHV<span class="sy0">,</span> kShininess <span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">// Write Pixel Colour</span>
	<span class="kw6">gl_FragData</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span> <span class="sy0">=</span> kColour<span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>As you can see, if the w component of the normal texture is <strong>0.0</strong> then the colour value is outputted as it is. If not, using the normal and the light currently setup, the colour is modified to be lit before being outputted.</p>
<p>I've just used a simple Blinn shader for calculating the lighting.</p>
<h3><span style="text-decoration: underline;">The Application</span></h3>
<p>Now for the application side of things, below is just some quick sample code for what I used for my example. As you can see I've used a unsigned byte to store the normal, a floating point texture may have better accuracy at describing it though.</p>
<div id="wpshdo_16" class="wp-synhighlighter-outer"><div id="wpshdt_16" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_16"></a><a id="wpshat_16" class="wp-synhighlighter-title" href="#codesyntax_16"  onClick="javascript:wpsh_toggleBlock(16)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_16" onClick="javascript:wpsh_code(16)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_16" onClick="javascript:wpsh_print(16)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_16" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw2">namespace</span> CommonApp
<span class="br0">&#123;</span>
	<span class="kw2">enum</span> TextureTypes
	<span class="br0">&#123;</span>
		kColour,
		kNormal,
		kDepthStencil,
		kNumTextureTypes,
	<span class="br0">&#125;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw4">const</span> <span class="kw4">int</span> kWidth <span class="sy1">=</span> <span class="nu0">800</span><span class="sy4">;</span>
	<span class="kw4">const</span> <span class="kw4">int</span> kHeight <span class="sy1">=</span> <span class="nu0">600</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	GLuint l_frameBuffer<span class="sy4">;</span>
	GLuint l_textures<span class="br0">&#91;</span>kNumTextureTypes<span class="br0">&#93;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	GLuint l_colourProgram<span class="sy4">;</span>
	GLuint l_lightingProgram<span class="sy4">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">// Use alternate INTERNAL_FORMAT if floating point textures are not supported</span>
<span class="co1">//#define INTERNAL_FORMAT GL_RGBA</span>
<span class="co2">#define INTERNAL_FORMAT GL_RGBA32F_ARB</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> CommonApp<span class="sy4">::</span><span class="me2">Init</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">// Code to load colouring shader program</span>
	<span class="co1">// ...</span>
&nbsp;
	<span class="co1">// Code to load lighting shader program and setup uniform variables</span>
	<span class="co1">// ...</span>
&nbsp;
	<span class="co1">// Generate Textures</span>
	glGenTextures<span class="br0">&#40;</span> kNumTextureTypes, l_textures <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Create Colour Texture</span>
	glBindTexture<span class="br0">&#40;</span> GL_TEXTURE_2D, l_textures<span class="br0">&#91;</span>kColour<span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	glTexImage2D<span class="br0">&#40;</span> GL_TEXTURE_2D, 0, INTERNAL_FORMAT, kWidth, kHeight, 0, GL_RGBA, GL_FLOAT, 0 <span class="br0">&#41;</span><span class="sy4">;</span>
	glTexParameteri<span class="br0">&#40;</span> GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST <span class="br0">&#41;</span><span class="sy4">;</span>
	glTexParameteri<span class="br0">&#40;</span> GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST <span class="br0">&#41;</span><span class="sy4">;</span>
	glTexParameteri<span class="br0">&#40;</span> GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE <span class="br0">&#41;</span><span class="sy4">;</span>
	glTexParameteri<span class="br0">&#40;</span> GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Create Normal Texture</span>
	glBindTexture<span class="br0">&#40;</span> GL_TEXTURE_2D, l_textures<span class="br0">&#91;</span>kNormal<span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	glTexImage2D<span class="br0">&#40;</span> GL_TEXTURE_2D, 0, INTERNAL_FORMAT, kWidth, kHeight, 0, GL_RGBA, GL_FLOAT, 0 <span class="br0">&#41;</span><span class="sy4">;</span>
	glTexParameteri<span class="br0">&#40;</span> GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST <span class="br0">&#41;</span><span class="sy4">;</span>
	glTexParameteri<span class="br0">&#40;</span> GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST <span class="br0">&#41;</span><span class="sy4">;</span>
	glTexParameteri<span class="br0">&#40;</span> GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE <span class="br0">&#41;</span><span class="sy4">;</span>
	glTexParameteri<span class="br0">&#40;</span> GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Create Depth Buffer</span>
	glBindTexture<span class="br0">&#40;</span> GL_TEXTURE_2D, l_textures<span class="br0">&#91;</span>kDepthStencil<span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	glTexImage2D<span class="br0">&#40;</span> GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8_EXT, kWidth, kHeight, 0, GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT, 0 <span class="br0">&#41;</span><span class="sy4">;</span>
	glTexParameteri<span class="br0">&#40;</span> GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST <span class="br0">&#41;</span><span class="sy4">;</span>
	glTexParameteri<span class="br0">&#40;</span> GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST <span class="br0">&#41;</span><span class="sy4">;</span>
	glTexParameteri<span class="br0">&#40;</span> GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE <span class="br0">&#41;</span><span class="sy4">;</span>
	glTexParameteri<span class="br0">&#40;</span> GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Generate Framebuffer and attach textures</span>
	glGenFramebuffersEXT<span class="br0">&#40;</span> 1, <span class="sy3">&amp;</span>l_frameBuffer <span class="br0">&#41;</span><span class="sy4">;</span>
	glBindFramebufferEXT<span class="br0">&#40;</span> GL_FRAMEBUFFER_EXT, l_frameBuffer <span class="br0">&#41;</span><span class="sy4">;</span>
	glFramebufferTexture2DEXT<span class="br0">&#40;</span> GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, l_textures<span class="br0">&#91;</span>kColour<span class="br0">&#93;</span>, 0 <span class="br0">&#41;</span><span class="sy4">;</span>
	glFramebufferTexture2DEXT<span class="br0">&#40;</span> GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, l_textures<span class="br0">&#91;</span>kNormal<span class="br0">&#93;</span>, 0 <span class="br0">&#41;</span><span class="sy4">;</span>
	glFramebufferTexture2DEXT<span class="br0">&#40;</span> GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, l_textures<span class="br0">&#91;</span>kDepthStencil<span class="br0">&#93;</span>, 0 <span class="br0">&#41;</span><span class="sy4">;</span>
	glFramebufferTexture2DEXT<span class="br0">&#40;</span> GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, l_textures<span class="br0">&#91;</span>kDepthStencil<span class="br0">&#93;</span>, 0 <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> CommonApp<span class="sy4">::</span><span class="me2">DeInit</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	glDeleteFramebuffersEXT<span class="br0">&#40;</span> 1, <span class="sy3">&amp;</span>l_frameBuffer <span class="br0">&#41;</span><span class="sy4">;</span>
	glDeleteTextures<span class="br0">&#40;</span> kNumTextureTypes, l_textures <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>Just the usual code as you can see for setting up a frame buffer. Now for rendering the scene...</p>
<div id="wpshdo_17" class="wp-synhighlighter-outer"><div id="wpshdt_17" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_17"></a><a id="wpshat_17" class="wp-synhighlighter-title" href="#codesyntax_17"  onClick="javascript:wpsh_toggleBlock(17)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_17" onClick="javascript:wpsh_code(17)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_17" onClick="javascript:wpsh_print(17)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_17" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> CommonApp<span class="sy4">::</span><span class="me2">Render</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">// Use Frame Buffer</span>
	glBindFramebufferEXT<span class="br0">&#40;</span> GL_FRAMEBUFFER_EXT, l_frameBuffer <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Set Draw Buffers</span>
	<span class="kw4">const</span> GLenum kBuffers<span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy1">=</span> <span class="br0">&#123;</span> GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT <span class="br0">&#125;</span><span class="sy4">;</span>
	glDrawBuffers<span class="br0">&#40;</span> 2, kBuffers <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Use Shading Program</span>
	glUseProgram<span class="br0">&#40;</span> l_colourProgram <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Perform Rendering</span>
	<span class="co1">// ...</span>
&nbsp;
	<span class="co1">// Reset Draw Buffer</span>
	glDrawBuffer<span class="br0">&#40;</span> GL_COLOR_ATTACHMENT0_EXT <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Reset Frame Buffer</span>
	glBindFramebufferEXT<span class="br0">&#40;</span> GL_FRAMEBUFFER_EXT, 0 <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>I've set it up so that drawing to draw buffer zero writes to the colour texture, whilst drawing to draw buffer one writes to the normal texture, to reflect what is being done in the shader.</p>
<p>You don't need to use the colour shader I described up above, as long as it writes out the normal and an unlit colour, any shader can be used for this process, but for demonstration I've made it in this example that all geometry is drawn with this shader program.</p>
<p>Finally with our scene rendered, we can moved onto the final post processing stage...</p>
<div id="wpshdo_18" class="wp-synhighlighter-outer"><div id="wpshdt_18" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_18"></a><a id="wpshat_18" class="wp-synhighlighter-title" href="#codesyntax_18"  onClick="javascript:wpsh_toggleBlock(18)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_18" onClick="javascript:wpsh_code(18)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_18" onClick="javascript:wpsh_print(18)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_18" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> CommonApp<span class="sy4">::</span><span class="me2">PostRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	glActiveTexture<span class="br0">&#40;</span> GL_TEXTURE0 <span class="br0">&#41;</span><span class="sy4">;</span>
	glBindTexture<span class="br0">&#40;</span> GL_TEXTURE_2D, l_textures<span class="br0">&#91;</span>kColour<span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	glActiveTexture<span class="br0">&#40;</span> GL_TEXTURE1 <span class="br0">&#41;</span><span class="sy4">;</span>
	glBindTexture<span class="br0">&#40;</span> GL_TEXTURE_2D, l_textures<span class="br0">&#91;</span>kNormal<span class="br0">&#93;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	glUseProgram<span class="br0">&#40;</span> l_lightingProgram <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Render Fullscreen Quad</span>
	<span class="co1">// ...</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2009/05/perpixel_direction_postprocess_2.png"><img class="size-medium wp-image-2391 alignright" title="More Per-Pixel Directional Lighting through Post Process" src="http://www.lastrayofhope.com/wp-content/uploads/2009/05/perpixel_direction_postprocess_2-300x233.png" alt="More Per-Pixel Directional Lighting through Post Process" width="180" height="140" /></a>Attaching the colour and normal texture, using the per pixel lighting shader program and drawing a quad. The images within this blog are the final result (showing a comparison would be useless since they look the same).</p>
<p>One downside to this is that all texels have to use the same shininess value, however the alpha value of the colour buffer could be used to control this.</p>
<p>It is only a start, I need to performance check it at some point to see if it gives better or worse performance and under what conditions.</p>
<blockquote>Download Source: <a href="http://www.lastrayofhope.com/wp-content/plugins/download-monitor/download.php?id=4" title="Per Pixel Lighting (MacOSX)">Per Pixel Lighting (MacOSX)</a></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2009/05/31/athena-cheaper-per-pixel-directional-lighting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Athena: More Uses for Framebuffer Objects</title>
		<link>http://www.lastrayofhope.com/2009/05/29/athena-more-framebuffer-objects/</link>
		<comments>http://www.lastrayofhope.com/2009/05/29/athena-more-framebuffer-objects/#comments</comments>
		<pubDate>Fri, 29 May 2009 20:21:45 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[FBO]]></category>
		<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=2352</guid>
		<description><![CDATA[As I was upgrading my engine to the latest version of OpenGL, I noticed how my depth buffer was broken again (I use it for debugging my scenes when on the debug menu), and despite many changes, I was still having trouble getting it to display properly. The glReadPixels() function with GL_DEPTH_COMPONENT was working, but [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2009/05/fbo_depthbuffer.png"><img class="alignleft size-medium wp-image-2377" title="Depth Buffer using Frame Buffer Object" src="http://www.lastrayofhope.com/wp-content/uploads/2009/05/fbo_depthbuffer-300x233.png" alt="Depth Buffer using Frame Buffer Object" width="180" height="140" /></a>As I was upgrading my engine to the latest version of OpenGL, I noticed how my depth buffer was broken again (I use it for debugging my scenes when on the debug menu), and despite many changes, I was still having trouble getting it to display properly.</p>
<p>The glReadPixels() function with GL_DEPTH_COMPONENT was working, but I was having no luck at getting it displaying at any colour other than white without converting it first before writing it to a texture with glDrawPixels() - on reflection a floating point texture might have made this easier.</p>
<p>Anyways, when looking for answers, I found a way of using frame buffer objects to write out to multiple colour buffers, which allowed me to modify my Blinn shader to render a scene and the depth buffer to texture at the same time.</p>
<p>First of all, the shader needs to be shaders, instead of gl_FragColor, we use gl_FragData[n], where n is the index of the colour buffer we want to write to.</p>
<div id="wpshdo_19" class="wp-synhighlighter-outer"><div id="wpshdt_19" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_19"></a><a id="wpshat_19" class="wp-synhighlighter-title" href="#codesyntax_19"  onClick="javascript:wpsh_toggleBlock(19)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_19" onClick="javascript:wpsh_code(19)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_19" onClick="javascript:wpsh_print(19)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_19" class="wp-synhighlighter-inner" style="display: block;"><pre class="glsl" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">// Varying Variables</span>
<span class="kw2">varying</span> <span class="kw3">float</span> v_farPlane<span class="sy0">;</span>
<span class="kw2">varying</span> <span class="kw3">float</span> v_nearPlane<span class="sy0">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">// Fragment Shader entry point</span>
<span class="kw3">void</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">// Write Pixel Colour</span>
	<span class="co1">//gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );</span>
	<span class="kw6">gl_FragData</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span> <span class="sy0">=</span> <span class="kw3">vec4</span><span class="br0">&#40;</span> 1<span class="sy0">.</span>0<span class="sy0">,</span> 0<span class="sy0">.</span>0<span class="sy0">,</span> 0<span class="sy0">.</span>0<span class="sy0">,</span> 1<span class="sy0">.</span>0 <span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
	<span class="co1">// Write Depth</span>
	<span class="kw3">float</span> kDepth <span class="sy0">=</span> 1<span class="sy0">.</span>0 <span class="sy0">-</span> <span class="br0">&#40;</span> <span class="kw6">gl_FragCoord</span><span class="sy0">.</span><span class="me1">z</span> <span class="sy0">/</span> <span class="kw6">gl_FragCoord</span><span class="sy0">.</span><span class="me1">w</span> <span class="br0">&#41;</span> <span class="sy0">/</span> <span class="br0">&#40;</span> v_farPlane <span class="sy0">-</span> v_nearPlane <span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="kw6">gl_FragData</span><span class="br0">&#91;</span>1<span class="br0">&#93;</span> <span class="sy0">=</span> <span class="kw3">vec4</span><span class="br0">&#40;</span> kDepth<span class="sy0">,</span> kDepth<span class="sy0">,</span> kDepth<span class="sy0">,</span> 1<span class="sy0">.</span>0 <span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// Luminace style</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>And finally when binding your frame buffer object.</p>
<div id="wpshdo_20" class="wp-synhighlighter-outer"><div id="wpshdt_20" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_20"></a><a id="wpshat_20" class="wp-synhighlighter-title" href="#codesyntax_20"  onClick="javascript:wpsh_toggleBlock(20)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_20" onClick="javascript:wpsh_code(20)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_20" onClick="javascript:wpsh_print(20)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_20" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> RenderTarget<span class="sy4">::</span><span class="me2">Setup</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">// Create Frame Buffer</span>
	glGenBuffers<span class="br0">&#40;</span> 1, <span class="sy3">&amp;</span>m_frameBuffer <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Bind Frame Buffer</span>
	glBindFramebufferEXT<span class="br0">&#40;</span> GL_FRAMEBUFFER_EXT, m_frameBuffer <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Attach texture to receive colour</span>
	glFramebufferTexture2DEXT<span class="br0">&#40;</span> GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_colorTexture, 0 <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Attach Texture to receive depth buffer</span>
	glFramebufferTexture2DEXT<span class="br0">&#40;</span> GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, m_depthTexture, 0 <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Attach Depth and Stencil Buffer (same object since my gfx card doesn't support them being separate)</span>
	glFramebufferTexture2DEXT<span class="br0">&#40;</span> GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, m_stencilDepthBuffer, 0 <span class="br0">&#41;</span><span class="sy4">;</span>
	glFramebufferTexture2DEXT<span class="br0">&#40;</span> GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, m_stencilDepthBuffer, 0 <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> RenderTarget<span class="sy4">::</span><span class="me2">StartRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">// Bind Frame Buffer</span>
	glBindFramebufferEXT<span class="br0">&#40;</span> GL_FRAMEBUFFER_EXT, m_frameBuffer <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Set which index draws to which colour attachment</span>
	<span class="kw4">const</span> GLenum kBuffers<span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy1">=</span> <span class="br0">&#123;</span> GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT <span class="br0">&#125;</span><span class="sy4">;</span>
	glDrawBuffers<span class="br0">&#40;</span> 2, kBuffers <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>The call to glDrawBuffers() sets where gl_FragData[x] is being wrote to, I've specified there are two indexes, the first one points to colour attachment 0, and the second points to colour attachment 1.</p>
<p>Now when an object is rendered using that shader, it will output the normal colour buffer to colour attachment 0, and output the depth buffer to colour attachment 1.</p>
<p>Also, don't forget to reset the draw buffer destination after you're finished with it.</p>
<div id="wpshdo_21" class="wp-synhighlighter-outer"><div id="wpshdt_21" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_21"></a><a id="wpshat_21" class="wp-synhighlighter-title" href="#codesyntax_21"  onClick="javascript:wpsh_toggleBlock(21)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_21" onClick="javascript:wpsh_code(21)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_21" onClick="javascript:wpsh_print(21)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_21" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">void</span> RenderTarget<span class="sy4">::</span><span class="me2">EndRender</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">// Reset draw buffer destination</span>
	glDrawBuffer<span class="br0">&#40;</span>GL_COLOR_ATTACHMENT0_EXT<span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Unbind frame buffer and all attachments</span>
	glBindFramebufferEXT<span class="br0">&#40;</span> GL_FRAMEBUFFER_EXT, 0 <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>I will probably remove the glBindFramebufferEXT() call to unbind the frame buffer at some point, so that the same frame buffer gets used for all render targets, only the attachments get changed.</p>
<p>I've read it is faster this way. For more information on this, there is this great article on GameDev.net about it called, <a href="http://www.gamedev.net/reference/articles/article2333.asp" target="_blank">OpenGL Frame Buffer Object 201</a>.</p>
<p>I probably won't use this for writing out the depth buffer, but it did give me some ideas for per pixel lighting.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2009/05/29/athena-more-framebuffer-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Athena: OpenGL 3.0 Upgrade in Progress&#8230;</title>
		<link>http://www.lastrayofhope.com/2009/05/28/opengl-30/</link>
		<comments>http://www.lastrayofhope.com/2009/05/28/opengl-30/#comments</comments>
		<pubDate>Thu, 28 May 2009 17:08:20 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=2304</guid>
		<description><![CDATA[I just upgraded to the latest version of gDEBugger, and it kept notifying me that a lot of my code is using deprecated functions as of OpenGL 3.0. So I have begun the long task of converting code and shaders alike, hopefully in a positive step since it will mean my DirectX 10 port of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2009/05/command-and-conquer-3.jpg"><img class="size-medium wp-image-2345 alignleft" title="Command and Conquer 3" src="http://www.lastrayofhope.com/wp-content/uploads/2009/05/command-and-conquer-3-300x176.jpg" alt="Building in progress..." width="180" height="106" /></a></p>
<p>I just upgraded to the latest version of gDEBugger, and it kept notifying me that a lot of my code is using deprecated functions as of OpenGL 3.0.</p>
<p>So I have begun the long task of converting code and shaders alike, hopefully in a positive step since it will mean my DirectX 10 port of the Athena engine will have little modifications to work like the OpenGL and vice versa.</p>
<p>These functions are the first on my list, they are used for rendering my geometry.</p>
<blockquote><p>glTexCoordPointer()</p>
<p>glColorPointer()</p>
<p>glNormalPointer()</p>
<p>glVertexPointer()</p></blockquote>
<p>These have been replaced with glVertexAttribPointer(), a more useful system so you can specify custom attributes a vertex has, and link them to attribute variables in shaders (shaders are required now for everything like DirectX 10). So for a simple array of vertices, I linked them to attribute variable in my shader which was bound to location 0.</p>
<div id="wpshdo_22" class="wp-synhighlighter-outer"><div id="wpshdt_22" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_22"></a><a id="wpshat_22" class="wp-synhighlighter-title" href="#codesyntax_22"  onClick="javascript:wpsh_toggleBlock(22)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_22" onClick="javascript:wpsh_code(22)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_22" onClick="javascript:wpsh_print(22)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_22" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">// Bind VBO Object</span>
glBindBuffer<span class="br0">&#40;</span> GL_ARRAY_BUFFER, m_vertexBuffer <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">// Enable vertex attribute streaming on location 0</span>
glEnableVertexAttribArray<span class="br0">&#40;</span> <span class="nu0">0</span> <span class="coMULTI">/* location */</span> <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">// Set attribute offset, size, and type details for location 0</span>
glVertexAttribPointer<span class="br0">&#40;</span>	<span class="nu0">0</span>, <span class="coMULTI">/* location */</span>
			<span class="nu0">4</span>, <span class="coMULTI">/* components */</span>
			GL_FLOAT, <span class="coMULTI">/* type */</span>
			GL_FALSE, <span class="coMULTI">/* normalize */</span>
			<span class="kw3">sizeof</span><span class="br0">&#40;</span> <span class="kw4">float</span> <span class="br0">&#41;</span> <span class="sy2">*</span> <span class="nu0">4</span>, <span class="coMULTI">/* stride */</span>
			0 <span class="br0">&#41;</span><span class="sy4">;</span> <span class="coMULTI">/* buffer offset */</span></pre></div></div>
<p>&nbsp;</p>
<p>I found out shortly after however that the index I was using for my vertex attribute is a reserved index by nVidia for their built-in attributes, as are 12 others.</p>
<blockquote><p><strong>0</strong> - gl_Vertex<br />
<strong>2</strong> - gl_Normal<br />
<strong>3</strong> - gl_Color<br />
<strong>4</strong> - gl_SecondaryColor<br />
<strong>5</strong> - gl_FogCoord<br />
<strong>8</strong> - gl_MultiTexCoord0<br />
<strong>9</strong> - gl_MultiTexCoord1<br />
<strong>10</strong> - gl_MultiTexCoord2<br />
<strong>11</strong> - gl_MultiTexCoord3<br />
<strong>12</strong> - gl_MultiTexCoord4<br />
<strong>13</strong> - gl_MultiTexCoord5<br />
<strong>14</strong> - gl_MultiTexCoord6<br />
<strong>15</strong> - gl_MultiTexCoord7</p></blockquote>
<p>So in the interest of keeping things sane I changed the location index to be 16, all that was needed was updating my shaders to use attributes rather than gl_Position, etc.</p>
<div id="wpshdo_23" class="wp-synhighlighter-outer"><div id="wpshdt_23" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_23"></a><a id="wpshat_23" class="wp-synhighlighter-title" href="#codesyntax_23"  onClick="javascript:wpsh_toggleBlock(23)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_23" onClick="javascript:wpsh_code(23)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_23" onClick="javascript:wpsh_print(23)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_23" class="wp-synhighlighter-inner" style="display: block;"><pre class="glsl" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">// Vertex Attributes</span>
<span class="kw2">attribute</span> <span class="kw3">vec4</span> a_vertex<span class="sy0">;</span>
<span class="kw2">attribute</span> <span class="kw3">vec2</span> a_texCoord0<span class="sy0">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">// Vertex Shader Entry Point</span>
<span class="kw3">void</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">//gl_TexCoord[0].xy = gl_MultiTexCoord0.xy;</span>
	<span class="kw6">gl_TexCoord</span><span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="sy0">.</span><span class="me1">xy</span> <span class="sy0">=</span> a_texCoord0<span class="sy0">;</span>
&nbsp;
	<span class="co1">//gl_Position = gl_Vertex;</span>
	<span class="kw6">gl_Position</span> <span class="sy0">=</span> a_vertex<span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>You may have noticed that I don't multiply my vertex by any transformation matrices, this is because my vertex coordinates for 2D are always in the range of -1.0 to 1.0. Doing it this way saves me changing projection matrix states whenever I want to draw 2D, so my projection matrix is always setup for 3D. Also, the source for this vertex shader program contains another attribute for the texture coordinate as well since I just copy-pasta'd my Bloom shader.</p>
<p>Now just two simple calls were needed to be added to my  shader program before it was linked, so that the vertex attribute is bound to the correct location.</p>
<div id="wpshdo_24" class="wp-synhighlighter-outer"><div id="wpshdt_24" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_24"></a><a id="wpshat_24" class="wp-synhighlighter-title" href="#codesyntax_24"  onClick="javascript:wpsh_toggleBlock(24)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_24" onClick="javascript:wpsh_code(24)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_24" onClick="javascript:wpsh_print(24)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_24" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;">glBindAttribLocation<span class="br0">&#40;</span> m_program, <span class="nu0">16</span> <span class="coMULTI">/* location */</span>, <span class="st0">&quot;a_vertex&quot;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
glBindAttribLocation<span class="br0">&#40;</span> m_program, <span class="nu0">17</span> <span class="coMULTI">/* location */</span>, <span class="st0">&quot;a_texCoord0&quot;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
glLinkProgram<span class="br0">&#40;</span> m_program <span class="br0">&#41;</span><span class="sy4">;</span></pre></div></div>
<p>&nbsp;</p>
<p>As you can see my vertex shader has a texture coordinate as well (I'm using my Bloom vertex shader as an example), so the final code for setting up the VBO for this is.</p>
<div id="wpshdo_25" class="wp-synhighlighter-outer"><div id="wpshdt_25" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_25"></a><a id="wpshat_25" class="wp-synhighlighter-title" href="#codesyntax_25"  onClick="javascript:wpsh_toggleBlock(25)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_25" onClick="javascript:wpsh_code(25)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_25" onClick="javascript:wpsh_print(25)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_25" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">// VBO layout</span>
<span class="kw4">struct</span> TexturedVertex
<span class="br0">&#123;</span>
	<span class="kw4">float</span> texCoord<span class="br0">&#91;</span>2<span class="br0">&#93;</span><span class="sy4">;</span>
	<span class="kw4">float</span> position<span class="br0">&#91;</span>4<span class="br0">&#93;</span><span class="sy4">;</span>
<span class="br0">&#125;</span><span class="sy4">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">// Bind VBO Object</span>
glBindBuffer<span class="br0">&#40;</span> GL_ARRAY_BUFFER, myVBO <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">// Enable vertex attribute streaming on location 0</span>
glEnableVertexAttribArray<span class="br0">&#40;</span> 16 <span class="br0">&#41;</span><span class="sy4">;</span>
glEnableVertexAttribArray<span class="br0">&#40;</span> 17 <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">// Set attribute offset, size, and type details for location 0</span>
glVertexAttribPointer<span class="br0">&#40;</span> 17, 2, GL_FLOAT, GL_FALSE, <span class="kw3">sizeof</span><span class="br0">&#40;</span> TexturedVertex <span class="br0">&#41;</span>, 0 <span class="br0">&#41;</span><span class="sy4">;</span>
glVertexAttribPointer<span class="br0">&#40;</span> 16, 4, GL_FLOAT, GL_FALSE, <span class="kw3">sizeof</span><span class="br0">&#40;</span> TexturedVertex <span class="br0">&#41;</span>, <span class="kw3">sizeof</span><span class="br0">&#40;</span> <span class="kw4">float</span> <span class="br0">&#41;</span> <span class="sy2">*</span> 2 <span class="br0">&#41;</span><span class="sy4">;</span></pre></div></div>
<p>&nbsp;</p>
<p>I'm sure there is a preprocessor command to get the offset of a variable within a structure, but "sizeof( float ) * 2" works as well. My code is a bit more complex than is shown here (I have a vertex format class that manages what needs to be set where, and something to managed state changes et cetera), but you get the idea of how it all works.</p>
<p>Well that was one big change down, on the next page of this blog entry will be another big change, replacing glMatrixMode() and glLoadMatrix().</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2009/05/28/opengl-30/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

