1

我是 opengl 的新手,并试图在 OpenGL ES 2.0 中获得类似 Apple 的 GLPaint 的行为。这是我得到的:

在此处输入图像描述

如您所见,手指绘制不是连续的,而是虚线的。我试图改变我的纹理,尝试使用 GL_STREAM_DRAW 选项,但它似乎不起作用。谁能给我一些线索来缩小问题范围或提供一些解决方案的提示?谢谢。

以下是着色器和相关代码:

顶点着色器:

attribute vec4 Position; 
attribute vec4 SourceColor; 
uniform mat4 Projection;
varying vec4 DestinationColor; 

void main(void) { 
    DestinationColor = SourceColor; 
    gl_Position = Position * Projection;
    gl_PointSize = 50.0;
}

片段着色器:

uniform sampler2D texture;

void main ( ){
    gl_FragColor = texture2D(texture, gl_PointCoord);
}

渲染过程:

- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end withContainer:    (DVDrawingElement*)container
{
static Vertex*      vertexBuffer = NULL;
static NSUInteger   vertexMax = 64;
NSUInteger          vertexCount = 0,
    count,
    i;

// 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(Vertex));

// 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);

    NSLog(@"start:%@ --- end: %@, count=%d", [NSValue valueWithCGPoint:start], [NSValue valueWithCGPoint:end], count);

for(i = 0; i < count; ++i) {
    if(vertexCount == vertexMax) {
        vertexMax = 2 * vertexMax;
        vertexBuffer = realloc(vertexBuffer, vertexMax * 2 * sizeof(Vertex));
    }
    vertexBuffer[i].Position[0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count);
    vertexBuffer[i].Position[1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count);
    vertexBuffer[i].Position[2] = 0.0f;        

    // color is RGBA.
    vertexBuffer[i].Color[0] = r;
    vertexBuffer[i].Color[1] = g;
    vertexBuffer[i].Color[2] = b;
    vertexBuffer[i].Color[3] = a;

    vertexCount += 1;
}

GLuint vbo;
glGenBuffers(1, &vbo);

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertexCount * sizeof(Vertex), vertexBuffer, GL_STATIC_DRAW);

glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE,
                      sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE,
                      sizeof(Vertex), (GLvoid*) (sizeof(float) *3));

glUniform1i(_textureSlot, 0);
glActiveTexture(GL_TEXTURE0);

glDrawArrays(GL_POINTS, 0, vertexCount);

[_context presentRenderbuffer:GL_RENDERBUFFER];

}
4

1 回答 1

3

最后我发现设置中遗漏了一个选项,如下代码所示:

- (void)setupLayer {
    _eaglLayer = (CAEAGLLayer*) self.layer;
    _eaglLayer.opaque = YES;
    _eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:**[NSNumber numberWithBool:YES
                                                                            ],     kEAGLDrawablePropertyRetainedBacking,**
                                     kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
                                     nil];
}

通过将此kEAGLDrawablePropertyRetainedBacking标志设置为 YES,opengl 将保留帧之间的绘图,而不是清理每一帧。

以下是固定版本:

在此处输入图像描述

于 2012-09-24T13:08:01.940 回答