本文介绍了OpenGL着色器GLSL中的颜色按位移位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将vec4传递给带有xyz和颜色值的着色器,我试图将颜色分量按位移动到它们自己的r g和b浮点数,但是有问题:
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:
顶点着色器:
#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);
}
片段着色器:
#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 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?
推荐答案
确定完成了:)
#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);
}
我正在将4位构形传递到点云中某个点的着色器x y z坐标,并且第4个值以浮点数进行了颜色编码.因此,我需要从浮动对象中提取rgb信息,以便为每个点赋予一种颜色.
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.
感谢帮助人员:)
这篇关于OpenGL着色器GLSL中的颜色按位移位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!