2012-06-15 57 views
0

使用cocos2d的繪製較厚的圓圈:鐵血ccDrawCircle

glLineWidth(20); 
ccDrawCircle(self.ripplePosition, _radius, 0, 50, NO); 

但是,這是顯示的內容(注意它看起來像是來自4個不同的部分創建):

http://i.stack.imgur.com/jYW4s.png

我嘗試將段數增加到更大的值,但結果是相同的。

這是Cocos2D中的錯誤嗎?關於如何實現「完美」圈子的任何想法?

這裏是ccDrawCircle從cocos2d的2.0RC2實現:

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); 
} 
+0

我想知道,也許GL_TRIANGLE_STRIP會是更好的選擇嗎? –

+0

這是一個常見的問題,與繪製原始和膨脹線寬不合理的厚度(即一切高於4像素)。繪圖原語僅用於調試目的。另一種方法是使用圓形圖像作爲精靈並適當縮放。 – LearnCocos2D

+0

我嘗試了一個圖像,但性能下降(我的應用程序正在繪製許多半透明的圓圈)。我認爲唯一可行的解​​決方案是使用三角形條 - 基本上與ccDrawCircle的方法相同,但在它們每個上創建兩個圓和交替點來構建條。 –

回答

1

我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); 
}