bloboland

diff src/level.cc @ 3:a39c301cdcce

terrain raytracing pretty much done
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 16 Dec 2012 14:24:16 +0200
parents cfe68befb7cc
children 9021a906c5d3
line diff
     1.1 --- a/src/level.cc	Sun Dec 16 00:37:35 2012 +0200
     1.2 +++ b/src/level.cc	Sun Dec 16 14:24:16 2012 +0200
     1.3 @@ -12,6 +12,19 @@
     1.4  	delete terrain;
     1.5  }
     1.6  
     1.7 +static float terrain_func(float x, float y, float z)
     1.8 +{
     1.9 +	float nx = (x * 2.0 - 1.0) * 0.9;
    1.10 +	float ny = (y * 2.0 - 1.0) * 0.9;
    1.11 +	float valley = nx * nx + ny * ny;
    1.12 +
    1.13 +	float s = opt.gen_noise_scale;
    1.14 +	float noise = 0.2 * fbm2(x * s, y * s, 3) * (valley + 0.25);
    1.15 +	float grad = 0.75 - z + noise + valley * 0.4;
    1.16 +
    1.17 +	return grad;
    1.18 +}
    1.19 +
    1.20  void Level::generate()
    1.21  {
    1.22  	delete terrain;
    1.23 @@ -24,29 +37,33 @@
    1.24  
    1.25  	for(int i=0; i<xsz; i++) {
    1.26  		for(int j=0; j<ysz; j++) {
    1.27 -			float x = opt.gen_noise_scale * (float)i / (float)xsz;
    1.28 -			float y = opt.gen_noise_scale * (float)j / (float)ysz;
    1.29 -			float peak = 0.4 * noise2(x, y) * 0.5 + 0.5;
    1.30 +			float x = (float)i / (float)xsz;
    1.31 +			float y = (float)j / (float)ysz;
    1.32 +			//float peak = 0.4 * noise2(x, y) * 0.5 + 0.5;
    1.33  
    1.34  			for(int k=0; k<zsz; k++) {
    1.35  				Vector4 voxel(1, 1, 1, 1);
    1.36  				float z = (float)k / (float)zsz;
    1.37  
    1.38 +				float alpha = terrain_func(x, y, z);
    1.39 +
    1.40  				if(z < 0.1) {
    1.41  					// lava
    1.42  					static const Vector4 col1(0.96, 0.3, 0.1, 1);
    1.43  					static const Vector4 col2(0.96, 0.75, 0.1, 1);
    1.44 -					float t = noise3(x, y, z);
    1.45 +					float t = noise3(x, y, z) * 0.5 + 0.5;
    1.46  					voxel = lerp(col1, col2, t);
    1.47 -				} else if(z < peak - 0.05) {
    1.48 -					// mud
    1.49 -					voxel = Vector4(0.57, 0.43, 0.29, 1);
    1.50 -				} else if(z < 0.85) {
    1.51 -					// grass
    1.52 -					voxel = Vector4(0.68, 0.95, 0.38);
    1.53 +				} else if(z < 0.8) {
    1.54 +					if(alpha < 0.56) {
    1.55 +						// grass
    1.56 +						voxel = Vector4(0.68, 0.95, 0.38);
    1.57 +					} else {
    1.58 +						// mud
    1.59 +						voxel = Vector4(0.57, 0.43, 0.29, 1);
    1.60 +					}
    1.61  				}	// else snow (default)
    1.62  
    1.63 -				voxel.w = z < peak ? 1.0 : 0.0;
    1.64 +				voxel.w = alpha;
    1.65  				terrain->set_voxel(i, j, k, voxel);
    1.66  			}
    1.67  		}