[英]How to draw a solid circle with cocos2d for iPhone
是否可以用cocos2d繪制一個實心圓圈? 可以使用drawCircle()函數完成輪廓圓,但有沒有辦法以某種顏色填充它? 也許通過使用純OpenGL?
在DrawingPrimitives.m中,在drawCricle中更改它:
glDrawArrays(GL_LINE_STRIP, 0, segs+additionalSegment);
至:
glDrawArrays(GL_TRIANGLE_FAN, 0, segs+additionalSegment);
你可以在這里閱讀更多關於opengl原語的內容: http : //www.informit.com/articles/article.aspx? p = 461848
這里有一個ccDrawCircle()的略微修改,可以讓你繪制圓的任何切片。 將其粘貼在CCDrawingPrimitives.m中,並將方法標題信息添加到CCDrawingPrimitives.h:
參數: a
:以弧度表示的起始角度, d
:delta或以弧度表示的角度變化(對於完整的圓圈,使用2*M_PI
)
更改被評論
void ccDrawFilledCircle( CGPoint center, float r, float a, float d, NSUInteger totalSegs)
{
int additionalSegment = 2;
const float coef = 2.0f * (float)M_PI/totalSegs;
NSUInteger segs = d / coef;
segs++; //Rather draw over than not draw enough
if (d == 0) return;
GLfloat *vertices = calloc( sizeof(GLfloat)*2*(segs+2), 1);
if( ! vertices )
return;
for(NSUInteger i=0;i<=segs;i++)
{
float rads = i*coef;
GLfloat j = r * cosf(rads + a) + center.x;
GLfloat k = r * sinf(rads + a) + center.y;
//Leave first 2 spots for origin
vertices[2+ i*2] = j * CC_CONTENT_SCALE_FACTOR();
vertices[2+ i*2+1] =k * CC_CONTENT_SCALE_FACTOR();
}
//Put origin vertices into first 2 spots
vertices[0] = center.x * CC_CONTENT_SCALE_FACTOR();
vertices[1] = center.y * CC_CONTENT_SCALE_FACTOR();
// Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
// Needed states: GL_VERTEX_ARRAY,
// Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vertices);
//Change to fan
glDrawArrays(GL_TRIANGLE_FAN, 0, segs+additionalSegment);
// restore default state
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
free( vertices );
}
調查:
這些將允許您填充圓圈而無需OpenGL
我也很想知道這一點,但還沒有真正做到這一點。 我嘗試使用Grouchal上面提到的CGContext的東西,但我不能讓它在屏幕上繪制任何東西。 這就是我嘗試過的:
-(void) draw
{
[self makestuff:UIGraphicsGetCurrentContext()];
}
-(void)makestuff:(CGContextRef)context
{
// Drawing lines with a white stroke color
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(context, 2.0);
// Draw a single line from left to right
CGContextMoveToPoint(context, 10.0, 30.0);
CGContextAddLineToPoint(context, 310.0, 30.0);
CGContextStrokePath(context);
// Draw a connected sequence of line segments
CGPoint addLines[] =
{
CGPointMake(10.0, 90.0),
CGPointMake(70.0, 60.0),
CGPointMake(130.0, 90.0),
CGPointMake(190.0, 60.0),
CGPointMake(250.0, 90.0),
CGPointMake(310.0, 60.0),
};
// Bulk call to add lines to the current path.
// Equivalent to MoveToPoint(points[0]); for(i=1; i<count; ++i) AddLineToPoint(points[i]);
CGContextAddLines(context, addLines, sizeof(addLines)/sizeof(addLines[0]));
CGContextStrokePath(context);
// Draw a series of line segments. Each pair of points is a segment
CGPoint strokeSegments[] =
{
CGPointMake(10.0, 150.0),
CGPointMake(70.0, 120.0),
CGPointMake(130.0, 150.0),
CGPointMake(190.0, 120.0),
CGPointMake(250.0, 150.0),
CGPointMake(310.0, 120.0),
};
// Bulk call to stroke a sequence of line segments.
// Equivalent to for(i=0; i<count; i+=2) { MoveToPoint(point[i]); AddLineToPoint(point[i+1]); StrokePath(); }
CGContextStrokeLineSegments(context, strokeSegments, sizeof(strokeSegments)/sizeof(strokeSegments[0]));
}
這些方法在cocos節點類中定義,而我從代碼示例中借用的makestuff方法...
注意:我正在嘗試繪制任何形狀或路徑並填充它。 我知道上面的代碼只畫線,但我不想繼續,直到我開始工作。
編輯:這可能是一個糟糕的解決方案,但我認為這至少可行 。
每個CocosNode都有一個紋理(Texture2D *)。 Texture2D類可以從UIImage初始化。 UIImage可以從CGImageRef初始化。 可以為quartz lib創建CGImageRef上下文。
那么,你要做的是:
問題是,這是否足夠快。 我更喜歡你是否可以直接從CocosNode獲取CGImageRef而不是通過所有這些步驟進入它,但我還沒有找到辦法做到這一點(而且我在這方面有點像菜鳥)所以實際上很難到達某個地方。
cocos2d CCDrawingPrimitives
有一個名為ccDrawSolidCircle(CGPoint center, float r, NSUInteger segs)
的新函數ccDrawSolidCircle(CGPoint center, float r, NSUInteger segs)
。 對於那些現在看這個的人來說,使用這個方法,然后你不必亂用cocos2d代碼,只需導入CCDrawingPrimitives.h
我在下面用這種方式。
glLineWidth(2);
for(int i=0;i<50;i++){
ccDrawCircle( ccp(s.width/2, s.height/2), i,0, 50, NO);
}
我用for循環制作了多個圓圈,看起來像一個圓圈。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.