<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>cocos2d for iPhone &#187; Tag: glReadPixels - Recent Topics</title>
		<link>http://www.cocos2d-iphone.org/forum/tags/glreadpixels</link>
		<description>A fast, easy to use, free, and community supported 2D game engine</description>
		<language>en-US</language>
		<pubDate>Fri, 10 Feb 2012 04:24:18 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.1</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://www.cocos2d-iphone.org/forum/search.php</link>
		</textInput>
		<atom:link href="http://www.cocos2d-iphone.org/forum/rss/tags/glreadpixels/topics" rel="self" type="application/rss+xml" />

		<item>
			<title>Dani on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522#post-103858</link>
			<pubDate>Sat, 09 Jul 2011 19:47:04 +0000</pubDate>
			<dc:creator>Dani</dc:creator>
			<guid isPermaLink="false">103858@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hello everybody.</p>
<p>I would like to implement a function to <strong>detect collisions between 2 sprites at pixel level</strong>. There is a good solution I've used before in ActionScript 3, but I need help to integrate it with cocos2d framework.</p>
<p><strong>Original AS2 solution:</strong> <a href="http://gskinner.com/blog/archives/2005/08/flash_8_shape_b.html">http://gskinner.com/blog/archives/2005/08/flash_8_shape_b.html</a></p>
<p><strong>Improved AS3 solution:</strong> <a href="http://troygilbert.com/2007/06/pixel-perfect-collision-detection-in-actionscript3/">http://troygilbert.com/2007/06/pixel-perfect-collision-detection-in-actionscript3/</a></p>
<p>Now look at this image:</p>
<p><img src='http://img190.imageshack.us/img190/7687/pixelperfectsample.png' /></p>
<p>Look at the third case: the method consist on drawing the first sprite in RED, the second in WHITE (with DIFFERENCE blending mode), and look if there is CYAN in the mix. We only draw the data inside the intersection, not the whole sprites.</p>
<p>The steps:</p>
<ol>
<li>Check for bounding box collision.</li>
<li>Get the intersect rect.</li>
<li>Create a bitmap object with the size of the intersection.</li>
<li>Draw the sprite A in RED (taking into account scaling, rotation, etc) into the bitmap object.</li>
<li>Draw the Sprite B in WHITE with DIFFERENCE blending (taking into account scaling, rotation, etc) into the same bitmap object.</li>
<li>Look if there is CYAN color in the bitmap object.</li>
</ol>
<p><strong><br />
I don't know how and where to draw the intersection data for both objects (steps 3, 4 and 5). Any ideas?</strong></p>
<p>This is how the function could look:</p>
<pre><code>-(BOOL) isCollisionBetweenSpriteA:(CCSprite*)spr1 spriteB:(CCSprite*)spr2 pixelPerfect:(BOOL)pp
{
    BOOL isCollision = NO;
    CGRect intersection = CGRectIntersection([spr1 boundingBox], [spr2 boundingBox]);

    if (!CGRectIsEmpty(intersection))
    {
        // If we&#39;re not checking for pixel perfect collisions, return true
        if (!pp) {return YES;}

        // 1) Draw the Sprite 1 intersection content in RED color into a bitmap object (must check if it&#39;s rotated, scaled, etc)

        // 2) Draw the Sprite 2 intersection content in WHITE color using DIFFERENCE blending option into the same bitmap object

        // 3) Check if there is CYAN color in the mix (WHITE difference blended on RED makes CYAN)
    }

    return isCollision;
}</code></pre>
<p>Please, any help and ideas to solve this will be very much appreciated. And maybe this function is a good feature for the cocos2d-iphone Extensions.</p>
<p>Regards.
</p></description>
		</item>
		<item>
			<title>pfg2009 on "Getting texture pixels without using glReadPixels()"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28677#post-141302</link>
			<pubDate>Thu, 26 Jan 2012 02:11:03 +0000</pubDate>
			<dc:creator>pfg2009</dc:creator>
			<guid isPermaLink="false">141302@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I have CCRenderTexture into which I draw stuff using the following method:</p>
<pre><code>CCRenderTexture* myTexture = ...;
[myTexture begin];
[someSprite visit];
[myTexture end];</code></pre>
<p>Occasionally, I want to peek back into this texture and read a bunch of pixels to do some additional processing with them.  I can accomplish this with glReadPixels() thusly:</p>
<pre><code>GLubyte* pixels = malloc(section_width * section_height * sizeof(GLubyte) * 4);
glBindTexture(GL_TEXTURE_2D, textureName);
glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
glBindTexture(GL_TEXTURE_2D, nil);</code></pre>
<p>This works, but it's slow for my needs and the question I have is whether I can accomplish this same task faster, by avoiding glReadPixels somehow.  Any suggestions?</p>
<p>I figured, I could keep a pointer to the raw data representing the texture in way similar to CCMutableTexture2D that has been used by folks on this forum (for example <a href="http://www.cocos2d-iphone.org/forum/topic/11745">here</a>).  In the CCMutableTexture2D, that data is passed to the GPU using glTexImage2D().  Does that method <strong>copy</strong> the data into GPU's memory or does it simply direct GPU to <strong>use</strong> that piece of memory without duplicating it?  If it's the former, I could just look through the raw data as it's updated via the CCRenderTexture begin/end method without having to call glReadPixels().  If it's the latter, I'm out of ideas.</p>
<p>Any help is much appreciated!
</p></description>
		</item>
		<item>
			<title>calin on "[BEST PRACTICE] Copying pixels in OpenGL ES and render at another location"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/25726#post-134031</link>
			<pubDate>Sun, 18 Dec 2011 13:03:14 +0000</pubDate>
			<dc:creator>calin</dc:creator>
			<guid isPermaLink="false">134031@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>What's the best practice for copying pixels from a portion of the screen and draw them at another location?</p>
<p>I want to implement a magnifying glass (maybe it will not magnify at all, just show the area as-is).<br />
I saw hm50's post (<a href="http://www.cocos2d-iphone.org/forum/topic/6871" rel="nofollow">http://www.cocos2d-iphone.org/forum/topic/6871</a>) but I wonder if this is the best method of implementing something like this. I would like to also thank araker for his great contribution on that post.</p>
<p>Maybe using <code>glCopyPixels</code> (or <code>glCopyTexSubImage2D</code>) instead of <code>glReadPixels</code> would be better in this case, since there is no transformation we need to perform out of the opengl environment? Also I am considering a mask for the new image (maybe render to a texture is to be used for this - not sure?!).</p>
<p>I'm not familiar with OpenGL. Please if anyone can give me an advice on this issue I would appreciate it. I will post the new Magnifier class here if it will be good enough for other people to use it easily, since I couldn't find anything that is plug and play.</p>
<p>I want to do this using cocos 2.0. Thank you for your time!
</p></description>
		</item>
		<item>
			<title>manucorporat on "Cocos2d screenShot"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1722#post-10843</link>
			<pubDate>Mon, 07 Sep 2009 00:30:24 +0000</pubDate>
			<dc:creator>manucorporat</dc:creator>
			<guid isPermaLink="false">10843@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Cocos2D should have a function for capture the OpenGL image in screen.<br />
I developed a function for this.</p>
<pre><code>- (Texture2D*) cocos2DScreenShot {
	CGSize size = [self displaySize];

	//Create un buffer for pixels
	int bufferLenght=size.width*size.height*4;
	unsigned char buffer[bufferLenght];

	//Read Pixels from OpenGL
	glReadPixels(0,0,size.width,size.height,GL_RGBA,GL_UNSIGNED_BYTE,&#38;buffer);
	CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, &#38;buffer, bufferLenght, NULL);
	CGImageRef iref = CGImageCreate(size.width,size.height,8,32,size.width*4,CGColorSpaceCreateDeviceRGB(),kCGBitmapByteOrderDefault,ref,NULL,true,kCGRenderingIntentDefault);

	uint32_t *pixels     = (uint32_t *)malloc(bufferLenght);
	CGContextRef context = CGBitmapContextCreate(pixels, [self winSize].width, [self winSize].height, 8, [self winSize].width*4, CGImageGetColorSpace(iref), kCGImageAlphaPremultipliedLast &#124; kCGBitmapByteOrder32Big);

	CGContextTranslateCTM(context,0, size.height);
	CGContextScaleCTM(context, 1.0, -1.0);

	switch (deviceOrientation_) {
		case CCDeviceOrientationPortrait:
			break;
		case CCDeviceOrientationPortraitUpsideDown:
			CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(180));
			CGContextTranslateCTM(context,-size.width, -size.height);
			break;
		case CCDeviceOrientationLandscapeLeft:
			CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(-90));
			CGContextTranslateCTM(context,-size.height, 0);
			break;
		case CCDeviceOrientationLandscapeRight:
			CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(90));
			CGContextTranslateCTM(context,size.width*0.5, -size.height);
			break;
	}
	CGContextDrawImage(context, CGRectMake(0.0, 0.0, size.width, size.height), iref);
	UIImage *outputImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
	Texture2D *texture = [[Texture2D alloc] initWithImage:outputImage];

	//Dealloc
	CGDataProviderRelease(ref);
	CGImageRelease(iref);
	CGContextRelease(context);
	free(pixels);

	return texture;
}</code></pre>
<p>I found the original code in: <a href="http://stackoverflow.com/questions/962390/capturing-eaglview-content-with-alpha-channel-on-iphone" rel="nofollow">http://stackoverflow.com/questions/962390/capturing-eaglview-content-with-alpha-channel-on-iphone</a></p>
<p>but I edited it because didn't support orientation and it didn't release the memory.</p>
<p>I open a inssue in the cocos2d-iphone Project:<br />
<a href="http://code.google.com/p/cocos2d-iphone/issues/detail?id=533" rel="nofollow">http://code.google.com/p/cocos2d-iphone/issues/detail?id=533</a>
</p></description>
		</item>
		<item>
			<title>Lam on "Fast set/getPixel for an opengl texture?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/2449#post-15316</link>
			<pubDate>Tue, 13 Oct 2009 21:21:55 +0000</pubDate>
			<dc:creator>Lam</dc:creator>
			<guid isPermaLink="false">15316@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I've been trying find a way to access pixels in a texture in order to get/set the color of a pixel.</p>
<p>I've been fumbling around quite a lot. Hopefully someone can present an idea to accomplish this. My implementation works but is really slow.</p>
<p>What I've done is extend Texture2D as a MutableTexture2D that allow you to access a pixel in a really slow manner.</p>
<p>Since texture data resides in the GPU I had to store the array of pixel values.</p>
<p>I've gone through these attempts at creating this class.<br />
1st:<br />
   Using glTexImage2D to create a new texture from the data array. Which means I'm releasing the old texture, then binding a new texture. It works though really slowly.</p>
<p>2nd:<br />
   glTexSubImage2D allows portions of a texture image replacement so this should be faster but implementing this made MutableTexture2D slower than before... =/</p>
<p>3rd:<br />
   Read about PBO 'Pixel Buffer Objects' and the idea seems to fit my needs but, I think, it's not implemented in opengles1.1.</p>
<p>Right now I'm at a loss of ideas. If anyone can suggest anything then that would be awesome.
</p></description>
		</item>
		<item>
			<title>jaylach on "Convert touch point of sprite to point within scaled CCTexture2D"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/16425#post-92598</link>
			<pubDate>Tue, 10 May 2011 15:35:52 +0000</pubDate>
			<dc:creator>jaylach</dc:creator>
			<guid isPermaLink="false">92598@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi Everyone -</p>
<p>I'm running into a slightly annoying problem. It's only annoying because I can't figure it out :)</p>
<p>I'm working on a game where the characters are built at runtime by combining a bunch of different part sprites together. By this I mean, there are different heads, hats, legs, arms, etc. These are all put together into a Character node to create the character sprite. Because of this, the textures I'm using have a lot of "empty" space (transparent). You can see here: <a href="http://s93.photobucket.com/albums/l64/jaylach/Patchwork%20Battle/Parts/?action=view&#038;current=Hat.png.This" rel="nofollow">http://s93.photobucket.com/albums/l64/jaylach/Patchwork%20Battle/Parts/?action=view&#038;current=Hat.png.This</a> was done so that each part will be guaranteed to fit together nicely with all the other parts, while still using the same anchor points (i.e: I don't have to manually each part separately). </p>
<p>This has caused a problem when it comes time to detect touches within my character sprite. As we all know, a sprite is the size of it' texture. Well, my texture is a lot bigger than the visible area and so the transparent areas should not be touchable. My solution was to use glReadPIxels to determine if the touched area was transparent. If so, that area is not touchable and I would return NO in containsTouchLocation. </p>
<p>The problem I'm having, however, is it seems my touch coordinates don't translate well into the actual touched location inside the texture. This problem is twofold, though, since my sprites are scaled down to about 80% the original size of the texture (this is so we can eventually use the same assets for iPhone, iPad, and Retina). </p>
<p>Can anyone provide me some guidence on converting the touch point into a point within a texture, while accounting for the scale? I've tried using convertTouchToNodeSpace and convertTouchToNodeSpaceAR. I've also tried using locationInView: from the UITouch object. I've also scaled my x and y up (x = x + (x * MY_SCALE)) to try and account for the scaled down sprite points. None of these seem to return the correct location within the texture.</p>
<p>Any ideas, suggestions, help would be greatly appreciated. Thanks many times over in advance.</p>
<p>Also, for reference, here's what the characters look like when they are "put together": <a href="http://s93.photobucket.com/albums/l64/jaylach/Patchwork%20Battle/Concepts/?action=view&#038;current=ClassesMarked.png" rel="nofollow">http://s93.photobucket.com/albums/l64/jaylach/Patchwork%20Battle/Concepts/?action=view&#038;current=ClassesMarked.png</a>
</p></description>
		</item>
		<item>
			<title>indy2005 on "Best Approach: glReadPixles and RenderTexture"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/19772#post-110663</link>
			<pubDate>Mon, 15 Aug 2011 00:20:04 +0000</pubDate>
			<dc:creator>indy2005</dc:creator>
			<guid isPermaLink="false">110663@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi,</p>
<p>I need to detect the exact position of a touch on a sprite and adjust the score/outcome based on it.  I know I could use physics bodies, but I don't want to have to introduce physics at the moment.</p>
<p>Also, I may require different parts of the sprite to mean different things at different times, so you may have to tap the characters ears one minute and their lips the next.</p>
<p>I have thought of using collision map sprites which just have an alpha value &#60;&#62; 0 where I want to detect the touch.</p>
<p>I have a few options I think:</p>
<p>1) Store an array of image data, representing the pixels of the collision map, and convert the touch co-ordinate in local node space to an index into the collision map to check for non-alpha values at that location.  I can then swap out collision maps based on the circumstances of the game.</p>
<p>Concerns, I think I need to work in pixels and not points - as 1 point could potentially be any one of 4 pixels, each pixel with its own alpha value.  I don't think I can use AtlasSprites.  Memory...I may need a lot of collision maps loaded into arrays before the level starts.</p>
<p>2) Use RenderTexture.  Detect a touch location in local node space, and then visit the collision map sprite using a RenderTexture and use glReadPixels to get the collision map data at the point of the touch on the main sprite.</p>
<p>Concerns...not sure how this will perform, if creating a render texture each time I create a touch...not sure I can keep the RenderTexture once created as a permanent collision map, as am not sure how glReadPixels will work once the RenderTexture has been created. </p>
<p>Think I can use Atlas Sprites using this method?</p>
<p>3) Anything else!?</p>
<p>Any advice appreciated.</p>
<p>Regards</p>
<p>i
</p></description>
		</item>
		<item>
			<title>derekvanvliet on "How to get pixel color within texture?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/822#post-4863</link>
			<pubDate>Wed, 22 Jul 2009 05:46:10 +0000</pubDate>
			<dc:creator>derekvanvliet</dc:creator>
			<guid isPermaLink="false">4863@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Is there a way to retrieve the color value of a pixel within a texture at a given x,y position?
</p></description>
		</item>
		<item>
			<title>hm50 on "Converting UIView references to CCDirector references:"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/6871#post-40334</link>
			<pubDate>Sat, 05 Jun 2010 22:55:50 +0000</pubDate>
			<dc:creator>hm50</dc:creator>
			<guid isPermaLink="false">40334@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I am trying (unsuccessfully) to use Chadwick Wood's "A Simpler Magnifying Glass" (<a href="http://coffeeshopped.com/2010/03/a-simpler-magnifying-glass-loupe-view-for-the-iphone" rel="nofollow">http://coffeeshopped.com/2010/03/a-simpler-magnifying-glass-loupe-view-for-the-iphone</a>) in cocos2d. Can someone point me in the right direction as to how the UIView references should instead use CCDirector references?</p>
<p>Thanks For Your Time!
</p></description>
		</item>

	</channel>
</rss>

