openGL 中正确混合的问题

发布于 2024-10-31 19:49:12 字数 1336 浏览 1 评论 0原文

我试图在 2d 图块地图上绘制 2d 角色精灵,但是当我绘制角色时,他身后有一些奇怪的东西。这不在精灵中,所以我认为是混合。

这就是我的 openGL 的设置方式:

void InitGL(int Width, int Height)          // We call this right after our OpenGL window is created.
{

    glViewport(0, 0, Width, Height);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);       // This Will Clear The Background Color To Black
    glClearDepth(1.0);              // Enables Clearing Of The Depth Buffer
    glDepthFunc(GL_LESS);               // The Type Of Depth Test To Do
    glDisable(GL_DEPTH_TEST);           // Enables Depth Testing
    //glShadeModel(GL_SMOOTH);          // Enables Smooth Color Shading
    glEnable(GL_TEXTURE_2D);                        // Enable Texture Mapping ( NEW )
    glShadeModel(GL_FLAT);
    glMatrixMode(GL_PROJECTION);
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE , GL_ONE_MINUS_SRC_ALPHA);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glAlphaFunc(GL_GREATER, 0.5f);

    glMatrixMode(GL_PROJECTION);//configuring projection matrix now
    glLoadIdentity();//reset matrix
    glOrtho(0, Width, 0, Height, 0.0f, 100.0f);//set a 2d projection matrix

}

我应该如何设置它才能正常工作(即绘制精灵,后面没有奇怪的东西。

这就是我所说的:https://i.sstatic.net/cmotJ.png

PS:我需要能够将透明/半透明图像放在彼此之上并且他们背后的东西也可见

I'm trying to draw a 2d character sprite on top of a 2d tilemap, but when I draw the character he's got odd stuff behind him. This isn't in the sprite, so I think its the blending.

This is how my openGL is set up:

void InitGL(int Width, int Height)          // We call this right after our OpenGL window is created.
{

    glViewport(0, 0, Width, Height);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);       // This Will Clear The Background Color To Black
    glClearDepth(1.0);              // Enables Clearing Of The Depth Buffer
    glDepthFunc(GL_LESS);               // The Type Of Depth Test To Do
    glDisable(GL_DEPTH_TEST);           // Enables Depth Testing
    //glShadeModel(GL_SMOOTH);          // Enables Smooth Color Shading
    glEnable(GL_TEXTURE_2D);                        // Enable Texture Mapping ( NEW )
    glShadeModel(GL_FLAT);
    glMatrixMode(GL_PROJECTION);
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE , GL_ONE_MINUS_SRC_ALPHA);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glAlphaFunc(GL_GREATER, 0.5f);

    glMatrixMode(GL_PROJECTION);//configuring projection matrix now
    glLoadIdentity();//reset matrix
    glOrtho(0, Width, 0, Height, 0.0f, 100.0f);//set a 2d projection matrix

}

How should I set this up to work properly (i.e. drawing the sprite without odd stuff behind him.

This is what I am talking about: https://i.sstatic.net/cmotJ.png

PS: I need to be able to put transparent/semi-transparent images on top of each other and have whats behind them visible too

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

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

发布评论

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

评论(1

原谅我要高飞 2024-11-07 19:49:12

你的精灵有预乘阿尔法吗?您的 glBlendFunc 设置有点不寻常,如果您没有预乘 alpha,则肯定会导致问题。

Does your sprite have premultiplied alpha? Your glBlendFunc setup is a little unusual, if you don't have premultiplied alpha it could definitely be causing the issue.

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