<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>OpenGL alpha channel woes!</title>
		<link>http://www.allegro.cc/forums/view/587562</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 16 Sep 2006 21:58:11 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
Ok, can someone explain something to me?</p><p>I have an image with an alpha channel attached to it.</p><p>If I set OpenGL to just work on the alpha channel with glColorMask( false, false, false, true ), I can then subtract from the alpha channel with a GL_ZERO, GL_ONE_MINUS_SRC_ALPHA blending mode and some drawing operations. </p><p>However every combination I&#39;ve tried to get it to add to the alpha channel (in some kind of additive blending mode) doesn&#39;t do anything. Even using a pure GL_ONE, GL_ONE doesn&#39;t touch the alpha channel..</p><p>Am I missing something here?</p><p>Rich.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Fri, 15 Sep 2006 19:06:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This could be a Z-buffering issue.</p><p>You should post a screen shot so we could better diagnose the problem.</p><p>I remember when I first was experimenting with the alpha channel textures.<br />I would draw a polygon with an alpha layered texture on top of it. The polygon would blot out some (not all) of the objects behind it. It turns the z buffer (the thing that determines if a pixel should be set to the value of a polygon or not set because it is behind another object) considered my Alpha channel polygon a solid object and refused to draw what was behind it.</p><p>The solution is to render your entire scene without your alpha textures first, then render your alpha textures in sorted order from furthest to closest. That way the z buffer has no opportunity to clobber a new polygon with blank alpha data.</p><p>Hope this helps
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Wilson Saunders)</author>
		<pubDate>Fri, 15 Sep 2006 20:31:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
Hmm.. Thanks for that. I&#39;m using Openlayer so am not using the Z buffer directly. Maybe that is the problem?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Fri, 15 Sep 2006 20:46:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>We need more information.  Is the alpha channel in the texture?  Are you trying to apply transparency to a normally translucent texture?  What API are you using to do it? OpenLayer or OpenGL or both?  What parts in Ogl and which are in Ol?</p><p>Post some code while you&#39;re at it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Sat, 16 Sep 2006 02:35:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok.</p><p>I have a texture with an alpha layer which I blit to the screen in OpenLayer. I then blit another smaller texture with an alpha channel to it in a SUBTRACTIVE blend mode. Then I copy the resulting RGBA image back into the original texture from the screen:</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>  <span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/mouse_b" target="_blank"><span class="a">mouse_b</span></a> <span class="k3">!</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">2</td><td>  <span class="k2">{</span>   </td></tr><tr><td class="number">3</td><td>   <span class="c">// Draw image.</span></td></tr><tr><td class="number">4</td><td>   Blenders::Set<span class="k2">(</span> ALPHA_BLENDER <span class="k2">)</span><span class="k2">;</span> </td></tr><tr><td class="number">5</td><td>   </td></tr><tr><td class="number">6</td><td>   gfxImages<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k3">-</span><span class="k3">&gt;</span>Blit<span class="k2">(</span><span class="n">0</span>, <span class="n">0</span>, <span class="n">1</span>.<span class="n">0</span><span class="k2">)</span><span class="k2">;</span>   </td></tr><tr><td class="number">7</td><td>   </td></tr><tr><td class="number">8</td><td>   <span class="c">// Only work on the Alpha.</span></td></tr><tr><td class="number">9</td><td>   Canvas::SetPixelWriteMode<span class="k2">(</span> ALPHA_ONLY <span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>  </td></tr><tr><td class="number">11</td><td>   <span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/mouse_b" target="_blank"><span class="a">mouse_b</span></a> <span class="k3">=</span><span class="k3">=</span> <span class="n">1</span><span class="k2">)</span> Blenders::Set<span class="k2">(</span> SUBTRACTIVE_BLENDER <span class="k2">)</span><span class="k2">;</span> </td></tr><tr><td class="number">12</td><td>   <span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/mouse_b" target="_blank"><span class="a">mouse_b</span></a> <span class="k3">=</span><span class="k3">=</span> <span class="n">2</span><span class="k2">)</span> Blenders::Set<span class="k2">(</span> ADDITIVE_BLENDER <span class="k2">)</span><span class="k2">;</span> </td></tr><tr><td class="number">13</td><td>  </td></tr><tr><td class="number">14</td><td>   <span class="c">// Draw mouse.</span></td></tr><tr><td class="number">15</td><td>   gfxImages<span class="k2">[</span><span class="n">2</span><span class="k2">]</span><span class="k3">-</span><span class="k3">&gt;</span>Blit<span class="k2">(</span><a href="http://www.allegro.cc/manual/mouse_x" target="_blank"><span class="a">mouse_x</span></a>, <a href="http://www.allegro.cc/manual/mouse_y" target="_blank"><span class="a">mouse_y</span></a>, <span class="n">1</span>.<span class="n">0</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>&#160;</td></tr><tr><td class="number">17</td><td>   Blenders::Set<span class="k2">(</span> ALPHA_BLENDER <span class="k2">)</span><span class="k2">;</span> </td></tr><tr><td class="number">18</td><td>   </td></tr><tr><td class="number">19</td><td>   <span class="c">// Copy new image.</span></td></tr><tr><td class="number">20</td><td>   gfxImages<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k3">-</span><span class="k3">&gt;</span>CopyFromScreen<span class="k2">(</span><span class="n">0</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">21</td><td>   </td></tr><tr><td class="number">22</td><td>   <span class="c">// Back to working on the full RGBA.</span></td></tr><tr><td class="number">23</td><td>   Canvas::SetPixelWriteMode<span class="k2">(</span> COLOR_AND_ALPHA <span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">24</td><td>  <span class="k2">}</span></td></tr></tbody></table></div></div><p>

Using the left mouse button I can draw holes in the texture, but with the right mouse button nothing happens as the ADDITIVE_BLENDER doesn&#39;t seem to work in this  situation.</p><p>Any ideas?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Sat, 16 Sep 2006 05:43:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Wow, OL is weird...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Sat, 16 Sep 2006 07:45:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
Why do you say that?</p><p>I&#39;ve tried using OpenGL commands here and gotten the same result. <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Sat, 16 Sep 2006 07:55:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/mouse_b" target="_blank"><span class="a">mouse_b</span></a> <span class="k3">=</span><span class="k3">=</span> <span class="n">2</span><span class="k2">)</span> Blenders::Set<span class="k2">(</span> ADDITIVE_BLENDER <span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
I&#39;m used to
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/mouse_b" target="_blank"><span class="a">mouse_b</span></a> <span class="k3">=</span><span class="k3">=</span> <span class="n">2</span><span class="k2">)</span> glBlendFunc<span class="k2">(</span>GL_SRC_ALPHA, GL_ONE<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
Which i&#39;ve only recently gotten a complete grasp over.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Sat, 16 Sep 2006 08:05:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In my experience the screen doesn&#39;t like to always store an alpha channel, even if you explicitly tell it to enable it... especially in windows. From what I read that&#39;s what you are trying to do, so it might be a bit complicated.</p><p>EDIT: Heh... &quot;there is no alpha channel&quot; <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (razor)</author>
		<pubDate>Sat, 16 Sep 2006 08:15:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
Razor: I thought of that as well, but it works with the subtractive blender. With that the alpha values in parts of the image are reduced or removed altogether. So that does seem to suggest to me that the screen is storing the alpha channel.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Sat, 16 Sep 2006 08:43:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have not used OpenLayer ever, but I do have used OpenGL extensively. You should check the <a href="http://fly.srk.fer.hr/~unreal/theredbook/chapter07.html">Red Book, Chapter 7 about Blending</a> if you want to know more about blending and transparency. Basically the blending operation takes the source color (the one coming from the pixel you are going to draw, say, Cs), and the destination color (the pixel that is already drawn in the framebuffer, let&#39;s call it Cd), multiplies each one by the factors you specify in glBlendFunc(), and adds them for the final color (Cf), which overwrites Cd in the framebuffer.</p><p>The OpenGL context has by default the (GL_ONE, GL_ZERO) blend function, which overwrites the pixels in the framebuffer. You should switch to the (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) blend function, which works pretty well for your usual transparency effect.  The blending function (GL_ZERO, GL_ONE_MINUS_SRC_ALPHA) in your post basically overwrites the source color (you multiply by zero the source color).</p><p>Make sure, being with the OpenLayer API or with OpenGL directly, to turn on the blending operation (glEnable(GL_BLEND).</p><p>I saw that OpenLayer is mainly 2D, so you should not be turning on the depth buffer test, in order to discard the Z-buffer issue W.S. points out.</p><p>Remember too that by drawing with the blending operation on will force you to draw your stuff ordered: solid things first and the transparent things.</p><p>I hope this helps you a bit.</p><p>Cheers,</p><p>Ciro.</p><p>EDIT: Clarifying the blending op.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ciro Duran)</author>
		<pubDate>Sat, 16 Sep 2006 21:21:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
The OpenGL context has by default the (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) blend function, which works pretty well for your usual transparency effect.
</p></div></div><p>
OpenGL specification says that it is (ONE, ZERO) by default. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Krzysztof Kluczek)</author>
		<pubDate>Sat, 16 Sep 2006 21:28:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Oops. My bad, it&#39;s edited :-). Thanks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ciro Duran)</author>
		<pubDate>Sat, 16 Sep 2006 21:30:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
EDIT:</p><p>I&#39;ve got it to work with the additive blend! It seems I was not drawing the image to the screen with the right blending. Now it works great!</p><p>Thanks for all the help!! <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Sat, 16 Sep 2006 21:58:11 +0000</pubDate>
	</item>
</rss>
