2017-04-09 15 views
0

은 대학 과제의 일부로 GLSL에서 Perlin 노이즈를 구현해야합니다. 나는 참고로이 슬라이드를 사용GLSL 2D Perlin 노이즈가 이상하게 보입니다.

을뿐만 아니라이 wikipedia article한다.

내 물론 나는 이런 식으로 뭔가를 얻을해야한다고 말했습니다 : 노이즈 값 I 0.5를 추가

: 내 코드를 실행하면

불행하게도 나는이를 얻을 수 얻으십시오 :

나에게 이상한 점은 보간이 한 셀에서 다른 셀 사이에서 잘 작동하지 않는다는 것입니다. 마치 보간 함수가 C2 연속성을 갖지 않는 것처럼. 보간에 사용하는 함수가이 속성을 가지고 있기 때문에 이상합니다. 이 문제가 어디서 발생했는지 알 수 있습니까?

이 내 조각 쉐이더 코드 : 이상한 일을 할 수있는 int에 의해 int에 의해

#version 330 

in vec2 uv; 

out vec3 color; 

uniform int width; 
uniform int height; 

float simple_rand(vec2 co) { 
    return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453); 
} 

vec2 rand(vec2 co) { 
    float angle = 3.141592654 * 2 * simple_rand(co); 
    return vec2(sin(angle), cos(angle)); 
} 

// mix function as described in the slides 
float lerp(float x, float y, float alpha) { 
    return (1 - alpha) * x + alpha * y; 
} 

// interpolation function as described in the slides 
float f(float t) { 
    return t*t*t * (t * (6.0*t - 15.0) + 10.0); 
} 

vec3 noise(vec2 vec) { 
    // number of pixels per cell 
    int cell_px = 50; 

    // get coordinates of current cell corners and calculate random gradients 
    // GRID coordinates 
    int cx = int(floor(vec.x * width/cell_px)); 
    int cy = int(floor(vec.y * height/cell_px)); 
    // ABSOLUTE coordinates 
    vec2 x0y0 = vec2(cx*cell_px, cy*cell_px); 
    vec2 x1y1 = vec2(x0y0.x + cell_px, x0y0.y + cell_px); 
    vec2 x0y1 = vec2(x0y0.x, x1y1.y); 
    vec2 x1y0 = vec2(x1y1.x, x0y0.y); 

    // vec translated to inner cell coordinates, relative to x0y0, must be between 0 and 1 
    vec2 cell_vec = vec2((vec.x * width - x0y0.x)/cell_px, (vec.y * height - x0y0.y)/cell_px); 

    // compute difference vectors 
    vec2 a = vec2(cell_vec); 
    vec2 b = vec2(1.0 - cell_vec.x, cell_vec.y); 
    vec2 c = vec2(cell_vec.x, 1.0 - cell_vec.y); 
    vec2 d = vec2(1.0 - cell_vec.x, 1.0 - cell_vec.y); 

    // dot products to get scalar values from the corners 
    float s = dot(rand(x0y0), a); 
    float t = dot(rand(x1y0), b); 
    float u = dot(rand(x0y1), c); 
    float v = dot(rand(x1y1), d); 

    float st_ = lerp(s, t, f(cell_vec.x)); 
    float uv_ = lerp(u, v, f(cell_vec.x)); 
    float noise = lerp(st_, uv_, f(cell_vec.y)); 

    return vec3(noise); 
} 

void main() { 
    color = noise(uv); 
} 

답변

0

vec2 cell_vec = vec2((vec.x * width - x0y0.x)/cell_px, (vec.y * height - x0y0.y)/cell_px);

나누. 먼저 cell_pxfloat으로 변환 해보세요.