I'm just trying to generate working normals in my simple Vertex Noise shader, and haven't been having much success.
I've been using GLSLs builtin noise3() function, and the simple 'neighbours' technique outlined by tonfilm on his blog
http://tonfilm.blogs...-in-shader.html for normal-estimation.
Just wondering if anyone can offer any advice on where I might be going wrong, or tell me whether or not this technique is suitable for use in this particular case.
Here is my vertex shader, which applies noise to the vertex position using noise3(), calculates a normal (wrongly), changes the texture coords for a simple Envmap lookup in the Fragment Shader, and sets a varying variable for basic diffuse lighting:
// 3D Noise controls
uniform vec3 Offset;
uniform float ScaleIn;
uniform float ScaleOut;
// 3D Noise function
vec4 noise3D(in vec4 vert, in vec3 gridOffset)
{
vert.xyz += noise3(gridOffset + Offset + vert.xyz * ScaleIn) * ScaleOut;
return vert;
}
// Structure to hold vertex position and normal
struct posNorm {vec4 pos;vec3 norm;};
// Calculate and return vertex position and normal
posNorm vNoise3D(in vec4 vert)
{
// Init output variable of custom type posNorm (defined above)
posNorm result;
// Calculate new vertex position using function defined above
result.pos = noise3D(vert, vec3(0.0));
// Calculate normals
float gridOffset = 0.001;
vec4 neighbourX0 = noise3D(vert, vec3(gridOffset, 0.0, 0.0));
vec4 neighbourY0 = noise3D(vert, vec3(0.0, gridOffset, 0.0));
vec3 tangent = neighbourX0.xyz - result.pos.xyz;
vec3 bitangent = neighbourY0.xyz - result.pos.xyz;
vec3 norm = cross(tangent, bitangent);
norm = normalize(norm);
norm = gl_NormalMatrix * norm;
result.norm = norm;
return result;
}
///////////////////////////
// Main Loop //
///////////////////////////
void main(void)
{
// Initial vertex position
vec4 vertex = gl_Vertex;
// get final vertex position and normals
posNorm outPut = vNoise3D(vertex);
// Call envMapVS function, passing it vertex position and normal
vec3 normal = gl_NormalMatrix * outPut.norm;
// Apply environment-map lighting function
envMapVS(vertex, normal);
gl_Position = gl_ModelViewProjectionMatrix * outPut.pos;
}
I don't think I need to post the Fragment Shader, as it's very simple, and I think it's the normal-estimation in the VS that's the problem here.
The screenshots below show the result I'm getting (with the envmap effect turned off):


As you can see, some of the Normals are clearly 'flipped'
Any advice on how I might solve this annoying issue much appreciated!
Thanks in advance,
a|x
http://machinesdontcare.wordpress.com

















