summaryrefslogtreecommitdiff
path: root/src/examples/evas/evas-3d-moon-space.c
blob: 928245cc3e15ac73c1e3644c02832f138a05ed9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/*
* This example illustrates the work of different callbacks of events from mouse
* and keyboard.
*
* Next tests are available:
* 1. All animations of scene will stop on mouse click on the background.
* 2. Clicking of any object on scene causes stop of its movement, but if click on
* the moon while it is in front of a planet will stop them both.
* 3. Camera position can be changed by pressing 'down' or 'up' key. Position of
* the camera changes in the respective direction.
* 4. The 'n' key pressing returns camera to its default position.
* 5. One of four types of vertex assembly of planet could be chosen by clicking on
* a number from 1 to 4 on keyboard.

* Compile with:
* gcc -o evas-3d-moon-space evas-3d-moon-space.c -g `pkg-config --libs --cflags evas ecore ecore-evas eo` -lm
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define PACKAGE_EXAMPLES_DIR "."
#define EFL_EO_API_SUPPORT
#define EFL_BETA_API_SUPPORT
#endif

#include <Ecore_Evas.h>
#include <Ecore.h>
#include <Evas.h>
#include <stdio.h>
#include <math.h>
#include <Eo.h>
#include "evas-common.h"

#define  WIDTH          1024
#define  HEIGHT         1024

static const char *bg_image = PACKAGE_EXAMPLES_DIR EVAS_IMAGE_FOLDER "/bg_space.jpg";
static const char *moon_image = PACKAGE_EXAMPLES_DIR EVAS_IMAGE_FOLDER "/moon.png";
static const char *earth_image = PACKAGE_EXAMPLES_DIR EVAS_IMAGE_FOLDER "/EarthDiffuse.png";

typedef struct _Scene_Data
{
   Eo   *texture_diffuse_moon;
   Eo   *texture_diffuse_planet;
   Eo   *root_node;
   Eo   *camera_node;
   Eo   *light_node;
   Eo   *mesh_node_cube;
   Eo   *mesh_node_planet;
   Eo   *mesh_node_moon;

   Eo   *scene;
   Eo   *camera;
   Eo   *light;
   Eo   *mesh_cube;
   Eo   *mesh_planet;
   Eo   *mesh_moon;
   Eo   *material_planet;
   Eo   *material_moon;
   Eo   *material_cube;
} Scene_Data;

Evas_3D_Vertex_Assembly assembly    = EVAS_3D_VERTEX_ASSEMBLY_LINES;
Ecore_Evas       *ecore_evas        = NULL;
Evas             *evas              = NULL;
Eo               *background        = NULL;
Eo               *image             = NULL;
float            d_angle_planet     = 0.1;
float            d_angle_moon       = 0.4;
float            d_angle_cube       = 0.5;
float            camera_position    = 15.0;
int              d_i                = 1;

static const float cube_vertices[] =
{
   /* Front */
   -1.0,  1.0,  1.0,     0.0,  0.0,  1.0,     1.0, 0.0, 0.0, 1.0,     0.0,  1.0,
    1.0,  1.0,  1.0,     0.0,  0.0,  1.0,     1.0, 0.0, 0.0, 1.0,     1.0,  1.0,
   -1.0, -1.0,  1.0,     0.0,  0.0,  1.0,     1.0, 0.0, 0.0, 1.0,     0.0,  0.0,
    1.0, -1.0,  1.0,     0.0,  0.0,  1.0,     1.0, 0.0, 0.0, 1.0,     1.0,  0.0,

   /* Back */
    1.0,  1.0, -1.0,     0.0,  0.0, -1.0,     0.0, 0.0, 1.0, 1.0,     0.0,  1.0,
   -1.0,  1.0, -1.0,     0.0,  0.0, -1.0,     0.0, 0.0, 1.0, 1.0,     1.0,  1.0,
    1.0, -1.0, -1.0,     0.0,  0.0, -1.0,     0.0, 0.0, 1.0, 1.0,     0.0,  0.0,
   -1.0, -1.0, -1.0,     0.0,  0.0, -1.0,     0.0, 0.0, 1.0, 1.0,     1.0,  0.0,

   /* Left */
   -1.0,  1.0, -1.0,    -1.0,  0.0,  0.0,     0.0, 1.0, 0.0, 1.0,     0.0,  1.0,
   -1.0,  1.0,  1.0,    -1.0,  0.0,  0.0,     0.0, 1.0, 0.0, 1.0,     1.0,  1.0,
   -1.0, -1.0, -1.0,    -1.0,  0.0,  0.0,     0.0, 1.0, 0.0, 1.0,     0.0,  0.0,
   -1.0, -1.0,  1.0,    -1.0,  0.0,  0.0,     0.0, 1.0, 0.0, 1.0,     1.0,  0.0,

   /* Right */
    1.0,  1.0,  1.0,     1.0,  0.0,  0.0,     1.0, 1.0, 0.0, 1.0,     0.0,  1.0,
    1.0,  1.0, -1.0,     1.0,  0.0,  0.0,     1.0, 1.0, 0.0, 1.0,     1.0,  1.0,
    1.0, -1.0,  1.0,     1.0,  0.0,  0.0,     1.0, 1.0, 0.0, 1.0,     0.0,  0.0,
    1.0, -1.0, -1.0,     1.0,  0.0,  0.0,     1.0, 1.0, 0.0, 1.0,     1.0,  0.0,

   /* Top */
   -1.0,  1.0, -1.0,     0.0,  1.0,  0.0,     1.0, 0.0, 1.0, 1.0,     0.0,  1.0,
    1.0,  1.0, -1.0,     0.0,  1.0,  0.0,     1.0, 0.0, 1.0, 1.0,     1.0,  1.0,
   -1.0,  1.0,  1.0,     0.0,  1.0,  0.0,     1.0, 0.0, 1.0, 1.0,     0.0,  0.0,
    1.0,  1.0,  1.0,     0.0,  1.0,  0.0,     1.0, 0.0, 1.0, 1.0,     1.0,  0.0,

   /* Bottom */
    1.0, -1.0, -1.0,     0.0, -1.0,  0.0,     0.0, 1.0, 1.0, 1.0,     0.0,  1.0,
   -1.0, -1.0, -1.0,     0.0, -1.0,  0.0,     0.0, 1.0, 1.0, 1.0,     1.0,  1.0,
    1.0, -1.0,  1.0,     0.0, -1.0,  0.0,     0.0, 1.0, 1.0, 1.0,     0.0,  0.0,
   -1.0, -1.0,  1.0,     0.0, -1.0,  0.0,     0.0, 1.0, 1.0, 1.0,     1.0,  0.0,
};

static const unsigned short cube_indices[] =
{
   /* Front */
   0,   1,  2,  2,  1,  3,

   /* Back */
   4,   5,  6,  6,  5,  7,

   /* Left */
   8,   9, 10, 10,  9, 11,

   /* Right */
   12, 13, 14, 14, 13, 15,

   /* Top */
   16, 17, 18, 18, 17, 19,

   /* Bottom */
   20, 21, 22, 22, 21, 23
};

typedef struct _vec4
{
   float   x;
   float   y;
   float   z;
   float   w;
} vec4;

typedef struct _vec3
{
   float   x;
   float   y;
   float   z;
} vec3;

typedef struct _vec2
{
   float   x;
   float   y;
} vec2;

typedef struct _vertex
{
   vec3    position;
   vec3    normal;
   vec3    tangent;
   vec4    color;
   vec3    texcoord;
} vertex;

static int vertex_count = 0;
static vertex *vertices = NULL;
static int index_count = 0;
static unsigned short *indices = NULL;

static inline vec3
_normalize(const vec3 *v)
{
   double l;
   vec3 vec;

   l = sqrt((v->x * v->x) + (v->y * v->y) + (v->z * v->z));

   if (l != 0)
     {
        vec.x = v->x / l;
        vec.y = v->y / l;
        vec.z = v->z / l;
     }

   return vec;
}

static void
_sphere_fini()
{
   free(vertices);
   free(indices);
}

static void
_sphere_init(int precision)
{
   int i, j;
   unsigned short *index;

   vertex_count = (precision + 1) * (precision + 1);
   index_count = precision * precision * 6;

   /* Allocate buffer. */
   vertices = malloc(sizeof(vertex) * vertex_count);
   indices = malloc(sizeof(unsigned short) * index_count);

   /* Calculate vertices position of the sphere mesh by using
      splitting of sphere by latitude and longitude. */
   for (i = 0; i <= precision; i++)
     {
        double lati, y, r;

        lati = (M_PI * (double)i) / (double)precision;
        y = cos(lati);
        r = fabs(sin(lati));

        for (j = 0; j <= precision; j++)
          {
             double longi;
             vertex *v;

             longi = (M_PI * 2.0 * j) / precision;
             v = &vertices[(i * (precision  + 1)) + j];

             if ((j == 0) || (j == precision)) v->position.x = 0.0;
             else v->position.x = r * sin(longi);

             v->position.y = y;

             if ((j == 0) || (j == precision)) v->position.z = r;
             else v->position.z = r * cos(longi);

             v->normal = v->position;

             if (v->position.x > 0.0)
               {
                  v->tangent.x = -v->normal.y;
                  v->tangent.y =  v->normal.x;
                  v->tangent.z =  v->normal.z;
               }
             else
               {
                  v->tangent.x =  v->normal.y;
                  v->tangent.y = -v->normal.x;
                  v->tangent.z =  v->normal.z;
               }

             v->color.x = v->position.x;
             v->color.y = v->position.y;
             v->color.z = v->position.z;
             v->color.w = 1.0;

             if (j == precision) v->texcoord.x = 1.0;
             else if (j == 0) v->texcoord.x = 0.0;
             else v->texcoord.x = (double)j / (double)precision;

             if (i == precision) v->texcoord.y = 1.0;
             else if (i == 0) v->texcoord.y = 0.0;
             else v->texcoord.y = 1.0 - ((double)i / (double)precision);
          }
     }

   index = &indices[0];

   /* Calculate and fill in the buffer of indices,
      prepare stage for gl triangulation. */
   for (i = 0; i < precision; i++)
     {
        for (j = 0; j < precision; j++)
          {
             *index++ = (i * (precision + 1)) + j;
             *index++ = (i * (precision + 1)) + j + 1;
             *index++ = ((i + 1) * (precision + 1)) + j;

             *index++ = ((i + 1) * (precision + 1)) + j;
             *index++ = (i * (precision + 1)) + j + 1;
             *index++ = ((i + 1) * (precision + 1)) + j + 1;
          }
     }

   /* Triangulation of sphere mesh in appliance with buffer of indices. */
   for (i = 0; i < index_count; i += 3)
     {
        vec3 e1, e2;
        float du1, du2, dv1, dv2, f;
        vec3 tangent;

        vertex *v0 = &vertices[indices[i + 0]];
        vertex *v1 = &vertices[indices[i + 1]];
        vertex *v2 = &vertices[indices[i + 2]];

        e1.x = v1->position.x - v0->position.x;
        e1.y = v1->position.y - v0->position.y;
        e1.z = v1->position.z - v0->position.z;

        e2.x = v2->position.x - v0->position.x;
        e2.y = v2->position.y - v0->position.y;
        e2.z = v2->position.z - v0->position.z;

        du1 = v1->texcoord.x - v0->texcoord.x;
        dv1 = v1->texcoord.y - v0->texcoord.y;

        du2 = v2->texcoord.x - v0->texcoord.x;
        dv2 = v2->texcoord.y - v0->texcoord.y;

        f = 1.0 / ((du1 * dv2) - (du2 * dv1));

        tangent.x = f * ((dv2 * e1.x) - (dv1 * e2.x));
        tangent.y = f * ((dv2 * e1.y) - (dv1 * e2.y));
        tangent.z = f * ((dv2 * e1.z) - (dv1 * e2.z));

        v0->tangent = tangent;
     }

   /* Coupling between vertices by calculation of tangent parametr correct value. */
   for (i = 0; i <= precision; i++)
     {
        for (j = 0; j <= precision; j++)
          {
             if (j == precision)
               {
                  vertex *v;
                  v = &vertices[(i * (precision  + 1)) + j];
                  v->tangent = vertices[i * (precision + 1)].tangent;
               }
          }
     }
}

static void
_on_delete(Ecore_Evas *ee EINA_UNUSED)
{
   ecore_main_loop_quit();
}

static void
_on_canvas_resize(Ecore_Evas *ee)
{
   int w, h;

   ecore_evas_geometry_get(ee, NULL, NULL, &w, &h);

   evas_object_resize(background, w, h);
   evas_object_resize(image, w, h);
}

static void
_key_down(void *data EINA_UNUSED,
         Evas *e EINA_UNUSED,
         Evas_Object *eo EINA_UNUSED,
         void *event_info)
{
   Evas_Event_Key_Down *ev = event_info;
   if (!strcmp(ev->key, "Up"))
     {
        if (camera_position < 99.0)
        camera_position += 0.5;
     }
   else if (!strcmp(ev->key, "Down"))
     {
        if (camera_position > 5.0)
          camera_position -= 0.5;
     }
   else if (!strcmp(ev->key, "n"))
     camera_position = 15.0;
   else if (!strcmp(ev->key, "1"))
     {
        assembly = EVAS_3D_VERTEX_ASSEMBLY_LINES;
        printf("Vertex assembly = EVAS_3D_VERTEX_ASSEMBLY_LINES\n");
     }
   else if (!strcmp(ev->key, "2"))
     {
        assembly = EVAS_3D_VERTEX_ASSEMBLY_LINE_STRIP;
        printf("Vertex assembly = EVAS_3D_VERTEX_ASSEMBLY_LINE_STRIP\n");
     }
   else if (!strcmp(ev->key, "3"))
     {
        assembly = EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES;
        printf("Vertex assembly = EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES\n");
     }
   else if (!strcmp(ev->key, "4"))
     {
        assembly = EVAS_3D_VERTEX_ASSEMBLY_TRIANGLE_FAN;
        printf("Vertex assembly = EVAS_3D_VERTEX_ASSEMBLY_TRIANGLE_FAN\n");
     }
}

static void
_camera_setup(Scene_Data *data)
{
   data->camera = eo_add(EVAS_3D_CAMERA_CLASS, evas);
   eo_do(data->camera,
         evas_3d_camera_projection_perspective_set(30.0, 1.0, 1.0, 100.0));

   data->camera_node = eo_add(EVAS_3D_NODE_CLASS, evas,
                                     evas_3d_node_constructor(EVAS_3D_NODE_TYPE_CAMERA));
   eo_do(data->camera_node,
         evas_3d_node_camera_set(data->camera),
         evas_3d_node_position_set(0.0, 0.0, 15.0),
         evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0, EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0));

   eo_do(data->root_node, evas_3d_node_member_add(data->camera_node));
}

static void
_light_setup(Scene_Data *data)
{
   data->light = eo_add(EVAS_3D_LIGHT_CLASS, evas);

   eo_do(data->light,
         evas_3d_light_ambient_set(0.2, 0.2, 0.2, 1.0),
         evas_3d_light_diffuse_set(1.0, 1.0, 1.0, 1.0),
         evas_3d_light_specular_set(1.0, 1.0, 1.0, 1.0));

   data->light_node = eo_add(EVAS_3D_NODE_CLASS, evas,
                                    evas_3d_node_constructor(EVAS_3D_NODE_TYPE_LIGHT));
   eo_do(data->light_node,
         evas_3d_node_light_set(data->light),
         evas_3d_node_position_set(0.0, 0.0, 10.0),
         evas_3d_node_look_at_set(EVAS_3D_SPACE_PARENT, 0.0, 0.0, 0.0, EVAS_3D_SPACE_PARENT, 0.0, 1.0, 0.0));
   eo_do(data->root_node, evas_3d_node_member_add(data->light_node));
}

static void
_mesh_setup(Scene_Data *data)
{
   _sphere_init(100);

   data->mesh_planet = eo_add(EVAS_3D_MESH_CLASS, evas);
   data->mesh_cube = eo_add(EVAS_3D_MESH_CLASS, evas);
   data->mesh_moon = eo_add(EVAS_3D_MESH_CLASS, evas);

   data->material_planet = eo_add(EVAS_3D_MATERIAL_CLASS, evas);
   data->material_cube = eo_add(EVAS_3D_MATERIAL_CLASS, evas);
   data->material_moon = eo_add(EVAS_3D_MATERIAL_CLASS, evas);

   data->texture_diffuse_planet = eo_add(EVAS_3D_TEXTURE_CLASS, evas);
   data->texture_diffuse_moon = eo_add(EVAS_3D_TEXTURE_CLASS, evas);

   data->mesh_node_planet = eo_add(EVAS_3D_NODE_CLASS, evas,
                                    evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH));
   data->mesh_node_cube = eo_add(EVAS_3D_NODE_CLASS, evas,
                                    evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH));
   data->mesh_node_moon = eo_add(EVAS_3D_NODE_CLASS, evas,
                                    evas_3d_node_constructor(EVAS_3D_NODE_TYPE_MESH));

   /* Setup material for cube. */
   eo_do(data->material_cube,
         evas_3d_material_enable_set(EVAS_3D_MATERIAL_AMBIENT, EINA_TRUE),
         evas_3d_material_enable_set(EVAS_3D_MATERIAL_DIFFUSE, EINA_TRUE),
         evas_3d_material_enable_set(EVAS_3D_MATERIAL_SPECULAR, EINA_TRUE),
         evas_3d_material_color_set(EVAS_3D_MATERIAL_AMBIENT, 0.2, 0.2, 0.2, 1.0),
         evas_3d_material_color_set(EVAS_3D_MATERIAL_DIFFUSE, 0.8, 0.8, 0.8, 1.0),
         evas_3d_material_color_set(EVAS_3D_MATERIAL_SPECULAR, 1.0, 1.0, 1.0, 1.0),
         evas_3d_material_shininess_set(70.0));

   /* Setup material and texture for planet. */
   eo_do(data->texture_diffuse_planet,
         evas_3d_texture_file_set(earth_image, NULL),
         evas_3d_texture_filter_set(EVAS_3D_TEXTURE_FILTER_LINEAR, EVAS_3D_TEXTURE_FILTER_LINEAR));

   eo_do(data->material_planet,
         evas_3d_material_texture_set(EVAS_3D_MATERIAL_DIFFUSE, data->texture_diffuse_planet),
         evas_3d_material_enable_set(EVAS_3D_MATERIAL_AMBIENT, EINA_TRUE),
         evas_3d_material_enable_set(EVAS_3D_MATERIAL_DIFFUSE, EINA_TRUE),
         evas_3d_material_enable_set(EVAS_3D_MATERIAL_SPECULAR, EINA_TRUE),
         evas_3d_material_color_set(EVAS_3D_MATERIAL_AMBIENT, 0.01, 0.01, 0.01, 1.0),
         evas_3d_material_color_set(EVAS_3D_MATERIAL_DIFFUSE, 1.0, 1.0, 1.0, 1.0),
         evas_3d_material_color_set(EVAS_3D_MATERIAL_SPECULAR, 1.0, 1.0, 1.0, 1.0),
         evas_3d_material_shininess_set(50.0));

   /* Setup material and texture for moon. */
   eo_do(data->texture_diffuse_moon,
         evas_3d_texture_file_set(moon_image, NULL),
         evas_3d_texture_filter_set(EVAS_3D_TEXTURE_FILTER_LINEAR, EVAS_3D_TEXTURE_FILTER_LINEAR));

   eo_do(data->material_moon,
         evas_3d_material_texture_set(EVAS_3D_MATERIAL_DIFFUSE, data->texture_diffuse_moon),
         evas_3d_material_enable_set(EVAS_3D_MATERIAL_AMBIENT, EINA_TRUE),
         evas_3d_material_enable_set(EVAS_3D_MATERIAL_DIFFUSE, EINA_TRUE),
         evas_3d_material_enable_set(EVAS_3D_MATERIAL_SPECULAR, EINA_TRUE),
         evas_3d_material_color_set(EVAS_3D_MATERIAL_AMBIENT, 0.01, 0.01, 0.01, 1.0),
         evas_3d_material_color_set(EVAS_3D_MATERIAL_DIFFUSE, 1.0, 1.0, 1.0, 1.0),
         evas_3d_material_color_set(EVAS_3D_MATERIAL_SPECULAR, 1.0, 1.0, 1.0, 1.0),
         evas_3d_material_shininess_set(50.0));

   /* Setup mesh for moon. */
   eo_do(data->mesh_moon,
         evas_3d_mesh_vertex_count_set(vertex_count),
         evas_3d_mesh_frame_add(0),
         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_POSITION,
                  sizeof(vertex), &vertices[0].position),
         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_NORMAL,
                  sizeof(vertex), &vertices[0].normal),
         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_TANGENT,
                  sizeof(vertex), &vertices[0].tangent),
         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_COLOR,
                  sizeof(vertex), &vertices[0].color),
         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_TEXCOORD,
                  sizeof(vertex), &vertices[0].texcoord),
         evas_3d_mesh_index_data_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT, index_count, &indices[0]),
         evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES),
         evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_DIFFUSE),
         evas_3d_mesh_frame_material_set(0, data->material_moon));

   eo_do(data->root_node,
         evas_3d_node_member_add(data->mesh_node_moon));

   eo_do(data->mesh_node_moon,
         evas_3d_node_mesh_add(data->mesh_moon));

   eo_do(data->mesh_node_moon, evas_3d_node_scale_set(0.2, 0.2, 0.2));

   /* Setup mesh for cube. */
   eo_do(data->mesh_cube,
         evas_3d_mesh_vertex_count_set(24),
         evas_3d_mesh_frame_add(0),
         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_POSITION,
                              12 * sizeof(float), &cube_vertices[ 0]),
         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_NORMAL,
                              12 * sizeof(float), &cube_vertices[ 3]),
         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_COLOR,
                              12 * sizeof(float), &cube_vertices[ 6]),
         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_TEXCOORD,
                              12 * sizeof(float), &cube_vertices[10]),
         evas_3d_mesh_index_data_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT,
                              36, &cube_indices[0]),
         evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_TRIANGLES),
         evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_PHONG),
         evas_3d_mesh_frame_material_set(0, data->material_cube));

   eo_do(data->root_node,
         evas_3d_node_member_add(data->mesh_node_cube));

   eo_do(data->mesh_node_cube,
         evas_3d_node_mesh_add(data->mesh_cube));

   eo_do(data->mesh_node_cube,
         evas_3d_node_scale_set(0.55, 0.55, 0.55));

   /* Setup mesh for planet. */
   eo_do(data->mesh_planet,
         evas_3d_mesh_vertex_count_set(vertex_count),
         evas_3d_mesh_frame_add(0),
         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_POSITION,
                  sizeof(vertex), &vertices[0].position),
         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_NORMAL,
                  sizeof(vertex), &vertices[0].normal),
         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_TANGENT,
                  sizeof(vertex), &vertices[0].tangent),
         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_COLOR,
                  sizeof(vertex), &vertices[0].color),
         evas_3d_mesh_frame_vertex_data_set(0, EVAS_3D_VERTEX_TEXCOORD,
                  sizeof(vertex), &vertices[0].texcoord),
         evas_3d_mesh_index_data_set(EVAS_3D_INDEX_FORMAT_UNSIGNED_SHORT, index_count, &indices[0]),
         evas_3d_mesh_vertex_assembly_set(EVAS_3D_VERTEX_ASSEMBLY_LINES),
         evas_3d_mesh_shade_mode_set(EVAS_3D_SHADE_MODE_DIFFUSE),
         evas_3d_mesh_frame_material_set(0, data->material_planet));

   eo_do(data->root_node,
         evas_3d_node_member_add(data->mesh_node_planet));

   eo_do(data->mesh_node_planet,
         evas_3d_node_mesh_add(data->mesh_planet));
}

static void
_scene_setup(Scene_Data *data)
{
   data->scene = eo_add(EVAS_3D_SCENE_CLASS, evas);

   /* Add the root node for the scene. */
   data->root_node = eo_add(EVAS_3D_NODE_CLASS, evas,
                             evas_3d_node_constructor(EVAS_3D_NODE_TYPE_NODE));

   eo_do(data->scene,
         evas_3d_scene_size_set(WIDTH, HEIGHT),
         evas_3d_scene_background_color_set(0.0, 0.0, 0.0, 0.0));

   _camera_setup(data);
   _light_setup(data);
   _mesh_setup(data);

   eo_do(data->scene,
         evas_3d_scene_root_node_set(data->root_node),
         evas_3d_scene_camera_node_set(data->camera_node));
}

static void
_stop_scene(void *data,
            Evas *e EINA_UNUSED,
            Evas_Object *eo EINA_UNUSED,
            void *event_info)
{
   Evas_Event_Mouse_Down *ev = event_info;
   Evas_3D_Node *n;
   Evas_3D_Mesh *m;
   Evas_Real s, t;
   Scene_Data *d = (Scene_Data *)data;
   Evas_3D_Node *pick;
   const Eina_List *nodes = NULL;
   Eina_List *picked_nodes = NULL;
   int root_count = 0, picked_count = 0;

   eo_do(d->scene,
         evas_3d_scene_pick(ev->canvas.x, ev->canvas.y, &n, &m, &s, &t));

   if (d->mesh_cube == m)
     d_angle_cube = 0;
   else if (d->mesh_moon == m)
     {
        d_angle_moon = 0;
        d_i = 0;
     }
   else if (d->mesh_planet == m)
     d_angle_planet = 0;
   else
     {
        d_angle_planet = 0;
        d_angle_cube = 0;
        d_angle_moon = 0;
        d_i = 0;
     }

   pick = eo_do(d->scene,
                evas_3d_scene_exist(ev->canvas.x,  ev->canvas.y, d->mesh_node_planet));

   if (pick) d_angle_planet = 0;

   nodes = eo_do(d->root_node, evas_3d_node_member_list_get());
   picked_nodes = eo_do(d->scene, evas_3d_scene_pick_member_list_get(ev->canvas.x, ev->canvas.y));

   root_count = eina_list_count(nodes);
   picked_count = eina_list_count(picked_nodes);

   printf("Count of members in the root node = %d,  count of the picked members = %d\n", root_count, picked_count);
}

static void
_play_scene(void *data EINA_UNUSED,
            Evas *e EINA_UNUSED,
            Evas_Object *eo EINA_UNUSED,
            void *event_info EINA_UNUSED)
{
   d_angle_planet = 0.1;
   d_angle_cube = 0.5;
   d_angle_moon = 0.4;
   d_i = 1;
}

static Eina_Bool
_animate_planet(void *data)
{
   static float angle = 0.0f;
   Scene_Data *scene = (Scene_Data *)data;

   angle += d_angle_planet;

   eo_do(scene->mesh_node_planet,
         evas_3d_node_orientation_angle_axis_set(angle, 0.0, 1.0, 0.0));
   eo_do(scene->mesh_planet,
         evas_3d_mesh_vertex_assembly_set(assembly));

   /* Rotate */
   if (angle > 360.0) angle -= 360.0f;

   return EINA_TRUE;
}

static Eina_Bool
_animate_moon(void *data)
{
   static float angle = 0.0f;
   static float longi = 0.0f;
   static int i = 0;
   Scene_Data *scene = (Scene_Data *)data;

   i += d_i;
   longi = (2 * M_PI * i)/2000;
   angle += d_angle_moon;

   eo_do(scene->mesh_node_moon,
         evas_3d_node_orientation_angle_axis_set(angle, 0.0, 1.0, 0.0),
         evas_3d_node_position_set(2.0 * sin(longi), 0.7 * sin(longi), 2.0 * cos(longi)));

   /* Rotate */
   if (angle > 360.0) angle -= 360.0f;

   return EINA_TRUE;
}

static Eina_Bool
_animate_cube(void *data)
{
   static float angle = 0.0f;
   Scene_Data *scene = (Scene_Data *)data;

   angle += d_angle_cube;

   eo_do(scene->mesh_node_cube,
         evas_3d_node_orientation_angle_axis_set(angle, 1.0, 0.0, 1.0));

   /* Rotate */
   if (angle > 360.0) angle -= 360.0f;

   return EINA_TRUE;
}

static Eina_Bool
_animate_camera(void *data)
{
   Scene_Data *scene = (Scene_Data *)data;

   eo_do(scene->camera_node,
         evas_3d_node_position_set(0.0, 0.0, camera_position));

   return EINA_TRUE;
}

int
main(void)
{
   Scene_Data data;

   //Unless Evas 3D supports Software renderer, we set gl backened forcely.
   setenv("ECORE_EVAS_ENGINE", "opengl_x11", 1);

   if (!ecore_evas_init()) return 0;

   ecore_evas = ecore_evas_new(NULL, 10, 10, WIDTH, HEIGHT, NULL);

   if (!ecore_evas) return 0;

   ecore_evas_callback_delete_request_set(ecore_evas, _on_delete);
   ecore_evas_callback_resize_set(ecore_evas, _on_canvas_resize);
   ecore_evas_show(ecore_evas);

   evas = ecore_evas_get(ecore_evas);

   _scene_setup(&data);

   /* Add evas objects. */
   background = evas_object_image_filled_add(evas);
   evas_object_image_file_set(background, bg_image, NULL),
   evas_object_resize(background, WIDTH, HEIGHT),
   evas_object_show(background);

   image = evas_object_image_filled_add(evas);
   evas_object_resize(image, WIDTH, HEIGHT),
   evas_object_show(image),
   evas_object_focus_set(image, EINA_TRUE);

   eo_do(image,
         evas_obj_image_scene_set(data.scene));

   evas_object_event_callback_add(image, EVAS_CALLBACK_MOUSE_DOWN, _stop_scene, &data);
   evas_object_event_callback_add(image, EVAS_CALLBACK_MOUSE_UP, _play_scene, &data);
   evas_object_event_callback_add(image, EVAS_CALLBACK_KEY_DOWN, _key_down, &data);

   ecore_timer_add(0.016, _animate_cube, &data);
   ecore_timer_add(0.016, _animate_camera, &data);
   ecore_timer_add(0.01, _animate_planet, &data);
   ecore_timer_add(0.01, _animate_moon, &data);

   ecore_main_loop_begin();

   ecore_evas_free(ecore_evas);
   ecore_evas_shutdown();
   _sphere_fini();

   return 0;
}