【发布时间】:2014-04-29 23:46:36
【问题描述】:
我目前正在使用一个基于 Apple 的 GLPaint 示例的库,用于在 Open GL 中的屏幕上绘图。目前,每当画布保存和恢复会话时,都会绘制线条(可以看到进度),如果要渲染很多点,则需要相当长的时间。有没有办法让它并行或更快地渲染?
这是我正在使用的绘图代码:
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的值不同? -
@RetoKoradi 代码被重复调用,一个包含许多步骤(每个都有开始和结束坐标)的数组调用每个步骤的函数
标签: ios objective-c opengl-es opengl-es-2.0