简体   繁体   中英

Draw Completely White Texture in OpenGL

What I want to do is draw a texture like normal in OpenGL, but completely white. I tried doing glColor3f(2.f, 2.f, 2.f) but that doesn't work. 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. 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).

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

To turn the color from the texture white, but still use the alpha value sampled from the texture, you can use:

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. 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. So the RGB color part of the output will always be white.

The less obvious part is what happens to the alpha value. 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. 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.

Create a 1x1 white RGBA bitmap in code.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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