我目前正在使用基於Apple的GLPaint示例的庫,用於在Open GL中的屏幕上繪圖。目前,當畫布保存並恢復會話時,繪製線條(可以看到進度),並且如果有很多點要渲染,則需要相當多的時間。有什麼辦法可以讓它平行或更快地渲染?OpenGL GLPaint線程渲染
這是我使用的繪圖代碼:
CGPoint start = step.start;
CGPoint end = step.end;
// Convert touch point from UIView referential to OpenGL one (upside-down flip)
CGRect bounds = [self bounds];
start.y = bounds.size.height - start.y;
end.y = bounds.size.height - end.y;
static GLfloat* vertexBuffer = NULL;
static NSUInteger vertexMax = 64;
NSUInteger vertexCount = 0,
count,
i;
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
// Convert locations from Points to Pixels
CGFloat scale = self.contentScaleFactor;
start.x *= scale;
start.y *= scale;
end.x *= scale;
end.y *= scale;
// Allocate vertex array buffer
if(vertexBuffer == NULL)
vertexBuffer = malloc(vertexMax * 2 * sizeof(GLfloat));
// Add points to the buffer so there are drawing points every X pixels
count = MAX(ceilf(sqrtf((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y))/kBrushPixelStep), 1);
for(i = 0; i < count; ++i) {
if(vertexCount == vertexMax) {
vertexMax = 2 * vertexMax;
vertexBuffer = realloc(vertexBuffer, vertexMax * 2 * sizeof(GLfloat));
}
vertexBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i/(GLfloat)count);
vertexBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i/(GLfloat)count);
vertexCount += 1;
}
// Render the vertex array
glVertexPointer(2, GL_FLOAT, 0, vertexBuffer);
glDrawArrays(GL_POINTS, 0, (int)vertexCount);
// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
我們在談論多少分?我解釋代碼的方式,它需要兩個屏幕位置(可能來自觸摸輸入),並在每個「kBrushPixelStep」像素之間繪製點。那應該不是那麼多點吧?或者你是否調用了我們重複看到的代碼,使用不同的'start'和'end'值? –
@ReetoKoradi代碼被重複調用,一個包含多個步驟(每個都有起始和結束座標)的數組調用每個步驟的函數 –