Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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*/)
}  
share|improve this question
    
You need to round pixelType to the nearest integer when you decode it. When you test pixelType == 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 if pixelType is 0.99998 or something. –  Nathan Reed Apr 10 '13 at 18:51
    
Another issue I spotted other than the one Nathan pointed out is that you really should number pixel types from 0, not 1. Ok, this is a somewhat arbitrary thing, but in programming it is generally nice to index from 0, since that is the smallest possible integer. And another small thing - having branches in your fragment shader may be slower than calculating all the possible colors, then multiplying each of them except the one you're using by 0, and adding them all up. –  maul Apr 10 '13 at 19:37
add comment

1 Answer

It is possible, you just need to scale it to the 0-1 range, and make sure to avoid precision issues.

For example if you have 6 pixel types, you can do

diffuseColorData.w = pixelType / 5.0;

Of course I would suggest setting up a define in the shader, possibly programatically.

When reading from the texture just do the exact opposite, multiply it by 5 and round the result to an int. You're probably safe from rounding and precision issues unless the number of possible pixel types is really high (I'd start worrying above 127).

You also need to make sure that you write each fragment exactly once (no alpha blending), otherwise you'll get some kind of interpolation between pixel types, which may not be something you want to achieve.

share|improve this answer
    
Please see Edit –  Irbis Apr 10 '13 at 13:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.