In a gbuffer I store a diffuse color in following texture:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
I want to store an integer data which represents a pixel type in channel A. Here is part of the fragment shader:
#version 420
in vec2 tCoord;
layout (location = 2) out vec4 diffuseColorData;
uniform sampler2D texture0;
uniform int pixelType;
void main(void)
{
diffuseColorData.xyz = texture(texture0, tCoord).xyz;
diffuseColorData.w = pixelType;
}
Is it possible ? How to decode a pixel type when reading from texture ?
Edit
I use a pixel type to choose a shading function (standard shading - int 1, shading with normal maps - int 2, shading with uniform color - int 3). I need to use additive blending because of multiple light sources (point lights).
I changed the code as you suggested:
diffuseColorData.w = pixelType / 4.0;
In the next fragment shader I read a data this way (I post only relevant part of the code):
uniform sampler2D texture2;
vec4 diffandPixelType = texture(texture2, texCoord);
vec4 diff = vec4(diffandPixelType.xyz, 1.0);
float pixelType = diffandPixelType.w * 4.0;
if(pixelType == 1)
{
outColor = shadingfun1(/*arguments*/)
}
else if(pixelType == 2)
{
outColor = shadingfun2(/*arguments*/)
}
else if(pixelType == 3)
{
outColor = shadingfun3(/*arguments*/)
}
The code doesn't work properly and I don't know how to explain that. What is wrong ?
I found other solution which works perfectly, but it require extra integer texture in the gbuffer:
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8I, width, height, 0, GL_RED_INTEGER, GL_INT, NULL);
//fragment shader
layout (location = 4) out int shadingData;
uniform int pixelType;
shadingData = pixelType;
//fragment shader
uniform isampler2D texture6;
int pixelType = texture(texture6, texCoord).x;
if(pixelType == 1)
{
outColor = shadingfun1(/*arguments*/)
}
else if(pixelType == 2)
{
outColor = shadingfun2(/*arguments*/)
}
else if(pixelType == 3)
{
outColor = shadingfun3(/*arguments*/)
}
pixelType
to the nearest integer when you decode it. When you testpixelType == 1
that's asking for it to be exactly equal to 1.0, which isn't likely to happen with floating-point. It will fail ifpixelType
is 0.99998 or something. \$\endgroup\$