要擦除的 OpenGL 纹理

发布于 2024-11-07 09:45:12 字数 1868 浏览 0 评论 0原文

我正在 iOS 上使用 OpenGL ES 1.1 创建绘图/绘画程序。我是 openGL 的新手,但根据有关的所有信息,我已经设法使用半透明 png 纹理用作画笔并绘制抗锯齿线。

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glColor4f( 0.5, 0, 0, 0.5 );

还使用 glColor 我成功地控制了画笔的颜色及其不透明度。

现在我尝试使用纹理来擦除部分绘图,并且在擦除时能够使用不透明度,以便我得到半透明的擦除效果,而不是所有内容都被清除。

我尝试了一些混合功能,但收效甚微。例如,我使用了混合设置:

    glBlendFunc( GL_ONE, GL_ZERO );

我得到的是确实有内容的纹理区域被擦除,并且完全透明的纹理区域变成了黑色,并且忽略了纹理的不透明度。

谁能给我一些关于如何用不透明和软边缘部分擦除的提示。

更新:

为了能够进一步探讨这个问题,我创建了一个 ipad 应用程序,其中有一个纹理/图像背景,在其顶部我放置了一个画笔纹理网格,它是一个带有褪色边缘的白色圆圈。单击纹理将打开一个弹出窗口,允许您设置纹理的颜色以及 OpenGL 源和目标混合。我可以更改设置,以便能够使用不同的颜色在背景上绘制,并达到预期的效果。但是,我似乎无法设置允许我使用纹理擦除背景的设置。大部分绘制代码是在“renderWithTexture”中的 GLElement 类中完成的。代码如下:

-( void ) renderWithTexture
{
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(blendSource_m, blendDest_m);
    [ self setPremultipliedColour ];
    static const GLfloat texCoords[] = { 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0 };
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glVertexPointer(2, GL_FLOAT, sizeof(Vertex), vertices_m );
    glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);
}

在此代码中,blendSource_m和blendDest_m是存储在类中的两个变量,可以通过弹出窗口进行设置。 请下载该项目并让我知道如何使其工作,以便我可以使用纹理进行擦除。希望这个小项目将来能够帮助其他人,因为它对于了解 OpenGL 中的混合如何影响绘图环境非常有用。

已解决:

我设法通过首先将 GLView 的 isOpaque 属性设置为 NO,并将 GL 设置的 glClearColor 设置为透明颜色来解决该问题。我在主视图中添加了背景颜色,以便能够看到某个区域何时被擦除。正如您从示例中看到的,第一行末尾的 2 个项目,一个被完全擦除,另一个被部分擦除。

ipad 测试项目

I am using OpenGl ES 1.1 on the iOS to create a draw/paint program. I am new to openGL but with all the information about I have managed to use a semi-trasnparent png texture to use as brush and draw anti-alise lines.

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glColor4f( 0.5, 0, 0, 0.5 );

Also using the glColor I have managed to control the colour of the brush and it opacity.

Now I am trying to use the texture to erase part of the drawing, and also to be able to use opacity when erasing so that I get a translucent erase and not everything is cleared.

I have tried a few blend functions with very little success. For example I used the blend settings:

    glBlendFunc( GL_ONE, GL_ZERO );

and what I got was the area of the texture that did have content was erased and the the area of the texture where it was totally transparent it turned out black, and the opacity of the texture was ignored.

Could anybody give me anyhints as how I can partly erase with opacity and soft edges.

Update:

To be able to explore this problem further, I created an ipad app where we have a texture/image background and on top of that I have placed a grid of brush texture which is a white circle with faded edges. Clicking on the textures will open a popup that allows you to set the colour to the texture as well as the OpenGL Source and Destination blends. I can change the settings to be able draw over the background with different colours with the expected effect. However I can not seem to be able to set a setting that will allow me for the texture to erase the background. Most of the drawing code is done is class GLElement in "renderWithTexture". Here is the code:

-( void ) renderWithTexture
{
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(blendSource_m, blendDest_m);
    [ self setPremultipliedColour ];
    static const GLfloat texCoords[] = { 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0 };
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glVertexPointer(2, GL_FLOAT, sizeof(Vertex), vertices_m );
    glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);
}

in this code blendSource_m and blendDest_m are two variables stored in the class and can be set through the popup.
Please download the project and please let me know how to get it working so that I can erase with a texture. Hopefully this little project would help others in the future as it can be very useful to see how blending in OpenGL can effect the drawing environment.

Resolved:

I managed to solve the problem by first setting the isOpaque property of the GLView to NO, and setting the glClearColor for GL settings to a clear colour. I added a background colour to the main view to be able to see when an areas are erased. As you can see from the example the 2 items at the end of the first row, one is totally erase and the other is partially erased.

ipad test project

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文