containing ui setup steps in the profiling again.
[platform/upstream/expedite.git] / src / bin / image_map_3d_2.c
1 #undef FNAME
2 #undef NAME
3 #undef ICON
4
5 /* metadata */
6 #define FNAME image_map_3d_2_start
7 #define NAME "Image Map 3D 2"
8 #define ICON "3d.png"
9
10 #ifndef PROTO
11 # ifndef UI
12 #  include "main.h"
13
14 typedef struct _Point
15 {
16    Evas_Coord x, y, z, u, v;
17 } Point;
18
19 typedef struct _Side
20 {
21    Evas_Object *o;
22    Point pt[4];
23 } Side;
24
25 typedef struct _Cube
26 {
27    Side side[6];
28 } Cube;
29
30 /* standard var */
31 static int done = 0;
32
33 /* private data */
34 static Cube *cubes[5];
35
36 #define POINT(n, p, xx, yy, zz, uu, vv) \
37    c->side[n].pt[p].x = xx; \
38    c->side[n].pt[p].y = yy; \
39    c->side[n].pt[p].z = zz; \
40    c->side[n].pt[p].u = uu; \
41    c->side[n].pt[p].v = vv
42
43 static Cube *
44 _cube_new(Evas_Coord w, Evas_Coord h, Evas_Coord d)
45 {
46    Cube *c;
47    int i;
48
49    w -= (w / 2);
50    h -= (h / 2);
51    d -= (d / 2);
52    c = calloc(1, sizeof(Cube));
53    for (i = 0; i < 6; i++)
54      {
55         Evas_Object *o;
56         char buf[256];
57         o = efl_add(EFL_CANVAS_IMAGE_CLASS, evas);
58         c->side[i].o = o;
59         snprintf(buf, sizeof(buf), "cube%i.png", i + 1);
60         efl_file_simple_load(o, build_path(buf), NULL);
61         efl_gfx_fill_set(o, EINA_RECT(0, 0, 256, 256));
62         efl_gfx_entity_size_set(o, EINA_SIZE2D(256, 256));
63         efl_gfx_entity_visible_set(o, EINA_TRUE);
64      }
65    POINT(0, 0, -w, -h, -d,   0,   0);
66    POINT(0, 1,  w, -h, -d, 256,   0);
67    POINT(0, 2,  w,  h, -d, 256, 256);
68    POINT(0, 3, -w,  h, -d,   0, 256);
69
70    POINT(1, 0,  w, -h, -d,   0,   0);
71    POINT(1, 1,  w, -h,  d, 256,   0);
72    POINT(1, 2,  w,  h,  d, 256, 256);
73    POINT(1, 3,  w,  h, -d,   0, 256);
74
75    POINT(2, 0,  w, -h,  d,   0,   0);
76    POINT(2, 1, -w, -h,  d, 256,   0);
77    POINT(2, 2, -w,  h,  d, 256, 256);
78    POINT(2, 3,  w,  h,  d,   0, 256);
79
80    POINT(3, 0, -w, -h,  d,   0,   0);
81    POINT(3, 1, -w, -h, -d, 256,   0);
82    POINT(3, 2, -w,  h, -d, 256, 256);
83    POINT(3, 3, -w,  h,  d,   0, 256);
84
85    POINT(4, 0, -w, -h,  d,   0,   0);
86    POINT(4, 1,  w, -h,  d, 256,   0);
87    POINT(4, 2,  w, -h, -d, 256, 256);
88    POINT(4, 3, -w, -h, -d,   0, 256);
89
90    POINT(5, 0, -w,  h, -d,   0,   0);
91    POINT(5, 1,  w,  h, -d, 256,   0);
92    POINT(5, 2,  w,  h,  d, 256, 256);
93    POINT(5, 3, -w,  h,  d,   0, 256);
94
95    return c;
96 }
97
98 static void
99 _cube_pos(Cube *c,
100           Evas_Coord x, Evas_Coord y, Evas_Coord z,
101           double dx, double dy, double dz)
102 {
103    static Evas_Map *m = NULL;
104    int i, j, order[6], sorted;
105    Evas_Coord mz[6];
106
107    if (!m) m = evas_map_new(4);
108
109    for (i = 0; i < 6; i++)
110      {
111         Evas_Coord tz[4];
112
113         for (j = 0; j < 4; j++)
114           {
115              evas_map_point_coord_set(m, j,
116                                       c->side[i].pt[j].x + x,
117                                       c->side[i].pt[j].y + y,
118                                       c->side[i].pt[j].z + z);
119              evas_map_point_image_uv_set(m, j,
120                                       c->side[i].pt[j].u,
121                                       c->side[i].pt[j].v);
122              evas_map_point_color_set(m, j, 255, 255, 255, 255);
123           }
124         evas_map_util_3d_rotate(m, dx, dy, dz, x, y, z);
125         evas_map_util_3d_lighting(m, -1000, -1000, -1000,
126                                   255, 255, 255,
127                                   20, 20, 20);
128         evas_map_util_3d_perspective(m, (win_w / 2), (win_h / 2), 0, 512);
129         if (evas_map_util_clockwise_get(m))
130           {
131              evas_object_map_enable_set(c->side[i].o, 1);
132              evas_object_map_set(c->side[i].o, m);
133              efl_gfx_entity_visible_set(c->side[i].o, EINA_TRUE);
134           }
135         else
136           {
137              efl_gfx_entity_visible_set(c->side[i].o, EINA_FALSE);
138           }
139
140         order[i] = i;
141         for (j = 0; j < 4; j++)
142           evas_map_point_coord_get(m, j, NULL, NULL, &(tz[j]));
143         mz[i] = (tz[0] + tz[1] + tz[2] + tz[3]) / 4;
144      }
145    sorted = 0;
146    do
147      {
148         sorted = 1;
149         for (i = 0; i < 5; i++)
150           {
151              if (mz[order[i]] > mz[order[i + 1]])
152                {
153                   j = order[i];
154                   order[i] = order[i + 1];
155                   order[i + 1] = j;
156                   sorted = 0;
157                }
158           }
159      }
160    while (!sorted);
161
162    efl_gfx_stack_raise_to_top(c->side[order[0]].o);
163    for (i = 1; i < 6; i++)
164      efl_gfx_stack_below(c->side[order[i]].o, c->side[order[i - 1]].o);
165 }
166
167 static void
168 _cube_free(Cube *c)
169 {
170    int i;
171
172    for (i = 0; i < 6; i++) efl_del(c->side[i].o);
173    free(c);
174 }
175
176 /* setup */
177 static void _setup(void)
178 {
179    cubes[0] = _cube_new(128, 128, 256);
180    cubes[1] = _cube_new(256, 128, 128);
181    cubes[2] = _cube_new(256, 256, 128);
182    cubes[3] = _cube_new(128, 256, 128);
183    cubes[4] = _cube_new(256, 256, 256);
184
185    done = 0;
186 }
187
188 /* cleanup */
189 static void _cleanup(void)
190 {
191    _cube_free(cubes[0]);
192    _cube_free(cubes[1]);
193    _cube_free(cubes[2]);
194    _cube_free(cubes[3]);
195    _cube_free(cubes[4]);
196 }
197
198 /* loop - do things */
199 static void _loop(double t, int f)
200 {
201    _cube_pos(cubes[0],
202              (win_w / 2) - 640, (win_h / 2) - 256, 512,
203              f / 2.0, f, f / 3.0);
204    _cube_pos(cubes[1],
205              (win_w / 2) + 512, (win_h / 2) - 128, 384,
206              f / 3.0, f / 2.0, f / 4.0);
207    _cube_pos(cubes[2],
208              (win_w / 2) - 384, (win_h / 2) + 128, 256,
209              f / 2.0, f / 3.0, f);
210    _cube_pos(cubes[3],
211              (win_w / 2) + 256, (win_h / 2) + 64, 128,
212              f, f / 5.0, f / 2.0);
213    _cube_pos(cubes[4],
214              (win_w / 2), (win_h / 2), 0,
215              f / 4.0, f / 3.0, f / 5.0);
216    FPS_STD(NAME);
217 }
218
219 /* prepend special key handlers if interactive (before STD) */
220 static void _key(const char *key)
221 {
222    KEY_STD;
223 }
224
225
226
227
228
229
230
231
232
233
234
235
236 /* template stuff - ignore */
237 # endif
238 #endif
239
240 #ifdef UI
241 _ui_menu_item_add(ICON, NAME, FNAME);
242 #endif
243
244 #ifdef PROTO
245 void FNAME(void);
246 #endif
247
248 #ifndef PROTO
249 # ifndef UI
250 void FNAME(void)
251 {
252    ui_func_set(_key, _loop, _setup);
253 }
254 # endif
255 #endif
256 #undef FNAME
257 #undef NAME
258 #undef ICON