Jump to content
  • Advertisement
Sign in to follow this  
4fingers

Bump mapping a brick wall

This topic is 3918 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to apply some bump mapping to a brick wall using fragment and vertex shaders. At the moment I managed to it kind of working but I am still not sure how you would maybe improve on it. Here is my fragment shader:
uniform vec3  BrickColor, MortarColor;
uniform vec2  BrickSize; // Size of brick
uniform vec2  BrickPct; // Percentage of brick that has no mortar

//uniform float BumpDensity;     // = 16.0

uniform float BumpSize;        // = 0.15
uniform float SpecularFactor;  // = 0.5

varying vec2  MCposition;
varying float LightIntensity;

varying vec3 LightDir;
varying vec3 EyeDir;

void main()
{
    vec3  color;
    vec2  position, useBrick, grooves;
    float groovesSq;
    position = MCposition / BrickSize;

    if (fract(position.y * 0.5) > 0.5)
        position.x += 0.5;

    position = fract(position);

    useBrick = step(position, BrickPct);
   
    color  = mix(MortarColor, BrickColor, useBrick.x * useBrick.y);
    color *= LightIntensity;
    
    grooves = smoothstep(BrickPct, 1.0, position );
    groovesSq = grooves.x * grooves.y;
    
    vec3 litColor;
    vec2 c = grooves * position;
    vec2 p = fract(c);// - vec2(0.5);

    float d, f;
    d = p.x * p.x + p.y * p.y;
    f = 1.0 / sqrt(d + 1.0);

    /*if (d >= BumpSize)
        { p = vec2(0.0); f = 1.0; }*/

    vec3 normDelta = vec3(p.x, p.y, 1.0) * f;
    litColor = color * max(dot(normDelta, LightDir), 0.0);
    
    vec3 reflectDir = reflect(LightDir, normDelta);
    float spec = max(dot(EyeDir, reflectDir), 0.0);
    spec = pow(spec, 6.0);
    spec *= SpecularFactor;
    
    litColor = min(litColor + spec, vec3(1.0));

    gl_FragColor = vec4(litColor, 1.0);
}
Here is my vertex shader if it helps:
uniform vec3 LightPosition;

const float SpecularContribution = 0.3;
const float DiffuseContribution  = 1.0 - SpecularContribution;

varying float LightIntensity;
varying vec2  MCposition;
varying vec3 LightDir;
varying vec3 EyeDir;

attribute vec3 Tangent;

void main()
{
    vec3 ecPosition = vec3(gl_ModelViewMatrix * gl_Vertex);
    vec3 tnorm      = normalize(gl_NormalMatrix * gl_Normal);
    vec3 lightVec   = normalize(LightPosition - ecPosition);
    vec3 reflectVec = reflect(-lightVec, tnorm);
    vec3 viewVec    = normalize(-ecPosition);
    float diffuse   = max(dot(lightVec, tnorm), 0.0);
    float spec      = 0.0;

    if (diffuse > 0.0)
    {
        spec = max(dot(reflectVec, viewVec), 0.0);
        spec = pow(spec, 16.0);
    }

    LightIntensity = DiffuseContribution * diffuse +
                     SpecularContribution * spec;

    MCposition     = gl_Vertex.xy;
    gl_Position    = ftransform();
    
    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(ecPosition, t);
    v.y = dot(ecPosition, b);
    v.z = dot(ecPosition, n);
    EyeDir = normalize(v);
}
Thanks again

Share this post


Link to post
Share on other sites
Advertisement
I understand that this is rather complex and not everyone is able to help but I did some more editing and this is my new fragment shader:
uniform vec3  BrickColor, MortarColor;
uniform vec2 BrickSize; // Size of brick
uniform vec2 BrickPct; // Percentage of brick that has no mor

//uniform float BumpDensity; // = 16.0

uniform float BumpSize; // = 0.15
uniform float SpecularFactor; // = 0.5

varying vec2 MCposition;
varying float LightIntensity;

varying vec3 LightDir;
varying vec3 EyeDir;

void main()
{
vec3 color;
vec2 position, useBrick, grooves;
float groovesSq;
position = MCposition / BrickSize;

if (fract(position.y * 0.5) > 0.5)
position.x += 0.5;

position = fract(position);

useBrick = step(position, BrickPct);

color = mix(MortarColor, BrickColor, useBrick.x * useBrick.y);
color *= LightIntensity;
/* Bump mupping below this point*/
grooves = smoothstep(BrickPct, vec2(1.0), position );
//groovesSq = grooves.y *grooves.x;

vec3 litColor;
vec2 c = grooves;
vec2 p = c;// - vec2(0.5);

float d, f;
d = c.x * c.x + c.y * c.y;
f = 1.0 / sqrt(d + 1.0);

if (grooves==vec2(0.0) )
{ p = vec2(0.0); f = 1.0; }

vec3 normDelta = vec3(grooves.y, grooves.x, 1.0) * f;
litColor = color * max(cross(normDelta, LightDir), 0.5);

vec3 reflectDir = reflect(LightDir, normDelta);
float spec = max(dot(EyeDir, reflectDir), 0.0);

spec = pow(spec, 6.0);
spec *= SpecularFactor;

litColor = min(litColor + spec, vec3(1.0));

gl_FragColor = vec4(litColor, 1.0);
}

This produces this output:
http://img99.imageshack.us/my.php?image=25916138bb8.jpg
But if I change this line:
vec3 normDelta = vec3(grooves.y, grooves.x, 1.0) * f;

To this:
vec3 normDelta = vec3(grooves.x, grooves.y, 1.0) * f;

I then get this result:
http://img87.imageshack.us/my.php?image=93886322mh0.jpg
If anyone is able to tell why the grooves are acting in that way when I presume I need a mixture of both images for the bump mapping to be successful.
Thanks

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!