撤消画图应用程序中的绘图
我正在使用 OpenGl 框架制作一个绘画应用程序,但我陷入了 UNDO/REDO 选项。 我实现的代码是这样的:
-(void)undo_called
{
artbrushAppDelegate *app=(artbrushAppDelegate *)[[UIApplication sharedApplication]delegate];
mbrushscale=app.brushscale;
brushimage=app.brush_image;
Erase=YES;
[self playRayundo];
}
-(void)playRayundo
{
artbrushAppDelegate *app=(artbrushAppDelegate *)[[UIApplication sharedApplication]delegate];
glColor4f(app.r1g,
app.b1g,
app.g1g,
0);
NSLog(@"%f",app.r1g);
if(undo != NULL)
{
for(int l = 0; l < [undo count]; l++)
{
//replays my writRay -1 because of location point
for(int p = 0; p < [[undo objectAtIndex:l]count]-1; p ++)
{
[self drawErase:[[[undo objectAtIndex:l]objectAtIndex:p]CGPointValue] toPoint:[[[undo objectAtIndex:l]objectAtIndex:p + 1]CGPointValue]];
}
}
}
Erase=NO;
glColor4f(app.rg,
app.bg,
app.gg,
kBrushOpacity);
}
-(void) drawErase:(CGPoint)start toPoint:(CGPoint)end
{
static GLfloat* eraseBuffer = NULL;
static NSUInteger eraseMax = 64;
NSUInteger vertexCount = 0,
count,
i;
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
// Convert locations from Points to Pixels
CGFloat scale = 1.0;//self.contentScaleFactor;
start.x *= scale;
start.y *= scale;
end.x *= scale;
end.y *= scale;
// Allocate vertex array buffer
if(eraseBuffer == NULL)
eraseBuffer = malloc(eraseMax * 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 == eraseMax)
{
eraseMax = 2 * eraseMax;
eraseBuffer = realloc(eraseBuffer, eraseMax * 2 * sizeof(GLfloat));
}
eraseBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count);
eraseBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count);
vertexCount += 1;
}
[self ChangebrushPic:brushimage];
//the erase brush color is transparent.
glEnable(GL_POINT_SPRITE_OES);
glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);
glPointSize(64/mbrushscale);
// Render the vertex array
glVertexPointer(2, GL_FLOAT, 0, eraseBuffer);
glDrawArrays(GL_POINTS, 0, vertexCount);
// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
// at last restore the mixed-mode
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
这段代码效率不高,而且有很多错误。看看撤消之前和之后的图像。
之前: http://imageshack.us/photo/my-images/577/ snapshot20110714at121.png/
之后:http://imageshack.us/photo/my-images/200/screenshot20110714at121.png/
所以我想在每次用户触摸结束时将图像保存在缓冲区中,并在 UNDO 上调用上一个图像。任何人都可以帮助我如何存储图像在缓冲区中并在 UNDO 上检索回来?我试图找到它的示例代码,但找不到..
谢谢..
I am making a paint application using OpenGl framework and I'm stuck at UNDO/REDO option..
The code I implemented is like this:
-(void)undo_called
{
artbrushAppDelegate *app=(artbrushAppDelegate *)[[UIApplication sharedApplication]delegate];
mbrushscale=app.brushscale;
brushimage=app.brush_image;
Erase=YES;
[self playRayundo];
}
-(void)playRayundo
{
artbrushAppDelegate *app=(artbrushAppDelegate *)[[UIApplication sharedApplication]delegate];
glColor4f(app.r1g,
app.b1g,
app.g1g,
0);
NSLog(@"%f",app.r1g);
if(undo != NULL)
{
for(int l = 0; l < [undo count]; l++)
{
//replays my writRay -1 because of location point
for(int p = 0; p < [[undo objectAtIndex:l]count]-1; p ++)
{
[self drawErase:[[[undo objectAtIndex:l]objectAtIndex:p]CGPointValue] toPoint:[[[undo objectAtIndex:l]objectAtIndex:p + 1]CGPointValue]];
}
}
}
Erase=NO;
glColor4f(app.rg,
app.bg,
app.gg,
kBrushOpacity);
}
-(void) drawErase:(CGPoint)start toPoint:(CGPoint)end
{
static GLfloat* eraseBuffer = NULL;
static NSUInteger eraseMax = 64;
NSUInteger vertexCount = 0,
count,
i;
[EAGLContext setCurrentContext:context];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
// Convert locations from Points to Pixels
CGFloat scale = 1.0;//self.contentScaleFactor;
start.x *= scale;
start.y *= scale;
end.x *= scale;
end.y *= scale;
// Allocate vertex array buffer
if(eraseBuffer == NULL)
eraseBuffer = malloc(eraseMax * 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 == eraseMax)
{
eraseMax = 2 * eraseMax;
eraseBuffer = realloc(eraseBuffer, eraseMax * 2 * sizeof(GLfloat));
}
eraseBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count);
eraseBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count);
vertexCount += 1;
}
[self ChangebrushPic:brushimage];
//the erase brush color is transparent.
glEnable(GL_POINT_SPRITE_OES);
glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);
glPointSize(64/mbrushscale);
// Render the vertex array
glVertexPointer(2, GL_FLOAT, 0, eraseBuffer);
glDrawArrays(GL_POINTS, 0, vertexCount);
// Display the buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
// at last restore the mixed-mode
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
This code is not efficient and it has so many faults..Have a look at images before and After Undo.
Before: http://imageshack.us/photo/my-images/577/screenshot20110714at121.png/
After: http://imageshack.us/photo/my-images/200/screenshot20110714at121.png/
So i want to Save image in buffer every time user's Touch ends and call previous image on UNDO..Can anyone help me how to Store image in buffer and retrieve back on UNDO?? I tried to find its sample code but could not find..
Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的。我正在发布我自己问题的答案,因为似乎有很多人提到这个问题。
有一个名为TouchPainter的应用程序及其源代码 可用。它包含绘图、颜色混合、撤消/重做(太棒了..!!)、保存/打开绘图..
注意:它可能需要非常深入的Objective C知识和设计模式(我仍然不知道什么是设计模式。但我找到了它,所以我分享......)。整个应用程序的源代码在《Apress.Pro Objective-C Design Patterns for iOS》这本书中进行了解释。
我希望它对你有帮助..:)祝你好运..
Ok. I am posting answer to my own Question as so many people seem to refer this question.
There is an application named TouchPainter and its source code is available. It contains drawing, color blending, Undo/Redo (Amazing..!!), Save/Open drawings..
Note : It may require very deep level knowledge of Objective C and design pattern (I still do not know what design pattern is. But I found it so I am sharing..). The whole application's source code is explained in this book "Apress.Pro Objective-C Design Patterns for iOS".
I hope it will be helpful to you.. :) Good luck..
使用 nsundomanager,这是撤消的最佳方法
use the nsundomanager, that is the best way to undo