我正在处理基本iOS绘画应用的撤消功能。当我只用一种颜色绘制笔划时,正常的撤消没有问题,但是当我使用不同的颜色时,总是使用最后的笔触颜色渲染所有笔划。
为了实现这一点,我将在撤销数组中存储包含点阵列和笔触颜色的自定义对象,最后在删除最后一个对象并清除屏幕后再次绘制它。
这是代码
- (void)undo {
static GLfloat* vertexBuffer = NULL;
static NSUInteger vertexMax = 64;
NSUInteger vertexCount = 0, count, i;
CGFloat oldRed = self.red;
CGFloat oldGreen = self.green;
CGFloat oldBlue = self.blue;
if (self.undoBuffer.count > 0)
{
TEUndoStroke *undoStroke = [self.undoBuffer lastObject];
if (!self.redoBuffer)
self.redoBuffer = [NSMutableArray array];
[self.redoBuffer addObject:undoStroke];
[self.undoBuffer removeLastObject];
[EAGLContext setCurrentContext:context];
glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
for (TEUndoStroke *_stroke in self.undoBuffer)
{
//Aplicamos el color que tenia
NSLog(@"R: %f G: %f B: %f", _stroke.red, _stroke.green, _stroke.blue);
[self setBrushColorWithRed:_stroke.red green:_stroke.green blue:_stroke.blue];
for (int k = 1; k < _stroke.arrayStrokes.count; k++)
{
CGPoint start = [[_stroke.arrayStrokes objectAtIndex:k-1] CGPointValue];
CGPoint end = [[_stroke.arrayStrokes objectAtIndex:k] CGPointValue];
glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);
// 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(GLfloat));
// 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);
for(i = 0; i < count; ++i) {
if(vertexCount == vertexMax) {
vertexMax = 2 * vertexMax;
vertexBuffer = realloc(vertexBuffer, vertexMax * 2 * sizeof(GLfloat));
}
vertexBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count);
vertexBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count);
vertexCount += 1;
}
}
}
//Render everithing at last to avoid blinking.
glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, 0);
// Draw
glUseProgram(program[PROGRAM_POINT].id);
glDrawArrays(GL_POINTS, 0, vertexCount);
// Load data to the Vertex Buffer Object
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, vertexCount*2*sizeof(GLfloat), vertexBuffer, GL_DYNAMIC_DRAW);
// Display the buffer
glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];
}
[self setBrushColorWithRed:oldRed green:oldGreen blue:oldBlue];
}
这是setBrushColorMethod
- (void)setBrushColorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue
{
self.red = red;
self.green = green;
self.blue = blue;
// Update the brush color
brushColor[0] = red * kBrushOpacity;
brushColor[1] = green * kBrushOpacity;
brushColor[2] = blue * kBrushOpacity;
brushColor[3] = kBrushOpacity;
if (initialized) {
glUseProgram(program[PROGRAM_POINT].id);
glUniform4fv(program[PROGRAM_POINT].uniform[UNIFORM_VERTEX_COLOR], 1, brushColor);
}
}
例如,如果我画三个字母:
R为红色 G为绿色 B为蓝色
我按下撤销按钮,字母“B”消失,但R和G都是绿色。
答案 0 :(得分:0)
最后我发现了问题。似乎变量声明应该逐点在绘制线内。
代码。
- (void)undo
{
if (self.undoBuffer.count > 0)
{
TEUndoStroke *undoStroke = [self.undoBuffer lastObject];
if (!self.redoBuffer)
self.redoBuffer = [NSMutableArray array];
[self.redoBuffer addObject:undoStroke];
[self.undoBuffer removeLastObject];
[EAGLContext setCurrentContext:context];
glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
for (TEUndoStroke *_stroke in self.undoBuffer)
{
for (int k = 1; k < _stroke.arrayStrokes.count; k++)
{
CGPoint start = [[_stroke.arrayStrokes objectAtIndex:k-1] CGPointValue];
CGPoint end = [[_stroke.arrayStrokes objectAtIndex:k] CGPointValue];
static GLfloat* vertexBuffer = NULL;
static NSUInteger vertexMax = 64;
NSUInteger vertexCount = 0,
count,
i;
[EAGLContext setCurrentContext:context];
glBindFramebuffer(GL_FRAMEBUFFER, viewFramebuffer);
// 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(GLfloat));
// 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);
for(i = 0; i < count; ++i) {
if(vertexCount == vertexMax) {
vertexMax = 2 * vertexMax;
vertexBuffer = realloc(vertexBuffer, vertexMax * 2 * sizeof(GLfloat));
}
vertexBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count);
vertexBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count);
vertexCount += 1;
}
CGFloat _strokeColor[4];
_strokeColor[0] = _stroke.red * kBrushOpacity;
_strokeColor[1] = _stroke.green * kBrushOpacity;
_strokeColor[2] = _stroke.blue * kBrushOpacity;
_strokeColor[3] = kBrushOpacity;
if (initialized) {
glUseProgram(program[PROGRAM_POINT].id);
glUniform4fv(program[PROGRAM_POINT].uniform[UNIFORM_VERTEX_COLOR], 1, _strokeColor);
}
// Load data to the Vertex Buffer Object
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
glUseProgram(program[PROGRAM_POINT].id);
glDrawArrays(GL_POINTS, 0, vertexCount);
}
}
// Display the buffer
glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER];
}
}