2012-09-24 138 views
1

我是opengl的新手,尝试在OpenGL ES 2.0中获得类似Apple的GLPaint的行为。下面是我的了:OpenGL ES中的绘制点2.0

enter image description here

正如你所看到的,手指绘画是不连续的,但落空。我试图改变我的纹理,试图使用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]; 

} 

回答

3

最后我发现, OPTI上是从设置,如以下代码错过示出:

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

通过这种kEAGLDrawablePropertyRetainedBacking标志设置为YES时,OpenGL将保留帧,而不是清洁的每一帧之间的图。

下面是一个固定的版本:

enter image description here

+1

在应该发生行的开始/结束加点? –