Ios 锯齿形圆
使用Cocos2d绘制更厚的圆:Ios 锯齿形圆,ios,cocos2d-iphone,Ios,Cocos2d Iphone,使用Cocos2d绘制更厚的圆: glLineWidth(20); ccDrawCircle(self.ripplePosition, _radius, 0, 50, NO); 但这就是显示的内容(注意它看起来像是由4个不同的段创建的): 我尝试将分段数增加到更大的值,但结果是一样的 这是Cocos2D中的一个bug吗?有没有关于如何实现“完美”循环的想法 以下是cocos2d 2.0rc2中ccDrawCircle的实现: void ccDrawCircle( CGPoint center
glLineWidth(20);
ccDrawCircle(self.ripplePosition, _radius, 0, 50, NO);
但这就是显示的内容(注意它看起来像是由4个不同的段创建的):
我尝试将分段数增加到更大的值,但结果是一样的
这是Cocos2D中的一个bug吗?有没有关于如何实现“完美”循环的想法
以下是cocos2d 2.0rc2中ccDrawCircle的实现:
void ccDrawCircle( CGPoint center, float r, float a, NSUInteger segs, BOOL drawLineToCenter)
{
lazy_init();
int additionalSegment = 1;
if (drawLineToCenter)
additionalSegment++;
const float coef = 2.0f * (float)M_PI/segs;
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;
vertices[i*2] = j;
vertices[i*2+1] = k;
}
vertices[(segs+1)*2] = center.x;
vertices[(segs+1)*2+1] = center.y;
[shader_ use];
[shader_ setUniformForModelViewProjectionMatrix];
[shader_ setUniformLocation:colorLocation_ with4fv:(GLfloat*) &color_.r count:1];
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segs+additionalSegment);
free( vertices );
CC_INCREMENT_GL_DRAWS(1);
}
void ccDrawCircle(CGPoint中心、浮点r、浮点a、整数分段、布尔drawLineToCenter)
{
lazy_init();
int additionalSegment=1;
如果(drawLineToCenter)
附加段++;
常量浮点系数=2.0f*(浮点)M_PI/segs;
GLfloat*顶点=calloc(sizeof(GLfloat)*2*(segs+2),1);
如果(!顶点)
返回;
对于(i=0;i我使用了一个稍微修改过的ccDrawCircle版本,它工作得非常好(比使用和调整精灵的大小要好得多):
void ccDrawDonut(CGPoint中心、浮点r1、浮点r2、整数分段)
{
lazy_init();
常量浮点系数=2.0f*(浮点)M_PI/segs;
GLfloat*顶点=calloc(sizeof(GLfloat)*4*segs+4,1);
如果(!顶点)
返回;
对于(整数i=0;我在想,也许GL_三角形_带是更好的选择?这是绘制基本体和将线宽膨胀到不合理的厚度(即超过4个像素)的常见问题。绘图原语仅用于调试目的。另一种方法是使用圆形图像作为精灵并适当缩放。我尝试了一个图像,但性能下降(我的应用程序正在绘制许多半透明的圆形).我认为唯一可行的解决方案是使用三角形条带-基本上与ccDrawCircle的方法相同,但在每个条带上都有两个圆和交替点来构建条带。
void ccDrawDonut( CGPoint center, float r1, float r2, NSUInteger segs)
{
lazy_init();
const float coef = 2.0f * (float)M_PI/segs;
GLfloat *vertices = calloc( sizeof(GLfloat)*4*segs+4, 1);
if( ! vertices )
return;
for(NSUInteger i = 0;i <= segs; i++) {
float rads = i*coef;
GLfloat j1 = r1 * cosf(rads) + center.x;
GLfloat k1 = r1 * sinf(rads) + center.y;
vertices[i*4] = j1;
vertices[i*4+1] = k1;
rads+= coef/2;
GLfloat j2 = r2 * cosf(rads) + center.x;
GLfloat k2 = r2 * sinf(rads) + center.y;
vertices[i*4+2] = j2;
vertices[i*4+3] = k2;
}
[shader_ use];
[shader_ setUniformForModelViewProjectionMatrix];
[shader_ setUniformLocation:colorLocation_ with4fv:(GLfloat*) &color_.r count:1];
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei) 2*segs+2);
free( vertices );
CC_INCREMENT_GL_DRAWS(1);
}