bloboland

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