Question

I'm trying to implement Sketchy Drawings. I'm at the part of the process which calls for the use of the noise texture to derive uncertainty values that will provide an offset into the edge map.

Here is a picture of my edge map for a torus:

enter image description here

And here is the noise texture I've gotten using the Perlin function as suggested:

enter image description here

I have these saved as textures in edgeTexture and noiseTexture respectively.

Now I'm stuck on the section where you have to offset the texture coordinates of the edge map by uncertainty values derived from the noise texture. This image is from the book:

enter image description here

offs = turbulence(s, t); 
offt = turbulence(1 - s, 1 - t);

I'm ignoring the 2x2 matrix for the time being. Here is my current fragment shader attempt and the result it produces:

#version 330

out vec4 vFragColor;

uniform sampler2D edgeTexture;
uniform sampler2D noiseTexture;

smooth in vec2 vTexCoords;

float turbulence(float s, float t) 
{
    float sum = 0;
    float scale = 1;
    float s1 = 1;
    vec2 coords = vec2(s,t);

    for (int i=0; i < 10; i++)
    {
        vec4 noise = texture(noiseTexture, 0.25 * s1 * coords);
        sum += scale * noise.x;
        scale = scale / 2;
        s1 = s1 * 2;
    }

    return sum;
}

void main( void )
{
    float off_s = turbulence(vTexCoords.s, vTexCoords.t);
    float off_t = turbulence(1 - vTexCoords.s, 1 - vTexCoords.t);

    vFragColor = texture(edgeTexture, vTexCoords + vec2(off_s, off_t));
}

enter image description here

Clearly my addition to the vTexCoords is way off, but I can't see why. I have tried several other turbulence function definitions but none were close to the desired output so I'm thinking my overall approach is flawed somewhere. Any help here is greatly appreciated, and please comment if I haven't been clear. The desired output for a torus would just look like a roughly drawn circle I would imagine.

Was it helpful?

Solution

Your turbulence function will return values in the range (0,1). Firstly you need to change this to get values centered on 0. This should be done inside the loop in the function or you'll end up with a strange distribution. So firstly, I think you should change the line:

vec4 noise = texture(noiseTexture, 0.25 * s1 * coords);

to

vec4 noise = texture(noiseTexture, 0.25 * s1 * coords) * 2.0 - 1.0;

You then need to scale the offset so that you're not sampling the edge texture too far away from the fragment being drawn. Change:

vFragColor = texture(edgeTexture, vTexCoords + vec2(off_s, off_t));

to

vFragColor = texture(edgeTexture, vTexCoords + vec2(off_s, off_t) * off_scale);

where off_scale is some small value (perhaps around 0.05) chosen by experimentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top