I render my hair geometry in line segments. The problem I have is with the correct shading of the hair.
From what I've read I understood that I need the direction of the line segment and the direction to the light source. Then the dot product of these direction vectors defines how the current vertex is shaded.
My vertex data is in one array buffer and I've decided to pass to GLSL 2 pointers - one for the current vertex and one for the next vertex, so I can calculate the direction of the line segment.
glEnableVertexAttribArray(gpuProgram->attrib("vert"));
glVertexAttribPointer(gpuProgram->attrib("vert"), 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(gpuProgram->attrib("vertNext"));
glVertexAttribPointer(gpuProgram->attrib("vertNext"), 3, GL_FLOAT, GL_FALSE, 0, (void*)sizeof(Vertex));
Where Vertex is a struct with 3 floats.
My vertex shader:
#version 150
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
in vec3 vert;
in vec3 vertNext;
in vec3 color;
out vec3 fragVert;
out vec3 fragColor;
out vec3 fragVertNext;
void main(void)
{
fragColor = color;
fragVert = vert;
fragVertNext = vertNext;
gl_Position = projection * view * model * vec4(vert, 1);
}
My fragment shader:
#version 150
uniform mat4 model;
uniform vec3 lightPosition;
uniform vec4 surfaceColor;
in vec3 fragVertNext;
in vec3 fragVert;
in vec3 fragColor;
out vec4 finalColor;
void main(void)
{
vec3 lightColor = vec3(1.0, 1.0, 1.0);
vec3 vertPos1 = vec3(model * vec4(fragVert, 1));
vec3 vertPos2 = vec3(model * vec4(fragVertNext, 1));
vec3 segmentDir = normalize(vertPos1-vertPos2);
vec3 dirToLight = normalize(lightPosition-vertPos1);
float diffCoef = 1.0 - dot(segmentDir, dirToLight);
vec3 diffuse = ambient + lightColor * diffCoef * fragColor;
vec3 result = min(diffuse, vec3(1.0));
finalColor = vec4(result, 1);
}
This is just the diffuse shading without specular reflection. And the result is wrong. I have to mention that 1 hair strand is composed of a couple line segments, and also there seems to be bug in the shading of the very last line segment - it appears black.