【问题标题】:cocos2d and glReadPixels don't work?cocos2d 和 glReadPixels 不起作用?
【发布时间】:2013-01-25 20:21:50
【问题描述】:

我对 cocos2d 和 glReadPixels 有疑问,因为它们不能正常工作。 我在网上找到了像素完美碰撞的代码,并为我的应用程序进行了修改,但动画或更快速的动画不起作用。

这是代码:

-(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're not checking for pixel perfect collisions, return true
    if (!pp) {return YES;}

    // Get intersection info
    unsigned int x = intersection.origin.x;
    unsigned int y = intersection.origin.y;
    unsigned int w = intersection.size.width;
    unsigned int h = intersection.size.height;
    unsigned int numPixels = w * h;
    //NSLog(@"\nintersection = (%u,%u,%u,%u), area = %u",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);
    [spr1 visit];
    glColorMask(0, 1, 0, 1);
    [spr2 visit];
    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);

    /******* All this is for testing purposes *********/


    // Draw the intersection rectangle in BLUE (testing purposes)


    /**************************************************/

    [_rt end];

    // Read buffer
    unsigned int step = 1;
    for(unsigned int q=0; q<1; q+=step)
    {
        ccColor4B color = buffer[q];

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

    // Free buffer memory
    free(buffer);
}

return isCollision;

}

问题出在哪里?我试过了,但没有。

非常感谢。 问候。

【问题讨论】:

    标签: cocos2d-iphone


    【解决方案1】:

    如果您使用的是 iOS6,请查看this post 以获得解决方案:

    CAEAGLLayer *eaglLayer = (CAEAGLLayer *) self.layer;
    eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithBool:YES],
                                        kEAGLDrawablePropertyRetainedBacking,
                                        kEAGLColorFormatRGBA8,        kEAGLDrawablePropertyColorFormat,
                                        nil];
    

    解释是 iOS6 修复了 iOS Open GL 实现中的一些错误,以便 GL 缓冲区在每次呈现到屏幕时都(正确地)清除。 Here what Apple writes about this:

    重要提示:您必须在调用 EAGLContext/-presentRenderbuffer: 之前调用 glReadPixels:以获得定义的结果,除非您使用保留的后台缓冲区。

    正确的解决方案是在渲染缓冲区呈现到屏幕之前调用glReadPixels。之后就失效了。

    上述解决方案只是使图像具有“粘性”的一种解决方法。

    请注意,它可能会影响您的应用渲染性能。关键是如果你使用的是cocos2d,在渲染缓冲区出现之前你不能轻易调用glReadPixels。

    希望对你有帮助。

    【讨论】:

    • 但我不明白这个问题......你可以给我发一个代码示例吗?因为如果我在 [_rt end] 之前和 glReadPixels() 工作之后调用,但正常碰撞并且没有像素完美碰撞。为什么? :(
    • 也许这会有所帮助:stackoverflow.com/questions/11863416/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多