Szahu
Szahu

Reputation: 43

GLSL Sending color data form Fragment Shader to Vertex Shader seems to be always equal to 0

So I have a plain and I want it to work with height maps so I just take heightMap as a regular texture, calculate it's color, but instead of setting color of my fragments to texture's color, I set my initial position.y to position.y + texColor.r * u_Scale so that the height of vertices will change according to color of the texture.

#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);
}

So what I did here was send in texture Coords to vertex shader, then pass it to fragment shader, in fragment shader I calculate the color of texture corresponding to current position and then I send that color back to Vertex Shader to perform calculations. However it seems that texColor.r is always 0 since the mesh behaves same as It did before with no changes.

I checked if all was correct by actually displaying texture on my mesh and it seemed fine but I don't know why this approach doesn't work.

Any clue where did I make a mistake?

Thanks for all the answers!

Upvotes: 1

Views: 785

Answers (1)

Szahu
Szahu

Reputation: 43

So what I should have done was simply doing it all in vertex shader like this:

#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);
};

Upvotes: -1

Related Questions