2012-09-24 177 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

在應該發生行的開始/結束加點? –