<?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>Game Rendering &#187; Depth buffer</title>
	<atom:link href="http://www.gamerendering.com/tag/depth-buffer/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gamerendering.com</link>
	<description></description>
	<lastBuildDate>Thu, 21 Jan 2010 01:32:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Forward Shadow Mapping</title>
		<link>http://www.gamerendering.com/2008/10/16/forward-shadow-mapping/</link>
		<comments>http://www.gamerendering.com/2008/10/16/forward-shadow-mapping/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 00:26:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Shadow Mapping]]></category>
		<category><![CDATA[Deferred Lightning]]></category>
		<category><![CDATA[Depth buffer]]></category>
		<category><![CDATA[Forward Shadow Mapping]]></category>
		<category><![CDATA[Game]]></category>
		<category><![CDATA[Shadows]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=354</guid>
		<description><![CDATA[This technique for shadow mapping does the reverse when comparing depths. Instead of the normal case of comparing depths in light space, this method suggest that the comparison should be done in eye space. This requires the depth buffer from the lights&#8217; view and from the camera&#8217;s view at the time of the comparison. This [...]]]></description>
			<content:encoded><![CDATA[<p>This technique for shadow mapping does the reverse when comparing depths. Instead of the normal case of comparing depths in light space, this method suggest that the comparison should be done in eye space. This requires the depth buffer from the lights&#8217; view and from the camera&#8217;s view at the time of the comparison. This method is probably most useful when doing deferred lightning.</p>
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/forwardshadowmapping.jpg"><img class="size-medium wp-image-355" title="Forward Shadow Mapping" src="http://www.gamerendering.com/wp-content/uploads/forwardshadowmapping.jpg" alt="Forward Shadow Mapping" width="229" height="231" /></a></div>
<p>Link to the paper &#8220;Forward Shadow Mapping&#8221;<br />
<a href="http://www.cs.unc.edu/~zhangh/shadow.html">http://www.cs.unc.edu/~zhangh/shadow.html</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Please share:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow" id="print" href="javascript:window.location='http%3A%2F%2Fwww.printfriendly.com%2Fprint%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F%26amp%3Bpartner%3Dsociable';" title="Print this article!"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="digg" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F%26amp%3Btitle%3DForward%2520Shadow%2520Mapping%26amp%3Bbodytext%3DThis%2520technique%2520for%2520shadow%2520mapping%2520does%2520the%2520reverse%2520when%2520comparing%2520depths.%2520Instead%2520of%2520the%2520normal%2520case%2520of%2520comparing%2520depths%2520in%2520light%2520space%252C%2520this%2520method%2520suggest%2520that%2520the%2520comparison%2520should%2520be%2520done%2520in%2520eye%2520space.%2520This%2520requires%2520the%2520depth%2520buffer%2520from%2520the%2520ligh';" title="Digg"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="sphinn" href="javascript:window.location='http%3A%2F%2Fsphinn.com%2Findex.php%3Fc%3Dpost%26m%3Dsubmit%26link%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F';" title="Sphinn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="del.icio.us" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F%26amp%3Btitle%3DForward%2520Shadow%2520Mapping%26amp%3Bnotes%3DThis%2520technique%2520for%2520shadow%2520mapping%2520does%2520the%2520reverse%2520when%2520comparing%2520depths.%2520Instead%2520of%2520the%2520normal%2520case%2520of%2520comparing%2520depths%2520in%2520light%2520space%252C%2520this%2520method%2520suggest%2520that%2520the%2520comparison%2520should%2520be%2520done%2520in%2520eye%2520space.%2520This%2520requires%2520the%2520depth%2520buffer%2520from%2520the%2520ligh';" title="del.icio.us"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="facebook" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F%26amp%3Bt%3DForward%2520Shadow%2520Mapping';" title="Facebook"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="mixx" href="javascript:window.location='http%3A%2F%2Fwww.mixx.com%2Fsubmit%3Fpage_url%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F%26amp%3Btitle%3DForward%2520Shadow%2520Mapping';" title="Mixx"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="google" href="javascript:window.location='http%3A%2F%2Fwww.google.com%2Fbookmarks%2Fmark%3Fop%3Dedit%26amp%3Bbkmk%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F%26amp%3Btitle%3DForward%2520Shadow%2520Mapping%26amp%3Bannotation%3DThis%2520technique%2520for%2520shadow%2520mapping%2520does%2520the%2520reverse%2520when%2520comparing%2520depths.%2520Instead%2520of%2520the%2520normal%2520case%2520of%2520comparing%2520depths%2520in%2520light%2520space%252C%2520this%2520method%2520suggest%2520that%2520the%2520comparison%2520should%2520be%2520done%2520in%2520eye%2520space.%2520This%2520requires%2520the%2520depth%2520buffer%2520from%2520the%2520ligh';" title="Google Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="current" href="javascript:window.location='http%3A%2F%2Fcurrent.com%2Fclipper.htm%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F%26amp%3Btitle%3DForward%2520Shadow%2520Mapping';" title="Current"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/current.png" title="Current" alt="Current" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="linkedin" href="javascript:window.location='http%3A%2F%2Fwww.linkedin.com%2FshareArticle%3Fmini%3Dtrue%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F%26amp%3Btitle%3DForward%2520Shadow%2520Mapping%26amp%3Bsource%3DGame%2BRendering%2B%26amp%3Bsummary%3DThis%2520technique%2520for%2520shadow%2520mapping%2520does%2520the%2520reverse%2520when%2520comparing%2520depths.%2520Instead%2520of%2520the%2520normal%2520case%2520of%2520comparing%2520depths%2520in%2520light%2520space%252C%2520this%2520method%2520suggest%2520that%2520the%2520comparison%2520should%2520be%2520done%2520in%2520eye%2520space.%2520This%2520requires%2520the%2520depth%2520buffer%2520from%2520the%2520ligh';" title="LinkedIn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="live" href="javascript:window.location='https%3A%2F%2Ffavorites.live.com%2Fquickadd.aspx%3Fmarklet%3D1%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F%26amp%3Btitle%3DForward%2520Shadow%2520Mapping';" title="Live"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="myspace" href="javascript:window.location='http%3A%2F%2Fwww.myspace.com%2FModules%2FPostTo%2FPages%2F%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F%26amp%3Bt%3DForward%2520Shadow%2520Mapping';" title="MySpace"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="netvibes" href="javascript:window.location='http%3A%2F%2Fwww.netvibes.com%2Fshare%3Ftitle%3DForward%2520Shadow%2520Mapping%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F';" title="Netvibes"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="stumbleupon" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F%26amp%3Btitle%3DForward%2520Shadow%2520Mapping';" title="StumbleUpon"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="twitter" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DForward%2520Shadow%2520Mapping%2520-%2520http%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F';" title="Twitter"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="reddit" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F%26amp%3Btitle%3DForward%2520Shadow%2520Mapping';" title="Reddit"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="technorati" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F';" title="Technorati"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow" id="yahoo! bookmarks" href="javascript:window.location='http%3A%2F%2Fbookmarks.yahoo.com%2Ftoolbar%2Fsavebm%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F16%252Fforward-shadow-mapping%252F%26amp%3Bt%3DForward%2520Shadow%2520Mapping%26opener%3Dbm%26amp%3Bei%3DUTF-8%26amp%3Bd%3DThis%2520technique%2520for%2520shadow%2520mapping%2520does%2520the%2520reverse%2520when%2520comparing%2520depths.%2520Instead%2520of%2520the%2520normal%2520case%2520of%2520comparing%2520depths%2520in%2520light%2520space%252C%2520this%2520method%2520suggest%2520that%2520the%2520comparison%2520should%2520be%2520done%2520in%2520eye%2520space.%2520This%2520requires%2520the%2520depth%2520buffer%2520from%2520the%2520ligh';" title="Yahoo! Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2008/10/16/forward-shadow-mapping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Screen Space</title>
		<link>http://www.gamerendering.com/2008/10/05/screen-space/</link>
		<comments>http://www.gamerendering.com/2008/10/05/screen-space/#comments</comments>
		<pubDate>Sun, 05 Oct 2008 10:10:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Cameras]]></category>
		<category><![CDATA[Depth buffer]]></category>
		<category><![CDATA[Fragment Shader]]></category>
		<category><![CDATA[Normalized Device Coordinates]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Screen Space]]></category>
		<category><![CDATA[Transformations]]></category>
		<category><![CDATA[Viewport]]></category>
		<category><![CDATA[Z-buffer]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=196</guid>
		<description><![CDATA[The last step of transformations (world to screen) when rendering is to get the rendered objects to screen space. This is a 2D space with the origin in the middle of the screen. After the vertex shader but before the fragment shader the fragments will be normalized device coordinates and will automatically be transformed to screen space. The fragments x,y [...]]]></description>
			<content:encoded><![CDATA[<p>The last step of transformations (world to screen) when rendering is to get the rendered objects to screen space. This is a 2D space with the origin in the middle of the screen. After the vertex shader but before the fragment shader the fragments will be normalized device coordinates and will automatically be transformed to screen space. The fragments x,y coordinates will be mapped to the x and y coordinates on the screen (the exact mapping is set by viewportbounds). And the z coordinates will be mapped to the depth buffer (z-buffer) as the following [-1] -&gt; [near clip plane limit] and [1] -&gt; [far clip plane limit].</p>
<p>The viewport mapping is the following [-1,1] in X is mapped to the left to right of the viewport range. The [-1,1] in Y is mapped to the bottom to top of the viewport range</p>
<p>Framents will automatically be transformed to screen space before the fragment shader, you just need to set the viewport mapping with the following OpenGL call.</p>

<div class="wp_codebox"><table width="100%" ><tr id="p1962"><td class="code" id="p196code2"><pre class="c" style="font-family:monospace;">glViewport<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> screenHeight<span style="color: #339933;">/</span><span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> screenWidth<span style="color: #339933;">/</span><span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> screenHeight<span style="color: #339933;">/</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can access the current fragment depth in a fragment shader with the variable gl_FragDepth which in default is the same as gl_FragCoord.z. The screen coordinates can be accessed in the variable gl_FragCoord.xy.</p>
<p>Information about pixel shaders in DirectX (most is the same in OpenGL):<br />
<a href="http://www.gamedev.net/columns/hardcore/dxshader3/">http://www.gamedev.net/columns/hardcore/dxshader3/</a></p>
<p>A quick reference guide to GLSL:<br />
<a href="http://www.opengl.org/sdk/libs/OpenSceneGraph/glsl_quickref.pdf">http://www.opengl.org/sdk/libs/OpenSceneGraph/glsl_quickref.pdf</a></p>
<p>Description of the mapping in DirectX:<br />
<a href="http://msdn.microsoft.com/en-us/library/bb219690.aspx">http://msdn.microsoft.com/en-us/library/bb219690.aspx</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Please share:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow" id="print" href="javascript:window.location='http%3A%2F%2Fwww.printfriendly.com%2Fprint%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F%26amp%3Bpartner%3Dsociable';" title="Print this article!"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="digg" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F%26amp%3Btitle%3DScreen%2520Space%26amp%3Bbodytext%3DThe%2520last%2520step%2520of%2520transformations%25C2%25A0%2528world%2520to%2520screen%2529%25C2%25A0when%2520rendering%2520is%2520to%2520get%2520the%2520rendered%2520objects%2520to%2520screen%2520space.%2520This%2520is%2520a%25202D%2520space%2520with%2520the%2520origin%2520in%2520the%2520middle%2520of%2520the%2520screen.%2520After%2520the%2520vertex%2520shader%2520but%2520before%2520the%2520fragment%2520shader%2520the%25C2%25A0fragments%2520';" title="Digg"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="sphinn" href="javascript:window.location='http%3A%2F%2Fsphinn.com%2Findex.php%3Fc%3Dpost%26m%3Dsubmit%26link%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F';" title="Sphinn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="del.icio.us" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F%26amp%3Btitle%3DScreen%2520Space%26amp%3Bnotes%3DThe%2520last%2520step%2520of%2520transformations%25C2%25A0%2528world%2520to%2520screen%2529%25C2%25A0when%2520rendering%2520is%2520to%2520get%2520the%2520rendered%2520objects%2520to%2520screen%2520space.%2520This%2520is%2520a%25202D%2520space%2520with%2520the%2520origin%2520in%2520the%2520middle%2520of%2520the%2520screen.%2520After%2520the%2520vertex%2520shader%2520but%2520before%2520the%2520fragment%2520shader%2520the%25C2%25A0fragments%2520';" title="del.icio.us"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="facebook" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F%26amp%3Bt%3DScreen%2520Space';" title="Facebook"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="mixx" href="javascript:window.location='http%3A%2F%2Fwww.mixx.com%2Fsubmit%3Fpage_url%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F%26amp%3Btitle%3DScreen%2520Space';" title="Mixx"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="google" href="javascript:window.location='http%3A%2F%2Fwww.google.com%2Fbookmarks%2Fmark%3Fop%3Dedit%26amp%3Bbkmk%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F%26amp%3Btitle%3DScreen%2520Space%26amp%3Bannotation%3DThe%2520last%2520step%2520of%2520transformations%25C2%25A0%2528world%2520to%2520screen%2529%25C2%25A0when%2520rendering%2520is%2520to%2520get%2520the%2520rendered%2520objects%2520to%2520screen%2520space.%2520This%2520is%2520a%25202D%2520space%2520with%2520the%2520origin%2520in%2520the%2520middle%2520of%2520the%2520screen.%2520After%2520the%2520vertex%2520shader%2520but%2520before%2520the%2520fragment%2520shader%2520the%25C2%25A0fragments%2520';" title="Google Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="current" href="javascript:window.location='http%3A%2F%2Fcurrent.com%2Fclipper.htm%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F%26amp%3Btitle%3DScreen%2520Space';" title="Current"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/current.png" title="Current" alt="Current" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="linkedin" href="javascript:window.location='http%3A%2F%2Fwww.linkedin.com%2FshareArticle%3Fmini%3Dtrue%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F%26amp%3Btitle%3DScreen%2520Space%26amp%3Bsource%3DGame%2BRendering%2B%26amp%3Bsummary%3DThe%2520last%2520step%2520of%2520transformations%25C2%25A0%2528world%2520to%2520screen%2529%25C2%25A0when%2520rendering%2520is%2520to%2520get%2520the%2520rendered%2520objects%2520to%2520screen%2520space.%2520This%2520is%2520a%25202D%2520space%2520with%2520the%2520origin%2520in%2520the%2520middle%2520of%2520the%2520screen.%2520After%2520the%2520vertex%2520shader%2520but%2520before%2520the%2520fragment%2520shader%2520the%25C2%25A0fragments%2520';" title="LinkedIn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="live" href="javascript:window.location='https%3A%2F%2Ffavorites.live.com%2Fquickadd.aspx%3Fmarklet%3D1%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F%26amp%3Btitle%3DScreen%2520Space';" title="Live"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="myspace" href="javascript:window.location='http%3A%2F%2Fwww.myspace.com%2FModules%2FPostTo%2FPages%2F%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F%26amp%3Bt%3DScreen%2520Space';" title="MySpace"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="netvibes" href="javascript:window.location='http%3A%2F%2Fwww.netvibes.com%2Fshare%3Ftitle%3DScreen%2520Space%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F';" title="Netvibes"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="stumbleupon" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F%26amp%3Btitle%3DScreen%2520Space';" title="StumbleUpon"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="twitter" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DScreen%2520Space%2520-%2520http%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F';" title="Twitter"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="reddit" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F%26amp%3Btitle%3DScreen%2520Space';" title="Reddit"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="technorati" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F';" title="Technorati"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow" id="yahoo! bookmarks" href="javascript:window.location='http%3A%2F%2Fbookmarks.yahoo.com%2Ftoolbar%2Fsavebm%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fscreen-space%252F%26amp%3Bt%3DScreen%2520Space%26opener%3Dbm%26amp%3Bei%3DUTF-8%26amp%3Bd%3DThe%2520last%2520step%2520of%2520transformations%25C2%25A0%2528world%2520to%2520screen%2529%25C2%25A0when%2520rendering%2520is%2520to%2520get%2520the%2520rendered%2520objects%2520to%2520screen%2520space.%2520This%2520is%2520a%25202D%2520space%2520with%2520the%2520origin%2520in%2520the%2520middle%2520of%2520the%2520screen.%2520After%2520the%2520vertex%2520shader%2520but%2520before%2520the%2520fragment%2520shader%2520the%25C2%25A0fragments%2520';" title="Yahoo! Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2008/10/05/screen-space/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clip Space</title>
		<link>http://www.gamerendering.com/2008/10/05/clip-space/</link>
		<comments>http://www.gamerendering.com/2008/10/05/clip-space/#comments</comments>
		<pubDate>Sun, 05 Oct 2008 00:53:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Cameras]]></category>
		<category><![CDATA[Clip Space]]></category>
		<category><![CDATA[Depth buffer]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Projection]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[Vertex Shader]]></category>
		<category><![CDATA[Z-buffer]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=186</guid>
		<description><![CDATA[Before rendering something all objects need to be projected to the screen ( a plane ). The first step, to project to the screen, is to project entities to clip space.
The mostly used projection type in games is perspective projection and therefore only this kind of projection will be explained. An other type of projection [...]]]></description>
			<content:encoded><![CDATA[<p>Before rendering something all objects need to be projected to the screen ( a plane ). The first step, to project to the screen, is to project entities to clip space.</p>
<p>The mostly used projection type in games is perspective projection and therefore only this kind of projection will be explained. An other type of projection is parallel projection which is a projection that will keep parallel lines parallel. Perspective projection gives a field of view and objects appears to be smaller when further away which will feel realistic. The projection volume is set up as the following image shows.</p>
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/projection.jpg"><img class="size-medium wp-image-206" title="The Projection Volume" src="http://www.gamerendering.com/wp-content/uploads/projection-400x212.jpg" alt="The Projection Volume" width="400" height="212" /></a></div>
<p>You apply a projection to the entities that is in <a title="Eye Space" href="http://www.gamerendering.com/2008/10/05/eye-space/" target="_self">eye space</a> to get them to clip space. This is the space that will soon decide if the vertex will be culled or not. Together with homogenization it can be thougt as a mapping from the projection volume to a cube with sizes 2 (in OpenGL) and with origin at 0,0,0. This mapping is showed in the image below.</p>
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/projectionvolumetocube.jpg"><img class="size-medium wp-image-205" title="Projection volume mapped to a cube" src="http://www.gamerendering.com/wp-content/uploads/projectionvolumetocube.jpg" alt="Projection volume mapped to a cube" width="255" height="86" /></a></div>
<p>In OpenGL you can access the projection matrix if you set the matrix mode to projection:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p1867"><td class="code" id="p186code7"><pre class="c" style="font-family:monospace;">glMatrixMode<span style="color: #009900;">&#40;</span>GL_PROJECTION<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The following vertex shaders shows some ways to transform a vertex to clip space. The fastest way is the third shader that uses the highly optimized ftransform() function.</p>

<div class="wp_codebox"><table width="100%" ><tr id="p1868"><td class="code" id="p186code8"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// vertex in clip space</span>
    gl_Position <span style="color: #339933;">=</span> gl_ProjectionMatrix <span style="color: #339933;">*</span> gl_ModelViewMatrix <span style="color: #339933;">*</span> gl_Vertex<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_codebox"><table width="100%" ><tr id="p1869"><td class="code" id="p186code9"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// vertex in clip space</span>
    gl_Position <span style="color: #339933;">=</span> gl_ModelViewProjectionMatrix <span style="color: #339933;">*</span> gl_Vertex<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_codebox"><table width="100%" ><tr id="p18610"><td class="code" id="p186code10"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// vertex in clip space</span>
    gl_Position <span style="color: #339933;">=</span> ftransform<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>One side effect from applying perspective projection is that you end up with a non-linear z-buffer. The buffer will be more detailed closer to the screen which is often good. More about this in the z-buffer article.</p>
<p>After applying projection to a point p = (x,y,z,w) you will end up with a w value that is most probably not zero or one. This means you need to divide all components by this value to obtain <a title="Normalized Device Coordinates" href="http://www.gamerendering.com/2008/10/05/normalized-device-coordinates/" target="_self">normalized device coordinates</a>. This will happen automatically between the vertex shader and fragment shader. Clipping will also be performed between these.</p>
<p>Information about different ways to transform vertices to clip space inside of a vertex shader:<br />
<a href="http://www.lighthouse3d.com/opengl/glsl/index.php?minimal">http://www.lighthouse3d.com/opengl/glsl/index.php?minimal</a></p>
<p>Description of different 3D spaces:<br />
<a href="http://vesta.astro.amu.edu.pl/Library/Linux/LinFocus/May1998/article6.html">http://vesta.astro.amu.edu.pl/Library/Linux/LinFocus/May1998/article6.html</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Please share:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow" id="print" href="javascript:window.location='http%3A%2F%2Fwww.printfriendly.com%2Fprint%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F%26amp%3Bpartner%3Dsociable';" title="Print this article!"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="digg" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F%26amp%3Btitle%3DClip%2520Space%26amp%3Bbodytext%3DBefore%2520rendering%2520something%2520all%2520objects%2520need%2520to%2520be%2520projected%2520to%2520the%2520screen%2520%2528%2520a%2520plane%2520%2529.%2520The%2520first%2520step%252C%2520to%2520project%2520to%2520the%2520screen%252C%2520is%2520to%2520project%2520entities%2520to%2520clip%2520space.%250D%250A%250D%250AThe%2520mostly%2520used%2520projection%2520type%2520in%2520games%2520is%2520perspective%2520projection%2520and%2520therefore';" title="Digg"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="sphinn" href="javascript:window.location='http%3A%2F%2Fsphinn.com%2Findex.php%3Fc%3Dpost%26m%3Dsubmit%26link%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F';" title="Sphinn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="del.icio.us" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F%26amp%3Btitle%3DClip%2520Space%26amp%3Bnotes%3DBefore%2520rendering%2520something%2520all%2520objects%2520need%2520to%2520be%2520projected%2520to%2520the%2520screen%2520%2528%2520a%2520plane%2520%2529.%2520The%2520first%2520step%252C%2520to%2520project%2520to%2520the%2520screen%252C%2520is%2520to%2520project%2520entities%2520to%2520clip%2520space.%250D%250A%250D%250AThe%2520mostly%2520used%2520projection%2520type%2520in%2520games%2520is%2520perspective%2520projection%2520and%2520therefore';" title="del.icio.us"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="facebook" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F%26amp%3Bt%3DClip%2520Space';" title="Facebook"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="mixx" href="javascript:window.location='http%3A%2F%2Fwww.mixx.com%2Fsubmit%3Fpage_url%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F%26amp%3Btitle%3DClip%2520Space';" title="Mixx"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="google" href="javascript:window.location='http%3A%2F%2Fwww.google.com%2Fbookmarks%2Fmark%3Fop%3Dedit%26amp%3Bbkmk%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F%26amp%3Btitle%3DClip%2520Space%26amp%3Bannotation%3DBefore%2520rendering%2520something%2520all%2520objects%2520need%2520to%2520be%2520projected%2520to%2520the%2520screen%2520%2528%2520a%2520plane%2520%2529.%2520The%2520first%2520step%252C%2520to%2520project%2520to%2520the%2520screen%252C%2520is%2520to%2520project%2520entities%2520to%2520clip%2520space.%250D%250A%250D%250AThe%2520mostly%2520used%2520projection%2520type%2520in%2520games%2520is%2520perspective%2520projection%2520and%2520therefore';" title="Google Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="current" href="javascript:window.location='http%3A%2F%2Fcurrent.com%2Fclipper.htm%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F%26amp%3Btitle%3DClip%2520Space';" title="Current"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/current.png" title="Current" alt="Current" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="linkedin" href="javascript:window.location='http%3A%2F%2Fwww.linkedin.com%2FshareArticle%3Fmini%3Dtrue%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F%26amp%3Btitle%3DClip%2520Space%26amp%3Bsource%3DGame%2BRendering%2B%26amp%3Bsummary%3DBefore%2520rendering%2520something%2520all%2520objects%2520need%2520to%2520be%2520projected%2520to%2520the%2520screen%2520%2528%2520a%2520plane%2520%2529.%2520The%2520first%2520step%252C%2520to%2520project%2520to%2520the%2520screen%252C%2520is%2520to%2520project%2520entities%2520to%2520clip%2520space.%250D%250A%250D%250AThe%2520mostly%2520used%2520projection%2520type%2520in%2520games%2520is%2520perspective%2520projection%2520and%2520therefore';" title="LinkedIn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="live" href="javascript:window.location='https%3A%2F%2Ffavorites.live.com%2Fquickadd.aspx%3Fmarklet%3D1%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F%26amp%3Btitle%3DClip%2520Space';" title="Live"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="myspace" href="javascript:window.location='http%3A%2F%2Fwww.myspace.com%2FModules%2FPostTo%2FPages%2F%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F%26amp%3Bt%3DClip%2520Space';" title="MySpace"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="netvibes" href="javascript:window.location='http%3A%2F%2Fwww.netvibes.com%2Fshare%3Ftitle%3DClip%2520Space%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F';" title="Netvibes"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="stumbleupon" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F%26amp%3Btitle%3DClip%2520Space';" title="StumbleUpon"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="twitter" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DClip%2520Space%2520-%2520http%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F';" title="Twitter"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="reddit" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F%26amp%3Btitle%3DClip%2520Space';" title="Reddit"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="technorati" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F';" title="Technorati"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow" id="yahoo! bookmarks" href="javascript:window.location='http%3A%2F%2Fbookmarks.yahoo.com%2Ftoolbar%2Fsavebm%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F10%252F05%252Fclip-space%252F%26amp%3Bt%3DClip%2520Space%26opener%3Dbm%26amp%3Bei%3DUTF-8%26amp%3Bd%3DBefore%2520rendering%2520something%2520all%2520objects%2520need%2520to%2520be%2520projected%2520to%2520the%2520screen%2520%2528%2520a%2520plane%2520%2529.%2520The%2520first%2520step%252C%2520to%2520project%2520to%2520the%2520screen%252C%2520is%2520to%2520project%2520entities%2520to%2520clip%2520space.%250D%250A%250D%250AThe%2520mostly%2520used%2520projection%2520type%2520in%2520games%2520is%2520perspective%2520projection%2520and%2520therefore';" title="Yahoo! Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2008/10/05/clip-space/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linear depth texture</title>
		<link>http://www.gamerendering.com/2008/09/28/linear-depth-texture/</link>
		<comments>http://www.gamerendering.com/2008/09/28/linear-depth-texture/#comments</comments>
		<pubDate>Sun, 28 Sep 2008 21:06:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Rendering Methods]]></category>
		<category><![CDATA[Depth buffer]]></category>
		<category><![CDATA[Fragment Shader]]></category>
		<category><![CDATA[GLSL]]></category>
		<category><![CDATA[Linear]]></category>
		<category><![CDATA[Linear Depth]]></category>
		<category><![CDATA[Linearized]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Rendering]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[Vertex Shader]]></category>
		<category><![CDATA[Z-buffer]]></category>

		<guid isPermaLink="false">http://www.gamerendering.com/?p=22</guid>
		<description><![CDATA[In some occasions a linear depth texture is preferred over the usual non-linear depth buffer (z-buffer). The linearized depth could for example be used when doing SSAO or depth of field. Rendering linear z-buffers is very easy and only the following lines of code is required ( a float texture should be set as render target):
Vertex shader:

varying float [...]]]></description>
			<content:encoded><![CDATA[<p>In some occasions a linear depth texture is preferred over the usual non-linear depth buffer (z-buffer). The linearized depth could for example be used when doing SSAO or depth of field. Rendering linear z-buffers is very easy and only the following lines of code is required ( a float texture should be set as render target):</p>
<p>Vertex shader:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p2214"><td class="code" id="p22code14"><pre class="c" style="font-family:monospace;">varying <span style="color: #993333;">float</span> depth<span style="color: #339933;">;</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    vec4 viewPos <span style="color: #339933;">=</span> gl_ModelViewMatrix <span style="color: #339933;">*</span> gl_Vertex<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this will transform the vertex into eyespace</span>
    depth <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>viewPos.<span style="color: #202020;">z</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// minus because in OpenGL we are looking in the negative z-direction</span>
    gl_Position <span style="color: #339933;">=</span> ftransform<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Fragment shader:</p>

<div class="wp_codebox"><table width="100%" ><tr id="p2215"><td class="code" id="p22code15"><pre class="c" style="font-family:monospace;">varying <span style="color: #993333;">float</span> depth<span style="color: #339933;">;</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    gl_FragColor.<span style="color: #202020;">r</span> <span style="color: #339933;">=</span> depth<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p> <br />
You might want to scale the depth in the vertex shader to a more appropriate range as the the distance will otherwise be the same as the distance from the camera to the vertex. The following vertex shader will map the near and far distances to 0..1 which is often a good idea.</p>

<div class="wp_codebox"><table width="100%" ><tr id="p2216"><td class="code" id="p22code16"><pre class="c" style="font-family:monospace;">varying <span style="color: #993333;">float</span> depth<span style="color: #339933;">;</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    vec4 viewPos <span style="color: #339933;">=</span> gl_ModelViewMatrix <span style="color: #339933;">*</span> gl_Vertex<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this will transform the vertex into eyespace</span>
    depth <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>viewPos.<span style="color: #202020;">z</span><span style="color: #339933;">-</span>near<span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span>far<span style="color: #339933;">-</span>near<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will map near..far to 0..1</span>
    gl_Position <span style="color: #339933;">=</span> ftransform<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here&#8217;s how the shader looks like if you render a cube.</p>
<div class="mceTemp"><a href="http://www.gamerendering.com/wp-content/uploads/lineardepth.jpg"><img class="size-medium wp-image-293" title="Linear Depth Rendering of a Cube" src="http://www.gamerendering.com/wp-content/uploads/lineardepth.jpg" alt="Linear Depth Rendering of a Cube" width="250" height="251" /></a></div>
<p>Link to a page about the depth buffer in DirectX<br />
<a href="http://www.mvps.org/directx/articles/linear_z/linearz.htm">http://www.mvps.org/directx/articles/linear_z/linearz.htm</a></p>
<p>A good introduction to the depth buffer in OpenGL<br />
<a href="http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html">http://www.sjbaker.org/steve/omniv/love_your_z_buffer.html</a></p>

<div class="sociable">
<div class="sociable_tagline">
<strong>Please share:</strong>
</div>
<ul>
	<li class="sociablefirst"><a rel="nofollow" id="print" href="javascript:window.location='http%3A%2F%2Fwww.printfriendly.com%2Fprint%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F%26amp%3Bpartner%3Dsociable';" title="Print this article!"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print this article!" alt="Print this article!" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="digg" href="javascript:window.location='http%3A%2F%2Fdigg.com%2Fsubmit%3Fphase%3D2%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F%26amp%3Btitle%3DLinear%2520depth%2520texture%26amp%3Bbodytext%3DIn%2520some%2520occasions%2520a%2520linear%2520depth%2520texture%25C2%25A0is%2520preferred%2520over%2520the%2520usual%2520non-linear%2520depth%2520buffer%25C2%25A0%2528z-buffer%2529.%2520The%2520linearized%2520depth%2520could%2520for%2520example%2520be%2520used%2520when%2520doing%2520SSAO%2520or%2520depth%2520of%2520field.%2520Rendering%2520linear%2520z-buffers%2520is%25C2%25A0very%2520easy%2520and%2520only%2520the%2520followi';" title="Digg"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="sphinn" href="javascript:window.location='http%3A%2F%2Fsphinn.com%2Findex.php%3Fc%3Dpost%26m%3Dsubmit%26link%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F';" title="Sphinn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="del.icio.us" href="javascript:window.location='http%3A%2F%2Fdelicious.com%2Fpost%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F%26amp%3Btitle%3DLinear%2520depth%2520texture%26amp%3Bnotes%3DIn%2520some%2520occasions%2520a%2520linear%2520depth%2520texture%25C2%25A0is%2520preferred%2520over%2520the%2520usual%2520non-linear%2520depth%2520buffer%25C2%25A0%2528z-buffer%2529.%2520The%2520linearized%2520depth%2520could%2520for%2520example%2520be%2520used%2520when%2520doing%2520SSAO%2520or%2520depth%2520of%2520field.%2520Rendering%2520linear%2520z-buffers%2520is%25C2%25A0very%2520easy%2520and%2520only%2520the%2520followi';" title="del.icio.us"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="facebook" href="javascript:window.location='http%3A%2F%2Fwww.facebook.com%2Fshare.php%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F%26amp%3Bt%3DLinear%2520depth%2520texture';" title="Facebook"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="mixx" href="javascript:window.location='http%3A%2F%2Fwww.mixx.com%2Fsubmit%3Fpage_url%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F%26amp%3Btitle%3DLinear%2520depth%2520texture';" title="Mixx"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="google" href="javascript:window.location='http%3A%2F%2Fwww.google.com%2Fbookmarks%2Fmark%3Fop%3Dedit%26amp%3Bbkmk%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F%26amp%3Btitle%3DLinear%2520depth%2520texture%26amp%3Bannotation%3DIn%2520some%2520occasions%2520a%2520linear%2520depth%2520texture%25C2%25A0is%2520preferred%2520over%2520the%2520usual%2520non-linear%2520depth%2520buffer%25C2%25A0%2528z-buffer%2529.%2520The%2520linearized%2520depth%2520could%2520for%2520example%2520be%2520used%2520when%2520doing%2520SSAO%2520or%2520depth%2520of%2520field.%2520Rendering%2520linear%2520z-buffers%2520is%25C2%25A0very%2520easy%2520and%2520only%2520the%2520followi';" title="Google Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="current" href="javascript:window.location='http%3A%2F%2Fcurrent.com%2Fclipper.htm%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F%26amp%3Btitle%3DLinear%2520depth%2520texture';" title="Current"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/current.png" title="Current" alt="Current" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="linkedin" href="javascript:window.location='http%3A%2F%2Fwww.linkedin.com%2FshareArticle%3Fmini%3Dtrue%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F%26amp%3Btitle%3DLinear%2520depth%2520texture%26amp%3Bsource%3DGame%2BRendering%2B%26amp%3Bsummary%3DIn%2520some%2520occasions%2520a%2520linear%2520depth%2520texture%25C2%25A0is%2520preferred%2520over%2520the%2520usual%2520non-linear%2520depth%2520buffer%25C2%25A0%2528z-buffer%2529.%2520The%2520linearized%2520depth%2520could%2520for%2520example%2520be%2520used%2520when%2520doing%2520SSAO%2520or%2520depth%2520of%2520field.%2520Rendering%2520linear%2520z-buffers%2520is%25C2%25A0very%2520easy%2520and%2520only%2520the%2520followi';" title="LinkedIn"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="live" href="javascript:window.location='https%3A%2F%2Ffavorites.live.com%2Fquickadd.aspx%3Fmarklet%3D1%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F%26amp%3Btitle%3DLinear%2520depth%2520texture';" title="Live"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="myspace" href="javascript:window.location='http%3A%2F%2Fwww.myspace.com%2FModules%2FPostTo%2FPages%2F%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F%26amp%3Bt%3DLinear%2520depth%2520texture';" title="MySpace"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="netvibes" href="javascript:window.location='http%3A%2F%2Fwww.netvibes.com%2Fshare%3Ftitle%3DLinear%2520depth%2520texture%26amp%3Burl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F';" title="Netvibes"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="stumbleupon" href="javascript:window.location='http%3A%2F%2Fwww.stumbleupon.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F%26amp%3Btitle%3DLinear%2520depth%2520texture';" title="StumbleUpon"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="twitter" href="javascript:window.location='http%3A%2F%2Ftwitter.com%2Fhome%3Fstatus%3DLinear%2520depth%2520texture%2520-%2520http%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F';" title="Twitter"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="reddit" href="javascript:window.location='http%3A%2F%2Freddit.com%2Fsubmit%3Furl%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F%26amp%3Btitle%3DLinear%2520depth%2520texture';" title="Reddit"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow" id="technorati" href="javascript:window.location='http%3A%2F%2Ftechnorati.com%2Ffaves%3Fadd%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F';" title="Technorati"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow" id="yahoo! bookmarks" href="javascript:window.location='http%3A%2F%2Fbookmarks.yahoo.com%2Ftoolbar%2Fsavebm%3Fu%3Dhttp%253A%252F%252Fwww.gamerendering.com%252F2008%252F09%252F28%252Flinear-depth-texture%252F%26amp%3Bt%3DLinear%2520depth%2520texture%26opener%3Dbm%26amp%3Bei%3DUTF-8%26amp%3Bd%3DIn%2520some%2520occasions%2520a%2520linear%2520depth%2520texture%25C2%25A0is%2520preferred%2520over%2520the%2520usual%2520non-linear%2520depth%2520buffer%25C2%25A0%2528z-buffer%2529.%2520The%2520linearized%2520depth%2520could%2520for%2520example%2520be%2520used%2520when%2520doing%2520SSAO%2520or%2520depth%2520of%2520field.%2520Rendering%2520linear%2520z-buffers%2520is%25C2%25A0very%2520easy%2520and%2520only%2520the%2520followi';" title="Yahoo! Bookmarks"><img src="http://www.gamerendering.com/wp-content/plugins/sociable/images/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.gamerendering.com/2008/09/28/linear-depth-texture/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
