Однако я застрял, пытаясь сделать это нормальным. Я передаю Fragpos и Normals фрагментированному шейдеру, но когда я пытаюсь сделать нормальный, я продолжаю получать цветовой градиент вместо линии, которая представляет нормальный. < /P>
Другой вариант, который я пробовал Чтобы иметь отдельный вызов рисования только для нормального, но потом, поскольку вершины на стороне процессора все еще остаются просто плоскостью, я продолжаю получать линии, которые проходят через вершины/плоскость, не совсем то, что я хочу.
Есть ли способ нарисовать нормаль, не имея отдельного вызова рисования? код: https://github.com/shreyas9699/fbm-opengl
Код: Выделить всё
Vertex Generation partКод: Выделить всё
struct VertexWithNormal
{
glm::vec3 position;
glm::vec3 normal;
};
int main()
{
//................
int gridSize = 10; // odd number the indices is getting messed up and creating some weird triangles
float planeSize = 5.0f;
for (int z = -gridSize / 2; z = 0 после добавления смещения).
Vertex ShaderКод: Выделить всё
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
out vec3 FragPos;
out vec3 Normal;
// uniforms declared
float random(vec2 st)
{ return fract(sin(dot(st.xy, vec2(12.9898,78.233))) * 43758.5453123); }
float noise(vec2 st)
{
vec2 i = floor(st);
vec2 f = fract(st);
float a = random(i);
float b = random(i + vec2(1.0, 0.0));
float c = random(i + vec2(0.0, 1.0));
float d = random(i + vec2(1.0, 1.0));
// Hermite smoothing function
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(a, b, u.x) + (c - a)* u.y * (1.0 - u.x) + (d - b) * u.x * u.y;
}
#define OCTAVE 10
float fbm(vec2 st)
{
float value = 0.0; // accumulates the final noise value.
float amplitude = 0.8; // controls the contribution of each octave.
float frequency = 2.0; // determines how "stretched" the noise is in each layer.
for (int i = 0; i < OCTAVE; i++)
{
value += amplitude * noise(st * frequency);
st *= frequency;
amplitude *= 0.6;
}
return value;
}
void main()
{
Normal = mat3(transpose(inverse(model))) * aNormal;
vec3 modifiedPosition = aPos;
vec2 gridCoords = aPos.xz * 5.0;
float displacement = fbm(gridCoords + time * 0.2) * 0.5; // Adjust multiplier for height variation
modifiedPosition.y += displacement;
modifiedPosition.y = max(modifiedPosition.y, 0.0);
FragPos = vec3(model * vec4(modifiedPosition, 1.0));
gl_Position = projection * view * model * vec4(modifiedPosition, 1.0);
}
< /code>
Fragment shader:in vec3 Normal;
// uniforms declared
void main()
{
// ambient
float ambientStrength = 0.1;
vec3 ambient = ambientStrength * lightColor;
// diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
// specular
float specularStrength = 0.3;
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0f);
vec3 specular = specularStrength * spec * lightColor;
vec3 result = (ambient + diffuse + specular) * objectColor;
if(showNormals)
{
// Render normals as a yellow color
vec3 normalColor = vec3(1.0, 1.0, 0.0); // Yellow color
vec3 normalVisualization = normalColor * abs(norm);
result = mix(result, normalVisualization, 0.5);
}
FragColor = vec4(result, 1.0);
}
< /code>
The other method I tried was to have a separate draw call for normal, have its own shader, this is not right as the data vertex points are not same since they have been displaced in FBM shader and also its not properly transform
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
out vec3 FragPos;
out vec3 Normal;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
Normal = mat3(transpose(inverse(model))) * aNormal;
FragPos = vec3(model * vec4(aPos, 1.0));
gl_Position = projection * view * model * vec4(aPos, 1.0);
}
< /code>
#version 330 core
out vec4 FragColor;
in vec3 Normal;
in vec3 FragPos;
void main()
{
vec3 norm = normalize(Normal);
vec3 normalColor = vec3(1.0, 1.0, 0.0); // Yellow color
FragColor = vec4(normalColor * abs(norm), 1.0);
}
< /code>
// normal draw call for each vertex and I was add the
glBegin(GL_LINES);
for (const auto& vertex : vertices)
{
// send data
}
glEnd();
< /code>
Without normal enabled:
https://i.sstatic.net/DdKqDBx4.png
With normal enabled:
https://i.sstatic.net/QSW9N16n.png
Expected something like this for all vertices.
https://i.sstatic.net/4heeDS3L.png
Подробнее здесь: https://stackoverflow.com/questions/794 ... ng-vectors
Мобильная версия