diff options
author | Dmytro Dadyka <d.dadyka@samsung.com> | 2015-02-26 20:42:36 +0100 |
---|---|---|
committer | Cedric BAIL <cedric@osg.samsung.com> | 2015-02-26 21:06:38 +0100 |
commit | d602597ac1eec0cf357e0f47de85b6f7290b7da0 (patch) | |
tree | 703330ccea09a9ec71477f9031dc881bc93e66ca | |
parent | 5eba6ae468e884d6e5015578fa3e37b53548c9f1 (diff) | |
download | efl-d602597ac1eec0cf357e0f47de85b6f7290b7da0.tar.gz |
evas: Evas_3D - add terrain mesh.
Reviewers: Hermet, Oleksander, cedric
Subscribers: cedric
Differential Revision: https://phab.enlightenment.org/D2042
Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
-rw-r--r-- | src/examples/evas/evas-3d-primitives.c | 85 | ||||
-rw-r--r-- | src/examples/evas/evas-3d-primitives.h | 4 |
2 files changed, 89 insertions, 0 deletions
diff --git a/src/examples/evas/evas-3d-primitives.c b/src/examples/evas/evas-3d-primitives.c index 4fefb5ab9e..f98ecb7abb 100644 --- a/src/examples/evas/evas-3d-primitives.c +++ b/src/examples/evas/evas-3d-primitives.c @@ -162,6 +162,80 @@ vec3 _get_func_normal(Surface *func, float x, float y) return normal; } +static float +_random(int x, int y) +{ + int k = x + y * 57; + k = (k << 13) ^ k; + return (1.0f - ((k * (k * k * 15731 + 789221) + 1376312589) & 0x7fffffff) / + 1073741824.0f); +} + +static float +_smooth(float x, float y) +{ + float res; + res = (_random(x - 1, y - 1) + _random(x + 1, y - 1) + + _random(x - 1, y + 1) + _random(x + 1, y + 1) ) / 16; + res += (_random(x - 1, y) + _random(x + 1, y) + + _random(x, y - 1) + _random(x, y + 1)) / 8; + res += _random(x, y) / 4; + return res; +} + +static float +_interpolate(float a, float b, float x) +{ + float ft = x * M_PI; + float f = (1 - cosf(ft)) * 0.5; + return a * (1 - f) + b * f; +} + +static float _noise(float x, float y) +{ + float ix = (int)(x); + float fx = x - ix; + float iy = (int)(y); + float fy = y - iy; + + float v1 = _smooth(ix, iy); + float v2 = _smooth(ix + 1, iy); + float v3 = _smooth(ix, iy + 1); + float v4 = _smooth(ix + 1, iy + 1); + + float i1 = _interpolate(v1, v2, fx); + float i2 = _interpolate(v3, v4, fx); + + return _interpolate(i1, i2, fy); +} + +static vec3 +_perlin_terrain(float x,float y) +{ + vec3 out; + float persistence = 0.5f; + float frequency = 5; + float amplitude = 1; + int i = 0; + int octaves = 5; + + out.x = x; + x += 0.5; + out.y = y; + y += 0.5; + out.z = 0; + + for(i = 0;i < octaves; i++) + { + out.z += _noise(x * frequency, y * frequency) * amplitude; + + amplitude *= persistence; + frequency *= 2; + } + + return out; +} + void _generate_grid_indices(unsigned short *indices, int count) { @@ -265,6 +339,11 @@ evas_3d_add_func_surface_frame(Eo *mesh, int frame, Surface func, int p, vec2 te vertices[i + j * vccount] = func(v, u); normals[i + j * vccount] = _get_func_normal(func, v, u); + // TODO Add tangent calculation + tangents[i + j * vccount].x = 0; + tangents[i + j * vccount].y = 0; + tangents[i + j * vccount].z = 0; + tex_coord[i + j * vccount].x = i / (float)(vccount - 1) * tex_scale.x; tex_coord[i + j *vccount].y = tex_scale.y - j / (float)(vccount - 1) * tex_scale.y; } @@ -275,6 +354,12 @@ evas_3d_add_func_surface_frame(Eo *mesh, int frame, Surface func, int p, vec2 te } void +evas_3d_add_terrain_frame(Eo *mesh, int frame, int p, vec2 tex_scale) +{ + evas_3d_add_func_surface_frame(mesh, frame, _perlin_terrain, p, tex_scale); +} + +void evas_3d_add_torus_frame(Eo *mesh, int frame, float rratio, int p, vec2 tex_scale) { int vcount, icount, vccount, i, j; diff --git a/src/examples/evas/evas-3d-primitives.h b/src/examples/evas/evas-3d-primitives.h index 3281f5d81b..c08ae6555b 100644 --- a/src/examples/evas/evas-3d-primitives.h +++ b/src/examples/evas/evas-3d-primitives.h @@ -52,4 +52,8 @@ evas_3d_add_square_frame(Eo *mesh, int frame); void evas_3d_add_cube_frame(Eo *mesh, int frame); +/* Set frame as terrain generated by perlin noise algorithm*/ +void +evas_3d_add_terrain_frame(Eo *mesh, int frame, int precision, vec2 tex_scale); + #endif // EVAS_3D_PRIMITIVES_H |