<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>[AGL]How do you make texture w/ alpha channel with allegro_gl_make_texture_ex()?</title>
		<link>http://www.allegro.cc/forums/view/586552</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 23 Jul 2006 19:45:48 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I asked this question further down in a different topic, however it was kind of appended as an afterthought and wasn&#39;t the main issue, so I think it just got lost in the nether.</p><p>Using either a PNG or TGA, what is necessary to assure that the alpha channel or transparency is used when the texture is bound?</p><p>I read the AllegroGL manual as well as some OpenGL stuff for the final parameter for allegro_gl_make_texture_ex() but none of the combinations of things I&#39;ve been trying seem to be working.  I think right now I&#39;m doing something like this on a PNG.</p><div class="source-code snippet"><div class="inner"><pre>texSprite <span class="k3">=</span> allegro_gl_make_texture_ex<span class="k2">(</span>AGL_TEXTURE_HAS_ALPHA <span class="k3">|</span> AGL_TEXTURE_FLIP,
 m_sprite, GL_RGBA<span class="k2">)</span>
</pre></div></div><p>

I&#39;ve also used GL_ALPHA at the end, as well as a combination of the two, and nothing is getting the desired result.  After the above code the texture is later blended with GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BrknPhoenix)</author>
		<pubDate>Wed, 19 Jul 2006 19:18:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Does your loaded bitmap contain the alpha channel you expect? The call to AGL your pasted looks correct.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob)</author>
		<pubDate>Wed, 19 Jul 2006 20:24:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Attach the TGA file so we can check if it does have an alpha channel saved.  Some paint progs will strip it on saving, and I for one am not sure under which circumstances load_tga() will strip it on loading.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matt Smith)</author>
		<pubDate>Wed, 19 Jul 2006 20:40:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Alright, I&#39;ll post it when I get off work...  And am I the only one who only sees a horizontal scrolling bar in my initial post in this topic and no code?</p><p>Here&#39;s the PNG I&#39;ve been using</p><p>[img]<a href="http://img.photobucket.com/albums/v160/BrknPhoenix/greencursor.png[/img">http://img.photobucket.com/albums/v160/BrknPhoenix/greencursor.png[/img</a>]</p><p>I&#39;m not even going to bother with the TGA version because I&#39;m pretty confident already it wasn&#39;t saving the alpha in photoshop.  I&#39;ve read on numerous forums that Photoshop has crap Targa support, and on Adobe&#39;s site it said PS7 required a patch for it to properly save the Targa alpha channel.  And I only have Photoshop Elements 4.0 <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" /></p><p>And I just posted this PNG image on a forum with a non-white background and the transparency is good.  So I&#39;m guessing that something with AllegroPNG isn&#39;t getting the transparency.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BrknPhoenix)</author>
		<pubDate>Wed, 19 Jul 2006 21:40:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Are you loading the bitmap as 32 bit?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Fri, 21 Jul 2006 05:46:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Are you loading the bitmap as 32 bit?
</p></div></div><p>
ie.  Are you doing this?
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> old_depth <span class="k3">=</span> <a href="http://www.allegro.cc/manual/get_color_depth" target="_blank"><span class="a">get_color_depth</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span> texSprite <span class="k3">=</span> <a href="http://www.allegro.cc/manual/load_tga" target="_blank"><span class="a">load_tga</span></a><span class="k2">(</span><span class="s">"blah.tga"</span>, NULL<span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/set_color_depth" target="_blank"><span class="a">set_color_depth</span></a><span class="k2">(</span>old_depth<span class="k2">)</span><span class="k2">;</span>

texSprite <span class="k3">=</span> allegro_gl_make_texture_ex<span class="k2">(</span>AGL_TEXTURE_HAS_ALPHA <span class="k3">|</span> AGL_TEXTURE_FLIP
    , m_sprite, GL_RGBA<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

This makes allegro load the bitmap as 32-bit.  In my opinion the API should make this native somehow (ie. load_tga_ex(32, &quot;blah.tga&quot;, NULL)) but it would require API breakage. Maybe in Allegro 5.0.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Erkle)</author>
		<pubDate>Fri, 21 Jul 2006 07:22:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Did you remember to enable blending in OpenGL? <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Fladimir da Gorf)</author>
		<pubDate>Fri, 21 Jul 2006 13:21:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, blending is enabled.  And no, I&#39;m not saving as 32 bit in targa :p That&#39;s part of the problem.  I haven&#39;t been using TGA images because I can&#39;t find anything that saves 32 bit Targa&#39;s.  My photoshop elements 4.0 apparently doesn&#39;t save TGA alpha channels at all, and GIMP keeps saving as 24 bit even though the targa has an alpha channel.</p><p>And I&#39;m not loading the png in 32 bit because it&#39;s not a 32 bit png...</p><p>I really don&#39;t know what to do here :\
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BrknPhoenix)</author>
		<pubDate>Sat, 22 Jul 2006 07:31:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>GIMP saves the alpha channel in TGA fine.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Milan Mimica)</author>
		<pubDate>Sat, 22 Jul 2006 12:39:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hm, I don&#39;t suppose someone could upload a Targa that they know for a fact is 32-bit with transparency so I could test it with my program?  So I could know whether it&#39;s the image or the code that is my problem?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BrknPhoenix)</author>
		<pubDate>Sun, 23 Jul 2006 06:13:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>here
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Milan Mimica)</author>
		<pubDate>Sun, 23 Jul 2006 12:24:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><b>edit:</b> Solved!  The problem was that GL_DEPTH_TEST was enabled while this was being done, so objects were failing the depth test instead of being blended transparently.<br />---<br /><b>edit:</b> Interesting update...  The texture appears transparent over the mouse coordinates I drew with Allegro&#39;s generic text functions.  But not over a white quad I placed in the middle of the screen!<br />---<br />It would appear the problem is my code, as the transparency doesn&#39;t show with that picture either <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" /> I don&#39;t know what the problem is, since everyone says I&#39;m doing it right...</p><p>Well, let me post the whole of what is going on in my code, and maybe someone will spot something.  Note that this code is rather poorly put together, mostly because it&#39;s the result of me deleting and re-typing over and over again to try and make things work.</p><p>As for what actually happens...  The image will display, but the parts that should be transparent appear as black instead.</p><p>Setting up the screen initially...
</p><div class="source-code snippet"><div class="inner"><pre>        glMatrixMode<span class="k2">(</span>GL_PROJECTION<span class="k2">)</span><span class="k2">;</span>
        glEnable<span class="k2">(</span>GL_TEXTURE_2D<span class="k2">)</span><span class="k2">;</span>
        glEnable<span class="k2">(</span>GL_DEPTH_TEST<span class="k2">)</span><span class="k2">;</span>   
        glEnable<span class="k2">(</span>GL_ALPHA_TEST<span class="k2">)</span><span class="k2">;</span>
        allegro_gl_use_alpha_channel<span class="k2">(</span>TRUE<span class="k2">)</span><span class="k2">;</span>     
        glLoadIdentity<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
       gluPerspective<span class="k2">(</span><span class="n">45</span>.<span class="n">0f</span>, <span class="k2">(</span>GLfloat<span class="k2">)</span>SCREEN_WIDTH <span class="k3">/</span> <span class="k2">(</span>GLfloat<span class="k2">)</span>SCREEN_HEIGHT, <span class="n">1</span>.<span class="n">0f</span>, <span class="n">1000</span>.<span class="n">0f</span><span class="k2">)</span><span class="k2">;</span>         
        glViewport<span class="k2">(</span><span class="n">0</span>, <span class="n">0</span>, SCREEN_WIDTH, SCREEN_HEIGHT<span class="k2">)</span><span class="k2">;</span>           
        glClearColor<span class="k2">(</span><span class="n">0</span>.<span class="n">0f</span>, <span class="n">0</span>.<span class="n">0f</span>, <span class="n">0</span>.<span class="n">0f</span>, <span class="n">0</span>.<span class="n">0f</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>


Loading the texture...
</p><div class="source-code snippet"><div class="inner"><pre>    <span class="k1">int</span> old_c_depth <span class="k3">=</span> <a href="http://www.allegro.cc/manual/get_color_depth" target="_blank"><span class="a">get_color_depth</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/set_color_depth" target="_blank"><span class="a">set_color_depth</span></a><span class="k2">(</span><span class="n">32</span><span class="k2">)</span><span class="k2">;</span>
    m_sprite <span class="k3">=</span> <a href="http://www.allegro.cc/manual/load_tga" target="_blank"><span class="a">load_tga</span></a><span class="k2">(</span>filename, NULL<span class="k2">)</span><span class="k2">;</span>
    texSprite <span class="k3">=</span> allegro_gl_make_texture_ex<span class="k2">(</span>AGL_TEXTURE_HAS_ALPHA <span class="k3">|</span> AGL_TEXTURE_FLIP, m_sprite, GL_RGBA<span class="k2">)</span><span class="k2">;</span>    
    <a href="http://www.allegro.cc/manual/set_color_depth" target="_blank"><span class="a">set_color_depth</span></a><span class="k2">(</span>old_c_depth<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Setting up the screen for 2D drawing for the mouse cursor...
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>    glMatrixMode<span class="k2">(</span>GL_PROJECTION<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">2</td><td>    glPushMatrix<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">3</td><td>    glLoadIdentity<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td>    gluOrtho2D<span class="k2">(</span><span class="n">0</span>, SCREEN_WIDTH, SCREEN_HEIGHT, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">5</td><td>    glMatrixMode<span class="k2">(</span>GL_MODELVIEW<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td>    <span class="k1">if</span> <span class="k2">(</span>Mouse-&gt;Visible<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span></td></tr><tr><td class="number">8</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">9</td><td>        Draw-&gt;Blit<span class="k2">(</span>Mouse-&gt;TextureSprite<span class="k2">(</span><span class="k2">)</span>,</td></tr><tr><td class="number">10</td><td>                   Mouse-&gt;X<span class="k2">(</span><span class="k2">)</span>, Mouse-&gt;Y<span class="k2">(</span><span class="k2">)</span>,</td></tr><tr><td class="number">11</td><td>                   Mouse-&gt;X<span class="k2">(</span><span class="k2">)</span> <span class="k3">+</span> Mouse-&gt;CursorWidth<span class="k2">(</span><span class="k2">)</span>,</td></tr><tr><td class="number">12</td><td>                   Mouse-&gt;Y<span class="k2">(</span><span class="k2">)</span> <span class="k3">+</span> Mouse-&gt;CursorHeight<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>        </td></tr><tr><td class="number">13</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">14</td><td>&#160;</td></tr><tr><td class="number">15</td><td>    glMatrixMode<span class="k2">(</span>GL_PROJECTION<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>    glPopMatrix<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td>    glMatrixMode<span class="k2">(</span>GL_MODELVIEW<span class="k2">)</span><span class="k2">;</span></td></tr></tbody></table></div></div><p>

The code that actually draws it...
</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">void</span> objDraw::Blit<span class="k2">(</span>GLuint sprite, <span class="k1">int</span> x1, <span class="k1">int</span> y1, <span class="k1">int</span> x2, <span class="k1">int</span> y2<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>    glEnable<span class="k2">(</span>GL_TEXTURE_2D<span class="k2">)</span><span class="k2">;</span>    </td></tr><tr><td class="number">4</td><td>    glEnable<span class="k2">(</span>GL_BLEND<span class="k2">)</span><span class="k2">;</span>    </td></tr><tr><td class="number">5</td><td>    glBlendFunc<span class="k2">(</span>GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA<span class="k2">)</span><span class="k2">;</span>          </td></tr><tr><td class="number">6</td><td>    </td></tr><tr><td class="number">7</td><td>    glBindTexture<span class="k2">(</span>GL_TEXTURE_2D, sprite<span class="k2">)</span><span class="k2">;</span>      </td></tr><tr><td class="number">8</td><td>&#160;</td></tr><tr><td class="number">9</td><td>    glColor4f<span class="k2">(</span><span class="n">1</span>.<span class="n">0f</span>, <span class="n">1</span>.<span class="n">0f</span>, <span class="n">1</span>.<span class="n">0f</span>, <span class="n">1</span>.<span class="n">0f</span><span class="k2">)</span><span class="k2">;</span>    </td></tr><tr><td class="number">10</td><td>&#160;</td></tr><tr><td class="number">11</td><td>    glBegin<span class="k2">(</span>GL_QUADS<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>        glTexCoord3f<span class="k2">(</span><span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span> glVertex3f<span class="k2">(</span>x1, y2, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>        glTexCoord3f<span class="k2">(</span><span class="n">1</span>, <span class="n">0</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span> glVertex3f<span class="k2">(</span>x2, y2, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>        glTexCoord3f<span class="k2">(</span><span class="n">1</span>, <span class="n">1</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span> glVertex3f<span class="k2">(</span>x2, y1, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>        glTexCoord3f<span class="k2">(</span><span class="n">0</span>, <span class="n">1</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span> glVertex3f<span class="k2">(</span>x1, y1, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>        </td></tr><tr><td class="number">16</td><td>    glEnd<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>    </td></tr><tr><td class="number">17</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BrknPhoenix)</author>
		<pubDate>Sun, 23 Jul 2006 19:45:48 +0000</pubDate>
	</item>
</rss>
