3

I am passing vec4 to the shaders with xyz and a colour value and I tried to bit-wise shift the colour component to their own r g and b floats but having issues:

Vertex shader:

#version 150

in vec4 position;

out vec2 Texcoord;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

void main()
{
    Texcoord = vec2(position.w, position.w);
    gl_Position = proj * view * model * vec4(position.xyz, 1.0);
} 

Fragment shader:

#version 150

in vec2 Texcoord;
out vec4 outColor;

uniform sampler2D tex;

void main()
{
    float data = Texcoord.r;
    float r = (data>> 16) & 0xff;
    float g = (data>> 8) & 0xff;
    float b = data & 0xff;
    outColor = vec4(r, g, b, 1.0);
}

Error:

Error compiling shader: 
0(11) : error C1021: operands to ">>" must be integral
0(12) : error C1021: operands to ">>" must be integral
0(13) : error C1021: operands to "&" must be integral

Any ideas what I am doing wrong?

genpfault
  • 51,148
  • 11
  • 85
  • 139
user1031204
  • 701
  • 1
  • 8
  • 30
  • 1
    There is no shift operator for floating point datatypes (at least in no language I know). See [this posting](http://stackoverflow.com/questions/9315539/left-shift-float-type) for an explaination – BDL Dec 18 '14 at 13:59
  • You may want the pack instruction instead – ratchet freak Dec 18 '14 at 14:01
  • 1
    I can't think of a good reason to ever want to bit shift a floating point value, could you clarify exactly what you're trying to do? – bfair Dec 18 '14 at 15:13
  • 1
    The floating-point value you get when you sample a traditional texture is normalized. While the color you originally started with is probably represented as an integer in the range [**0**,**255**], what you get in GLSL is [**0.0**,**1.0**]. To do what you want, you need to multiply this normalized value by **255** and cast to `uint`. Of course since you have to write a floating-point color back, you have to reverse the process as well. _However, the thing is your texture coordinates themselves are not really colors - you should be using them to sample a texture, which **will** produce color._ – Andon M. Coleman Dec 18 '14 at 15:59
  • But what I just talked about still does not make a lot of sense, because you can always extract a specific component of a texture using a swizzle instead of bit-shifts. You are absolutely going to have to explain why you want to do this. – Andon M. Coleman Dec 18 '14 at 16:20

1 Answers1

5

OK got it done :)

#version 150

in vec2 Texcoord;
out vec4 outColor;

uniform sampler2D tex;

vec3 unpackColor(float f) 
{
    vec3 color;
    color.r = floor(f / 65536);
    color.g = floor((f - color.r * 65536) / 256.0);
    color.b = floor(f - color.r * 65536 - color.g * 256.0);
    return color / 256.0;
}

void main()
{
    float data = Texcoord.r;
    vec3 unpackedValues = unpackColor(Texcoord.r);

    outColor = vec4(unpackedValues.bgr, 1.0);
}

I was passing 4 bits of formation to my shader x y z coordinate of a point in the point cloud and 4th value was encoded colour in a float. So I needed to extract rgb information from the float to give each point a colour.

Thanks for help guys :)

user1031204
  • 701
  • 1
  • 8
  • 30