我的应用程序使用OpenGL ES来渲染用户绘制签名的屏幕。我的代码已经工作了3年多了,但是在升级到Xcode9之后,我绘制的线条出现了奇怪的颜色变化(无论是在模拟器上还是在设备上)。这些线条过去有纯红色、绿色或蓝色,现在它们有一条灰色/黑色混合的线条。
我对OpenGL了解不多,我的代码是从示例应用程序和教程中拼凑而成的。
可能是什么原因导致了这种变化?
这是升级到Xcode 9后的图形:

更改颜色的代码:
// Change the brush color
- (void)changeBrushColor:(NSString *) newColor
{
SEL setcolor = NSSelectorFromString(newColor);
UIColor *nColor = [UIColor performSelector:setcolor];
CGColorRef color = nColor.CGColor;
const CGFloat *components = CGColorGetComponents(color);
brushColor[0] = (GLfloat) components[0];
brushColor[1] = (GLfloat) components[1];
brushColor[2] = (GLfloat) components[2];
brushColor[3] = (GLfloat) components[3];
if (initialized) {
glUseProgram(program[PROGRAM_POINT].id);
glUniform4fv(program[PROGRAM_POINT].uniform[UNIFORM_VERTEX_COLOR], 1, brushColor);
}
}绘制代码: CGPoint newMidPoint = CGPointMake(x/scale,y/scale);
[currentStroke insertObject:[NSValue valueWithCGPoint:newMidPoint] atIndex:2];
[currentStroke removeObjectAtIndex:0];
[currentStroke removeObjectAtIndex:0];
//NSLog(@" after draw currentstroke %@: ", currentStroke);
// Load data to the Vertex Buffer Object & Draw it
//glUseProgram(program[PROGRAM_POINT].id);
//glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vertexCount*2*sizeof(GLfloat), vertexBuffer, GL_DYNAMIC_DRAW);
//glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, 0);
// Draw
glDrawArrays(GL_POINTS, 0, (int)vertexCount);
// Display the buffer
//glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];初始化代码:
- (BOOL)initGL
{
// Generate IDs for a framebuffer object and a color renderbuffer
glGenFramebuffers(1, &viewFramebuffer);
g
lGenRenderbuffers(1, &viewRenderbuffer);
glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);
glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
// This cal
l associates the storage for the current render buffer with the EAGLDrawable (our CAEAGLLayer)
// allowing us to draw into a buffer that will later be rendered to screen wherever the layer is (which corresponds with our view).
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(id<EAGLDrawable>)self.layer];
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, viewRenderbuffer);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
return NO;
}
// Setup the view port in Pixels
glViewport(0, 0, backingWidth, backingHeight);
// Create a Vertex Buffer Object to hold our data
glGenBuffers(1, &vboId);
// Load the brush texture
brushTexture = [self textureFromName:@"brush"];
// Load shaders
[self setupShaders];
// Enable blending and set a blending function appropriate for premultiplied alpha pixel data
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
//moved from draw
glUseProgram(program[PROGRAM_POINT].id);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glEnableVertexAttribArray(ATTRIB_VERTEX);
// //***** for testing ******
// [self sample];
// //***** for testing ******
return YES;
}发布于 2017-10-15 13:57:47
我终于找到了解决我的问题的办法。我使用了64x64与32x32的画笔纹理图像,解决了这个问题。我不确定为什么会这样,我仍然想要一些关于是什么导致了这个问题的评论。
https://stackoverflow.com/questions/46722116
复制相似问题