2

I am trying to create a stucco texture similar to the image below in an GLSL fragment and vertex shader.

enter image description here

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..

  1. Send the noise function my current position scaled by some scalar.
  2. 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.

  1. 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.

  2. 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.

Freddy
  • 2,249
  • 1
  • 22
  • 31
  • Where are your `#version` directives? – genpfault Apr 19 '13 at 18:28
  • 1
    Added - I was lazy when I was typing them over. – Freddy Apr 19 '13 at 18:32
  • 1
    noisexy?? :-| If you can get it to look like your example photo, maybe then that variable will have earned its name. Until then, use an underscore. –  Apr 19 '13 at 18:54
  • 3
    Just a hint: This stucco looks has some very low frequency noise, and some high frequency noise. But there's almost nothing in the middle frequencies. If you're using some Perlin Noise or similar I suggest you omit the middle frequency octaves. – datenwolf Apr 20 '13 at 01:09
  • 1
    It might be easier to start with a texture as your bump map instead of using a noise function. That way you can more easily tell if the rest of your pipeline is working properly. Once you have that working, swap out your `NoiseXY()` (underscores are ugly!) method with the texture. – user1118321 Apr 20 '13 at 02:54
  • "*Calculate Tangent is a helper method to build the tangent vector based on the object norm*" The efficacy of your code is dependent on how this function works. Without a proper tangent-space basis, you're not going to be able to make tangent-space bump mapping work. – Nicol Bolas Apr 20 '13 at 10:39
  • "`noise2(MCPoint.xy * 1.65);`" Your code appears to be expecting the GLSL noise functions to actually produce noise. They probably won't. At least, I haven't heard of an implementation that actually implements them correctly. You should use a [real noise function](https://github.com/ashima/webgl-noise/wiki) that's likely to work. – Nicol Bolas Apr 20 '13 at 10:41

0 Answers0