OpenGL ES:在像素绘画应用程序中撤消

发布于 2024-11-14 06:16:58 字数 112 浏览 2 评论 0原文

我目前正在开发一个应用程序,允许用户使用 OpenGL ES 绘制像素化图像,但我不知道如何实现撤消功能。 我怎样才能做到呢?我想到为每个像素使用一个图像并将其添加到一个数组中。基本上,如何存储用作像素的矩形?

I'm currently working on an app that allows the user to draw pixelated images using OpenGL ES, but I don't know how to implement an undo function.
How could I do it? I thought of using an image for every pixel and adding it to an array. Basically, how can I store the rectangles I use as pixels?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小草泠泠 2024-11-21 06:16:58
how can I store the rectangles I use as pixels?

我不确定您的基本设置是否正确。您应该使用大纹理作为画布。任何用户绘画操作都应该只影响该纹理(您将使用 glTexSubImage2D 更新该纹理)。然后在每一帧上,您应该在屏幕上重新绘制该纹理。

一个简单的 N 步撤消系统将包含 N 个纹理/画布的循环列表。

how can I store the rectangles I use as pixels?

I'm not sure you've got the basic setup right. You should be using a big texture acting as the canvas. Any user painting operations should affect only this texture (which you will be updating with glTexSubImage2D). Then on every frame you should redraw this texture on the screen.

A simple N-steps undo system would consist on a circular list of N textures / canvases.

衣神在巴黎 2024-11-21 06:16:58

你可以尝试:

    NSData *data = [NSData dataWithBytes:vertexBuffer length:vertexCount * sizeof(GL_FLOAT) * 2] ;
if (self.vertexBuffers == nil) self.vertexBuffers = [[NSMutableArray alloc] init];
[self.vertexBuffers addObject:data];

save every draw point to a array;

if undo 
  1. 清除旧的缓冲区

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    
  2. 数组中删除点;

    for (int i = 0; i < 50; ++i)
    {
    [self.vertexBuffersremoveLastObject];
    }
    
  3. 渲染

    for (NSData *self.vertexBuffers 中的点)
    {
        NSUInteger count = point.length / (sizeof(GL_FLOAT) * 2);
        glVertexPointer(2, GL_FLOAT, 0, point.bytes);
        glDrawArrays(GL_POINTS, 0, 计数);
    }
    
  4. 显示缓冲区

     glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
     [上下文presentRenderbuffer:GL_RENDERBUFFER_OES];
    

其他解决方案:
每次绘制内容时,您都可以从 OpenGL ES 上下文中获取图像,并将其作为图像文件保存在应用程序包中。这节省了应用程序的运行内存。
当按下撤消按钮时,您只需将之前保存的图像绘制到上下文中即可。

请参阅 OpenGL ES 简单撤消上次绘图

you can try :

    NSData *data = [NSData dataWithBytes:vertexBuffer length:vertexCount * sizeof(GL_FLOAT) * 2] ;
if (self.vertexBuffers == nil) self.vertexBuffers = [[NSMutableArray alloc] init];
[self.vertexBuffers addObject:data];

save every draw point to a array;

if undo 
  1. clear old buffer

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    
  2. remove point from array;

    for (int i = 0; i < 50; ++i)
    {
    [self.vertexBuffers removeLastObject];
    }
    
  3. render

    for (NSData *point in self.vertexBuffers)
    {
        NSUInteger count = point.length / (sizeof(GL_FLOAT) * 2);
        glVertexPointer(2, GL_FLOAT, 0, point.bytes);
        glDrawArrays(GL_POINTS, 0, count);
    }
    
  4. display buffer

     glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
     [context presentRenderbuffer:GL_RENDERBUFFER_OES];
    

other solution:
You can grab an image from OpenGL ES context everytime you draw something and save in application's bundle as image file. This saves application's run memory.
When undo is pressed you just draw previous saved image into the context and that's it.

See OpenGL ES Simple Undo Last Drawing

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文