渲染几何体中的 alpha 透明度并计算位图中的 alpha 颜色 - OpenGL
这是一个由两部分组成的问题。我正在渲染一些 BSP 几何体,并且某些纹理具有应该不可见的 alpha 颜色。
所以第一步是找出 Alpha 颜色,这样我就可以正确渲染它。我上传了一个示例位图:
如您所见,它是一个栏杆纹理,其中 RGB(160,158,148) 是颜色应该是不可见的。我只要看一下就能弄清楚这一点。问题是,如何在文件中找到这个值?
这是第一步。第二个是 OpenGL 问题,我用顶点数组渲染一堆几何体。如何适应渲染时需要从纹理中删除颜色的事实?我需要按特定顺序渲染它吗?另外,我应该提到我正在使用 SDL,如果这有助于使这一切变得更容易的话。
谢谢你们两位的精彩回答。我希望我能选择你们两个...
This is a two part question. I am rendering some BSP geometry and some of the textures have an alpha color which should be invisible.
So the first step would be figuring out the alpha color so I can correctly render it. I uploaded a sample bitmap:
As you can see, it is a railing texture where RGB(160,158,148) is the color that should be invisible. I can figure this out just by looking at it. The problem is, how do I find this value in the file?
So that is step one. The second is an OpenGL issue where I am rendering a bunch of geometry with vertex arrays. How do I accommodate for the fact a color needs to be removed from the texture when rendered? Do I need to render this in a specific order? Also, I should mention I am using SDL if that helps make any of this a bit easier.
Thank you both for fantastic answers. I wish I could pick both of you...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实际上称为键控颜色。
Alpha 通道是一个附加通道,添加到“正常”RGB 通道中。当使用 Alpha 混合时,使用 Alpha 通道值作为不透明度。
如果使用颜色键,则可以使用某种特定颜色(用于所有图像),或者单独存储键控值,并将其与图像文件一起加载。另一种常见的技术是使用左上角像素的值作为键控颜色。
通过使用颜色键生成的 Alpha 通道准备图像数据:
更新:在颜色键生成的 Alpha 通道的情况下,可以使用Alpha 测试而不是混合。 Alpha 测试的好处是,它不需要在绘制之前对几何图形进行深度排序。
理想的方法是使用支持 Alpha 通道的图像文件格式(PNG、TGA、OpenEXR... ),并且不使用 BMP(实际上 BMP 文件格式允许第四个通道,事实上,很多年前我曾经编写过自己的带有 Alpha 通道支持的 BMP 加载器和 Photoshop 导出插件)。
This is actually called a keying color.
The alpha channel is an additional channel, adding to the "normal" RGB channels. When alpha blending is used, one uses the alpha channel value as opacity.
If a color key is used, one uses either some specific color, used for all images, or stores the keying value separately, loading it with the image file. Another common technique is to use the value of the upper left pixels as keying color.
By preparing the image data with a color key generated alpha channel:
Update: In the case of color key generated alpha channel, one can use alpha testing instead of blending. The nice thing with alpha testing is, that it doesn't requires to depth sort the geometry prior to drawing.
The ideal way would be to use a image file format with alpha channel support (PNG, TGA, OpenEXR, …), and not using BMPs (actually the BMP file format would allow for a 4th channel, and in fact I once wrote my own BMP loader with alpha channel support, and a Photoshop export plugin, many years ago).
如果我想要完全透明,Alpha 测试 是我的首选技术。无需排序。
就您的图像而言,将其转换为 PNG 并将每个 RGB(160,158,148) 像素的 alpha 设置为零,其他位置设置为 255。然后你可以使用 alpha 测试。
您可能需要将 RGB(160,158,148) 像素更改为 RGB(255,255,255),以避免
GL_LINEAR
过滤出现一些奇怪的光晕。但已经有一段时间了,我对此并不能百分百确定。Alpha testing is my go-to technique if I want full transparency. No sorting needed.
As far as your image goes convert it to a PNG and set your alpha to zero for each RGB(160,158,148) pixel and 255 everywhere else. Then you can use the alpha test.
You may need to change your RGB(160,158,148) pixels to RGB(255,255,255) to avoid some odd haloing with
GL_LINEAR
filtering. But it's been a while and I'm not 100% sure on that.