<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>AllegroGL - how to getpixel32 from a Texture ?</title>
		<link>http://www.allegro.cc/forums/view/591658</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Mon, 04 Jun 2007 03:56:52 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi everyone,<br />I&#39;m coding something trying to use the last AllegroGL version. I know there is OpenLayer, but I would like to try this way to have more freedom of movement...</p><p>The question is simple, but the answer, I don&#39;t know...<br />I &#39;ve got a lot of opengl textures, that are images converted from the common allegro BITMAPS; I&#39;ve destroyed the BITMAPS, and kept the Textures.</p><p><b>
I would like to know how can I read the single pixel in 32 bit format (I&#39;m interested in Alpha channel) defining X and Y of the Texture.
</b></p><p>The Allegro code I would like to translate into an OpengL routine is the following
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> <a href="http://www.allegro.cc/manual/_getpixel32" target="_blank"><span class="a">_getpixel32</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span>bmp, <span class="k1">int</span> x, <span class="k1">int</span> y<span class="k2">)</span><span class="k2">;</span>
<span class="c">// this should become:</span>
GLint myOpenGLgetpixel<span class="k2">(</span> GLint <span class="k3">*</span> my_texture, <span class="k1">int</span> x, <span class="k1">int</span> y,<span class="k2">)</span><span class="k2">;</span> <span class="c">// &lt;--- pseudocode!!</span>
</pre></div></div><p>

Can someone help me ? <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" /><br />thanks in advance
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LordHolNapul)</author>
		<pubDate>Thu, 31 May 2007 04:25:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I can&#39;t recall any OpenGL function for reading from texture, so probably the only way is to draw the pixel and read it from the screen, which is as slow as it sounds. Basically, reading anything from GPU is usually a bad idea.</p><p>Why wouldn&#39;t you juct keep the bitmap yourself and read from this? If you are afraid of memory usage, you can keep only alpha channel in separate array. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Krzysztof Kluczek)</author>
		<pubDate>Thu, 31 May 2007 04:56:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
Would some kind of glGetPixels work?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Thu, 31 May 2007 12:38:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>yes, I know there is a way using glGetPixelMap, but I don&#39;t know how to do it...</p><p>In some way I must choose the buffer where to do the alpha test, between a certain number of buffer...</p><p>I would like to get the alpha of the SPRITES and not the alpha of the BACKGROUND (it&#39;s always full opacity).</p><p>Do you have some example code?</p><p><img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LordHolNapul)</author>
		<pubDate>Thu, 31 May 2007 16:11:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If EXT_framebuffer_object is supported you could bind a texture to a framebuffer and then read from the framebuffer with glReadPixels()... it&#39;s a bit clumsy. That&#39;s why AllegroGL provides you video bitmaps which use textures and you can use normal allegro functions like getpixel on them.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Milan Mimica)</author>
		<pubDate>Thu, 31 May 2007 18:04:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I would like to get the alpha of the SPRITES and not the alpha of the BACKGROUND (it&#39;s always full opacity).
</p></div></div><p>
Then make sure you create backbuffer with alpha channel and write sprite alpha value to it. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Krzysztof Kluczek)</author>
		<pubDate>Thu, 31 May 2007 21:26:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Won&#39;t:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> color <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>

glBindTexture<span class="k2">(</span>GL_TEXTURE_2D, texture<span class="k2">)</span><span class="k2">;</span>
glGetTexSubImage2D<span class="k2">(</span>GL_TEXTURE_2D, <span class="n">0</span>, x, y, <span class="n">1</span>, <span class="n">1</span>, GL_RGBA, GL_UNSIGNED_BYTE, <span class="k3">&amp;</span>color<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
work? Just be careful of byte order.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Fri, 01 Jun 2007 01:24:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Then make sure you create backbuffer with alpha channel and write sprite alpha value to it. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div></div><p>

Yes, this is what I must do!<br />I&#39;m still trying to understand how the function below works ..(?)
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> glReadBuffer<span class="k2">(</span> GLenum mode <span class="k2">)</span>
</pre></div></div><p>

Allegro use the following code:
</p><div class="source-code snippet"><div class="inner"><pre>allegro_gl_set<span class="k2">(</span>AGL_DOUBLEBUFFER , <span class="n">1</span><span class="k2">)</span> 
<span class="c">//[....]</span>
allegro_gl_flip<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>   <span class="c">// Flips the front and back framebuffers.</span>
</pre></div></div><p> </p><p>So we are in a DOUBLE BUFFER mode, and this is a swap between GL_FRONT and GL_BACK<br />buffers... <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p><b>Now, how can I define a new Buffer , isolated from this two , that can be used exclusively to test the alpha channel. Probably it can also be used for drawing operations, but the main purpose is the alpha check.

Do someone of you know the Opengl sequence of commands to define this new buffer ? </b></p><p>So , my workspace should be like this....</p><p>- BACKBUFFER:<br />1) Backround   - BackBuffer<br />2) Sprites     - BackBuffer<br />3) Alpha Sprites - (....?)<br />4) Lighting/post effects - backbuffer</p><p>- Front BUFFER<br />1) Screen (with the previous image)</p><p>Lot of thanks   <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /></p><p>*************************************************<br />EDIT for KITTY KAT</p><p>Your function is very interesting, but I cannot find any documentation about on my pages.... The only I&#39;ve found similar to your is the following... very interesting.. how it works ? <img src="http://www.allegro.cc/forums/smileys/cool.gif" alt="8-)" /></p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> glTexImage2D<span class="k2">(</span> GLenum target, GLint level, GLint components, GLsizei width, 
GLsizei height, GLint border, GLenum format, GLenum type, <span class="k1">const</span> GLvoid <span class="k3">*</span>pixels <span class="k2">)</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LordHolNapul)</author>
		<pubDate>Fri, 01 Jun 2007 01:25:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
int color = 0;</p><p>glBindTexture(GL_TEXTURE_2D, texture);<br />glGetTexSubImage2D(GL_TEXTURE_2D, 0, x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &amp;color);<br />work? Just be careful of byte order.
</p></div></div><p>

Or use this type-safe, endian-safe code instead:</p><div class="source-code snippet"><div class="inner"><pre>GLubyte color<span class="k2">[</span><span class="n">4</span><span class="k2">]</span><span class="k2">;</span>

glBindTexture<span class="k2">(</span>GL_TEXTURE_2D, texture<span class="k2">)</span><span class="k2">;</span>
glGetTexSubImage2D<span class="k2">(</span>GL_TEXTURE_2D, <span class="n">0</span>, x, y, <span class="n">1</span>, <span class="n">1</span>, GL_RGBA, GL_UNSIGNED_BYTE, <span class="k3">&amp;</span>color<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

color[0] is the R channel, color[1] is G, etc. If you want the reverse order, just use GL_ABGR_EXT instead of GL_RGBA.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
how can I define a new Buffer , isolated from this two , that can be used exclusively to test the alpha channel. Probably it can also be used for drawing operations, but the main purpose is the alpha check.
</p></div></div><p>
Can you describe, exactly, the operation you want to do to your bitmaps? It&#39;s not clear at all what you are trying to accomplish, since you are mixing intent with implementation.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob)</author>
		<pubDate>Fri, 01 Jun 2007 01:33:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Where can I find useful documentation about this function ?<br />It could be the quick solution to alpha check of a texture...</p><div class="source-code snippet"><div class="inner"><pre>glGetTexSubImage2D<span class="k2">(</span>...<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LordHolNapul)</author>
		<pubDate>Fri, 01 Jun 2007 01:43:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hmm, apparently I got it mixed up with <tt>glTexSubImage2D</tt>. The Get version doesn&#39;t exist. You can however use glGetTexImage2D to get the whole texture, then read whichever pixel(s) you want.</p><p>But as Bob asked, what exactly are you trying to accomplish with this? There may be a better way to do it. You don&#39;t generally need to use a getpixel-like function in accelerated rendering contexts, since it negates a big chunk of its advantages.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Fri, 01 Jun 2007 05:58:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I just want to access the 32bits of a TEXTURE, 8bit red, 8 bit green, 8bit blue and 8 bit alpha....<br />I don&#39;t know how. I don&#39;t know the sequence of Opengl commands...</p><p>When I&#39;ve got all this kind of informations I can do some features to my games..</p><p>My Texture are 32bit texture (with alpha channel) and my screen buffer is 32 bit.<br />thanks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LordHolNapul)</author>
		<pubDate>Fri, 01 Jun 2007 14:50:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
When I&#39;ve got all this kind of informations I can do some features to my games..
</p></div></div><p>
Are you going to tell us what that feature is? </p><p>glGetTexImage() is how you get the information you want.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob)</author>
		<pubDate>Fri, 01 Jun 2007 21:04:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>well , ok, this is not a real feature...<br />I&#39;m trying to convert my game (download it below) to an OpengL version.. <br />then I should move on in further programming and new features about my game...</p><p>if you write to me some code I will surely try it <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /><br />The problem is that I&#39;m not familiar with opengl yet. I&#39;ve got output of my sprites, but advanced tests are very difficult to me. </p><p><img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /> <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" /> <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LordHolNapul)</author>
		<pubDate>Sat, 02 Jun 2007 00:02:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
I think what you mean is that you want to convert your memory sprites to a texture format that OpenGL can use. This is different from reading from a texture in video memory.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Sat, 02 Jun 2007 02:36:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I think what you mean is that you want to convert your memory sprites to a texture format that OpenGL can use. This is different from reading from a texture in video memory.
</p></div></div><p>

Plz, do not think. <br />I want to read all the BITS of an OpenGL Texture <s>or</s> at least, some specific BITS inside the Texture, giving the coordinates. the Allegro BITMAPS has been delete to free the space, and they cannot be used. So I need to replace all BITAMP commons operation upon the TEXTURES.</p><p>(Video memory , in this case, is ambiguos, because we could have Allegro video bitmaps and Opengl video bitmaps, so forget about that. Call them Textures..)</p><p>Someone know how ?<br />Thanks <img src="http://www.allegro.cc/forums/smileys/lipsrsealed.gif" alt=":-X" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LordHolNapul)</author>
		<pubDate>Sat, 02 Jun 2007 22:27:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
I&#39;ll try again. Reading from a texture is EXTREEMLY SLOW. Since most PC&#39;s have more main memory than video card memory, you might as well keep the original images in memory, use getpixel or something similar with them. You would also need the original images in memory to restore them to the gfx card if the user tabs away and back or you change screen resolution.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Sat, 02 Jun 2007 22:33:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
the Allegro BITMAPS has been delete to free the space, and they cannot be used.
</p></div></div><p>
Then don&#39;t delete them. They don&#39;t take that much space and if they take less than 100MB, then its fine if you leave them in memory and use them. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
You would also need the original images in memory to restore them to the gfx card if the user tabs away and back or you change screen resolution.
</p></div></div><p>
If user tabs away OpenGL restores everything for you. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Krzysztof Kluczek)</author>
		<pubDate>Sat, 02 Jun 2007 23:01:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
How, if the video card memory is changed?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Sat, 02 Jun 2007 23:57:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
How, if the video card memory is changed?
</p></div></div><p>
Probably driver keeps a copy in system memory. OpenGL standard doesn&#39;t say anything that you may lose a texture after it has been properly created, so driver somehow has to restore it for you. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Krzysztof Kluczek)</author>
		<pubDate>Sun, 03 Jun 2007 01:07:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I want to read all the BITS of an OpenGL Texture <s>or</s> at least, some specific BITS inside the Texture, giving the coordinates.
</p></div></div><p>
You&#39;re still not saying what for. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> If you need to read pixels off a texture/bitmap, then the program is possibly not designed to be accelerated well at all.</p><p>If you say what you need to use the pixels for, perhaps we can help figure out a way to do it to take advantage of the provided acceleration.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Sun, 03 Jun 2007 01:52:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
OpenLayer has a setting to keep a memory copy or not, so I didn&#39;t think it was automatic in the OpenGL driver.. But I wouldn&#39;t know for sure! <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Sun, 03 Jun 2007 13:42:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
OpenLayer has a setting to keep a memory copy or not, so I didn&#39;t think it was automatic in the OpenGL driver.
</p></div></div><p>
This is probably for reading pixels from Allegro BITMAP, like we are suggesting in this thread. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Krzysztof Kluczek)</author>
		<pubDate>Sun, 03 Jun 2007 17:21:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well if it is not enabled than alt-tabbing away and then back results in a black screen as all the textures are lost (IIRC). <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Sun, 03 Jun 2007 17:25:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><b>Ok.
I&#39;ll keep allegro bitmap, but in my opinion it&#39;s not the best way, because:</b></p><p>If I enlarge the Texture, his alpha channel enlarges too... so if I must get a point from the BITMAP, I must do more computing (enlarged coordinates) to get the right point.<br />If I ROTATE the sprite, his alpha channel rotates too... same problem.</p><p>Anyway all of this sounds much difficult.</p><p>Until today I&#39;ve rotated my sprites drawing rotated images, and not rotating the Square in which they are...</p><p><b>Thanks everyone guys. Simple is perfect, a bit expensive in memory, but perfect (for 2D games i mean).</b></p><p><img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LordHolNapul)</author>
		<pubDate>Sun, 03 Jun 2007 17:34:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Well if it is not enabled than alt-tabbing away and then back results in a black screen as all the textures are lost (IIRC). <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div></div><p>Might you be thinking of D3D? I&#39;m quite sure OGL specifies that textures must not be lost.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sun, 03 Jun 2007 18:16:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
Thomas: It could be.. now I&#39;m confused!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Sun, 03 Jun 2007 18:38:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
If I enlarge the Texture, his alpha channel enlarges too... so if I must get a point from the BITMAP, I must do more computing (enlarged coordinates) to get the right point.<br />If I ROTATE the sprite, his alpha channel rotates too... same problem.
</p></div></div><p>
If you enlarge the texture you need to have something to rebuffer it with. I don&#39;t think you can just &quot;enlarge a texture&quot; while keeping its contents.. and rotate? How would you rotate a texture?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Mon, 04 Jun 2007 03:56:52 +0000</pubDate>
	</item>
</rss>
