I am trying to create a stucco texture similar to the image below in an GLSL fragment and vertex shader.
I know there are multiple ways to accomplish this. However, I want to focus on perturbing the surface normal in the fragment shader based on the values I receive from the noise function.
My thought process is the following..
- Send the noise function my current position scaled by some scalar.
- Once I have my noise, I can then perturb the surface normal and use it with my Phong shading model.
However, I'm not sure where I go from there.
Here is what I have so far.
Vertex shader - Calculate Tangent is a helper method to build the tangent vector based on the object norm.
#version 120
varying vec3 LightDir;
varying vec3 EyeDir;
varying vec3 MCPoint;
main(){
MCPoint = gl_Vertex.xyz;
EyeDir = vec3(gl_ModelViewMatrix * gl_Vertex);
vec3 LightPosition = gl_LightSource[0].position.xyz;
vec3 tangent = calculateTangent(gl_Normal);
// Building the Tangent matrix
vec3 n = normalize(gl_NormalMatrix * gl_Normal);
vec3 t = normalize(gl_NormalMatrix * tangent);
vec3 b = cross(n, t);
vec3 v;
v.x = dot(LightPosition, t);
v.y = dot(LightPosition, b);
v.z = dot(LightPosition, n);
LightDir = normalize(v);
v.x = dot(gl_vertex.xyz, t);
v.y = dot(gl_Vertex.xyz, b);
v.z = dot(gl_Vertex.xyz, n);
EyeDir = normalize(v);
gl_Position = ftransform();
}
Fragment Shader - so far...
#version 120
varying vec3 LightDir;
varying vec3 EyeDir;
varying vec3 MCPoint;
main(){
vec2 _noisexy = noise2(MCPoint.xy * 1.65);
// offset normal
// Calculate phong shading based on LightDir, EyeDir, & Newly distorted Normal
gl_FragColor= intensity * vec4(my_surface_color, 1.0);
}
Now, to my actual questions.
What normal do I offset? Would I offset the object normal? I have been reading that the bump mapping should take place in object space.
What is the best way to apply the offset to the normal?
Some system info: Mac OSX - OpenGL 3.2 - Shader must be able to work on older macs that do not have the new 3.2 support.