John Lingburg
John Lingburg

Reputation: 276

Draw point in OpenGL ES 2.0

I'm new to opengl and tried to get the behavior like Apple's GLPaint in OpenGL ES 2.0. Here's what I got:

enter image description here

As you can see, the finger drawing is not continuous but dashed. I tried to change my texture, tried to use GL_STREAM_DRAW option but it seems not work. Can anyone give me some clue to narrow down the problem scope or some hint to a solution? Thanks.

Below are shaders and related code:

Vertex Shader:

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

Fragment Shader:

uniform sampler2D texture;

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

render procedure:

- (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];

}

Upvotes: 1

Views: 5076

Answers (1)

John Lingburg
John Lingburg

Reputation: 276

Finally I found that an option is missed from setting, as following code shows:

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

By setting this kEAGLDrawablePropertyRetainedBacking flag to YES, opengl will retain drawings between frames instead of cleaning every frame.

Following is a fixed version:

enter image description here

Upvotes: 3

Related Questions