所以,我试图找到屏幕上任何像素的位置,这是特定的颜色。从CCRenderTexture获取像素的颜色
下面的代码有效,但速度非常慢,因为我必须迭代每个像素坐标,并且有很多。
有没有什么办法可以改善下面的代码以提高效率?
// Detect the position of all red points in the sprite
UInt8 data[4];
CCRenderTexture* renderTexture = [[CCRenderTexture alloc] initWithWidth: mySprite.boundingBox.size.width * CC_CONTENT_SCALE_FACTOR()
height: mySprite.boundingBox.size.height * CC_CONTENT_SCALE_FACTOR()
pixelFormat:kCCTexture2DPixelFormat_RGBA8888];
[renderTexture begin];
[mySprite draw];
for (int x = 0; x < 960; x++)
{
for (int y = 0; y < 640; y++)
{
ccColor4B *buffer = malloc(sizeof(ccColor4B));
glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
ccColor4B color = buffer[0];
if (color.r == 133 && color.g == 215 && color.b == 44)
{
NSLog(@"Found the red point at x: %d y: %d", x, y);
}
}
}
[renderTexture end];
[renderTexture release];
从帧缓冲区读取像素很慢,特别是如果你逐像素地进行读取。取得渲染纹理的纹理内存,并在没有glReadPixel的情况下进行迭代,应该快得多! – LearnCocos2D
@ LearnCocos2D嘿,伙计!我会怎么做呢?你有没有可以分享的例子?然后我可以将你的回答标记为正确:) – Sneaksta
我的错误是,CCTexture2D不保留内存缓冲区,无论如何它都在GL内存中。 – LearnCocos2D