<?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; OpenGL</title>
	<atom:link href="http://www.lastrayofhope.com/tag/opengl/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>OpenGL Shader Debugging</title>
		<link>http://www.lastrayofhope.com/2011/10/28/opengl-shader-debugging/</link>
		<comments>http://www.lastrayofhope.com/2011/10/28/opengl-shader-debugging/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 21:17:50 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[OpenGL ES]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3969</guid>
		<description><![CDATA[One thing that always annoys me with using glGetShaderInfoLog(), is that when compiling OpenGL shaders multiple sources can be used. Because of this, the line numbers get shifted, and trying to find line 123 where a syntax error exists is a length process. So I made a function that will take the sources list and [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that always annoys me with using glGetShaderInfoLog(), is that when compiling OpenGL shaders multiple sources can be used. Because of this, the line numbers get shifted, and trying to find line 123 where a syntax error exists is a length process.</p>
<p>So I made a function that will take the sources list and output the lines with line numbers.</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="kw4">void</span> OutputWithLineNumbers<span class="br0">&#40;</span> <span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">**</span> inSources, <span class="kw4">const</span> <span class="kw4">unsigned</span> <span class="kw4">int</span> inCount <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw4">bool</span> unendedLineNo <span class="sy1">=</span> <span class="kw2">false</span><span class="sy4">;</span>
	<span class="kw4">int</span> lineNumber <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>
&nbsp;
	<span class="kw1">for</span><span class="br0">&#40;</span> <span class="kw4">unsigned</span> <span class="kw4">int</span> i <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span> i <span class="sy1">&lt;</span> inCount<span class="sy4">;</span> <span class="sy2">++</span>i <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span> cur <span class="sy1">=</span> inSources<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="sy4">;</span>
		<span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span> next <span class="sy1">=</span> <span class="kw2">NULL</span><span class="sy4">;</span>
&nbsp;
		<span class="kw1">do</span>
		<span class="br0">&#123;</span>
			<span class="co1">//</span>
			<span class="kw1">if</span><span class="br0">&#40;</span> <span class="sy3">!</span>unendedLineNo <span class="br0">&#41;</span>
			<span class="br0">&#123;</span>
				<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;%04d: &quot;</span>, lineNumber <span class="br0">&#41;</span><span class="sy4">;</span>
			<span class="br0">&#125;</span>
&nbsp;
			<span class="co1">//</span>
			next <span class="sy1">=</span> <span class="kw3">strchr</span><span class="br0">&#40;</span> cur, <span class="st0">'<span class="es1">\r</span>'</span> <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
			<span class="co1">//</span>
			<span class="kw1">if</span><span class="br0">&#40;</span> next <span class="br0">&#41;</span>
			<span class="br0">&#123;</span>
				<span class="kw4">size_t</span> len <span class="sy1">=</span> <span class="br0">&#40;</span> next <span class="sy2">-</span> cur <span class="br0">&#41;</span><span class="sy4">;</span>
				<span class="kw4">char</span> <span class="sy2">*</span> lineText <span class="sy1">=</span> <span class="br0">&#40;</span><span class="kw4">char</span> <span class="sy2">*</span><span class="br0">&#41;</span>alloca<span class="br0">&#40;</span> len <span class="br0">&#41;</span><span class="sy4">;</span>
				<span class="kw3">memcpy</span><span class="br0">&#40;</span> lineText, cur, len <span class="br0">&#41;</span><span class="sy4">;</span>
				lineText<span class="br0">&#91;</span>len<span class="br0">&#93;</span> <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span>
&nbsp;
				<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;%s<span class="es1">\n</span>&quot;</span>, lineText <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
				unendedLineNo <span class="sy1">=</span> <span class="kw2">false</span><span class="sy4">;</span>
				<span class="sy2">++</span>lineNumber<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;%s&quot;</span>, cur <span class="br0">&#41;</span><span class="sy4">;</span>
				unendedLineNo <span class="sy1">=</span> <span class="kw2">true</span><span class="sy4">;</span>
			<span class="br0">&#125;</span>
&nbsp;
			<span class="co1">//</span>
			cur <span class="sy1">=</span> next <span class="sy2">+</span> <span class="nu0">2</span><span class="sy4">;</span>
		<span class="br0">&#125;</span> <span class="kw1">while</span><span class="br0">&#40;</span> next <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="kw1">if</span><span class="br0">&#40;</span> unendedLineNo <span class="br0">&#41;</span>
	<span class="br0">&#123;</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>
<span class="br0">&#125;</span></pre></div></div>
<p>This code requires the source files to have a carriage return as well as a new line character.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2011/10/28/opengl-shader-debugging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iOS: Using Separate Shaders</title>
		<link>http://www.lastrayofhope.com/2011/08/18/ios-using-separate-shaders/</link>
		<comments>http://www.lastrayofhope.com/2011/08/18/ios-using-separate-shaders/#comments</comments>
		<pubDate>Thu, 18 Aug 2011 20:22:37 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[GL_EXT_separate_shader_objects]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[OpenGLES]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3916</guid>
		<description><![CDATA[One of the lovely additions to iOS5 is there is now the ability to use separate vertex and fragment shaders (GL_EXT_separate_shader_objects). The first step in using separate shaders is to create an object to bind them onto that will be our overall shader program, known as a pipeline object. This pipeline object acts similar to vertex array [...]]]></description>
			<content:encoded><![CDATA[<p>One of the lovely additions to iOS5 is there is now the ability to use separate vertex and fragment shaders (GL_EXT_separate_shader_objects).</p>
<p>The first step in using separate shaders is to create an object to bind them onto that will be our overall shader program, known as a pipeline object.</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;">GLuint gProgramPipeline <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span>
GLuint gVertexProgram <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span>
GLuint gFragmentProgram <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	CreatePipeline<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
	CreateShaders<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	BindShadersToPipeline<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">do</span>
	<span class="br0">&#123;</span>
		<span class="co1">//</span>
		UpdateUniforms<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
		<span class="co1">//</span>
		BindPipeline<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
		DrawSomething<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
		UnbindPipeline<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
	<span class="kw1">while</span><span class="br0">&#40;</span> <span class="kw2">true</span> <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	DestroyShaders<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
	DestroyPipeline<span class="br0">&#40;</span><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> CreatePipeline<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	glGenProgramPipelinesEXT<span class="br0">&#40;</span> 1, <span class="sy3">&amp;</span>gProgramPipeline <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> DestroyPipeline<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw1">if</span><span class="br0">&#40;</span> gProgramPipeline <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		glDeleteProgramPipelinesEXT<span class="br0">&#40;</span> 1, <span class="sy3">&amp;</span>gProgramPipeline <span class="br0">&#41;</span><span class="sy4">;</span>
		gProgramPipeline <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>This pipeline object acts similar to vertex array object (VAO) only with shader programs, however instead of using glUseProgram, a separate function glUseProgramStagesEXT() must be used to bind the shader program to the pipeline, specifying the role of the shader program.</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="kw4">void</span> BindShadersToPipeline<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	glUseProgramStagesEXT<span class="br0">&#40;</span> gProgramPipeline, GL_VERTEX_SHADER_BIT_EXT, gVertexProgram <span class="br0">&#41;</span><span class="sy4">;</span>
	glUseProgramStagesEXT<span class="br0">&#40;</span> gProgramPipeline, GL_FRAGMENT_SHADER_BIT_EXT, gFragmentProgram <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>Updating uniforms is slightly different as well.</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> UpdateUniforms<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	GLint uniformLoc <span class="sy1">=</span> glGetUniformLocation<span class="br0">&#40;</span> gVertexProgram, <span class="st0">&quot;myVertUniform&quot;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	glProgramUniform1fEXT<span class="br0">&#40;</span> gVertexProgram,
			       uniformLoc,
			       1.248f <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	uniformLoc <span class="sy1">=</span> glGetUniformLocation<span class="br0">&#40;</span> gFragmentProgram, <span class="st0">&quot;myFragUniform&quot;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	glProgramUniform1fEXT<span class="br0">&#40;</span> gFragmentProgram,
			       uniformLoc,
			       6.1218f <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	uniformLoc <span class="sy1">=</span> glGetUniformLocation<span class="br0">&#40;</span> gVertexProgram, <span class="st0">&quot;myCommonUniform&quot;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="kw4">float</span> commonValue <span class="sy1">=</span> <span class="nu17">2.0f</span><span class="sy4">;</span>
&nbsp;
	glProgramUniform1fEXT<span class="br0">&#40;</span> gVertexProgram,
			       uniformLoc,
			       commonValue <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	uniformLoc <span class="sy1">=</span> glGetUniformLocation<span class="br0">&#40;</span> gFragmentProgram, <span class="st0">&quot;myCommonUniform&quot;</span> <span class="br0">&#41;</span><span class="sy4">;</span>
	glProgramUniform1fEXT<span class="br0">&#40;</span> gVertexProgram,
			       uniformLoc,
			       commonValue <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>Finally to use the program pipeline, we just need to bind it.</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> BindPipeline<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	glBindProgramPipelineEXT<span class="br0">&#40;</span> gProgramPipeline <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> UnbindPipeline<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	glBindProgramPipelineEXT<span class="br0">&#40;</span> 0 <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>fef</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2011/08/18/ios-using-separate-shaders/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Vertex Array Objects</title>
		<link>http://www.lastrayofhope.com/2011/07/30/using-vertex-array-objects/</link>
		<comments>http://www.lastrayofhope.com/2011/07/30/using-vertex-array-objects/#comments</comments>
		<pubDate>Sat, 30 Jul 2011 12:10:32 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[GL_OES_vertex_array_object]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[OpenGL ES]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3843</guid>
		<description><![CDATA[With my recent post about what iOS devices supported what extensions, I noticed a lot of people got to my website trying to work out how to use Vertex Array Objects (VAO) with the GL_OES_vertex_array_object extension. They're easy to use so I thought I would write up a quick how-to use them. There is a limitation with [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_3846" class="wp-caption alignleft" style="width: 160px"><a href="http://www.lastrayofhope.com/wp-content/uploads/2011/07/Karen-Gillan-001.jpg"><img class="size-thumbnail wp-image-3846" title="Amy Pond" src="http://www.lastrayofhope.com/wp-content/uploads/2011/07/Karen-Gillan-001-150x150.jpg" alt="" width="150" height="150" /></a><p class="wp-caption-text">Amy Pond &lt;3</p></div>
<p>With my recent post about what iOS devices supported what extensions, I noticed a lot of people got to my website trying to work out how to use Vertex Array Objects (VAO) with the GL_OES_vertex_array_object extension.</p>
<p>They're easy to use so I thought I would write up a quick how-to use them. There is a limitation with the VAO however, it cannot be shared between contexts and can be problematic for multithreading.</p>
<p>If you plan to use one, either generate and create for each context you will be using it on. Or generate and create on the context you will be mostly using it on. It might be a mistake, since it says they <a href="http://www.khronos.org/registry/gles/extensions/OES/OES_vertex_array_object.txt" target="_blank">didn't want to create the first non-shared named object in OpenGL ES</a>, however at the same time they say no to sharing.</p>
<p>Like with any OpenGL object, first a name must be generated for the object, make sure you always initialise your name variables to 0, since this refers to a non-object.</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;">GLuint gVAO <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	GenerateVAO<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
	CreateVAO<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">do</span>
	<span class="br0">&#123;</span>
		BindVAO<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
		DrawSomething<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
		UnbindVAO<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
	<span class="kw1">while</span><span class="br0">&#40;</span> 1 <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	DestroyVAO<span class="br0">&#40;</span><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> GenerateVAO<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">// Generate Name for VAO</span>
	glGenVertexArraysOES<span class="br0">&#40;</span> 1, <span class="sy3">&amp;</span>gVAO <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> DestroyVAO<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw1">if</span><span class="br0">&#40;</span> gVAO <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		glDeleteVertexArraysOES<span class="br0">&#40;</span> 1, <span class="sy3">&amp;</span>gVAO <span class="br0">&#41;</span><span class="sy4">;</span>
		gVAO <span class="sy1">=</span> <span class="nu0">0</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>I've also put the syntax in there for deleting the VAO when you do not need it anymore.</p>
<p>Now for filling in the VAO. The VAO is basically a block of memory for storing vertex data schematic. For fixed function, the schematic includes information about data type, count, stride, buffer offset and whether it is enabled. This is for every pointer, glVertexPointer, glNormalPointer, glColorPointer, glTexCoordPointer, etc.</p>
<p>And for the shader program pipeline, it contains the schematic for each glVertexAttribPointer.</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="kw4">void</span> CreateVAO<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">// Bind VAO</span>
	glBindVertexArrayOES<span class="br0">&#40;</span> gVAO <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Bind Vertex Buffer</span>
	glBindBuffer<span class="br0">&#40;</span> GL_ARRAY_BUFFER, gSomeVBO <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Fill in the VAO with our vertex schematic</span>
	<span class="kw1">if</span><span class="br0">&#40;</span> hasShaders <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		glEnableVertexAttribArray<span class="br0">&#40;</span> 0 <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
		glVertexAttribPointer<span class="br0">&#40;</span> <span class="nu0">0</span>,			<span class="co1">// Attribute bind location</span>
				       <span class="nu0">3</span>,			<span class="co1">// Data type count</span>
				       GL_FLOAT,		<span class="co1">// Data type</span>
				       GL_FALSE,		<span class="co1">// Normalise this data types?</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">3</span>,	<span class="co1">// Stride to the next vertex</span>
				       0 <span class="br0">&#41;</span><span class="sy4">;</span>			<span class="co1">// Vertex Buffer starting offset</span>
	<span class="br0">&#125;</span>
	<span class="kw1">else</span>
	<span class="br0">&#123;</span>
		glEnableClientState<span class="br0">&#40;</span> GL_VERTEX_ARRAY <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
		glVertexPointer<span class="br0">&#40;</span> <span class="nu0">3</span>,			<span class="co1">// Data type count</span>
				 GL_FLOAT,		<span class="co1">// Data type</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">3</span>,	<span class="co1">// Stride to the next vertex</span>
				 0 <span class="br0">&#41;</span><span class="sy4">;</span>			<span class="co1">// Vertex Buffer starting offset</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">// Unbind VAO</span>
	glBindVertexArrayOES<span class="br0">&#40;</span> 0 <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>You'll notice that when I am finished with with the VAO, I unbind it. This is because any changes to the vertex pointers will override any we have set within this function, so to protect the VAO we have just filled in, we unbind it.</p>
<p>When using a VAO, you will not need to bind the GL_ARRAY_BUFFER again the next time you use the VAO, since whenever you make a call to gl*Pointer() or glVertexAttribPointer(), the vertex buffer is stored with the vertex schematic. Because of this, you can have multiple streams of data from different vertex buffers.</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>
<span class="kw4">void</span> CreateVAO<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">// Bind VAO</span>
	glBindVertexArrayOES<span class="br0">&#40;</span> gVAO <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Vertex Buffer 1</span>
	<span class="br0">&#123;</span>
		<span class="co1">// Bind Vertex Buffer</span>
		glBindBuffer<span class="br0">&#40;</span> GL_ARRAY_BUFFER, gSomeVBO1 <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
		<span class="co1">// Fill in the VAO with our vertex schematic</span>
		<span class="kw1">if</span><span class="br0">&#40;</span> hasShaders <span class="br0">&#41;</span>
		<span class="br0">&#123;</span>
			<span class="co1">//</span>
			glEnableVertexAttribArray<span class="br0">&#40;</span> 0 <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
			glVertexAttribPointer<span class="br0">&#40;</span> <span class="nu0">0</span>,			<span class="co1">// Attribute bind location</span>
					       <span class="nu0">3</span>,			<span class="co1">// Data type count</span>
					       GL_FLOAT,		<span class="co1">// Data type</span>
					       GL_FALSE,		<span class="co1">// Normalise this data types?</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">5</span>,	<span class="co1">// Stride to the next vertex</span>
					       0 <span class="br0">&#41;</span><span class="sy4">;</span>			<span class="co1">// Vertex Buffer starting offset</span>
&nbsp;
			<span class="co1">//</span>
			glEnableVertexAttribArray<span class="br0">&#40;</span> 1 <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
			glVertexAttribPointer<span class="br0">&#40;</span> <span class="nu0">1</span>,			<span class="co1">// Attribute bind location</span>
					       <span class="nu0">2</span>,			<span class="co1">// Data type count</span>
					       GL_FLOAT,		<span class="co1">// Data type</span>
					       GL_FALSE,		<span class="co1">// Normalise this data types?</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">5</span>,	<span class="co1">// Stride to the next vertex</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> 3 <span class="br0">&#41;</span><span class="sy4">;</span>	<span class="co1">// Vertex Buffer starting offset</span>
		<span class="br0">&#125;</span>
		<span class="kw1">else</span>
		<span class="br0">&#123;</span>
			<span class="co1">//</span>
			glEnableClientState<span class="br0">&#40;</span> GL_VERTEX_ARRAY <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
			glVertexPointer<span class="br0">&#40;</span> <span class="nu0">3</span>,			<span class="co1">// Data type count</span>
					 GL_FLOAT,		<span class="co1">// Data type</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">5</span>,	<span class="co1">// Stride to the next vertex</span>
					 0 <span class="br0">&#41;</span><span class="sy4">;</span>			<span class="co1">// Vertex Buffer starting offset</span>
&nbsp;
			<span class="co1">//</span>
			glClientActiveTexture<span class="br0">&#40;</span> GL_TEXTURE0 <span class="br0">&#41;</span><span class="sy4">;</span>
			glEnableClientState<span class="br0">&#40;</span> GL_TEXTURE_COORD_ARRAY <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
			glTexCoordPointer<span class="br0">&#40;</span> <span class="nu0">2</span>,						<span class="co1">// Data type count</span>
					   GL_FLOAT,					<span class="co1">// Data type</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">5</span>,				<span class="co1">// Stride to the next vertex</span>
					   <span class="br0">&#40;</span><span class="kw4">const</span> GLvoid <span class="sy2">*</span><span class="br0">&#41;</span><span class="br0">&#40;</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> 3<span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy4">;</span>	<span class="co1">// Vertex Buffer starting offset</span>
		<span class="br0">&#125;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">// Vertex Buffer 2</span>
	<span class="br0">&#123;</span>
		<span class="co1">// Bind Vertex Buffer</span>
		glBindBuffer<span class="br0">&#40;</span> GL_ARRAY_BUFFER, gSomeVBO2 <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
		<span class="co1">// Fill in the VAO with our vertex schematic</span>
		<span class="kw1">if</span><span class="br0">&#40;</span> hasShaders <span class="br0">&#41;</span>
		<span class="br0">&#123;</span>
			glEnableVertexAttribArray<span class="br0">&#40;</span> 2 <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
			glVertexAttribPointer<span class="br0">&#40;</span> <span class="nu0">2</span>,			<span class="co1">// Attribute bind location</span>
					       <span class="nu0">2</span>,			<span class="co1">// Data type count</span>
					       GL_FLOAT,		<span class="co1">// Data type</span>
					       GL_FALSE,		<span class="co1">// Normalise this data types?</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">2</span>,	<span class="co1">// Stride to the next vertex</span>
					       0 <span class="br0">&#41;</span><span class="sy4">;</span>			<span class="co1">// Vertex Buffer starting offset</span>
		<span class="br0">&#125;</span>
		<span class="kw1">else</span>
		<span class="br0">&#123;</span>
			glClientActiveTexture<span class="br0">&#40;</span> GL_TEXTURE1 <span class="br0">&#41;</span><span class="sy4">;</span>
			glEnableClientState<span class="br0">&#40;</span> GL_TEXTURE_COORD_ARRAY <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
			glTexCoordPointer<span class="br0">&#40;</span> <span class="nu0">2</span>,			<span class="co1">// Data type count</span>
					   GL_FLOAT,		<span class="co1">// Data type</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">2</span>,	<span class="co1">// Stride to the next vertex</span>
					   0 <span class="br0">&#41;</span><span class="sy4">;</span>			<span class="co1">// Vertex Buffer starting offset</span>
		<span class="br0">&#125;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">// Unbind VAO</span>
	glBindVertexArrayOES<span class="br0">&#40;</span> 0 <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>In the code above, the first texture coordinate set is interlaced with the position data (offset by the sizeof three floats), and is bound using the first vertex buffer (gSomeVBO1). But the second texture coordinate is contained within a second vertex buffer (gSomeVBO2).</p>
<p>Finally, using the VAO is easy.</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> BindVAO<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	glBindVertexArrayOES<span class="br0">&#40;</span> gVAO <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> UnbindVAO<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	glBindVertexArrayOES<span class="br0">&#40;</span> 0 <span class="br0">&#41;</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>The call to glBindVertexArrayOES() with the VAO object name is just like calling everything we did in the create function. The only limitation is that one VAO is needed per vertex buffer, even if you have the same vertex schematic.</p>
<p>However, this is a great place for optimisation by sharing the same vertex buffer for vertex data with the same schematic. Then whenever you make a call to glDrawArrays() you can specify which vertex to start from. glDrawElements() is a little more complicated, you will need to increase all your elements in your index buffer so they point to the correct position within the shared buffer.</p>
<p>Or alternatively for glDrawElements(), you could share the same index buffer, and just specify the index buffer offset.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2011/07/30/using-vertex-array-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iOS OpenGL ES Extension Support</title>
		<link>http://www.lastrayofhope.com/2011/07/24/ios-opengl-es-extension-support/</link>
		<comments>http://www.lastrayofhope.com/2011/07/24/ios-opengl-es-extension-support/#comments</comments>
		<pubDate>Sun, 24 Jul 2011 04:26:21 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[OpenGLES]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3779</guid>
		<description><![CDATA[This weekend I was adding improved extension support to the Athena engine for iOS. There is a page on the Apple site that tells what device has what extensions but it hasn't been updated since iOS 4.2. So I ran through each version of GLES on a 1st Generate iPod Touch (MBX), an iPhone 4 [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2011/07/Screen-shot-2011-07-24-at-05.17.56.png"><img class="alignleft size-thumbnail wp-image-3780" style="border-width: 1px; border-color: black; border-style: solid;" title="Supported iOS OpenGL ES Extensions" src="http://www.lastrayofhope.com/wp-content/uploads/2011/07/Screen-shot-2011-07-24-at-05.17.56-150x150.png" alt="Supported iOS OpenGL ES Extensions" width="150" height="150" /></a>This weekend I was adding improved extension support to the Athena engine for iOS. There is a page on the Apple site that tells what device has what extensions but it hasn't been updated since iOS 4.2.</p>
<p>So I ran through each version of GLES on a 1st Generate iPod Touch (MBX), an iPhone 4 (SGX) and an iPad 2 (A5), printing the returned string from glGetString( GL_EXTENSIONS ) for each device.</p>
<p>There are a some new extensions in iOS5, some of which are iPad2 specific. But you can see from the image, each newer device supports all the previous extensions.</p>
<p>I haven't had chance to run it on an iPad1 yet, so it will be interesting to see if there is any difference. I've only wrote a rough idea of what version of what SDK is required to use them.</p>
<p><strong>Anything with '***' does not need the extension to work and has an implementation for that version of OpenGL ES.</strong></p>
<div id="wpshdo_10" class="wp-synhighlighter-outer"><div id="wpshdt_10" class="wp-synhighlighter-collapsed"><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: none;"><pre class="c" style="font-family:monospace;">GL_OES_depth_texture
GL_OES_blend_equation_separate
GL_OES_blend_func_separate
GL_OES_blend_subtract
GL_OES_compressed_paletted_texture
GL_OES_depth24
GL_OES_draw_texture
GL_OES_element_index_uint
GL_OES_fbo_render_mipmap
GL_OES_framebuffer_object
GL_OES_mapbuffer
GL_OES_matrix_palette
GL_OES_packed_depth_stencil
GL_OES_point_size_array
GL_OES_point_sprite
GL_OES_read_format
GL_OES_rgb8_rgba8
GL_OES_stencil_wrap
GL_OES_stencil8
GL_OES_texture_mirrored_repeat
GL_OES_vertex_array_object
GL_OES_standard_derivatives
GL_OES_texture_float
GL_OES_texture_half_float
GL_OES_texture_half_float_linear
&nbsp;
GL_EXT_blend_minmax
GL_EXT_color_buffer_half_float
GL_EXT_debug_label
GL_EXT_debug_marker
GL_EXT_discard_framebuffer
GL_EXT_occlusion_query_boolean
GL_EXT_read_format_bgra
GL_EXT_separate_shader_objects
GL_EXT_shader_texture_lod
GL_EXT_shadow_samplers
GL_EXT_texture_filter_anisotropic
GL_EXT_texture_lod_bias
GL_EXT_texture_rg				
&nbsp;
GL_APPLE_framebuffer_multisample
GL_APPLE_rgb_422
GL_APPLE_texture_2D_limited_npot
GL_APPLE_texture_format_BGRA8888
GL_APPLE_texture_max_level	
&nbsp;
GL_IMG_read_format
GL_IMG_texture_compression_pvrtc</pre></div></div>
<p>For more information on each extension, most of them can be <a href="http://www.khronos.org/registry/gles/" target="_blank">viewed here</a>. Some of the newer ones in the EXT namespace are viewable on the what's new page of the beta for iOS5, though the page calls them by the APPLE namespace still.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2011/07/24/ios-opengl-es-extension-support/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iOS Multithreading OpenGL</title>
		<link>http://www.lastrayofhope.com/2011/07/07/iphone-multithreading-opengl/</link>
		<comments>http://www.lastrayofhope.com/2011/07/07/iphone-multithreading-opengl/#comments</comments>
		<pubDate>Thu, 07 Jul 2011 21:38:41 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Multithreading]]></category>
		<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3644</guid>
		<description><![CDATA[I've been meaning to do this in my engine for a while, but just never had a time. Multithreading in OpenGL is simple enough as long as you are not accessing the same object as something on another thread. In my case, I'm only using it for my loading thread so I will not be [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_3693" class="wp-caption alignleft" style="width: 160px"><a href="http://www.lastrayofhope.com/wp-content/uploads/2011/07/IMG_0248.jpg"><img class="size-thumbnail wp-image-3693" title="Vashta Nerada" src="http://www.lastrayofhope.com/wp-content/uploads/2011/07/IMG_0248-150x150.jpg" alt="Vashta Nerada" width="150" height="150" /></a><p class="wp-caption-text">LEGO Vashta Nerada</p></div>
<p>I've been meaning to do this in my engine for a while, but just never had a time.</p>
<p>Multithreading in OpenGL is simple enough as long as you are not accessing the same object as something on another thread.</p>
<p>In my case, I'm only using it for my loading thread so I will not be accessing any of the objects created until loading is complete, and I will not be rendering on the separate thread either, at least not yet, I could use it for render targets.</p>
<p>So first of all, the creation of a OpenGL context on iOS. The examples I'm writing here are just simple main() loops so simplify what has to be done.</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="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	EAGLContext <span class="sy2">*</span> mContext <span class="sy1">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>EAGLContext alloc<span class="br0">&#93;</span> initWithAPI<span class="sy4">:</span>kEAGLRenderingAPIOpenGLES2<span class="br0">&#93;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Make context active for this thread</span>
	<span class="br0">&#91;</span>EAGLContext setCurrentContext<span class="sy4">:</span>mContext<span class="br0">&#93;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">while</span><span class="br0">&#40;</span> <span class="nu0">1</span> <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="co1">// do some rendering</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">return</span> <span class="nu0">0</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>Simple enough, this code most people who have written any iPhone OpenGL apps will recognise, it creates an ES2 context and bind it to the active thread.</p>
<p>However, now we want a separate thread that does OpenGL commands. We could use the same context, but a call binding a buffer on one thread could override to bind on the other causing invalid or wrong calls, so we would need to synchronise the threads.</p>
<p>Instead we create a second context that shares objects with our first one.</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="cpp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
EAGLContext <span class="sy2">*</span> mMainContext <span class="sy1">=</span> nil<span class="sy4">;</span>
EAGLContext <span class="sy2">*</span> mThreadContext <span class="sy1">=</span> nil<span class="sy4">;</span>
pthread_attr_t mThreadAttributes<span class="sy4">;</span>
pthread_t mThread<span class="sy4">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw4">int</span> main<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	mMainContext <span class="sy1">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>EAGLContext alloc<span class="br0">&#93;</span> initWithAPI<span class="sy4">:</span>kEAGLRenderingAPIOpenGLES2<span class="br0">&#93;</span><span class="sy4">;</span>
	mThreadContext <span class="sy1">=</span> <span class="br0">&#91;</span><span class="br0">&#91;</span>EAGLContext alloc<span class="br0">&#93;</span> initWithAPI<span class="sy4">:</span><span class="br0">&#91;</span>mMainContext API<span class="br0">&#93;</span>
					       sharegroup<span class="sy4">:</span><span class="br0">&#91;</span>mMainContext sharegroup<span class="br0">&#93;</span><span class="br0">&#93;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Make context active for this thread</span>
	<span class="br0">&#91;</span>EAGLContext setCurrentContext<span class="sy4">:</span>mMainContext<span class="br0">&#93;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">// Create second thread for OpenGL stuff</span>
	pthread_attr_init<span class="br0">&#40;</span> <span class="sy3">&amp;</span>mThreadAttributes <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	pthread_create<span class="br0">&#40;</span> <span class="sy3">&amp;</span>mThread,
			<span class="sy3">&amp;</span>mThreadAttributes,
			OGLThread,
			<span class="kw2">NULL</span> <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="nu0">1</span> <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="co1">// do some rendering</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">return</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">void</span> <span class="sy2">*</span> OGLThread<span class="br0">&#40;</span> <span class="kw4">void</span> <span class="sy2">*</span> inParam <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	<span class="br0">&#91;</span>EAGLContext setCurrentContext<span class="sy4">:</span>mThreadContext<span class="br0">&#93;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">while</span><span class="br0">&#40;</span> loadingSomething <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="co1">// other opengl commands</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">// Flush changes</span>
	glFlush<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">return</span> <span class="nu0">0</span><span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>With this second context, any calls to binding objects to the context will not affect the other thread's context. the [API] just makes it so that I'm using the same ES version as the first context, the [sharegroup] is the shared resource group that will be used by both contexts.</p>
<p>However you must take care not to update an object that is in use in the main thread, and whenever you make a change that will affect the other thread, call glFlush() on the thread that modified, and then rebind on the other thread so the changes take place.</p>
<p>In my own engine I hold shadow states of all options set, so one problem with this system would be I would need to pass in the Context depending on the thread. Instead I wrapped a class around the context that holds the shadow states and the context, as well as a pthread_t object for the thread it is assigned to.</p>
<p>Then whenever I do anything that will alter my shadow states, I make a call to pthread_self() to get the current thread, and find the relevant context.</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="cpp" style="font-family:monospace;"><span class="kw4">struct</span> MyContext
<span class="br0">&#123;</span>
	EAGLContext <span class="sy2">*</span> context<span class="sy4">;</span>
	ShadowStates states<span class="sy4">;</span>
	pthread_t thread<span class="sy4">;</span>
<span class="br0">&#125;</span><span class="sy4">;</span>
&nbsp;
MyContext mMainContext<span class="sy4">;</span>
MyContext mThreadContext<span class="sy4">;</span>
&nbsp;
MyContext <span class="sy2">*</span> GetCurrentContext<span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw4">int</span> ret <span class="sy1">=</span> pthread_equal<span class="br0">&#40;</span> pthread_self<span class="br0">&#40;</span><span class="br0">&#41;</span>, mMainContext.<span class="me1">thread</span> <span class="br0">&#41;</span><span class="sy4">;</span>
&nbsp;
	<span class="kw1">if</span><span class="br0">&#40;</span> ret <span class="sy1">==</span> <span class="nu0">0</span> <span class="br0">&#41;</span> <span class="co1">// 0 means not equal when using pthread_equal()</span>
	<span class="br0">&#123;</span>
		<span class="kw1">return</span> <span class="sy3">&amp;</span>mThreadContext<span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="kw1">return</span> <span class="sy3">&amp;</span>mMainContext<span class="sy4">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>There is only one object so far that I have found doesn't share between contexts, and that is a Vertex Array Object (VAO).</p>
<p>For now I'm just making my code think the extension isn't present when multithreading is enabled, but I need to come up with a solution to generate the VAO for each thread it gets used on.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2011/07/07/iphone-multithreading-opengl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Checking for OpenGL Errors</title>
		<link>http://www.lastrayofhope.com/2011/02/02/checking-for-errors/</link>
		<comments>http://www.lastrayofhope.com/2011/02/02/checking-for-errors/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 21:38:37 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Athena]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[OpenGLES]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=3942</guid>
		<description><![CDATA[Every so often I come by a bug that is annoying to debug. While I would like to write out lots of extra code that tells me exactly what went wrong with a function, it is just quicker for me to get the error type and look it up in the documentation. For that reason, [...]]]></description>
			<content:encoded><![CDATA[<p>Every so often I come by a bug that is annoying to debug. While I would like to write out lots of extra code that tells me exactly what went wrong with a function, it is just quicker for me to get the error type and look it up in the documentation.</p>
<p>For that reason, I made these helper functions. One clears the errors in OpenGL, the other outputs the error type as a string to the console and then asserts.</p>
<p>If the macro is used, this check can be turned off in release, and in debug the static bool within the function can be used to disable the check at runtime.</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="cpp" style="font-family:monospace;"><span class="co2">#ifdef DEBUG</span>
<span class="co2">#	define GL_CHECK_FOR_ERRORS()	GL::CheckForErrors()</span>
<span class="co2">#else</span>
<span class="co2">#	define GL_CHECK_FOR_ERRORS()</span>
<span class="co2">#endif</span>
&nbsp;
<span class="kw2">namespace</span> GL
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	<span class="co1">//</span>
	<span class="kw4">bool</span> CheckForErrors<span class="br0">&#40;</span><span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw4">static</span> <span class="kw4">bool</span> checkForErrors <span class="sy1">=</span> <span class="kw2">true</span><span class="sy4">;</span>
&nbsp;
		<span class="co1">//</span>
		<span class="kw1">if</span><span class="br0">&#40;</span> <span class="sy3">!</span>checkForErrors <span class="br0">&#41;</span>
		<span class="br0">&#123;</span>
			<span class="kw1">return</span> <span class="kw2">false</span><span class="sy4">;</span>
		<span class="br0">&#125;</span>
&nbsp;
		<span class="co1">//</span>
		<span class="kw4">const</span> <span class="kw4">char</span> <span class="sy2">*</span> errorString <span class="sy1">=</span> <span class="kw2">NULL</span><span class="sy4">;</span>
		<span class="kw4">bool</span> retVal <span class="sy1">=</span> <span class="kw2">false</span><span class="sy4">;</span>
&nbsp;
		<span class="kw1">switch</span><span class="br0">&#40;</span> glGetError<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>
		<span class="br0">&#123;</span>
		<span class="kw1">case</span> GL_NO_ERROR<span class="sy4">:</span>
			retVal <span class="sy1">=</span> <span class="kw2">true</span><span class="sy4">;</span>
			<span class="kw1">break</span><span class="sy4">;</span>
&nbsp;
		<span class="kw1">case</span> GL_INVALID_ENUM<span class="sy4">:</span>
			errorString <span class="sy1">=</span> <span class="st0">&quot;GL_INVALID_ENUM&quot;</span><span class="sy4">;</span>
			<span class="kw1">break</span><span class="sy4">;</span>
&nbsp;
		<span class="kw1">case</span> GL_INVALID_VALUE<span class="sy4">:</span>
			errorString <span class="sy1">=</span> <span class="st0">&quot;GL_INVALID_VALUE&quot;</span><span class="sy4">;</span>
			<span class="kw1">break</span><span class="sy4">;</span>
&nbsp;
		<span class="kw1">case</span> GL_INVALID_OPERATION<span class="sy4">:</span>
			errorString <span class="sy1">=</span> <span class="st0">&quot;GL_INVALID_OPERATION&quot;</span><span class="sy4">;</span>
			<span class="kw1">break</span><span class="sy4">;</span>
&nbsp;
		<span class="kw1">case</span> GL_INVALID_FRAMEBUFFER_OPERATION<span class="sy4">:</span>
			errorString <span class="sy1">=</span> <span class="st0">&quot;GL_INVALID_FRAMEBUFFER_OPERATION&quot;</span><span class="sy4">;</span>
			<span class="kw1">break</span><span class="sy4">;</span>
&nbsp;
<span class="co1">// OpenGLES Specific Errors</span>
<span class="co2">#ifdef ATHENA_OPENGLES</span>
		<span class="kw1">case</span> GL_STACK_OVERFLOW<span class="sy4">:</span>
			errorString <span class="sy1">=</span> <span class="st0">&quot;GL_STACK_OVERFLOW&quot;</span><span class="sy4">;</span>
			<span class="kw1">break</span><span class="sy4">;</span>
&nbsp;
		<span class="kw1">case</span> GL_STACK_UNDERFLOW<span class="sy4">:</span>
			errorString <span class="sy1">=</span> <span class="st0">&quot;GL_STACK_UNDERFLOW&quot;</span><span class="sy4">;</span>
			<span class="kw1">break</span><span class="sy4">;</span>
<span class="co2">#endif</span>
&nbsp;
		<span class="kw1">case</span> GL_OUT_OF_MEMORY<span class="sy4">:</span>
			errorString <span class="sy1">=</span> <span class="st0">&quot;GL_OUT_OF_MEMORY&quot;</span><span class="sy4">;</span>
			<span class="kw1">break</span><span class="sy4">;</span>
&nbsp;
		<span class="kw1">default</span><span class="sy4">:</span>
			errorString <span class="sy1">=</span> <span class="st0">&quot;UNKNOWN&quot;</span><span class="sy4">;</span>
			<span class="kw1">break</span><span class="sy4">;</span>
		<span class="br0">&#125;</span>
&nbsp;
		<span class="co1">//</span>
		<span class="kw1">if</span><span class="br0">&#40;</span> <span class="sy3">!</span>retVal <span class="br0">&#41;</span>
		<span class="br0">&#123;</span>
			<span class="kw3">printf</span><span class="br0">&#40;</span> <span class="st0">&quot;%s<span class="es1">\n</span>&quot;</span>, errorString <span class="br0">&#41;</span><span class="sy4">;</span>
			<span class="kw3">assert</span><span class="br0">&#40;</span> retVal <span class="br0">&#41;</span><span class="sy4">;</span>
		<span class="br0">&#125;</span>
&nbsp;
		<span class="co1">//</span>
		<span class="kw1">return</span> retVal<span class="sy4">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="co1">//</span>
	<span class="kw4">void</span> ClearErrors<span class="br0">&#40;</span><span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw1">while</span><span class="br0">&#40;</span> glGetError<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy3">!</span><span class="sy1">=</span> GL_NO_ERROR <span class="br0">&#41;</span><span class="sy4">;</span>
	<span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2011/02/02/checking-for-errors/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_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">// 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_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="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_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="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_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="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_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="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_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> 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_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> 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_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="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_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="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_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;"><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_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">// 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_26" class="wp-synhighlighter-outer"><div id="wpshdt_26" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_26"></a><a id="wpshat_26" class="wp-synhighlighter-title" href="#codesyntax_26"  onClick="javascript:wpsh_toggleBlock(26)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_26" onClick="javascript:wpsh_code(26)" 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_26" onClick="javascript:wpsh_print(26)" 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_26" 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_27" class="wp-synhighlighter-outer"><div id="wpshdt_27" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_27"></a><a id="wpshat_27" class="wp-synhighlighter-title" href="#codesyntax_27"  onClick="javascript:wpsh_toggleBlock(27)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_27" onClick="javascript:wpsh_code(27)" 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_27" onClick="javascript:wpsh_print(27)" 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_27" 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_28" class="wp-synhighlighter-outer"><div id="wpshdt_28" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_28"></a><a id="wpshat_28" class="wp-synhighlighter-title" href="#codesyntax_28"  onClick="javascript:wpsh_toggleBlock(28)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_28" onClick="javascript:wpsh_code(28)" 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_28" onClick="javascript:wpsh_print(28)" 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_28" 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>

