bloboland
diff src/level.cc @ 4:9021a906c5d3
lots of stuff
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 18 Dec 2012 06:13:09 +0200 |
parents | a39c301cdcce |
children | 2f4406cc341e |
line diff
1.1 --- a/src/level.cc Sun Dec 16 14:24:16 2012 +0200 1.2 +++ b/src/level.cc Tue Dec 18 06:13:09 2012 +0200 1.3 @@ -5,6 +5,7 @@ 1.4 Level::Level() 1.5 { 1.6 terrain = 0; 1.7 + world_size = Vector3(1, 1, 1); 1.8 } 1.9 1.10 Level::~Level() 1.11 @@ -19,7 +20,7 @@ 1.12 float valley = nx * nx + ny * ny; 1.13 1.14 float s = opt.gen_noise_scale; 1.15 - float noise = 0.2 * fbm2(x * s, y * s, 3) * (valley + 0.25); 1.16 + float noise = 0.2 * fbm2(x * s, y * s, 4) * (valley + 0.25); 1.17 float grad = 0.75 - z + noise + valley * 0.4; 1.18 1.19 return grad; 1.20 @@ -56,7 +57,7 @@ 1.21 } else if(z < 0.8) { 1.22 if(alpha < 0.56) { 1.23 // grass 1.24 - voxel = Vector4(0.68, 0.95, 0.38); 1.25 + voxel = Vector4(0.49, 0.72, 0.48); 1.26 } else { 1.27 // mud 1.28 voxel = Vector4(0.57, 0.43, 0.29, 1); 1.29 @@ -68,6 +69,14 @@ 1.30 } 1.31 } 1.32 } 1.33 + 1.34 + // generate some blobs 1.35 + for(int i=0; i<opt.gen_num_blobs; i++) { 1.36 + Blob b; 1.37 + b.pos = Vector3(frand(1.0) - 0.5, 10.0, frand(1.0) - 0.5); 1.38 + b.velocity = Vector3(0, 0, 0); 1.39 + blobs.push_back(b); 1.40 + } 1.41 } 1.42 1.43 bool Level::load(const char *fname) 1.44 @@ -79,3 +88,50 @@ 1.45 { 1.46 return false; 1.47 } 1.48 + 1.49 +#define CLAMP(x, a, b) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x))) 1.50 + 1.51 +const Vector4 &Level::get_voxel(const Vector3 &pos) const 1.52 +{ 1.53 + Vector3 p = pos / world_size + Vector3(0.5, 0.5, 0.5); 1.54 + 1.55 + int xsz = terrain->get_size(0); 1.56 + int ysz = terrain->get_size(1); 1.57 + int zsz = terrain->get_size(2); 1.58 + 1.59 + int i = (int)(p.x * xsz); 1.60 + int j = (int)(p.y * ysz); 1.61 + int k = (int)(p.z * zsz); 1.62 + 1.63 + return terrain->get_voxel(CLAMP(i, 0, xsz - 1), CLAMP(j, 0, ysz - 1), CLAMP(k, 0, zsz - 1)); 1.64 +} 1.65 + 1.66 +Vector3 Level::calc_normal(const Vector3 &pos) const 1.67 +{ 1.68 + int xsz = terrain->get_size(0); 1.69 + int ysz = terrain->get_size(1); 1.70 + int zsz = terrain->get_size(2); 1.71 + 1.72 + float dx = world_size.x / (float)xsz; 1.73 + float dy = world_size.y / (float)ysz; 1.74 + float dz = world_size.z / (float)zsz; 1.75 + 1.76 + float dfdx = get_voxel(pos + Vector3(dx, 0, 0)).w - get_voxel(pos - Vector3(dx, 0, 0)).w; 1.77 + float dfdy = get_voxel(pos + Vector3(0, dy, 0)).w - get_voxel(pos - Vector3(0, dy, 0)).w; 1.78 + float dfdz = get_voxel(pos + Vector3(0, 0, dz)).w - get_voxel(pos - Vector3(0, 0, dz)).w; 1.79 + 1.80 + return Vector3(dfdx, dfdy, dfdz); 1.81 +} 1.82 + 1.83 +bool Level::collision(const Vector3 &pos0, const Vector3 &pos1, Vector3 *outpos, Vector3 *outnorm) const 1.84 +{ 1.85 + Vector4 vox0 = get_voxel(pos0); 1.86 + Vector4 vox1 = get_voxel(pos1); 1.87 + 1.88 + if(vox0.w < 0.5 && vox1.w >= 0.5) { 1.89 + *outpos = lerp(pos0, pos1, 0.5); // TODO 1.90 + *outnorm = calc_normal(*outpos); 1.91 + return true; 1.92 + } 1.93 + return false; 1.94 +}