简体   繁体   English

OpenGL:禁用纹理颜色?

[英]OpenGL: Disable texture colors?

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. 它仍然会读取纹理,所以我不能使用glDisable(GL_TEXTURE_2D),因为我也想渲染alpha通道。

All i can think of now is to make new texture where all color data is white, remaining alpha as it is. 我现在能想到的就是制作一个所有颜色数据都是白色的新纹理,保持原样的alpha状态。

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 编辑:显然这是不可能的

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. 我的想法是,当你调用glTexImage2D ,你指定你正在加载的纹素的格式, 指定你从那些纹素创建的纹理的“内部格式”。 In a typical case, you specify (roughly) the same format for both -- eg you'll typically use GL_RGBA for both. 在典型情况下,您为两者指定(大致)相同的格式 - 例如,您通常会同时使用GL_RGBA

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 the internal format (the one close to the beginning of the parameter list) specifies the format for the actual texture you create from those texels. 有两个虽然指定一个原因是: format (接近列表的末尾参数)指定你加载纹理像素的格式,但internal format (一接近参数列表的开头)指定从这些纹素中创建的实际纹理的格式。

If you want to load some texels, but only actually use the alpha channel from them, you can specify GL_ALPHA for the internal 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. 如果你想加载一些纹素,但实际上只使用它们的alpha通道,你可以为internal format指定GL_ALPHA ,这就是你得到的全部 - 当你将纹理映射到表面时,它只会影响阿尔法,而不是颜色。 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. 这不仅可以避免制作纹素的额外副本,而且(至少通常)也会减少纹理本身消耗的内存,因为它只包含alpha通道,而不包括三个颜色通道。

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 with GL_REPLACE , but when you only want to use the alpha channel, you call it with GL_BLEND . 首先,设置混合功能,只需使用alpha通道,那么当你要复制的纹理的颜色,你叫glTextureEnvfGL_REPLACE ,但是当你只想使用alpha通道,你叫它GL_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. 例如,让我们创建一个绿色纹理,并在蓝色四边形上绘制(两次),一次使用GL_REPLACE,一次使用GL_BLEND。 For simplicity, we'll use a solid gree texture, with alpha increasing linearly from top (0) to bottom (1): 为简单起见,我们将使用纯绿色纹理,alpha从顶部(0)到底部(1)线性增加:

static GLubyte Image[128][128][4];

for (int i=0; i<128; i++)
    for (int j=0; j<128; j++) {
        Image[i][j][0] = 0;
        Image[i][j][1] = 255;
        Image[i][j][2] = 0;
        Image[i][j][3] = i;
    }

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: 我将跳过大部分创建和绑定纹理,设置参数等,并直接使用纹理绘制几个四边形:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

glBegin(GL_QUADS);
    glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
    glTexCoord2f(0.0, 0.0); glVertex3f(-1.0f, 1.0f, 0.0f);
    glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
    glTexCoord2f(1.0, 0.0); glVertex3f(0.0f, 1.0f, 0.0f);
    glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
    glTexCoord2f(1.0, 1.0); glVertex3f(0.0f, -1.0f, 0.0f);
    glColor4f(0.0, 0.0f, 1.0f, 1.0f);
    glTexCoord2f(0.0, 1.0); glVertex3f(-1.0f, -1.0f, 0.0f);
glEnd();

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);

glBegin(GL_QUADS);
    glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
    glTexCoord2f(0.0, 0.0); glVertex3f(0.0f, 1.0f, 0.0f);
    glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
    glTexCoord2f(1.0, 0.0); glVertex3f(1.0f, 1.0f, 0.0f);
    glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
    glTexCoord2f(1.0, 1.0); glVertex3f(1.0f, -1.0f, 0.0f);
    glColor4f(0.0, 0.0f, 1.0f, 1.0f);
    glTexCoord2f(0.0, 1.0); glVertex3f(0.0f, -1.0f, 0.0f);
glEnd();

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. 因此,在使用GL_REPLACE绘制的左侧,我们得到纹理的绿色,但是在右侧,用GL_BLEND绘制的位置(并且glBlendFunc设置为仅使用alpha通道),我们得到蓝色四边形,但是从纹理中取出的Alpha - 但我们对两者使用完全相同的纹理。

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. 编辑2:如果你确定你确实需要一个全白的纹理,我只需创建一个1像素的纹理,并将GL_TEXTURE_WRAP_S和GL_TEXTURE_WRAP_T设置为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. 我没有尝试过测试,但你可能会选择像8x8或16x16像素块这样的东西。 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. 这仍然很小,几乎不重要,但这些分别是JPEG和MPEG中使用的尺寸,我可以看到卡和(特别是)驱动程序可能针对它们进行优化的位置。 It might help, and won't hurt (enough to care about anyway). 它可能会有所帮助,也不会受到伤害(无论如何都要关心)。

What about changing all texture color (except alpha) to white after they are loaded and before they are utilized in OpenGL? 在加载之后以及在OpenGL中使用它们之前,将所有纹理颜色(除了alpha)更改为白色怎么样? If you have them as bitmaps in memory at some point, it should be easy and you won't need separate texture files. 如果你在某些时候将它们作为内存中的位图,它应该很容易,你不需要单独的纹理文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM