GLSL从片段着色器向顶点着色器发送颜色数据似乎总是等于0

问题描述 投票:0回答:1

所以我有一个平原,我希望它能与高度图一起使用,所以我只将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,因为网格的行为与之前相同,没有任何变化。

我通过在我的网格上实际显示纹理来检查所有方法是否正确,这看起来不错,但我不知道为什么这种方法不起作用。

任何线索我在哪里弄错了?

感谢所有答案!

c++ opengl glsl shader fragment-shader
1个回答
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);
};
© www.soinside.com 2019 - 2024. All rights reserved.