<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>OpenGL Depth Buffer Problems</title>
		<link>http://www.allegro.cc/forums/view/616150</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 25 Mar 2016 05:59:38 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m trying to write to an OpenGL depth buffer using a slight modification of the default fragment shader. The idea is that I can just set the uniform depth for a sprite (not sure how efficient this will be, but trying to get it to work first.)</p><p>Shader:
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="p">#ifdef GL_ES</span>
<span class="number">  2</span>precision mediump <span class="k1">float</span><span class="k2">;</span>
<span class="number">  3</span><span class="p">#endif</span>
<span class="number">  4</span>uniform sampler2D al_tex<span class="k2">;</span>
<span class="number">  5</span>uniform <span class="k1">bool</span> al_use_tex<span class="k2">;</span>
<span class="number">  6</span>varying vec4 varying_color<span class="k2">;</span>
<span class="number">  7</span>varying vec2 varying_texcoord<span class="k2">;</span>
<span class="number">  8</span>
<span class="number">  9</span>uniform <span class="k1">float</span> depth<span class="k2">;</span>
<span class="number"> 10</span>
<span class="number"> 11</span><span class="k1">void</span> main<span class="k2">(</span><span class="k2">)</span>
<span class="number"> 12</span><span class="k2">{</span>
<span class="number"> 13</span>
<span class="number"> 14</span>  <span class="k1">if</span> <span class="k2">(</span>al_use_tex<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 15</span>    gl_FragColor <span class="k3">=</span> varying_color <span class="k3">*</span> texture2D<span class="k2">(</span>al_tex, varying_texcoord<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 16</span>  <span class="k2">}</span> <span class="k1">else</span> <span class="k2">{</span> 
<span class="number"> 17</span>    gl_FragColor <span class="k3">=</span> varying_color<span class="k2">;</span>
<span class="number"> 18</span>  <span class="k2">}</span>
<span class="number"> 19</span>
<span class="number"> 20</span>  gl_FragDepth <span class="k3">=</span> depth<span class="k2">;</span>
<span class="number"> 21</span>
<span class="number"> 22</span><span class="k2">}</span>
</div></div><p>

Setting up the depth buffer:  (NOT using allegro&#39;s display options to specify a buffer, but I&#39;ve done this manually before and it worked fine.)</p><div class="source-code snippet"><div class="inner"><pre>        glGenTextures<span class="k2">(</span><span class="n">1</span>, <span class="k3">&amp;</span>depthTexture_GL<span class="k2">)</span><span class="k2">;</span>
  glBindTexture<span class="k2">(</span>GL_TEXTURE_2D, depthTexture_GL<span class="k2">)</span><span class="k2">;</span>


  glTexParameteri<span class="k2">(</span>GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR<span class="k2">)</span><span class="k2">;</span>
  glTexParameteri<span class="k2">(</span>GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR<span class="k2">)</span><span class="k2">;</span>
  glTexParameterf<span class="k2">(</span>GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP<span class="k2">)</span><span class="k2">;</span>
  glTexParameterf<span class="k2">(</span>GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP<span class="k2">)</span><span class="k2">;</span>


  glTexImage2D<span class="k2">(</span>GL_TEXTURE_2D, <span class="n">0</span>, GL_DEPTH_COMPONENT, W, H, <span class="n">0</span>, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
  

  glBindTexture<span class="k2">(</span>GL_TEXTURE_2D, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

And binding the depth buffer like this:</p><div class="source-code snippet"><div class="inner"><pre>  glBindFramebuffer<span class="k2">(</span>GL_FRAMEBUFFER, <a href="http://www.allegro.cc/manual/al_get_opengl_fbo"><span class="a">al_get_opengl_fbo</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_target_bitmap"><span class="a">al_get_target_bitmap</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
  glFramebufferTexture2DEXT<span class="k2">(</span>GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, depthTexture_GL, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
  GLenum err <span class="k3">=</span> glCheckFramebufferStatus<span class="k2">(</span>GL_FRAMEBUFFER<span class="k2">)</span><span class="k2">;</span>
  ...
 glDepthMask<span class="k2">(</span><span class="k1">true</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

<b>But the fragment shader does not appear to be writing to the buffer when i set gl_fragDepth.</b></p><p>If I pre-load the depth buffer with pixel data I can display it fine (if bind the buffer as a texture at a later draw call) so I know the buffer is being setup correctly, it just does not appear to be binding correctly even though I don&#39;t get any errors when I bind.  gl_fragDepth just does not appear to be writing to the buffer.</p><p>Is allegro doing something behind the scenes that would be unbinding the depth buffer when I do a simple al_draw_bitmap() call? (binding a different FBO?)</p><p>HALP!?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (thebignic)</author>
		<pubDate>Fri, 25 Mar 2016 00:28:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You could check with an OpenGL debugger (I&#39;ve found &quot;apitrace&quot; for Linux, which is a rather hard to use one...). As far as I know Allegro should not do anything like that. Did you call glEnable(GL_DEPTH_TEST)?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Fri, 25 Mar 2016 03:26:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve tried glEnable(GL_DEPTH_TEST) but afaik that only discards the fragment routine if it fails the criteria for the depth test which at this point, I don&#39;t even care about.   </p><p>I know its not writing to it, so enabling the actual depth test wont help.</p><p>I&#39;ll check out apitrace.  I didn&#39;t know anything like that even existed...</p><p>Edit: apitrace basically just shows a log of the APi calls which isn&#39;t all that useful. it doesnt look like allergo is unbinding anything... and I already know the framebuffer is complete and there are no errors after attaching the depth buffer.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (thebignic)</author>
		<pubDate>Fri, 25 Mar 2016 03:34:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/616150/1021176#target">thebignic</a> said:</div><div class="quote"><p>I&#39;ve tried glEnable(GL_DEPTH_TEST) but afaik that only discards the fragment routine if it fails the criteria for the depth test which at this point, I don&#39;t even care about. </p></div></div><p>

</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Even if the depth buffer exists and the depth mask is non-zero, the depth buffer is not updated if the depth test is disabled. In order to unconditionally write to the depth buffer, the depth test should be enabled and set to GL_ALWAYS (see glDepthFunc). </p></div></div><p>

From: <a href="https://www.opengl.org/sdk/docs/man/html/glDepthMask.xhtml">https://www.opengl.org/sdk/docs/man/html/glDepthMask.xhtml</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Fri, 25 Mar 2016 04:06:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I had the following:</p><div class="source-code snippet"><div class="inner"><pre>    glDepthFunc<span class="k2">(</span>GL_ALWAYS<span class="k2">)</span><span class="k2">;</span>
    glEnable<span class="k2">(</span>GL_DEPTH_TEST<span class="k2">)</span><span class="k2">;</span>
    glDepthMask<span class="k2">(</span><span class="k1">true</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

But that just seemed to cause the buffer to be full of 0.5 even though I had never cleared the buffer and had pre-loaded it with randomized pixel values.</p><p>Good to know that it has to be enabled for the mask to be written but doesn&#39;t seem to help, and in fact made things more difficult to debug because it was clearing the buffer.</p><p>Even if my fragment shader specifically sets gl_fragDepth=0 or gl_fragDepth=1, the buffer is still a neutral grey when GL_DEPTH_TEST is enabled.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (thebignic)</author>
		<pubDate>Fri, 25 Mar 2016 04:45:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>How are you setting your value for depth?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Fri, 25 Mar 2016 05:30:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Guess who was changing the target bitmap AFTER setting the shader?</p><p>&lt;--- this guy.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (thebignic)</author>
		<pubDate>Fri, 25 Mar 2016 05:59:38 +0000</pubDate>
	</item>
</rss>
