如何在iPhone上使用cocos2d绘制实心圆
可以用cocos2d绘制一个填充圆吗? 可以使用drawCircle()函数绘制轮廓圆,但有没有办法将其填充为特定颜色?也许通过使用纯OpenGL?查看:如何在iPhone上使用cocos2d绘制实心圆,iphone,opengl-es,cocos2d-iphone,geometry,Iphone,Opengl Es,Cocos2d Iphone,Geometry,可以用cocos2d绘制一个填充圆吗? 可以使用drawCircle()函数绘制轮廓圆,但有没有办法将其填充为特定颜色?也许通过使用纯OpenGL?查看: CGContextAddArc CGContextFillPath 这些将允许您在不需要OpenGL的情况下填充一个圆我也想知道这一点,但还没有真正做到这一点。我试着使用Grouchal在上面提到的CGContext工具,但我无法让它在屏幕上画出任何东西。这就是我尝试过的: -(void) draw { [self makestu
- CGContextAddArc
- CGContextFillPath
这些将允许您在不需要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]));
}
-(无效)绘图
{
[self-makestuff:UIGraphicsGetCurrentContext()];
}
-(void)makestuff:(CGContextRef)上下文
{
//用白色笔划绘制线条
CGContextSetRGBStrokeColor(上下文,1.0,1.0,1.0,1.0);
//以2.0笔划宽度绘制它们,使它们更为可见。
CGContextSetLineWidth(上下文,2.0);
//从左到右画一条线
CGContextMoveToPoint(上下文,10.0,30.0);
CGContextAddLineToPoint(上下文,310.0,30.0);
CGContextStrokePath(上下文);
//绘制连接的线段序列
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),
};
//向当前路径添加行的批量调用。
//相当于MoveToPoint(点[0]);对于DrawingPrimitives.m中的(i=1;i),请在drawCricle中更改此值:
glDrawArrays(GL_LINE_STRIP, 0, segs+additionalSegment);
致:
您可以在此处阅读有关opengl原语的更多信息:
这里是对ccDrawCircle()的一个轻微修改,允许您绘制圆的任何部分。将其粘贴到CCDrawingPrimitives.m中,并将方法标题信息添加到CCDrawingPrimitives.h中:
参数:a
:以弧度表示的起始角度,d
:以弧度表示的角度增量或变化(使用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 );
}
void ccdrawFilled圆(CGPoint center,float r,float a,float d,integer totalSegs)
{
int附加段=2;
常量浮点数系数=2.0f*(浮点数)M_PI/totalSegs;
nsus整数segs=d/coef;
segs++;//与其画得不够,不如画得太多
如果(d==0)返回;
GLfloat*顶点=calloc(sizeof(GLfloat)*2*(segs+2),1);
如果(!顶点)
返回;
对于(i=0;i的整数,我在下面使用了这种方式
glLineWidth(2);
for(int i=0;i<50;i++){
ccDrawCircle( ccp(s.width/2, s.height/2), i,0, 50, NO);
}
glLineWidth(2);
对于(int i=0;icocos2dCCDrawingPrimitives
中有一个新函数称为ccDrawSolidCircle(CGPoint center,float r,NSU Integer segs)
。对于那些现在正在研究这个问题的人,使用这个方法,那么你就不必弄乱CoCoCoS2D代码了,只需导入CCDrawingPrimitives.h
glLineWidth(2);
for(int i=0;i<50;i++){
ccDrawCircle( ccp(s.width/2, s.height/2), i,0, 50, NO);
}