我正在使用高度图进行地形生成实验,我只是写了一个简单的值噪声。我在我的代码中看不到任何东西,但除了我自己的愚蠢或发生浮点错误之外,我看不到任何东西:
#include
#include <stdlib.h>
#include <math.h>
long jenkins( long a ){
a = (a+0x7ed55d16) + (a>19);
a = (a+0x165667b1) + (a>16);
return a;
}
float random(long x, long y, long seed1, long seed2, long seed3){
long state = jenkins( x ) - jenkins(y);
state = jenkins(state - seed1);
state -= jenkins(state ^ seed2);
state -= jenkins(state - seed2);
state ^= (x*y) - jenkins( (seed1-seed2) ^ seed3 );
float ret = ((float)(state&0xFFFF))/65535.0f;
return ret;
}
float valueNoise( float x, float y, long seed1, long seed2, long seed3 ){
float fx = x-floor(x);
float fy = y-floor(y);
long flx = (long) x-fx;
long fly = (long) y-fy;
fx = fx*fx*(3 - 2*fx);
fy = fy*fy*(3 - 2*fy);
float r00 = random(flx,fly,seed1,seed2,seed3);
float r10 = random(flx+1,fly,seed1,seed2,seed3);
float r01 = random(flx,fly+1,seed1,seed2,seed3);
float r11 = random(flx+1,fly+1,seed1,seed2,seed3);
float i0 = (1-fx)*r00 + fx*r10;
float i1 = (1-fx)*r01 + fx*r11;
return ( (1-fy)*i0 + fy*i1 );
}
void writeOutPGM(char *filename, unsigned char *data){
FILE *file = fopen(filename,"w");
char header[] = "P5 512 512 255\n";
fwrite(header,sizeof(header),1,file);
fwrite(data,262144,1,file);
}
extern float valueNoise( float x, float y, long seed1, long seed2, long seed3 );
int main(){
unsigned char *data = malloc(512*512);
float fx,fy;
int x,y;
long s1 = 123,s2 = 456,s3 = 789;
for( y = 0; y < 512; ++y ){
for( x = 0; x < 512; ++x ){
fx = floor( ((float)x)/10.0f );
fy = floor( ((float)y)/10.0f );
data[x + y*512] = (unsigned char) 255.0f*valueNoise(fx,fy,s1,s2,s3);
}
}
writeOutPGM("noise.pgm",data);
}
我放了 floor() 以更清楚地显示线条,否则它们很难看到(尽管噪音仍然非常混乱)
当我不划分 x/y 时,它会输出良好的噪音。我想这要么是除法,要么是我的插值。但我真的对正在发生的事情一无所知。
感谢您的帮助,埃克林