我是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;
}
Fragment Shader:
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];
}
答案 0 :(得分:3)
最后我发现设置错过了一个选项,如下面的代码所示:
- (void)setupLayer {
_eaglLayer = (CAEAGLLayer*) self.layer;
_eaglLayer.opaque = YES;
_eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:**[NSNumber numberWithBool:YES
], kEAGLDrawablePropertyRetainedBacking,**
kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
nil];
}
通过将此 kEAGLDrawablePropertyRetainedBacking 标志设置为YES,opengl将在帧之间保留绘图,而不是每帧清除。
以下是固定版本: