2012-07-14 48 views
1

我需要在一個框架上繪製小圓圈,並使它們在幾幀後消失(淡出)。我如何通過OpenGL ES實現這一目標?如何在openGL中處理幾個圈的淡出?

這是我到目前爲止,該代碼繪製圓框:

[EAGLContext setCurrentContext:context]; 
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); 
    glViewport(0, 0, backingWidth, backingHeight); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    glOrthof(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f); 
    glMatrixMode(GL_MODELVIEW); 

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
    glClear(GL_COLOR_BUFFER_BIT); 

    GLfloat vertices[720]; 

    for (int i = 0; i < 720; i += 2) { 
     vertices[i] = (cos(DEGREES_TO_RADIANS(i)) * 1); 
     vertices[i+1] = (sin(DEGREES_TO_RADIANS(i)) * 1); 
    } 

    glVertexPointer(2, GL_FLOAT, 0, vertices); 
    glEnableClientState(GL_VERTEX_ARRAY); 

    glColor4f(1.0f, 1.0f, 0.0f, 1.0f); 

    glDrawArrays(GL_TRIANGLE_FAN, 0, 360); 

    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); 
    [context presentRenderbuffer:GL_RENDERBUFFER_OES]; 

我不明白的是如何保持同一個圓的軌跡,並在連續消除它幀。 謝謝

回答

1

您將需要重新渲染每個幀上的所有對象(圓圈)。所以你需要保持一個數據結構來保存關於圓圈的信息(可能他們的中心,半徑和不透明度就足夠了)。事情是這樣的:

typedef struct Circle { 
    NSPoint center; 
    float radius; 
    float opacity; 
} Circle; 

Circle allCircles [ kMaxNumCircles ]; 

然後,當你去吸引他們,你迭代圈陣列上,更新他們的不透明度,然後畫他們:

for (int i = 0; i < kMaxNumCircles; i++) 
{ 
    allCircles [ i ].opacity -= someDelta; 
    drawCircle (allCircles [ i ]); 
} 

drawCircle()功能可能只是什麼你上面寫的:

void drawCircle(Circle c) 
{ 
    GLfloat vertices[720]; 

    for (int i = 0; i < 720; i += 2) { 
     vertices[i] = (cos(DEGREES_TO_RADIANS(i)) * 1) + c.center.x; 
     vertices[i+1] = (sin(DEGREES_TO_RADIANS(i)) * 1) + c.center.y; 
    } 

    glVertexPointer(2, GL_FLOAT, 0, vertices); 
    glEnableClientState(GL_VERTEX_ARRAY); 

    glColor4f(1.0f, 1.0f, 0.0f, c.opacity); 

    glDrawArrays(GL_TRIANGLE_FAN, 0, 360); 

}