summaryrefslogtreecommitdiff
path: root/evas-3d-elm.c
blob: 458e17cf4f1054e16fadd1d8cc0b2bcbafbe96d0 (plain)
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
774
775
776
777
778
779
780
781
782
783
784
785
786
/*  evas-3d-elm - a demonstration of using evas_3d in Elm. */

//#define EFL_API_OVERRIDE 1
/* Enable access to unstable EFL API that are still in beta */
#define EFL_BETA_API_SUPPORT 1
/* Enable access to unstable EFL EO API. */
#define EFL_EO_API_SUPPORT   1

#include <stdio.h>
#include <ctype.h>

#include <Elementary.h>

#define WIDTH  (400)
#define HEIGHT (400)

typedef struct _globals
{
   Evas        *evas; // Our Evas.
   Evas_Object *win; // Our Elm window.
} globals;

globals ourGlobals;

typedef struct _Scene_Data
{
   Evas_Object	*image; // Our Elm image.
   Eo		*scene;
   Eo		*root_node;
   Eo		*camera_node;
   Eo		*light_node;

   Eo		*camera;
   Eo		*light;

   Eo		*mesh;
   Eo		*mesh_node;
   Eo		*material0;
   Eo		*material1;
   Eo		*texture0;
   Eo		*texture1;
   Eo		*texture_normal;

   Eo		*mesh2;
   Eo		*mesh2_node;
   Eo		*material2;
   Eo		*texture2;

   Eo		*mesh3;
   Eo		*mesh3_node;
   Eo		*material3;
   Eo		*texture_diffuse;
} Scene_Data;

static Scene_Data ourScene;

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
};

static const unsigned int pixels0[] =
{
   0xff0000ff, 0xff0000ff, 0xffff0000, 0xffff0000,
   0xff0000ff, 0xff0000ff, 0xffff0000, 0xffff0000,
   0xff00ff00, 0xff00ff00, 0xff000000, 0xff000000,
   0xff00ff00, 0xff00ff00, 0xff000000, 0xff000000,
};

static const unsigned int pixels1[] =
{
   0xffff0000, 0xffff0000, 0xff00ff00, 0xff00ff00,
   0xffff0000, 0xffff0000, 0xff00ff00, 0xff00ff00,
   0xff0000ff, 0xff0000ff, 0xffffffff, 0xffffffff,
   0xff0000ff, 0xff0000ff, 0xffffffff, 0xffffffff,
};

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 *sphere_vertices = NULL;

static int index_count = 0;
static unsigned short *sphere_indices = NULL;

static inline vec3
_normalize(const vec3 *v)
{
   double l = sqrt(v->x * v->x + v->y * v->y + v->z * v->z);
   vec3 vec;

   vec.x = v->x / l;
   vec.y = v->y / l;
   vec.z = v->z / l;

   return vec;
}

static void
_sphere_fini()
{
   if (sphere_vertices)
     free(sphere_vertices);

   if (sphere_indices)
     free(sphere_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. */
   sphere_vertices = malloc(sizeof(vertex) * vertex_count);
   sphere_indices = malloc(sizeof(unsigned short) * index_count);

   for (i = 0; i <= precision; i++)
     {
        double lati = (M_PI * (double)i) / (double)precision;
        double y = cos(lati);
        double r = fabs(sin(lati));

        for (j = 0; j <= precision; j++)
          {
             double longi = (M_PI * 2.0 * j) / precision;
             vertex *v = &sphere_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 = &sphere_indices[0];

   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;
          }
     }

   for (i = 0; i < index_count; i += 3)
     {
        vertex *v0 = &sphere_vertices[sphere_indices[i + 0]];
        vertex *v1 = &sphere_vertices[sphere_indices[i + 1]];
        vertex *v2 = &sphere_vertices[sphere_indices[i + 2]];

        vec3 e1, e2;
        float du1, du2, dv1, dv2, f;
        vec3 tangent;

        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;
     }

   for (i = 0; i <= precision; i++)
     {
        for (j = 0; j <= precision; j++)
          {
             if (j == precision)
               {
                  vertex *v = &sphere_vertices[i * (precision + 1) + j];
                  v->tangent = sphere_vertices[i * (precision + 1)].tangent;
               }
          }
     }
}

static Eina_Bool
_animate_scene(void *data)
{
   static float angle = 0.0f;
   static float earthAngle = 0.0f;
   static int frame = 0;
   static int inc = 1;
   static int sonicFrame = 0;

   Scene_Data *scene = (Scene_Data *)data;

   // Animate cube.
   angle += 0.5;
   if (angle > 360.0) angle -= 360.0f;

   frame += inc;

   if (frame >= 20) inc = -1;
   else if (frame <= 0)
     inc = 1;

   eo_do(scene->mesh_node,
         evas_canvas3d_node_orientation_angle_axis_set(angle, 1.0, 1.0, 1.0),
         evas_canvas3d_node_mesh_frame_set(scene->mesh, frame)
         );

   // Animate sonic.
   sonicFrame += 32;
   if (sonicFrame > 256 * 50) sonicFrame = 0;
   eo_do(scene->mesh2_node,
         evas_canvas3d_node_mesh_frame_set(scene->mesh2, sonicFrame)
         );

   // Animate earth.
   earthAngle += 0.3;
   if (earthAngle > 360.0) earthAngle -= 360.0f;
   eo_do(scene->mesh3_node,
         evas_canvas3d_node_orientation_angle_axis_set(angle, 0.0, 1.0, 0.0)
         );

   return EINA_TRUE;
}

static void
_camera_setup(Evas *evas, Scene_Data *scene)
{
   scene->camera = eo_add(EVAS_CANVAS3D_CAMERA_CLASS, evas,
                          evas_canvas3d_camera_projection_perspective_set(60.0, 1.0, 1.0, 500.0)
                          );

   scene->camera_node = eo_add(EVAS_CANVAS3D_NODE_CLASS, evas, evas_canvas3d_node_constructor(EVAS_CANVAS3D_NODE_TYPE_CAMERA),
                                      evas_canvas3d_node_camera_set(scene->camera),
                                      evas_canvas3d_node_position_set(50.0, 0.0, 20.0),
                                      evas_canvas3d_node_look_at_set(EVAS_CANVAS3D_SPACE_PARENT, 0.0, 0.0, 20.0, EVAS_CANVAS3D_SPACE_PARENT, 0.0, 0.0, 1.0)
                                      );

   eo_do(scene->root_node, evas_canvas3d_node_member_add(scene->camera_node));
   eo_do(scene->scene, evas_canvas3d_scene_camera_node_set(scene->camera_node));
}

static void
_light_setup(Evas *evas, Scene_Data *scene)
{
   scene->light = eo_add(EVAS_CANVAS3D_LIGHT_CLASS, evas,
                         evas_canvas3d_light_ambient_set(1.0, 1.0, 1.0, 1.0),
                         evas_canvas3d_light_diffuse_set(1.0, 1.0, 1.0, 1.0),
                         evas_canvas3d_light_specular_set(1.0, 1.0, 1.0, 1.0),
                         evas_canvas3d_light_directional_set(EINA_TRUE)
                         );
   scene->light_node = eo_add(EVAS_CANVAS3D_NODE_CLASS, evas, evas_canvas3d_node_constructor(EVAS_CANVAS3D_NODE_TYPE_LIGHT),
                                     evas_canvas3d_node_light_set(scene->light),
                                     evas_canvas3d_node_position_set(1000.0, 0.0, 1000.0),
                                     evas_canvas3d_node_look_at_set(EVAS_CANVAS3D_SPACE_PARENT, 0.0, 0.0, 0.0, EVAS_CANVAS3D_SPACE_PARENT, 0.0, 1.0, 0.0)
                                     );
   eo_do(scene->root_node, evas_canvas3d_node_member_add(scene->light_node));
}

static void
_cube_setup(Evas *evas, Scene_Data *scene)
{
   // Setup cube materials.
   scene->texture0 = eo_add(EVAS_CANVAS3D_TEXTURE_CLASS, evas,
                            evas_canvas3d_texture_data_set(EVAS_COLORSPACE_ARGB8888, 4, 4, &pixels0[0])
                            );

   scene->texture1 = eo_add(EVAS_CANVAS3D_TEXTURE_CLASS, evas,
                            evas_canvas3d_texture_data_set(EVAS_COLORSPACE_ARGB8888, 4, 4, &pixels1[0])
                            );

   scene->texture_normal = eo_add(EVAS_CANVAS3D_TEXTURE_CLASS, evas,
                                  evas_canvas3d_texture_file_set("media/normal_lego.png", NULL)
                                  );

   scene->material0 = eo_add(EVAS_CANVAS3D_MATERIAL_CLASS, evas,
                             evas_canvas3d_material_enable_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_AMBIENT, EINA_TRUE),
                             evas_canvas3d_material_enable_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_DIFFUSE, EINA_TRUE),
                             evas_canvas3d_material_enable_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_SPECULAR, EINA_TRUE),
                             evas_canvas3d_material_enable_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_NORMAL, EINA_TRUE),

                             evas_canvas3d_material_color_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_AMBIENT, 0.2, 0.2, 0.2, 1.0),
                             evas_canvas3d_material_color_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_DIFFUSE, 0.8, 0.8, 0.8, 1.0),
                             evas_canvas3d_material_color_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_SPECULAR, 1.0, 1.0, 1.0, 1.0),
                             evas_canvas3d_material_shininess_set(100.0),
                             evas_canvas3d_material_texture_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_DIFFUSE, scene->texture0)
                             );

   scene->material1 = eo_add(EVAS_CANVAS3D_MATERIAL_CLASS, evas,
                             evas_canvas3d_material_enable_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_AMBIENT, EINA_TRUE),
                             evas_canvas3d_material_enable_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_DIFFUSE, EINA_TRUE),
                             evas_canvas3d_material_enable_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_SPECULAR, EINA_TRUE),
                             evas_canvas3d_material_enable_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_NORMAL, EINA_TRUE),

                             evas_canvas3d_material_color_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_AMBIENT, 0.2, 0.2, 0.2, 1.0),
                             evas_canvas3d_material_color_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_DIFFUSE, 0.8, 0.8, 0.8, 1.0),
                             evas_canvas3d_material_color_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_SPECULAR, 1.0, 1.0, 1.0, 1.0),
                             evas_canvas3d_material_shininess_set(100.0),

                             evas_canvas3d_material_texture_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_DIFFUSE, scene->texture1),
                             evas_canvas3d_material_texture_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_NORMAL, scene->texture_normal)
                             );

   // Setup CUBE mesh.
   scene->mesh = eo_add(EVAS_CANVAS3D_MESH_CLASS, evas,
                        evas_canvas3d_mesh_vertex_count_set(24),
                        evas_canvas3d_mesh_frame_add(0),

                        evas_canvas3d_mesh_frame_vertex_data_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION, 12 * sizeof(float), &cube_vertices[ 0]),
                        evas_canvas3d_mesh_frame_vertex_data_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL, 12 * sizeof(float), &cube_vertices[ 3]),
                        evas_canvas3d_mesh_frame_vertex_data_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR, 12 * sizeof(float), &cube_vertices[ 6]),
                        evas_canvas3d_mesh_frame_vertex_data_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD, 12 * sizeof(float), &cube_vertices[10]),

                        evas_canvas3d_mesh_index_data_set(EVAS_CANVAS3D_INDEX_FORMAT_UNSIGNED_SHORT, 36, &cube_indices[0]),
                        evas_canvas3d_mesh_vertex_assembly_set(EVAS_CANVAS3D_VERTEX_ASSEMBLY_TRIANGLES),

                        evas_canvas3d_mesh_shade_mode_set(EVAS_CANVAS3D_SHADE_MODE_NORMAL_MAP),

                        evas_canvas3d_mesh_frame_material_set(0, scene->material0),

                        evas_canvas3d_mesh_frame_add(20),
                        evas_canvas3d_mesh_frame_material_set(20, scene->material1)
                        );

   scene->mesh_node = eo_add(EVAS_CANVAS3D_NODE_CLASS, evas, evas_canvas3d_node_constructor(EVAS_CANVAS3D_NODE_TYPE_MESH),
                                    eo_key_data_set("Name", "cube"),
                                    evas_canvas3d_node_position_set(40.0, 3.5, 23.0),
                                    evas_canvas3d_node_mesh_add(scene->mesh)
                                    );

   eo_do(scene->root_node, evas_canvas3d_node_member_add(scene->mesh_node));
}

static void
_sonic_setup(Evas *evas, Scene_Data *scene)
{
   // Setup an MD2 mesh.
   scene->texture2 = eo_add(EVAS_CANVAS3D_TEXTURE_CLASS, evas,
                            evas_canvas3d_texture_file_set("media/sonic.png", NULL),
                            evas_canvas3d_texture_filter_set(EVAS_CANVAS3D_TEXTURE_FILTER_NEAREST, EVAS_CANVAS3D_TEXTURE_FILTER_NEAREST),
                            evas_canvas3d_texture_wrap_set(EVAS_CANVAS3D_WRAP_MODE_REPEAT, EVAS_CANVAS3D_WRAP_MODE_REPEAT)
                            );

   scene->material2 = eo_add(EVAS_CANVAS3D_MATERIAL_CLASS, evas,
                             evas_canvas3d_material_texture_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_DIFFUSE, scene->texture2),

                             evas_canvas3d_material_enable_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_AMBIENT, EINA_TRUE),
                             evas_canvas3d_material_enable_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_DIFFUSE, EINA_TRUE),
                             evas_canvas3d_material_enable_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_SPECULAR, EINA_TRUE),
                             evas_canvas3d_material_enable_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_NORMAL, EINA_TRUE),

                             evas_canvas3d_material_color_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_AMBIENT, 0.01, 0.01, 0.01, 1.0),
                             evas_canvas3d_material_color_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_DIFFUSE, 1.0, 1.0, 1.0, 1.0),
                             evas_canvas3d_material_color_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_SPECULAR, 1.0, 1.0, 1.0, 1.0),
                             evas_canvas3d_material_shininess_set(50.0)
                             );

   scene->mesh2 = eo_add(EVAS_CANVAS3D_MESH_CLASS, evas,
                         efl_file_set("media/sonic.md2", NULL),
                         evas_canvas3d_mesh_frame_material_set(0, scene->material2),
                         evas_canvas3d_mesh_shade_mode_set(EVAS_CANVAS3D_SHADE_MODE_PHONG)
                         );

   scene->mesh2_node = eo_add(EVAS_CANVAS3D_NODE_CLASS, evas, evas_canvas3d_node_constructor(EVAS_CANVAS3D_NODE_TYPE_MESH),
                                     eo_key_data_set("Name", "sonic"),
                                     evas_canvas3d_node_mesh_add(scene->mesh2)
                                     );

   eo_do(scene->root_node, evas_canvas3d_node_member_add(scene->mesh2_node));
}

static void
_earth_setup(Evas *evas, Scene_Data *scene)
{
   // Setup earth material.
   scene->texture_diffuse = eo_add(EVAS_CANVAS3D_TEXTURE_CLASS, evas,
                                   evas_canvas3d_texture_file_set("media/EarthDiffuse.png", NULL),
                                   evas_canvas3d_texture_filter_set(EVAS_CANVAS3D_TEXTURE_FILTER_LINEAR, EVAS_CANVAS3D_TEXTURE_FILTER_LINEAR)
                                   );

   scene->material3 = eo_add(EVAS_CANVAS3D_MATERIAL_CLASS, evas,
                             evas_canvas3d_material_texture_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_DIFFUSE, scene->texture_diffuse),

                             evas_canvas3d_material_enable_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_AMBIENT, EINA_TRUE),
                             evas_canvas3d_material_enable_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_DIFFUSE, EINA_TRUE),
                             evas_canvas3d_material_enable_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_SPECULAR, EINA_TRUE),

                             evas_canvas3d_material_color_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_AMBIENT, 0.01, 0.01, 0.01, 1.0),
                             evas_canvas3d_material_color_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_DIFFUSE, 1.0, 1.0, 1.0, 1.0),
                             evas_canvas3d_material_color_set(EVAS_CANVAS3D_MATERIAL_ATTRIB_SPECULAR, 1.0, 1.0, 1.0, 1.0),
                             evas_canvas3d_material_shininess_set(50.0)
                             );

   // Setup earth mesh.
   _sphere_init(100);

   scene->mesh3 = eo_add(EVAS_CANVAS3D_MESH_CLASS, evas,
                         evas_canvas3d_mesh_vertex_count_set(vertex_count),
                         evas_canvas3d_mesh_frame_add(0),
                         evas_canvas3d_mesh_frame_vertex_data_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_POSITION, sizeof(vertex), &sphere_vertices[0].position),
                         evas_canvas3d_mesh_frame_vertex_data_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_NORMAL, sizeof(vertex), &sphere_vertices[0].normal),
                         evas_canvas3d_mesh_frame_vertex_data_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_TANGENT, sizeof(vertex), &sphere_vertices[0].tangent),
                         evas_canvas3d_mesh_frame_vertex_data_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_COLOR, sizeof(vertex), &sphere_vertices[0].color),
                         evas_canvas3d_mesh_frame_vertex_data_set(0, EVAS_CANVAS3D_VERTEX_ATTRIB_TEXCOORD, sizeof(vertex), &sphere_vertices[0].texcoord),

                         evas_canvas3d_mesh_index_data_set(EVAS_CANVAS3D_INDEX_FORMAT_UNSIGNED_SHORT, index_count, &sphere_indices[0]),
                         evas_canvas3d_mesh_vertex_assembly_set(EVAS_CANVAS3D_VERTEX_ASSEMBLY_TRIANGLES),
                         evas_canvas3d_mesh_frame_material_set(0, scene->material3),

                         evas_canvas3d_mesh_shade_mode_set(EVAS_CANVAS3D_SHADE_MODE_DIFFUSE)
                         );

   scene->mesh3_node = eo_add(EVAS_CANVAS3D_NODE_CLASS, evas, evas_canvas3d_node_constructor(EVAS_CANVAS3D_NODE_TYPE_MESH),
                                     eo_key_data_set("Name", "earth"),
                                     evas_canvas3d_node_position_set(40.0, -3.5, 23.0),
                                     evas_canvas3d_node_mesh_add(scene->mesh3)
                                     );

   eo_do(scene->root_node, evas_canvas3d_node_member_add(scene->mesh3_node));
}

static void
_scene_setup(Evas *evas, Scene_Data *scene)
{
   scene->root_node = eo_add(EVAS_CANVAS3D_NODE_CLASS, evas, evas_canvas3d_node_constructor(EVAS_CANVAS3D_NODE_TYPE_NODE));

   // -TODO - set the size based on the size of the enclosing window.
   scene->scene = eo_add(EVAS_CANVAS3D_SCENE_CLASS, evas,
                         evas_canvas3d_scene_root_node_set(scene->root_node),
                         evas_canvas3d_scene_size_set(512, 512),
                         evas_canvas3d_scene_background_color_set(0.0, 0.0, 0.0, 0.0)
                         );

   _camera_setup(evas, scene);
   _light_setup(evas, scene);
   _cube_setup(evas, scene);
   _sonic_setup(evas, scene);
   _earth_setup(evas, scene);
}

static void
_on_mouse_move(void *data, Evas *e EINA_UNUSED, Evas_Object *o, void *einfo)
{
   Scene_Data *scene = data;
   Evas_Event_Mouse_Move *ev = einfo;
   Evas_Coord x, y, w, h;
   Evas_Coord obj_x, obj_y;
   int scene_w, scene_h;
   Evas_Real scene_x, scene_y;
   Evas_Real s, t;
   Eo *n;
   Eo *m;
   Eina_Bool pick;
   char *name = NULL;

   evas_object_geometry_get(o, &x, &y, &w, &h);

   obj_x = ev->cur.canvas.x - x;
   obj_y = ev->cur.canvas.y - y;

   eo_do(scene->scene, evas_canvas3d_scene_size_get(&scene_w, &scene_h));

   scene_x = obj_x * scene_w / (Evas_Real)w;
   scene_y = obj_y * scene_h / (Evas_Real)h;

   eo_do(scene->scene, pick = evas_canvas3d_scene_pick(scene_x, scene_y, &n, &m, &s, &t));
   if (pick)
     name = evas_object_data_get(n, "Name");
   // This is a raw Evas callback, on the Elm image internal Evas_Object.
   // So we need to get the Elm Image back from the raw Evas_Object.
   // Which is why we stuffed it in the scene structure.
   if (name)
     {
        elm_object_tooltip_text_set(scene->image, name);
        elm_object_tooltip_show(scene->image);
     }
   else
     elm_object_tooltip_hide(scene->image);
}

static void
_on_mouse_down(void *data, Evas *e EINA_UNUSED, Evas_Object *o, void *einfo)
{
   Scene_Data *scene = data;
   Evas_Event_Mouse_Down *ev = einfo;
   Evas_Coord x, y, w, h;
   Evas_Coord obj_x, obj_y;
   int scene_w, scene_h;
   Evas_Real scene_x, scene_y;
   Evas_Real s, t;
   Eo *n;
   Eo *m;
   Eina_Bool pick;
   char *name = NULL;

   evas_object_geometry_get(o, &x, &y, &w, &h);

   obj_x = ev->canvas.x - x;
   obj_y = ev->canvas.y - y;

   eo_do(scene->scene, evas_canvas3d_scene_size_get(&scene_w, &scene_h));

   scene_x = obj_x * scene_w / (Evas_Real)w;
   scene_y = obj_y * scene_h / (Evas_Real)h;

   eo_do(scene->scene, pick = evas_canvas3d_scene_pick(scene_x, scene_y, &n, &m, &s, &t));
   if (pick)
     {
        name = evas_object_data_get(n, "Name");
        printf("Picked     : ");
     }
   else
     printf("Not picked : ");
   if (NULL == name)
     name = "";

   printf("output(%d, %d) canvas(%d, %d) object(%d, %d) scene(%f, %f) texcoord(%f, %f) "
          "node(%p) %s mesh(%p)\n",
          ev->output.x, ev->output.y,
          ev->canvas.x, ev->canvas.y,
          obj_x, obj_y,
          scene_x, scene_y,
          s, t, n, name, m);
}

static void
_changed_cb(void *data, Evas_Object *obj, void *event_info)
{
   Scene_Data *scene = data;
   double val = elm_slider_value_get(obj);

   eo_do(scene->camera_node, evas_canvas3d_node_position_set(50.0 + val, 0.0, 20.0));
}

static void
_on_done(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   // Tell the main loop to stop, which it will, eventually.
   elm_exit();
}

static void
open_window(globals *ourGlobals)
{
   char *name = "evas-3d-elm";
   char *title = "Evas_3d Elementary demonstration";
   Evas_Object *obj, *temp;
   int w = WIDTH, h = HEIGHT;

   // Set the engine to opengl_x11, then open the window.
   setenv("ELM_ENGINE", "opengl_x11", 1);
   elm_config_preferred_engine_set("opengl_x11");
   if ((ourGlobals->win = elm_win_util_standard_add(name, title)))
     {
        evas_object_smart_callback_add(ourGlobals->win, "delete,request", _on_done, NULL);
        evas_object_resize(ourGlobals->win, w, h);
        evas_object_move(ourGlobals->win, 0, 0);
        evas_object_show(ourGlobals->win);

        // Get the Evas / canvas from the elm window (that the Evas_Object "lives on"), which is itself an Evas_Object created by Elm.
        ourGlobals->evas = evas_object_evas_get(ourGlobals->win);

        _scene_setup(ourGlobals->evas, &ourScene);

        // Add a background image object.
        obj = eo_add(ELM_IMAGE_CLASS, ourGlobals->win);
        eo_do(obj,
              evas_obj_size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND),
              elm_obj_image_fill_outside_set(EINA_TRUE),
              efl_file_set("media/sky_01.jpg", NULL),
              efl_gfx_visible_set(EINA_TRUE)
              );
        elm_win_resize_object_add(ourGlobals->win, obj);

        // Add an image object for 3D scene rendering.
        obj = eo_add(ELM_IMAGE_CLASS, ourGlobals->win);
        ourScene.image = obj;
        eo_do(obj,
              evas_obj_size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND),
              elm_obj_image_fill_outside_set(EINA_TRUE),
              efl_gfx_visible_set(EINA_TRUE),
              temp = elm_obj_image_object_get()
              );
        eo_do(temp,
              evas_obj_image_scene_set(ourScene.scene)
              );
        elm_object_tooltip_text_set(obj, "");
        elm_object_tooltip_hide(obj);
        // Elm can't seem to be able to tell us WHERE an image was clicked, so use raw Evas calbacks instead.
        evas_object_event_callback_add(temp, EVAS_CALLBACK_MOUSE_MOVE, _on_mouse_move, &ourScene);
        evas_object_event_callback_add(temp, EVAS_CALLBACK_MOUSE_DOWN, _on_mouse_down, &ourScene);
        elm_win_resize_object_add(ourGlobals->win, obj);

        // Add animation timer callback.
        ecore_timer_add(0.016, _animate_scene, &ourScene);

        // Add a zoom slider.
        obj = eo_add(ELM_SLIDER_CLASS, ourGlobals->win);
        eo_do(obj,
              elm_obj_slider_min_max_set(-10, 10),
              evas_obj_size_hint_align_set(1.0, EVAS_HINT_FILL),
              evas_obj_size_hint_weight_set(EVAS_HINT_EXPAND, EVAS_HINT_EXPAND),
              efl_gfx_visible_set(EINA_TRUE)
              );
        elm_win_resize_object_add(ourGlobals->win, obj);
        evas_object_smart_callback_add(obj, "changed", _changed_cb, &ourScene);
     }

   // Set preferred engine back to default from config
   elm_config_preferred_engine_set(NULL);
}

static void
close_window(globals *ourGlobals)
{
   if (ourGlobals->win)
     {
        evas_object_del(ourGlobals->win);
        _sphere_fini();
     }

   // This shuts down Elementary, but keeps the main loop running until all ecore_evas are freed.
   elm_shutdown();
}

EAPI_MAIN int
elm_main(int argc, char **argv)
{
   elm_policy_set(ELM_POLICY_EXIT, ELM_POLICY_EXIT_NONE);
   elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_NONE);
   elm_policy_set(ELM_POLICY_THROTTLE, ELM_POLICY_THROTTLE_HIDDEN_ALWAYS);

   open_window(&ourGlobals);

   if (ourGlobals.win)
     elm_run();

   close_window(&ourGlobals);

   return 0;
}

ELM_MAIN()