Colour bit-wise shift in OpenGL shader GLSL

廉价感情. 提交于 2020-01-04 05:14:08

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/27548216/colour-bit-wise-shift-in-opengl-shader-glsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!