简体   繁体   English

在OpenGL中绘制完全白色的纹理

[英]Draw Completely White Texture in OpenGL

What I want to do is draw a texture like normal in OpenGL, but completely white. 我想做的是在OpenGL中绘制一个像普通的纹理,但是要完全白色。 I tried doing glColor3f(2.f, 2.f, 2.f) but that doesn't work. 我尝试做glColor3f(2.f, 2.f, 2.f)但这不起作用。 I just want to draw the shape of a certain texture but without color and just white, so I'm trying to draw a texture but white... 我只想绘制某种纹理的形状,但没有颜色,只有白色,所以我想绘制一种纹理,但白色...

To clarify the desired result: I want the RGB part of all colors sampled from the texture to be white, while the alpha value is the one sampled from the texture. 为了阐明期望的结果:我希望从纹理采样的所有颜色的RGB部分为白色,而alpha值是从纹理采样的一个。 So if the value in the texture is (R, G, B, A), I want the sampled color to be (1.0f, 1.0f, 1.0f, A). 因此,如果纹理中的值为(R,G,B,A),我希望采样的颜色为(1.0f,1.0f,1.0f,A)。

使用像素着色器,并将每个片段的颜色设置为白色。

To turn the color from the texture white, but still use the alpha value sampled from the texture, you can use: 要将纹理的颜色变为白色,但仍使用从纹理采样的Alpha值,可以使用:

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

With the GL_ADD texture env mode, the incoming fragment color (which the second call above sets to white) will be added to the color sampled from the texture to obtain the output color. GL_ADD纹理环境模式下,传入的片​​段颜色(上面的第二个调用将其设置为白色)将添加到从纹理采样的颜色中,以获得输出颜色。 Since output colors are clamped to the range [0.0, 1.0], the color components from the texture do not matter, since they will be added to 1.0 before the result is clamped to 1.0. 由于输出颜色被限制在[0.0,1.0]范围内,因此纹理中的颜色分量无关紧要,因为在将结果限制为1.0之前会将它们添加到1.0。 So the RGB color part of the output will always be white. 因此,输出的RGB颜色部分将始终为白色。

The less obvious part is what happens to the alpha value. 不太明显的部分是alpha值发生了什么。 According to the spec, the alpha value from the incoming fragment and the alpha value sampled from the texture are multiplied for the GL_ADD texture env mode. 根据规范,对于GL_ADD纹理环境模式,将来自传入片段的alpha值和从纹理采样的alpha值相乘 With the fragment alpha set to 1.0, this means that the resulting value is the alpha value from the texture, which is what you wanted. 将片段alpha设置为1.0时,这意味着结果值是纹理中的alpha值,这就是您想要的。

Create a 1x1 white RGBA bitmap in code. 在代码中创建一个1x1白色RGBA位图。

GLubyte texData[] = { 255, 255, 255, 255 };

Copy data to texture. 将数据复制到纹理。

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData);

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

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