OpenGL:禁用纹理颜色?
是否可以禁用纹理颜色并仅使用白色作为颜色?它仍然会读取纹理,所以我不能使用 glDisable(GL_TEXTURE_2D) 因为我也想渲染 Alpha 通道。
我现在能想到的就是制作新的纹理,其中所有颜色数据都是白色,保持 alpha 不变。
我需要不使用着色器来完成此操作,那么这可能吗?
编辑:澄清:我想使用两种纹理:白色和普通颜色。
编辑:显然这是不可能的
Is it possible to disable texture colors, and use only white as the color? It would still read the texture, so i cant use glDisable(GL_TEXTURE_2D) because i want to render the alpha channels too.
All i can think of now is to make new texture where all color data is white, remaining alpha as it is.
I need to do this without shaders, so is this even possible?
Edit: to clarify: i want to use both textures: white and normal colors.
Edit: APPARENTLY THIS IS NOT POSSIBLE
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我仍然不完全确定我正确理解你想要什么,但我会尝试一下。我的想法是,当您调用
glTexImage2D
时,您指定要加载的纹理像素的格式,并且指定您要加载的纹理的“内部格式”从这些纹理元素重新创建。在典型情况下,您为两者指定(大致)相同的格式 - 例如,您通常会为两者使用GL_RGBA
。不过,指定这两者是有原因的:
format
(靠近列表末尾的参数)指定了您要加载的纹素的格式自,但是内部格式
(靠近参数列表开头的格式)指定从这些纹素创建的实际纹理的格式。如果您想加载一些纹理像素,但实际上仅使用它们的 Alpha 通道,则可以为
内部格式
指定GL_ALPHA
,这就是您将得到的一切 - 当您将该纹理映射到表面时,它只会影响 Alpha,而不影响颜色。这不仅避免了对纹素进行额外的复制,而且(至少通常)还减少了纹理本身消耗的内存,因为它只包含一个 Alpha 通道,而不包含三个颜色通道。编辑:好的,再考虑一下,有一种方法可以只使用一个纹理来完成(我认为)你想要的事情。首先,您将混合函数设置为仅使用 Alpha 通道,然后当您想要复制纹理的颜色时,您可以使用
GL_REPLACE
调用glTextureEnvf
,但是当您只使用想要使用 Alpha 通道,可以使用 GL_BLEND 来调用它。例如,让我们创建一个绿色纹理,并在蓝色四边形上绘制它(两次),一次使用 GL_REPLACE,一次使用 GL_BLEND。为简单起见,我们将使用纯格子纹理,alpha 从顶部 (0) 到底部 (1) 线性增加:我将跳过大部分创建和绑定纹理、设置参数等,并直接获取用纹理绘制几个四边形:
这会产生:
所以,在左边的位置使用 GL_REPLACE 绘制,我们得到纹理的绿色,但在右侧,使用 GL_BLEND 绘制(并且 glBlendFunc 设置为仅使用 Alpha 通道),我们得到蓝色四边形,但其 Alpha 取自纹理 -但我们对两者使用完全相同的纹理。
编辑 2:如果您确定确实需要全白色的纹理,我只需创建一个 1 像素纹理,并将 GL_TEXTURE_WRAP_S 和 GL_TEXTURE_WRAP_T 设置为 GL_REPEAT。尽管这仍然使用额外的纹理,但该额外的纹理将非常很小,因为它只有一个像素。加载它的时间和消耗的内存都非常小——像素的数据基本上是“噪声”。我没有尝试测试,但您可能会更好地使用 8x8 或 16x16 像素块。这仍然很小,没什么关系,但这些是分别在 JPEG 和 MPEG 中使用的大小,我可以看到卡和(特别是)驱动程序可以在哪里针对它们进行优化。它可能会有所帮助,并且不会造成伤害(无论如何都足以关心)。
I'm still not entirely sure I understand correctly what you want, but I'll give it a shot. What I had in mind is that when you call
glTexImage2D
, you specify the format of the texels you're loading and you specify the "internal format" of the texture you're creating from those texels. In a typical case, you specify (roughly) the same format for both -- e.g. you'll typically useGL_RGBA
for both.There is a reason for specifying both though: the
format
(the parameter close to the end of the list) specifies the format of the texels you're loading from, but theinternal format
(the one close to the beginning of the parameter list) specifies the format for the actual texture you create from those texels.If you want to load some texels, but only actually use the alpha channel from them, you can specify
GL_ALPHA
for theinternal format
, and that's all you'll get -- when you map that texture to a surface, it'll affect only the alpha, not the color. This not only avoids making an extra copy of your texels, but (at least usually) reduces the memory consumed by the texture itself as well, since it only includes an alpha channel, not the three color channels.Edit: Okay, thinking about it a bit more, there's a way to do what (I think) you want, using only the one texture. First, you set the blend function to just use the alpha channel, then when you want to copy the color of the texture, you call
glTextureEnvf
withGL_REPLACE
, but when you only want to use the alpha channel, you call it withGL_BLEND
. For example, let's create a green texture, and draw it (twice) over a blue quad, once with GL_REPLACE, and one with GL_BLEND. For simplicity, we'll use a solid gree texture, with alpha increasing linearly from top (0) to bottom (1):I'll skip over most of creating and binding the texture, setting the parameters, etc., and get directly to drawing a couple of quads with the texture:
This produces:
So, on the left where it's drawn with GL_REPLACE, we get the green of the texture, but on the right, where it's drawn with GL_BLEND (and glBlendFunc was set to use only the alpha channel) we get the blue quad, but with its Alpha taken from the texture -- but we use exactly the same texture for both.
Edit 2: If you decide you really do need a texture that's all white, I'd just create a 1-pixel texture, and set GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T to GL_REPEAT. Even though this is still using an extra texture, that extra texture will be really tiny, since it's only one pixel. Both the time to load it and the memory consumed will be truly minuscule -- the data for the pixel is basically "noise". I haven't tried to test, but you might be better off with something like an 8x8 or 16x16 block of pixels instead. This is still so small it hardly matters, but those are the sizes used in JPEG and MPEG respectively, and I can see where the card and (especially) driver might be optimized for them. It might help, and won't hurt (enough to care about anyway).
在加载所有纹理颜色(Alpha 除外)之后以及在 OpenGL 中使用它们之前将它们更改为白色怎么样?如果您在某个时候将它们作为位图存储在内存中,那么这应该很容易,并且您不需要单独的纹理文件。
What about changing all texture color (except alpha) to white after they are loaded and before they are utilized in OpenGL? If you have them as bitmaps in memory at some point, it should be easy and you won't need separate texture files.