2014-09-25 4 views
1

이렇게 공부 한 후 : Wikipedia on Perlin Noise, Perlin 노이즈 텍스처를 생성하는 클래스를 만들려고했습니다. 나중에 텍스처로 변환 될 Color [,]로 출력됩니다. 그러나, 일단 내가 그것을 실행하게, 모든 실행에서 나는 경계 예외를 얻는다. 다음은 수업입니다.C# XNA Perlin Noise, 범위를 벗어나는 배열

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 


namespace Noise 
{ 
class PerlinNoise : Noise 
{ 
    private float[,,] gradient; 
    private float[,] noise; 
    private bool generated; 
    private int width; 
    private int height; 

    /// <summary> 
    /// Class constructor. 
    /// </summary> 
    /// <param name="x">Width of the perlin noise</param> 
    /// <param name="y">Height of the perlin noise</param> 
    public PerlinNoise(int width, int height) 
    { 
     gradient = new float[width,height,2]; 

     Random r = new Random(); 

     for (int i = 0; i < width; i++) 
     { 
      for (int o = 0; o < height; o++) 
      { 
       for (int p = 0; p < 2; p++) 
       { 
        gradient[i, o, p] = ((float)r.NextDouble() * 2f) -1; 
       } 
      } 
     } 

     this.width = width; 
     this.height = height; 
     noise = new float[width, height]; 
     generated = false; 


    } 



    float Lerp(float a0, float a1, float w) 
    { 
     return (1.0f - w) * a0 + w * a1; 
    } 

    float DotGridGradient(int ix, int iy, float x, float y) 
    { 
     float dx = x - (float)ix; 
     float dy = y - (float)iy; 


     return (dx * gradient[iy, ix, 0]) + (dy * gradient[iy, ix, 1]); //It blows up here, usually with either iy or ix = -1 
    } 

    public float GenerateValue(float x, float y) 
    { 

     int x0 = x > 0.0f ? (int)x : (int)x - 1; 
     int x1 = x0 + 1; 
     int y0 = y > 0.0f ? (int)y : (int)y - 1; 
     int y1 = y0 + 1; 

     float sx = x - (float)x0; 
     float sy = y - (float)y0; 

     float n0, n1, ix0, ix1, value; 
     n0 = DotGridGradient(x0, y0, x, y); 
     n1 = DotGridGradient(x1, y0, x, y); 
     ix0 = Lerp(n0, n1, sx); 
     n0 = DotGridGradient(x0, y1, x, y); 
     n1 = DotGridGradient(x1, y1, x, y); 
     ix1 = Lerp(n0, n1, sx); 
     value = Lerp(ix0, ix1, sy); 

     return value; 

    } 

    public Color GenerateColor(int x, int y) 
    { 
     if (!generated) GenerateNoise(); 

     Color c = new Color(); 
     c.R = c.G = c.B = (byte)(256 * noise[x,y]); 
     return c; 
    } 

    public Color[,] GenerateTexture() 
    { 
     if (!generated) GenerateNoise(); 

     Color[,] color = new Color[width,height]; 

     for (int x = 0; x < width; x++) 
     { 

      for (int y = 0; y < height; y++) 
      { 
       color[x, y] = GenerateColor(x, y); 
      } 


     } 

     return color; 
    } 

    public void GenerateNoise() 
    { 
     for (int x =0; x < width; x++) 
      for (int y = 0; y < height; y++) 
       noise[x, y] = GenerateValue(x, y); 


     generated = true; 
    } 

} 
} 

그래서 수정하려면 어떻게해야합니까?

미리 감사드립니다.

+0

범위를 벗어나는 예외는 어디에서 발생합니까? –

+1

코드에서 주석이 발생합니다 ... 그러나 DotGridGradient 메서드의 반환 줄에 있습니다. –

+0

죄송합니다. 잠깐 살펴 보았습니다. _ _ –

답변

0

n1에서 x1x을 전달합니다. x이 0이면 x0은 -1입니다. 그런 다음 DotGridGradient 방법으로 dxx0 - x으로 설정합니다.이 경우 -1 - 0입니다. y는 동일합니다. 그게 오류의 원인입니다. for 루프에서 1로 시작하면 모든 것이 정상입니다. 나는 펄린 소음에 대해 아무것도 모른다. 그래서 나는 너를 더 멀리 도울 수 없다.

+0

그래, 그게 무슨 일이야 ... 그게 내가 이해하기 시작 했어, 펄린 노이즈 값은 0과 1, 그리고 그것의 좌표와 함께 ... 구현을 다시 써야 겠어. –