在cocos2d/IOS中获取背景颜色
问题描述:
在我的项目中,我想让用户触摸屏幕&当他移动时会画出一条线。在cocos2d/IOS中获取背景颜色
我也想确保用户不会与他之前绘制的任何现有线相交(包括同一线本身)。
我搜索周围的线交集算法或函数,但它们太复杂,性能也很不好。所以,我想到另一种方式来做到这一点。通过设置背景和线条的颜色不同,如果我可以读取当前触点的颜色,则可以将其与线条颜色进行比较,并确定是否发生任何交点。
我尝试过使用glReadPixel方法,但它对所有未设置为背景或线条的接触点返回绿色。我的背景是默认颜色(黑色),线条默认为白色。所有线都绘制在同一图层中。我没有把背景作为一个单独的层。只使用默认值。
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CCLOG(@"touch moved");
UITouch* touch = [touches anyObject];
CGPoint currentTouchPoint = [touch locationInView:[touch view]];
CGPoint lastTouchPoint = [touch previousLocationInView:[touch view]];
currentTouchPoint = [[CCDirector sharedDirector] convertToGL:currentTouchPoint];
lastTouchPoint = [[CCDirector sharedDirector] convertToGL:lastTouchPoint];
CCRenderTexture* renderTexture = [CCRenderTexture renderTextureWithWidth:1 height:1];
[renderTexture begin];
[self visit];
Byte pixelColors[4];
glReadPixels(currentTouchPoint.x, currentTouchPoint.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixelColors[0]);
[renderTexture end];
CCLOG(@"pixel color: %u, %u, %u", pixelColors[0], pixelColors[1], pixelColors[2]);
CCLOG(@"last a=%.0f, b=%.0f", lastTouchPoint.x, lastTouchPoint.y);
CCLOG(@"Current x=%.0f, y=%.0f",currentTouchPoint.x, currentTouchPoint.y);
[touchPoints addObject:NSStringFromCGPoint(currentTouchPoint)];
[touchPoints addObject:NSStringFromCGPoint(lastTouchPoint)];
}
-(void)draw{
CGPoint start;
CGPoint end;
glLineWidth(4.0f);
for (int i=0; i<[touchPoints count]; i=i+2) {
start = CGPointFromString([touchPoints objectAtIndex:i]);
end = CGPointFromString([touchPoints objectAtIndex:i+1]);
ccDrawLine(start, end);
}
}
答
您只能使用无论是在平局或访问方法OpenGL的方法(glReadPixels这里)。这很可能是你为什么总是变绿的原因。
在渲染纹理的开始/结束方法中,您只能访问渲染纹理,而不能访问帧缓冲区。
你的意思是我应该在我的绘制方法中移动glReadPixel调用?这将触发读取每帧画面上的像素。对性能不坏?截至目前,我在绘制方法下绘制线条。在哪里以及如何放置glReadPixel调用?谢谢你的帮助 !!! – ganesh 2013-03-17 06:36:56
谢谢,它是在我将glReadPixel移入draw方法后工作的。 – ganesh 2013-03-19 01:54:56