所以,我試圖找到屏幕上任何像素的位置,這是特定的顏色。從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