summaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorDmytro Dadyka <d.dadyka@samsung.com>2015-02-26 20:42:36 +0100
committerCedric BAIL <cedric@osg.samsung.com>2015-02-26 21:06:38 +0100
commitd602597ac1eec0cf357e0f47de85b6f7290b7da0 (patch)
tree703330ccea09a9ec71477f9031dc881bc93e66ca /src/examples
parent5eba6ae468e884d6e5015578fa3e37b53548c9f1 (diff)
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>
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/evas/evas-3d-primitives.c85
-rw-r--r--src/examples/evas/evas-3d-primitives.h4
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 4fefb5a..f98ecb7 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)
162 return normal; 162 return normal;
163} 163}
164 164
165static float
166_random(int x, int y)
167{
168 int k = x + y * 57;
169 k = (k << 13) ^ k;
170 return (1.0f - ((k * (k * k * 15731 + 789221) + 1376312589) & 0x7fffffff) /
171 1073741824.0f);
172}
173
174static float
175_smooth(float x, float y)
176{
177 float res;
178 res = (_random(x - 1, y - 1) + _random(x + 1, y - 1) +
179 _random(x - 1, y + 1) + _random(x + 1, y + 1) ) / 16;
180 res += (_random(x - 1, y) + _random(x + 1, y) +
181 _random(x, y - 1) + _random(x, y + 1)) / 8;
182 res += _random(x, y) / 4;
183 return res;
184}
185
186static float
187_interpolate(float a, float b, float x)
188{
189 float ft = x * M_PI;
190 float f = (1 - cosf(ft)) * 0.5;
191 return a * (1 - f) + b * f;
192}
193
194static float _noise(float x, float y)
195{
196 float ix = (int)(x);
197 float fx = x - ix;
198 float iy = (int)(y);
199 float fy = y - iy;
200
201 float v1 = _smooth(ix, iy);
202 float v2 = _smooth(ix + 1, iy);
203 float v3 = _smooth(ix, iy + 1);
204 float v4 = _smooth(ix + 1, iy + 1);
205
206 float i1 = _interpolate(v1, v2, fx);
207 float i2 = _interpolate(v3, v4, fx);
208
209 return _interpolate(i1, i2, fy);
210}
211
212static vec3
213_perlin_terrain(float x,float y)
214{
215 vec3 out;
216 float persistence = 0.5f;
217 float frequency = 5;
218 float amplitude = 1;
219 int i = 0;
220 int octaves = 5;
221
222 out.x = x;
223 x += 0.5;
224 out.y = y;
225 y += 0.5;
226 out.z = 0;
227
228 for(i = 0;i < octaves; i++)
229 {
230 out.z += _noise(x * frequency, y * frequency) * amplitude;
231
232 amplitude *= persistence;
233 frequency *= 2;
234 }
235
236 return out;
237}
238
165void 239void
166_generate_grid_indices(unsigned short *indices, int count) 240_generate_grid_indices(unsigned short *indices, int count)
167{ 241{
@@ -265,6 +339,11 @@ evas_3d_add_func_surface_frame(Eo *mesh, int frame, Surface func, int p, vec2 te
265 vertices[i + j * vccount] = func(v, u); 339 vertices[i + j * vccount] = func(v, u);
266 normals[i + j * vccount] = _get_func_normal(func, v, u); 340 normals[i + j * vccount] = _get_func_normal(func, v, u);
267 341
342 // TODO Add tangent calculation
343 tangents[i + j * vccount].x = 0;
344 tangents[i + j * vccount].y = 0;
345 tangents[i + j * vccount].z = 0;
346
268 tex_coord[i + j * vccount].x = i / (float)(vccount - 1) * tex_scale.x; 347 tex_coord[i + j * vccount].x = i / (float)(vccount - 1) * tex_scale.x;
269 tex_coord[i + j *vccount].y = tex_scale.y - j / (float)(vccount - 1) * tex_scale.y; 348 tex_coord[i + j *vccount].y = tex_scale.y - j / (float)(vccount - 1) * tex_scale.y;
270 } 349 }
@@ -275,6 +354,12 @@ evas_3d_add_func_surface_frame(Eo *mesh, int frame, Surface func, int p, vec2 te
275} 354}
276 355
277void 356void
357evas_3d_add_terrain_frame(Eo *mesh, int frame, int p, vec2 tex_scale)
358{
359 evas_3d_add_func_surface_frame(mesh, frame, _perlin_terrain, p, tex_scale);
360}
361
362void
278evas_3d_add_torus_frame(Eo *mesh, int frame, float rratio, int p, vec2 tex_scale) 363evas_3d_add_torus_frame(Eo *mesh, int frame, float rratio, int p, vec2 tex_scale)
279{ 364{
280 int vcount, icount, vccount, i, j; 365 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 3281f5d..c08ae65 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);
52void 52void
53evas_3d_add_cube_frame(Eo *mesh, int frame); 53evas_3d_add_cube_frame(Eo *mesh, int frame);
54 54
55/* Set frame as terrain generated by perlin noise algorithm*/
56void
57evas_3d_add_terrain_frame(Eo *mesh, int frame, int precision, vec2 tex_scale);
58
55#endif // EVAS_3D_PRIMITIVES_H 59#endif // EVAS_3D_PRIMITIVES_H