<?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 Posts</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 03:49:43 +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" rel="self" type="application/rss+xml" />

		<item>
			<title>Some Guy on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/4#post-143386</link>
			<pubDate>Thu, 09 Feb 2012 03:04:36 +0000</pubDate>
			<dc:creator>Some Guy</dc:creator>
			<guid isPermaLink="false">143386@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Thanks Dani - I didn't change very much of your code :) - I just dynamically created the render texture, and matched its size to that of the intersection rectangle. </p>
<p>After fixing the error in calculating the offset, my fps is just ~50fps on a first gen iPhone when the bounding boxes constantly overlap.<br />
The fps should be better on 3gs - I don't have one so I can't check. On 4th Gen iPod it is 60 (for checks every frame when bounding boxes overlap). </p>
<p>On older devices this test is probably ideal for projectiles, or objects which will stop checking after one positive instance of collision, or in cases where bounding boxes won't overlap for too many consecutive frames. I'm not aware of any other technique which is more efficient or easier to use for sprites of irregular shapes and orientations.
</p></description>
		</item>
		<item>
			<title>Dani on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/4#post-143248</link>
			<pubDate>Wed, 08 Feb 2012 10:57:49 +0000</pubDate>
			<dc:creator>Dani</dc:creator>
			<guid isPermaLink="false">143248@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p><strong>@SomeGuy</strong>: It's a common error, I have make millions of mistakes with offsets, and in general, false positives are caused by them. By the way, very good improvements, congratulations!
</p></description>
		</item>
		<item>
			<title>Some Guy on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/4#post-143218</link>
			<pubDate>Wed, 08 Feb 2012 07:03:19 +0000</pubDate>
			<dc:creator>Some Guy</dc:creator>
			<guid isPermaLink="false">143218@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I figured it out - it was due to an error on my part in calculating the offset. Sorry about the confusion.
</p></description>
		</item>
		<item>
			<title>Some Guy on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/3#post-142742</link>
			<pubDate>Sun, 05 Feb 2012 13:20:21 +0000</pubDate>
			<dc:creator>Some Guy</dc:creator>
			<guid isPermaLink="false">142742@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>After more testing - I have found some false positives &#38; false negatives using this algorithm.<br />
For example, sometimes a collision is detected even when two sprites are close to each other but not touching.</p>
<p>No collision:<br />
<img src="http://img210.imageshack.us/img210/2417/nocollision.png" alt="Image Hosted by ImageShack.us" /></p>
<p>False collision:<br />
<img src="http://img846.imageshack.us/img846/1409/collision.png" alt="Image Hosted by ImageShack.us" /></p>
<p>What could be wrong? :(<br />
Here is my code:<br />
<pre><code>-(BOOL) isCollisionBetweenSpriteA:(CCSprite*)spr1 spriteB:(CCSprite*)spr2 pixelPerfect:(BOOL)pp
{
    BOOL isCollision = NO; 

    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc]init];
    CGRect intersection = CGRectIntersection([spr1 boundingBox], [spr2 boundingBox]);

    // Look for simple bounding box collision
    if (!CGRectIsEmpty(intersection))
    {
        // If we&#039;re not checking for pixel perfect collisions, return true
        if (!pp) {return YES;}

        CGPoint spr1OldPosition = spr1.position;
        CGPoint spr2OldPosition = spr2.position;

        [self offsetX:spr1 intersection:intersection];
        [self offsetY:spr1 intersection:intersection];

        [self offsetX:spr2 intersection:intersection];
        [self offsetY:spr2 intersection:intersection];

        intersection = CGRectIntersection([spr1 boundingBox], [spr2 boundingBox]);

        // Offset
        CCSpriteBatchNode* _sbnMain =(CCSpriteBatchNode*) spr1.parent;

        //NOTE: We are assuming that the spritebatchnode is always at 0,0
        //        CGPoint oldBatchPosition = _sbnMain.position;        

        // Get intersection info
        unsigned int x = (intersection.origin.x)* CC_CONTENT_SCALE_FACTOR();
        unsigned int y = (intersection.origin.y)* CC_CONTENT_SCALE_FACTOR();
        unsigned int w = intersection.size.width* CC_CONTENT_SCALE_FACTOR();
        unsigned int h = intersection.size.height* CC_CONTENT_SCALE_FACTOR();
        unsigned int numPixels = w * h;// * CC_CONTENT_SCALE_FACTOR();

        // create render texture and make it visible for testing purposes
        int renderWidth = w;
        int renderHeight = h;

        if(renderWidth&#60;16)
        {
            renderWidth = 16;
        }

        if(renderHeight &#60; 16)
        {
            renderHeight = 16;
        }

        CCRenderTexture* _rt = [CCRenderTexture renderTextureWithWidth:renderWidth height:renderHeight];

        //rt is always going to be at 0,0 - can&#039;t change it.
        _rt.position = CGPointMake(0, 0);
        [[RIGameScene sharedGameScene]addChild:_rt];
        _rt.visible = YES;

        //NSLog(@&#34;\nintersection = (%u,%u,%u,%u), area = %u&#34;,x,y,w,h,numPixels);

        // Draw into the RenderTexture
        [_rt beginWithClear:0 g:0 b:0 a:0];

        // Render both sprites: first one in RED and second one in GREEN
        glColorMask(1, 0, 0, 1);
        [_sbnMain visitSprite:spr1];
        glColorMask(0, 1, 0, 1);
        [_sbnMain visitSprite:spr2];
        glColorMask(1, 1, 1, 1);

        // Get color values of intersection area
        ccColor4B *buffer = malloc( sizeof(ccColor4B) * numPixels );
        glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

        [_rt end];

        // Read buffer

        for(unsigned int i=0; i&#60;numPixels; i+=1)
        {
            ccColor4B color = buffer[i];

            if (color.r &#62; 0 &#38;&#38; color.g &#62; 0)
            {
                isCollision = YES;
                break;
            }
        }

        // Free buffer memory
        free(buffer);
        //        _sbnMain.position = oldBatchPosition;
        spr1.position = spr1OldPosition;
        spr2.position = spr2OldPosition;
        [[RIGameScene sharedGameScene]removeChild:_rt cleanup:YES];
    }

    [pool drain];

    return isCollision;
}</code></pre>
<p>And my code for custom spritebatchnode draw and visit:<br />
<pre><code>#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> &#34;CCSpriteBatchNodeExtended.h&#34;

static 	SEL selUpdate = NULL;

@implementation CCSpriteBatchNode(drawSpritesPPCD)

-(void) drawSprite:(CCSprite*)spr
{
	[super draw];

	// Optimization: Fast Dispatch
	if( textureAtlas_.totalQuads == 0 )
		return;	

	CCSprite *child;
	ccArray *array = descendants_-&#62;data;

	NSUInteger i = array-&#62;num;
	id *arr = array-&#62;arr;

	unsigned int index = 0;

	if (i &#62; 0)
	{
		while (i-- &#62; 0)
		{
			child = *arr++;

			// fast dispatch
			if (child == spr)
			{
				index = [child atlasIndex];
				child-&#62;updateMethod(child, selUpdate);
			}
		}

		// Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
		// Needed states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
		// Unneeded states: -

		BOOL newBlend = blendFunc_.src != CC_BLEND_SRC &#124;&#124; blendFunc_.dst != CC_BLEND_DST;
		if( newBlend )
			glBlendFunc( blendFunc_.src, blendFunc_.dst );

		[textureAtlas_ drawNumberOfQuads:1 fromIndex:index];

		if( newBlend )
			glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
	}
}

-(void) visitSprite:(CCSprite*)spr
{
	if (!visible_)
		return;

	glPushMatrix();

	if ( grid_ &#38;&#38; grid_.active) {
		[grid_ beforeDraw];
		[self transformAncestors];
	}

	[self transform];

	[self drawSprite:spr];

	if ( grid_ &#38;&#38; grid_.active)
		[grid_ afterDraw:self];

	glPopMatrix();
}

@end</code></pre></description>
		</item>
		<item>
			<title>Some Guy on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/3#post-141936</link>
			<pubDate>Tue, 31 Jan 2012 05:04:08 +0000</pubDate>
			<dc:creator>Some Guy</dc:creator>
			<guid isPermaLink="false">141936@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>After optimizing my code, I now get 60fps on my 1st Generation iPhone in debug mode. :)<br />
Part of achieving the good frame rate had to do with optimizing some other code which runs in the background. Also, since I have different "classes" of sprites which interact with each other (enemy / hero / bullet) etc, not all of them need to check for collisions with everyone else. For example if hero checks for collisions with enemy, then enemy may not have to check for collisions with hero as long as hero informs enemy of a collision detection. This can be used to reduce the number of simultaneous calls to this collision checking method improving framewrate.</p>
<p>The code outlined by Dani is efficient. The only changes I made were to create a renderTexture the same size as the intersection box or 16px, whichever is greater.<br />
The reason for a minimum size, is that I received errors related to the frame buffer if the render texture was too low. I picked 16px, since some of the source code looked like it changes the size of the texture to the next power of two if it isn't a power of two.<br />
I also change the step of the loop dynamically - with larger step for larger numPixels, and smaller step for smaller numPixels.</p>
<p>As the renderTexture.sprite is always at (0,0), I move the sprites so that the origin of the intersection box is at 0,0. This is easily done since the origin of the original intersection box can be used as an offset to move the sprites.</p>
<p>After the draw routine I put the sprite back.
</p></description>
		</item>
		<item>
			<title>Some Guy on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/3#post-141869</link>
			<pubDate>Mon, 30 Jan 2012 15:23:19 +0000</pubDate>
			<dc:creator>Some Guy</dc:creator>
			<guid isPermaLink="false">141869@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Thanks Dani - Collisions perform really well for me! (on my 1st generation iPhone).</p>
<p>When the bounding boxes of both sprites intersect = 55fps<br />
When the actual sprites overlap = 55fps.</p>
<p>Every few seconds it drops to 47fps (barely for an instant), so its almost a constant 55fps.<br />
It didn't seem to make any difference whether I was on debug or release build.</p>
<p>I need to test out many more scenarios for performance, but its looking good so far.
</p></description>
		</item>
		<item>
			<title>Dani on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/3#post-141867</link>
			<pubDate>Mon, 30 Jan 2012 14:53:40 +0000</pubDate>
			<dc:creator>Dani</dc:creator>
			<guid isPermaLink="false">141867@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p><strong>@Martin</strong>: what Kobold2D does it's not pixel perfect in all situations, only works for non rotated sprites (or that's what I read time ago <a href="http://www.learn-cocos2d.com/2011/12/fast-pixelperfect-collision-detection-cocos2d-code-1of2/">here</a>).</p>
<p>The most easy and exact collision detection is to read what's on the screen after making all tranformations, but that seems to be expensive if it's done without diving into OpenGL waters and making substantial changes to cocos2d core, I suppose.</p>
<p>I think that some people here are still using this pixel perfect method and have improved it, so they should know how to optimize it, and the limitations.
</p></description>
		</item>
		<item>
			<title>Martin on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/3#post-141862</link>
			<pubDate>Mon, 30 Jan 2012 14:19:36 +0000</pubDate>
			<dc:creator>Martin</dc:creator>
			<guid isPermaLink="false">141862@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Doesn't kobold2d come with a pixel perfect collision example?
</p></description>
		</item>
		<item>
			<title>Dani on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/3#post-141859</link>
			<pubDate>Mon, 30 Jan 2012 12:47:10 +0000</pubDate>
			<dc:creator>Dani</dc:creator>
			<guid isPermaLink="false">141859@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p><strong>@Some Guy</strong>: Hello man.</p>
<p>I don't work in this collision method anymore, but I remember some things. If you make your <em>CCRenderTexture</em> smaller than screen size and place it in, for instance, (23,60), you must move the sprites involved in the collision to that location. I was using a <em>CCSpriteBatchNode</em>, so I moved the whole batch node, something like this (<em>_sbnMain</em> is the batch node):</p>
<pre><code>-(BOOL) isCollisionBetweenSpriteA:(CCSprite*)spr1 spriteB:(CCSprite*)spr2 pixelPerfect:(BOOL)pp
{
    BOOL isCollision = NO;
    CGRect intersection = CGRectIntersection([spr1 boundingBox], [spr2 boundingBox]);

    // Look for simple bounding box collision
    if (!CGRectIsEmpty(intersection))
	{
        // If we&#039;re not checking for pixel perfect collisions, return true
        if (!pp) {return YES;}

		// Offset
		int offsetX = -spr1.position.x + K_RT_WIDTH*0.5f;
		int offsetY = -spr1.position.y + K_RT_HEIGHT*0.5f;

        // Get intersection info
        unsigned int x = intersection.origin.x + offsetX;
        unsigned int y = intersection.origin.y + offsetY;
        unsigned int w = intersection.size.width;
        unsigned int h = intersection.size.height;
        unsigned int numPixels = w * h;

		_sbnMain.position = ccp(offsetX,offsetY);

		// Draw into the RenderTexture

		[_rt begin];
		[_clearSprite visit];

		// Render both sprites: first one in RED and second one in GREEN
        glColorMask(1, 0, 0, 1);
        [_sbnMain visitSprite:spr1];
        glColorMask(0, 1, 0, 1);
        [_sbnMain visitSprite:spr2];
        glColorMask(1, 1, 1, 1);

        ccColor4B *buffer = malloc( sizeof(ccColor4B) * numPixels );
        glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

        [_rt end];

		_sbnMain.position = ccp(0,0);

        // Read buffer
        unsigned int step = 1;
        for(unsigned int i=0; i&#60;numPixels; i+=step)
        {
            ccColor4B color = buffer[i];

            if (color.r &#62; 0 &#38;&#38; color.g &#62; 0)
            {
                isCollision = YES;
                break;
            }
        }

        // Free buffer memory
        free(buffer);
    }

    return isCollision;
}</code></pre>
<p>I'm not sure, but if I remember well, you cannot change the <strong>anchor point</strong> of a <em>CCRenderTexture</em>, so it's always (0.5f,0.5f).</p>
<p>Also, to visit only one sprite from the batch node, you can use a category like this:</p>
<p><strong>CCSpriteBatchNodeExtended.h</strong></p>
<pre><code>@interface CCSpriteBatchNode(drawSpritesPPCD)

-(void) drawSprite:(CCSprite*)spr;
-(void) visitSprite:(CCSprite*)spr;

@end</code></pre>
<p><strong>CCSpriteBatchNodeExtended.m</strong></p>
<pre><code>#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> &#34;CCSpriteBatchNodeExtended.h&#34;

static 	SEL selUpdate = NULL;

@implementation CCSpriteBatchNode(drawSpritesPPCD)

+(void) initialize
{
	if ( self == [CCSpriteBatchNode class] ) {
		selUpdate = @selector(updateTransform);
	}
}

-(void) drawSprite:(CCSprite*)spr
{
	[super draw];

	// Optimization: Fast Dispatch
	if( textureAtlas_.totalQuads == 0 )
		return;	

	CCSprite *child;
	ccArray *array = descendants_-&#62;data;

	NSUInteger i = array-&#62;num;
	id *arr = array-&#62;arr;

	unsigned int index = 0;

	if (i &#62; 0)
	{
		while (i-- &#62; 0)
		{
			child = *arr++;

			// fast dispatch
			if (child == spr)
			{
				index = [child atlasIndex];
				child-&#62;updateMethod(child, selUpdate);
			}
		}

		// Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
		// Needed states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
		// Unneeded states: -

		BOOL newBlend = blendFunc_.src != CC_BLEND_SRC &#124;&#124; blendFunc_.dst != CC_BLEND_DST;
		if( newBlend )
			glBlendFunc( blendFunc_.src, blendFunc_.dst );

		[textureAtlas_ drawNumberOfQuads:1 fromIndex:index];

		if( newBlend )
			glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
	}
}

-(void) visitSprite:(CCSprite*)spr
{
	if (!visible_)
		return;

	glPushMatrix();

	if ( grid_ &#38;&#38; grid_.active) {
		[grid_ beforeDraw];
		[self transformAncestors];
	}

	[self transform];

	[self drawSprite:spr];

	if ( grid_ &#38;&#38; grid_.active)
		[grid_ afterDraw:self];

	glPopMatrix();
}

@end</code></pre>
<p>It's old code for cocos2d v1.0, I don't know if it's still working. But the idea is to modify the draw and visit method from CCSpriteBatchNode to draw only the sprite you are passing as parameter.</p>
<p>Hope this helps.
</p></description>
		</item>
		<item>
			<title>Some Guy on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/3#post-141843</link>
			<pubDate>Mon, 30 Jan 2012 10:00:19 +0000</pubDate>
			<dc:creator>Some Guy</dc:creator>
			<guid isPermaLink="false">141843@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Also, you have mentioned modifying the draw and visit methods when using spriteBatchNodes, so that only the sprite concerned is passed in - Can you show me how you did that? (I don't understand the openGl API and how the draw &#38; visit methods work).</p>
<p>Right now, I get the currently displayed flame from the sprite, create a separate sprite, and visit the new sprite instead.
</p></description>
		</item>
		<item>
			<title>Some Guy on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/3#post-141842</link>
			<pubDate>Mon, 30 Jan 2012 09:56:53 +0000</pubDate>
			<dc:creator>Some Guy</dc:creator>
			<guid isPermaLink="false">141842@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi, I have run into some trouble trying to integrate Dani's code with my project. I hope you can help me out:<br />
If I integrate the code without any changes, then it works as expected. The problem appears when I try to create the CCRenderTexture of a size other than the screen size, and try to position it at a location other than (winSize*width*0.5f, winSize*height*0.5f).</p>
<p>When I set the size to be less than screen size, collisions seem to be detected even when there is a noticeable amount of space between the colliding sprites (~10px?)<br />
Likewise, regardless of where  I specify the location, it seems that the render texture starts at 0,0 till the height, width specified.</p>
<p>Since many of you have created 10x10 size renderTextures, and repositioned them and the sprites, I was wondering if you could show me some sample code of what you did. I'm probably doing something incorrect, but I can't figure it out.
</p></description>
		</item>
		<item>
			<title>pfg2009 on "Getting texture pixels without using glReadPixels()"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28677#post-141312</link>
			<pubDate>Thu, 26 Jan 2012 04:23:39 +0000</pubDate>
			<dc:creator>pfg2009</dc:creator>
			<guid isPermaLink="false">141312@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Here is some more interesting data that doesn't seem to make much sense.  I ran the same glReadPixels() test on two different devices - a gen 2 iPod Touch running iOS4.2 and gen &#60;latest&#62; iPod Touch running iOS 5.0.1.  The test simply created an empty CCRenderTexture of 512x512 pixels, calling glReadPixels on it every frame and measuring the delay.  Here is the code:</p>
<pre><code>if (tex == nil) {
		tex = [[CCRenderTexture renderTextureWithWidth:512 height:512] retain];
	}

	[tex begin];

	float x = 0;
	float y = 0;
	float w = 512;
	float h = 512;

	GLubyte* pixels = malloc(w * h * sizeof(GLubyte) * 4);
	glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

	[tex end];
	free(pixels);</code></pre>
<p>On the older device running iOS4.2, the glReadPixels call (which I measured independent of the other calls (mallocs and such)) hovered at at around 25ms.  On the new device, the glReadPixels call lasted a whooping (and consistent) 100ms.</p>
<p>Anyone know why that might be?
</p></description>
		</item>
		<item>
			<title>pfg2009 on "Getting texture pixels without using glReadPixels()"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28677#post-141309</link>
			<pubDate>Thu, 26 Jan 2012 03:23:17 +0000</pubDate>
			<dc:creator>pfg2009</dc:creator>
			<guid isPermaLink="false">141309@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Thanks for the reply and code sample, @<a href='http://www.cocos2d-iphone.org/forum/profile/11274'>araker</a>!  And you are totally right about the texture binding - my bad.</p>
<p>I found this interesting post on <a href="http://stackoverflow.com/questions/7895856/pixel-formats-cvpixelbufferrefs-and-glreadpixels">CVPixelBufferRefs</a> supposedly part of iOS5 but I haven't dug into those yet (and according to the post, they only have hardware support on the new devices, reverting to glReadPixels on older hardware).  Most of this is way over my head in terms of my knowledge of OpenGL, but if I sniff out something interesting, I'll let you know.</p>
<p>EDIT: as far as the type of processing I need - I was looking at the possibility of rendering dynamic 2D shadows based on programmatically generated textures.  I believe this would be an easy accomplishment with shaders, but I'm not quite ready to jump onto OpenGL ES 2.0 just yet.  So, I was sniffing around to see if I could hack something together on the CPU instead.
</p></description>
		</item>
		<item>
			<title>araker on "Getting texture pixels without using glReadPixels()"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/28677#post-141308</link>
			<pubDate>Thu, 26 Jan 2012 03:01:03 +0000</pubDate>
			<dc:creator>araker</dc:creator>
			<guid isPermaLink="false">141308@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>We all wish that glReadPixels could be faster. It's not possible to use a pixel array like in CCMutableTexture because the render texture is a color buffer and not a texture, the texture is generated from the color buffer, but the manipulation happens on the buffer. </p>
<p>The buffer itself can't be accessed directly, only glDraw commands can write to it and glReadPixels can retrieve the color values. The texture can only read the buffer, but not manipulate it.</p>
<p>Where exactly do you use that code? glReadPixels reads values from a color buffer, not from a texture. So the texture bind calls aren't necessary. </p>
<p>I usually read the buffer like this..<br />
<pre><code>GLubyte* pixels = malloc(section_width * section_height * sizeof(GLubyte) * 4);
CCRenderTexture* myTexture = ...;
[myTexture begin];
  [someSprite visit];
  glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
[myTexture end];</code></pre>
<p>One other thing that can speed things up a bit is using a smaller render textures and scale the sprite when needed. </p>
<p>Depending on where you need the color values for, there may be ways to avoid using glReadPixels. What exactly do you mean with additional processing?
</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>Dani on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/3#post-140424</link>
			<pubDate>Fri, 20 Jan 2012 15:34:13 +0000</pubDate>
			<dc:creator>Dani</dc:creator>
			<guid isPermaLink="false">140424@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p><strong>@<a href='http://www.cocos2d-iphone.org/forum/profile/69967'>Firstmage</a></strong>: UUfff, I think I can help you faster if I revise your project directly, I need to know the values of some variables. I will send you a PM.</p>
<p><strong>@<a href='http://www.cocos2d-iphone.org/forum/profile/2043'>indy2005</a></strong>: In the link I posted there is an example project from another developer, check it out. It's work in progress, but the idea is to add two new properties (a <em>b2PolygonShape</em> and a <em>b2Transform</em>) to your collidable objects (inside Projectile and Enemy class, for instance) and initialize them, then in your custom <strong><em>isCollisionBetween...</em></strong> function you can call <em>b2CollidePolygons(...)</em> using the Projectile and Enemy polygons and transforms as parameters. I will try to finish and test the result and, if it works as expected, I will post the solution in the other thread.</p>
<p>Also, I don't know much about physics engines, I only need to test collision between polygons, so for Box2D questions I'm not the guy.
</p></description>
		</item>
		<item>
			<title>Firstmage on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/3#post-140416</link>
			<pubDate>Fri, 20 Jan 2012 15:02:40 +0000</pubDate>
			<dc:creator>Firstmage</dc:creator>
			<guid isPermaLink="false">140416@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Thanks to your answer Dani. and I'm sorry to lack of explain.</p>
<p>Here is my method. and plz check this Image. This is what I want to.</p>
<p><a href="http://firstmage.tistory.com/65" rel="nofollow">http://firstmage.tistory.com/65</a></p>
<p>*spriteOne is a small sprite made for collision. spriteTwo either.</p>
<p>-(BOOL) hitJudgeBetweenSpriteOne:(CCSprite *)spriteOne andSpriteTwo:(CCSprite *)spriteTwo</p>
<p>    BOOL isCollision = NO;</p>
<p>    CGRect intersection = CGRectIntersection([spriteOne boundingBox], [spriteTwo boundingBox]);</p>
<p>    // Look for simple bounding box collision<br />
    if (!CGRectIsEmpty(intersection))<br />
    {<br />
        unsigned int x = intersection.origin.x;<br />
        unsigned int y = intersection.origin.y;<br />
        unsigned int w = intersection.size.width;<br />
        unsigned int h = intersection.size.height;<br />
        unsigned int numPixels = w * h;</p>
<p>        [hitJudgeTexture beginWithClear:0 g:0 b:0 a:0];</p>
<p>        glColorMask(1, 0, 0, 1);<br />
        [spriteOne visit];<br />
        glColorMask(0, 1, 0, 1);<br />
        [spriteTwo visit];</p>
<p>        glColorMask(1, 1, 1, 1);</p>
<p>        // Get color values of intersection area</p>
<p>        CCSprite *sprite = [CCSprite spriteWithTexture:hitJudgeTexture.sprite.texture rect:CGRectMake(x, y, w, h)];<br />
        sprite.flipY = YES;<br />
        sprite.anchorPoint = ccp(0,0);<br />
        sprite.position = ccp(0, 0);<br />
        [self addChild:sprite];<br />
        ccColor4B *buffer = malloc(sizeof(ccColor4B) * numPixels * CC_CONTENT_SCALE_FACTOR() * CC_CONTENT_SCALE_FACTOR());</p>
<p>        glReadPixels(0, 0, w * CC_CONTENT_SCALE_FACTOR(), h * CC_CONTENT_SCALE_FACTOR(), GL_RGBA, GL_UNSIGNED_BYTE, buffer);</p>
<p>        [hitJudgeTexture end];</p>
<p>        // Read buffer<br />
        unsigned int step = 1;<br />
        for(unsigned int i=0; i&#60;numPixels; i+=step)<br />
        {<br />
            ccColor4B color = buffer[i];<br />
            if (color.r &#62; 0 &#38;&#38; color.g &#62; 0)<br />
            {<br />
                isCollision = YES;<br />
                break;<br />
            }<br />
        }</p>
<p>        [self removeChild:sprite cleanup:YES];<br />
        free(buffer);</p>
<p>    }</p>
<p>    return isCollision;<br />
}
</p></description>
		</item>
		<item>
			<title>indy2005 on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/3#post-140408</link>
			<pubDate>Fri, 20 Jan 2012 14:34:57 +0000</pubDate>
			<dc:creator>indy2005</dc:creator>
			<guid isPermaLink="false">140408@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>How are you mapping your collision polygons...and is it possible to use multiple polygons and check which part of a body has been touched?
</p></description>
		</item>
		<item>
			<title>Dani on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/3#post-140385</link>
			<pubDate>Fri, 20 Jan 2012 11:28:25 +0000</pubDate>
			<dc:creator>Dani</dc:creator>
			<guid isPermaLink="false">140385@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Please, show the code of your <strong>isCollision...</strong> function, so that we could help you. Are you using <em>CCSpriteBatchNode</em> and calling to a custom <em>visit</em> and <em>draw</em> method? I used this function with cocos2d v2.0b and it worked fine.</p>
<p>Although finally I have moved to Box2D polygonal collision detection. Needs more work because now I have to define the collision polygons for each object of the game, but it's much faster for intensive games, and I recommend it. It's not complicated to use Box2D for just collision detection, some steps in this thread: <a href="http://www.cocos2d-iphone.org/forum/topic/2692">Using Box2D just for collision detection (no physics)</a></p>
<p>Regards.
</p></description>
		</item>
		<item>
			<title>Firstmage on "Pixel Perfect Collision Detection using Color Blending"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/18522/page/3#post-140383</link>
			<pubDate>Fri, 20 Jan 2012 10:51:00 +0000</pubDate>
			<dc:creator>Firstmage</dc:creator>
			<guid isPermaLink="false">140383@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>when I using cocos2d 1.0, It works fine.</p>
<p>but now, I migrate to cocos2d 2.0b and It doesn't work.</p>
<p>I guess something change at 'glReadPixels'...but I can't fix it...</p>
<p>could anyone modify this project for cocos2d 2.0?
</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-134168</link>
			<pubDate>Mon, 19 Dec 2011 11:21:46 +0000</pubDate>
			<dc:creator>calin</dc:creator>
			<guid isPermaLink="false">134168@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>UPDATE:</p>
<p>The semi-transparent <code>CCRenderTexture</code> image on iPhone 4S is caused by the fact that the sprite that's being drawn to the <code>CCRenderTexture</code> has opacity set to 200.<br />
So the problem is actually with the other devices (3GS for sure) because the <code>CCRenderTexture</code> image should be semi-transparent.</p>
<p>Thanks to @<a href='http://www.cocos2d-iphone.org/forum/profile/20192'>Birkemose</a> and @<a href='http://www.cocos2d-iphone.org/forum/profile/11274'>araker</a> for all their help. I think we can close this post since it clearly describes the best method for implementing a magnifying glass.
</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-134129</link>
			<pubDate>Mon, 19 Dec 2011 02:44:58 +0000</pubDate>
			<dc:creator>calin</dc:creator>
			<guid isPermaLink="false">134129@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Looks like there are some problems on iPhone 4S, as described in this post:<br />
<a href="http://www.cocos2d-iphone.org/forum/topic/22223" rel="nofollow">http://www.cocos2d-iphone.org/forum/topic/22223</a></p>
<p>That solution, setting {GL_ONE,GL_ZERO} for the blend function doesn't work for me since I'm using a shader mask.
</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-134124</link>
			<pubDate>Mon, 19 Dec 2011 01:41:01 +0000</pubDate>
			<dc:creator>calin</dc:creator>
			<guid isPermaLink="false">134124@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>There is a strange thing happening though. On a retina display, the resulting image from the <code>CCRenderTexture</code> is semi-transparent. On a non-retina display, this doesn't happen, the image is fully opaque.</p>
<p>I use this to begin rendering to texture:<br />
<code>[renderTexture beginWithClear:0.0 g:0.0 b:0.0 a:1.0];</code><br />
If I change the colors up to 1.0, then the image is a bit more white.</p>
<p>Any idea why is this happening?
</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-134120</link>
			<pubDate>Mon, 19 Dec 2011 01:10:48 +0000</pubDate>
			<dc:creator>calin</dc:creator>
			<guid isPermaLink="false">134120@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Thanks @<a href='http://www.cocos2d-iphone.org/forum/profile/11274'>araker</a>, I have it sorted out now exactly as @<a href='http://www.cocos2d-iphone.org/forum/profile/20192'>Birkemose</a> said. Works perfect (60 fps) on an iPhone 3GS.</p>
<p>If anyone is interested on this simple class, I can create a test project and share it here.</p>
<p>Thank you all.
</p></description>
		</item>
		<item>
			<title>araker on "[BEST PRACTICE] Copying pixels in OpenGL ES and render at another location"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/25726#post-134118</link>
			<pubDate>Mon, 19 Dec 2011 01:06:28 +0000</pubDate>
			<dc:creator>araker</dc:creator>
			<guid isPermaLink="false">134118@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I think this is the way to go. glCopyPixels doesn't exist in OpenGL ES 1.1 and glCopyTexSubImage2D works on copying data from a texture, not from the screen. To make glCopyTexSubImage2D work, you would first have to draw the screen to a texture (a render texture) and then copy the data. So glCopyTexSubImage2D won't be any faster. </p>
<p>Another reason to stick to the render texture is that it's more convenient if you want to do some processing afterwards, like scaling or applying a mask. </p>
<p>Changing the position is something you can't avoid.
</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-134101</link>
			<pubDate>Sun, 18 Dec 2011 20:29:43 +0000</pubDate>
			<dc:creator>calin</dc:creator>
			<guid isPermaLink="false">134101@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/20192'>Birkemose</a></p>
<p>Your solution works great! I have a small question though.<br />
As I render everything for the second time in my <code>CCRenderTexture</code> object of 100x100, it obviously starts from (0,0). To change this (say render the middle part of the screen), I have to shift the layer I'm drawing (change it's position by an offset) before calling <code>visit</code> and then shift it back, or is there another solution for this?</p>
<p>Thanks!
</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-134059</link>
			<pubDate>Sun, 18 Dec 2011 16:14:20 +0000</pubDate>
			<dc:creator>calin</dc:creator>
			<guid isPermaLink="false">134059@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Thanks again Birkemose. I'll go for the rendering to the <code>CCRendertexture</code> as you described.<br />
I'll keep researching this when I'll have time and if I find anything different than what you told me I'll let you know.</p>
<p>Maybe @riq or @<a href='http://www.cocos2d-iphone.org/forum/profile/11274'>araker</a> can also post an advice regarding this.</p>
<p>Thanks!
</p></description>
		</item>
		<item>
			<title>Birkemose on "[BEST PRACTICE] Copying pixels in OpenGL ES and render at another location"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/25726#post-134056</link>
			<pubDate>Sun, 18 Dec 2011 16:06:11 +0000</pubDate>
			<dc:creator>Birkemose</dc:creator>
			<guid isPermaLink="false">134056@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>To get a definite answer, you would have to get it from one of the top-dogs like @<a href='http://www.cocos2d-iphone.org/forum/profile/11274'>araker</a> or @riq, but my take on glCopyPixels is, that it is just as bad as glReadPixels.<br />
The main problem is, that unless you really really knows what you are doing, reading from OpenGL buffers can potentially stall the render pipeline, and completely kill performance. So it is not really a matter of how many pixels you want to copy, it is a matter of switching OpenGL into reading mode at the wrong time.<br />
I know it can be done, and I am pretty sure I once read an explanation from @<a href='http://www.cocos2d-iphone.org/forum/profile/11274'>araker</a> on how to do it, but it is beyond me to try it. My own take at implementing a simple glReadPixel, grinded graphics to a halt. </p>
<p>Rendering through a CCRendertexture isn't as costly as some might think. No matter how clever you are, you easily get an overdraw of 2-3 on a simple scene, so changing that to 3-4 ( because a CCRenderTexture will guaranteed only render any pixel on the screen only once ), really isn't the showstopper. In the water ripple example I posted a while back, I prerender the entire screen to a CCRenderTexture ( buttons, animations, the whole shebang ), and then add the ripples on top of that.<br />
It runs okay ( fps 15-20+ ) on a 3G.
</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-134054</link>
			<pubDate>Sun, 18 Dec 2011 15:50:37 +0000</pubDate>
			<dc:creator>calin</dc:creator>
			<guid isPermaLink="false">134054@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Thanks Birkemose. So rendering the screen twice is faster than using <code>glReadPixels</code> for an area of 128x128 max and render those in a <code>CCRenderTexture</code>?<br />
What about <code>glCopyPixels</code>?
</p></description>
		</item>
		<item>
			<title>Birkemose on "[BEST PRACTICE] Copying pixels in OpenGL ES and render at another location"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/25726#post-134034</link>
			<pubDate>Sun, 18 Dec 2011 13:14:43 +0000</pubDate>
			<dc:creator>Birkemose</dc:creator>
			<guid isPermaLink="false">134034@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Stay away from glReadPixels if you can. Used unwisely, it can complete kill performance.<br />
One approach to making a magnifying class, is rendering your actual layer to a CCRenderTexture first. You can then render the rendertexture to screen, and you can grab any portion of it in a sprite, stretch it, mask it, etc, and render it on top.<br />
This will of course force you to render the screen twice, but even on old devices, this rarely is a problem.
</p></description>
		</item>

	</channel>
</rss>

