bloboland

annotate 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
rev   line source
nuclear@1 1 #include "level.h"
nuclear@1 2
nuclear@1 3 #include "opt.h"
nuclear@1 4
nuclear@1 5 Level::Level()
nuclear@1 6 {
nuclear@1 7 terrain = 0;
nuclear@1 8 }
nuclear@1 9
nuclear@1 10 Level::~Level()
nuclear@1 11 {
nuclear@1 12 delete terrain;
nuclear@1 13 }
nuclear@1 14
nuclear@3 15 static float terrain_func(float x, float y, float z)
nuclear@3 16 {
nuclear@3 17 float nx = (x * 2.0 - 1.0) * 0.9;
nuclear@3 18 float ny = (y * 2.0 - 1.0) * 0.9;
nuclear@3 19 float valley = nx * nx + ny * ny;
nuclear@3 20
nuclear@3 21 float s = opt.gen_noise_scale;
nuclear@3 22 float noise = 0.2 * fbm2(x * s, y * s, 3) * (valley + 0.25);
nuclear@3 23 float grad = 0.75 - z + noise + valley * 0.4;
nuclear@3 24
nuclear@3 25 return grad;
nuclear@3 26 }
nuclear@3 27
nuclear@1 28 void Level::generate()
nuclear@1 29 {
nuclear@1 30 delete terrain;
nuclear@1 31
nuclear@1 32 int xsz = opt.world_size[0];
nuclear@1 33 int ysz = opt.world_size[1];
nuclear@1 34 int zsz = opt.world_size[2];
nuclear@1 35
nuclear@1 36 terrain = new Volume(xsz, ysz, zsz);
nuclear@1 37
nuclear@1 38 for(int i=0; i<xsz; i++) {
nuclear@1 39 for(int j=0; j<ysz; j++) {
nuclear@3 40 float x = (float)i / (float)xsz;
nuclear@3 41 float y = (float)j / (float)ysz;
nuclear@3 42 //float peak = 0.4 * noise2(x, y) * 0.5 + 0.5;
nuclear@1 43
nuclear@1 44 for(int k=0; k<zsz; k++) {
nuclear@1 45 Vector4 voxel(1, 1, 1, 1);
nuclear@1 46 float z = (float)k / (float)zsz;
nuclear@1 47
nuclear@3 48 float alpha = terrain_func(x, y, z);
nuclear@3 49
nuclear@1 50 if(z < 0.1) {
nuclear@1 51 // lava
nuclear@1 52 static const Vector4 col1(0.96, 0.3, 0.1, 1);
nuclear@1 53 static const Vector4 col2(0.96, 0.75, 0.1, 1);
nuclear@3 54 float t = noise3(x, y, z) * 0.5 + 0.5;
nuclear@1 55 voxel = lerp(col1, col2, t);
nuclear@3 56 } else if(z < 0.8) {
nuclear@3 57 if(alpha < 0.56) {
nuclear@3 58 // grass
nuclear@3 59 voxel = Vector4(0.68, 0.95, 0.38);
nuclear@3 60 } else {
nuclear@3 61 // mud
nuclear@3 62 voxel = Vector4(0.57, 0.43, 0.29, 1);
nuclear@3 63 }
nuclear@1 64 } // else snow (default)
nuclear@1 65
nuclear@3 66 voxel.w = alpha;
nuclear@1 67 terrain->set_voxel(i, j, k, voxel);
nuclear@1 68 }
nuclear@1 69 }
nuclear@1 70 }
nuclear@1 71 }
nuclear@1 72
nuclear@1 73 bool Level::load(const char *fname)
nuclear@1 74 {
nuclear@1 75 return false;
nuclear@1 76 }
nuclear@1 77
nuclear@1 78 bool Level::save(const char *fname) const
nuclear@1 79 {
nuclear@1 80 return false;
nuclear@1 81 }