GLSL 将颜色数据从片段着色器发送到顶点着色器似乎总是等于 0
GLSL Sending color data form Fragment Shader to Vertex Shader seems to be always equal to 0
所以我有一个平面,我希望它能与高度贴图一起使用,所以我只是将 heightMap 作为常规纹理,计算它的颜色,但我没有将片段的颜色设置为纹理的颜色,而是设置了初始 position.y
到 position.y + texColor.r * u_Scale
这样顶点的高度会根据纹理的颜色而变化。
#shader vertex
#version 330 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color_in;
layout(location = 3) in vec2 texCoord;
uniform mat4 u_MVP;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform float u_Scale;
in vec4 texColor;
out vec3 FragPos;
out vec3 color_f;
out vec2 v_TexCoord;
void main()
{
v_TexCoord = texCoord;
color_f = color_in;
vec3 newPosition = vec3(position.x, position.y + texColor.b * u_Scale, position.z);
FragPos = vec3(model * vec4(newPosition, 1.0));
gl_Position = projection * view * vec4(FragPos, 1.0);
};
#shader fragment
#version 330 core
out vec4 color;
in vec3 FragPos;
in vec2 v_TexCoord;
in flat vec3 color_f;
out vec4 texColor;
uniform sampler2D u_Texture;
void main()
{
texColor = texture(u_Texture, v_TexCoord);
color = vec4(color_f, 1.0);
}
所以我在这里做的是将纹理坐标发送到顶点着色器,然后将它传递给片段着色器,在片段着色器中我计算与当前位置对应的纹理颜色,然后将该颜色发送回顶点着色器以进行计算。然而,似乎 texColor.r
始终为 0,因为网格的行为与之前相同,没有任何变化。
我通过在我的网格上实际显示纹理来检查是否一切正确,它看起来不错,但我不知道为什么这种方法不起作用。
知道我哪里出错了吗?
感谢大家的回答!
所以我应该做的就是像这样在顶点着色器中完成所有操作:
#shader vertex
#version 330 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color_in;
layout(location = 3) in vec2 texCoord;
uniform mat4 u_MVP;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform float u_Scale;
in vec4 texColor;
out vec3 FragPos;
out vec3 color_f;
void main()
{
vec4 texColor = texture(u_Texture, texCoord);
color_f = color_in;
vec3 newPosition = vec3(position.x, position.y + texColor.b * u_Scale, position.z);
FragPos = vec3(model * vec4(newPosition, 1.0));
gl_Position = projection * view * vec4(FragPos, 1.0);
};
所以我有一个平面,我希望它能与高度贴图一起使用,所以我只是将 heightMap 作为常规纹理,计算它的颜色,但我没有将片段的颜色设置为纹理的颜色,而是设置了初始 position.y
到 position.y + texColor.r * u_Scale
这样顶点的高度会根据纹理的颜色而变化。
#shader vertex
#version 330 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color_in;
layout(location = 3) in vec2 texCoord;
uniform mat4 u_MVP;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform float u_Scale;
in vec4 texColor;
out vec3 FragPos;
out vec3 color_f;
out vec2 v_TexCoord;
void main()
{
v_TexCoord = texCoord;
color_f = color_in;
vec3 newPosition = vec3(position.x, position.y + texColor.b * u_Scale, position.z);
FragPos = vec3(model * vec4(newPosition, 1.0));
gl_Position = projection * view * vec4(FragPos, 1.0);
};
#shader fragment
#version 330 core
out vec4 color;
in vec3 FragPos;
in vec2 v_TexCoord;
in flat vec3 color_f;
out vec4 texColor;
uniform sampler2D u_Texture;
void main()
{
texColor = texture(u_Texture, v_TexCoord);
color = vec4(color_f, 1.0);
}
所以我在这里做的是将纹理坐标发送到顶点着色器,然后将它传递给片段着色器,在片段着色器中我计算与当前位置对应的纹理颜色,然后将该颜色发送回顶点着色器以进行计算。然而,似乎 texColor.r
始终为 0,因为网格的行为与之前相同,没有任何变化。
我通过在我的网格上实际显示纹理来检查是否一切正确,它看起来不错,但我不知道为什么这种方法不起作用。
知道我哪里出错了吗?
感谢大家的回答!
所以我应该做的就是像这样在顶点着色器中完成所有操作:
#shader vertex
#version 330 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color_in;
layout(location = 3) in vec2 texCoord;
uniform mat4 u_MVP;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform float u_Scale;
in vec4 texColor;
out vec3 FragPos;
out vec3 color_f;
void main()
{
vec4 texColor = texture(u_Texture, texCoord);
color_f = color_in;
vec3 newPosition = vec3(position.x, position.y + texColor.b * u_Scale, position.z);
FragPos = vec3(model * vec4(newPosition, 1.0));
gl_Position = projection * view * vec4(FragPos, 1.0);
};