Home > Software design >  How to convert GL_RED to GL_RGBA format
How to convert GL_RED to GL_RGBA format

Time:12-17

This is the code for the fragment shader.

in vec2 TexCoord;
uniform sampler2D texture1;
out vec4 OutColor;
    
void main()
{
    OutColor = texture( texture1 , TexCoord);
}

Whenever any GL_RED format texture is passed the greyscale image outputs as red in color.

I can fix that by using the red parameter of the texture in the shader but is it possible to send GL_RED image as GL_RGBA image to the shader.

unsigned char* image = SOIL_load_image(file, &width, &height,  &channels , SOIL_LOAD_AUTO);  
// Set The Internal Format
if (channels == 4)
{
    texture.Internal_Format = gammaCorrect ? GL_SRGB_ALPHA : GL_RGBA;
    texture.Image_Format = gammaCorrect ? GL_SRGB_ALPHA : GL_RGBA;
}
else if(channels == 3)
{
    
    texture.Internal_Format = gammaCorrect ? GL_SRGB : GL_RGB;
    texture.Image_Format = gammaCorrect ? GL_SRGB : GL_RGB;
}
else if (channels == 1)
{
    texture.Internal_Format = GL_RED;
    texture.Image_Format = GL_RED;
}

CodePudding user response:

Set the texture swizzle parameters with glTexParameter. e.g.:

glBindTexture(GL_TEXTURE_2D, textureObject);

if (channels == 1)
{
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED);
}

This sets the swizzle that will be applied to a component. With that a component can be taken from a different channel.

  • Related