summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSung W. Park <sungwoo@gmail.com>2014-03-26 00:26:46 +0900
committerSung W. Park <sungwoo@gmail.com>2014-03-26 00:39:49 +0900
commit8e2d312aeb28e8cf64f47eafda425e3566cef290 (patch)
treebe7a5388ab20c39622af2a547f6da972cef8548f
parentf0a9f992bc08065e41653be5a0cf1e274f16f1a6 (diff)
ELM_GLView: added glview_manygears example to elementary_test
Added a more complex glview example to the elementary test. - the gears lighting matches the original glx gears example. - Added a background image to show that Glview works well with existing ELM winsets. - Added 5 gears and buttons to increase/decrease gear teeth
-rw-r--r--src/bin/Makefile.am1
-rw-r--r--src/bin/test.c6
-rw-r--r--src/bin/test_glview_manygears.c951
3 files changed, 956 insertions, 2 deletions
diff --git a/src/bin/Makefile.am b/src/bin/Makefile.am
index 3ebd91fdd..98b20fc2f 100644
--- a/src/bin/Makefile.am
+++ b/src/bin/Makefile.am
@@ -82,6 +82,7 @@ test_gesture_layer2.c \
82test_gesture_layer3.c \ 82test_gesture_layer3.c \
83test_glview_simple.c \ 83test_glview_simple.c \
84test_glview.c \ 84test_glview.c \
85test_glview_manygears.c \
85test_grid.c \ 86test_grid.c \
86test_hover.c \ 87test_hover.c \
87test_hoversel.c \ 88test_hoversel.c \
diff --git a/src/bin/test.c b/src/bin/test.c
index 1dd840c2c..268f739d1 100644
--- a/src/bin/test.c
+++ b/src/bin/test.c
@@ -211,6 +211,7 @@ void test_win_wm_rotation(void *data, Evas_Object *obj, void *event_info);
211void test_grid(void *data, Evas_Object *obj, void *event_info); 211void test_grid(void *data, Evas_Object *obj, void *event_info);
212void test_glview_simple(void *data, Evas_Object *obj, void *event_info); 212void test_glview_simple(void *data, Evas_Object *obj, void *event_info);
213void test_glview(void *data, Evas_Object *obj, void *event_info); 213void test_glview(void *data, Evas_Object *obj, void *event_info);
214void test_glview_manygears(void *data, Evas_Object *obj, void *event_info);
214void test_3d(void *data, Evas_Object *obj, void *event_info); 215void test_3d(void *data, Evas_Object *obj, void *event_info);
215void test_naviframe(void *data, Evas_Object *obj, void *event_info); 216void test_naviframe(void *data, Evas_Object *obj, void *event_info);
216void test_naviframe2(void *data, Evas_Object *obj, void *event_info); 217void test_naviframe2(void *data, Evas_Object *obj, void *event_info);
@@ -671,9 +672,10 @@ add_tests:
671 ADD_TEST(NULL, "General", "Scaling 2", test_scaling2); 672 ADD_TEST(NULL, "General", "Scaling 2", test_scaling2);
672 673
673 //------------------------------// 674 //------------------------------//
674 ADD_TEST(NULL, "3D", "GLViewSimple", test_glview_simple);
675 ADD_TEST(NULL, "3D", "GLView", test_glview);
676 ADD_TEST(NULL, "3D", "Evas Map 3D", test_3d); 675 ADD_TEST(NULL, "3D", "Evas Map 3D", test_3d);
676 ADD_TEST(NULL, "3D", "GLViewSimple", test_glview_simple);
677 ADD_TEST(NULL, "3D", "GLView Gears", test_glview);
678 ADD_TEST(NULL, "3D", "GLView Many Gears", test_glview_manygears);
677 679
678 //------------------------------// 680 //------------------------------//
679#ifdef HAVE_ELEMENTARY_WEB 681#ifdef HAVE_ELEMENTARY_WEB
diff --git a/src/bin/test_glview_manygears.c b/src/bin/test_glview_manygears.c
new file mode 100644
index 000000000..34627a510
--- /dev/null
+++ b/src/bin/test_glview_manygears.c
@@ -0,0 +1,951 @@
1/*
2 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22/*
23 * Ported to GLES2.
24 * Kristian Høgsberg <krh@bitplanet.net>
25 * May 3, 2010
26 *
27 * Improve GLES2 port:
28 * * Refactor gear drawing.
29 * * Use correct normals for surfaces.
30 * * Improve shader.
31 * * Use perspective projection transformation.
32 * * Add FPS count.
33 * * Add comments.
34 * Alexandros Frantzis <alexandros.frantzis@linaro.org>
35 * Jul 13, 2010
36 *
37 * Ported to Elm_GLView and added features for testing purposes:
38 * * Has 5 gears
39 * * Number of teeth can be increased to test under many vertices
40 * * Up/Down button increases the teeth number
41 * Sung W. Park <sungwoo@gmail.com>
42 * (Some positioning and numbers taken from KHCho's example)
43 * Oct 27, 2011
44 *
45 */
46#ifdef HAVE_CONFIG_H
47# include "elementary_config.h"
48#endif
49#include <Elementary.h>
50
51#ifndef M_PI
52#define M_PI 3.14159265
53#endif
54
55#define STRIPS_PER_TOOTH 7
56#define VERTICES_PER_TOOTH 34
57#define GEAR_VERTEX_STRIDE 6
58
59/* A set of macros for making the creation of the gears easier */
60#define GEAR_POINT(r, da) { (r) * c[(da)], (r) * s[(da)] }
61#define SET_NORMAL(x, y, z) do { \
62 normal[0] = (x); normal[1] = (y); normal[2] = (z); \
63} while(0);
64
65#define GEAR_VERT(v, point, sign) vert((v), p[(point)].x, p[(point)].y, \
66 (sign) * width * 0.5, normal);
67
68#define START_STRIP do { \
69 gear->strips[cur_strip].first = v - gear->vertices; \
70} while(0);
71
72#define END_STRIP do { \
73 int _tmp = (v - gear->vertices); \
74 gear->strips[cur_strip].count = _tmp - gear->strips[cur_strip].first; \
75 cur_strip++; \
76} while (0);
77
78#define QUAD_WITH_NORMAL(p1, p2) do { \
79 SET_NORMAL((p[(p1)].y - p[(p2)].y), -(p[(p1)].x - p[(p2)].x), 0); \
80 v = GEAR_VERT(v, (p1), -1); \
81 v = GEAR_VERT(v, (p1), 1); \
82 v = GEAR_VERT(v, (p2), -1); \
83 v = GEAR_VERT(v, (p2), 1); \
84} while(0);
85
86// Struct describing a point
87typedef struct _Point
88{
89 GLfloat x;
90 GLfloat y;
91} Point;
92
93// Struct describing the vertices in triangle strip
94typedef struct _VertexStrip
95{
96 GLint first;
97 GLint count;
98} VertexStrip;
99
100// Each vertex consist of GEAR_VERTEX_STRIDE GLfloat attributes
101typedef GLfloat GearVertex[GEAR_VERTEX_STRIDE];
102
103// Struct representing a gear.
104typedef struct _Gear
105{
106 GearVertex *vertices;
107 int nvertices;
108 VertexStrip *strips;
109 int nstrips;
110 GLuint vbo;
111} Gear;
112
113// GL related data here..
114typedef struct _GLData
115{
116 Evas_GL_API *glapi;
117 GLuint program;
118 GLuint vtx_shader;
119 GLuint fgmt_shader;
120 int initialized : 1;
121 int mouse_down : 1;
122
123 // Gear Stuff
124 Gear *gear[6];
125 GLfloat view_rot[3];
126 GLfloat light_pos[4];
127 GLfloat proj_mat[16];
128
129 GLfloat angle;
130
131 GLuint mvp_loc; // ModelViewPorjection Matrix Loc
132 GLuint norm_mat_loc;
133 GLuint light_pos_loc;
134 GLuint material_loc;
135
136 GLuint gear_teeth;
137 GLuint tot_vertices;
138
139 int recreate_gears;
140} GLData;
141
142static void gears_init(GLData *gld);
143static void create_gears(GLData *gld);
144static void render_gears(GLData *gld);
145static void gears_reshape(GLData *gld, int width, int height);
146
147//--------------------------------//
148// Fills a gear vertex.
149static GearVertex *
150vert(GearVertex *v, GLfloat x, GLfloat y, GLfloat z, GLfloat n[3])
151{
152 v[0][0] = x;
153 v[0][1] = y;
154 v[0][2] = z;
155 v[0][3] = n[0];
156 v[0][4] = n[1];
157 v[0][5] = n[2];
158
159 return v + 1;
160}
161
162// Create a gear wheel.
163static Gear *
164create_gear(GLData *gld, GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
165 GLint teeth, GLfloat tooth_depth)
166{
167 GLfloat r0, r1, r2;
168 GLfloat da;
169 Gear *gear;
170 GearVertex *v;
171 double s[5], c[5];
172 GLfloat normal[3];
173 int cur_strip = 0;
174 int i;
175 Evas_GL_API *gl = gld->glapi;
176
177 gear = (Gear*)malloc(sizeof(Gear));
178 if (gear == NULL)
179 return NULL;
180
181 r0 = inner_radius;
182 r1 = outer_radius - tooth_depth / 2.0;
183 r2 = outer_radius + tooth_depth / 2.0;
184
185 da = 2.0 * M_PI / teeth / 4.0;
186
187 // Allocate memory for the triangle strip information
188 gear->nstrips = STRIPS_PER_TOOTH * teeth;
189 gear->strips = calloc(gear->nstrips, sizeof (*gear->strips));
190
191 // Allocate memory for the vertices
192 gear->vertices = calloc(VERTICES_PER_TOOTH * teeth,
193 sizeof(*gear->vertices));
194
195 v = gear->vertices;
196 for (i = 0; i < teeth; i++)
197 {
198 // Calculate needed sin/cos for varius angles
199 s[0] = sin(i * 2.0 * M_PI / teeth);
200 c[0] = cos(i * 2.0 * M_PI / teeth);
201 s[1] = sin(i * 2.0 * M_PI / teeth + da);
202 c[1] = cos(i * 2.0 * M_PI / teeth + da);
203 s[2] = sin(i * 2.0 * M_PI / teeth + da * 2);
204 c[2] = cos(i * 2.0 * M_PI / teeth + da * 2);
205 s[3] = sin(i * 2.0 * M_PI / teeth + da * 3);
206 c[3] = cos(i * 2.0 * M_PI / teeth + da * 3);
207 s[4] = sin(i * 2.0 * M_PI / teeth + da * 4);
208 c[4] = cos(i * 2.0 * M_PI / teeth + da * 4);
209
210 // Create the 7 points (only x,y coords) used to draw a tooth
211 Point p[7] =
212 {
213 GEAR_POINT(r2, 1), // 0
214 GEAR_POINT(r2, 2), // 1
215 GEAR_POINT(r1, 0), // 2
216 GEAR_POINT(r1, 3), // 3
217 GEAR_POINT(r0, 0), // 4
218 GEAR_POINT(r1, 4), // 5
219 GEAR_POINT(r0, 4), // 6
220 };
221
222 // Front face
223 START_STRIP;
224 SET_NORMAL(0, 0, 1.0);
225 v = GEAR_VERT(v, 0, +1);
226 v = GEAR_VERT(v, 1, +1);
227 v = GEAR_VERT(v, 2, +1);
228 v = GEAR_VERT(v, 3, +1);
229 v = GEAR_VERT(v, 4, +1);
230 v = GEAR_VERT(v, 5, +1);
231 v = GEAR_VERT(v, 6, +1);
232 END_STRIP;
233
234 // Inner face
235 START_STRIP;
236 QUAD_WITH_NORMAL(4, 6);
237 END_STRIP;
238
239 // Back face
240 START_STRIP;
241 SET_NORMAL(0, 0, -1.0);
242 v = GEAR_VERT(v, 6, -1);
243 v = GEAR_VERT(v, 5, -1);
244 v = GEAR_VERT(v, 4, -1);
245 v = GEAR_VERT(v, 3, -1);
246 v = GEAR_VERT(v, 2, -1);
247 v = GEAR_VERT(v, 1, -1);
248 v = GEAR_VERT(v, 0, -1);
249 END_STRIP;
250
251 // Outer face
252 START_STRIP;
253 QUAD_WITH_NORMAL(0, 2);
254 END_STRIP;
255
256 START_STRIP;
257 QUAD_WITH_NORMAL(1, 0);
258 END_STRIP;
259
260 START_STRIP;
261 QUAD_WITH_NORMAL(3, 1);
262 END_STRIP;
263
264 START_STRIP;
265 QUAD_WITH_NORMAL(5, 3);
266 END_STRIP;
267 }
268
269 gear->nvertices = (v - gear->vertices);
270
271 // Store the vertices in a vertex buffer object (VBO)
272 gl->glGenBuffers(1, &gear->vbo);
273 gl->glBindBuffer(GL_ARRAY_BUFFER, gear->vbo);
274 gl->glBufferData(GL_ARRAY_BUFFER, gear->nvertices * sizeof(GearVertex),
275 gear->vertices, GL_STATIC_DRAW);
276
277 return gear;
278}
279
280static void
281free_gear(GLData *gld, Gear *gear)
282{
283 Evas_GL_API *gl = gld->glapi;
284
285 gl->glDeleteBuffers(1, &gear->vbo);
286 free(gear->strips);
287 free(gear->vertices);
288 free(gear);
289}
290
291
292static void
293multiply(GLfloat *m, const GLfloat *n)
294{
295 GLfloat tmp[16];
296 const GLfloat *row, *column;
297 div_t d;
298 int i, j;
299
300 for (i = 0; i < 16; i++)
301 {
302 tmp[i] = 0;
303 d = div(i, 4);
304 row = n + d.quot * 4;
305 column = m + d.rem;
306 for (j = 0; j < 4; j++)
307 tmp[i] += row[j] * column[j * 4];
308 }
309 memcpy(m, &tmp, sizeof tmp);
310}
311
312static void
313rotate(GLfloat *m, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
314{
315 double s, c;
316
317 s = sin(angle);
318 c = cos(angle);
319 GLfloat r[16] =
320 {
321 x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
322 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) + x * s, 0,
323 x * z * (1 - c) + y * s, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
324 0, 0, 0, 1
325 };
326
327 multiply(m, r);
328}
329
330static void
331translate(GLfloat *m, GLfloat x, GLfloat y, GLfloat z)
332{
333 GLfloat t[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 };
334
335 multiply(m, t);
336}
337
338static void
339identity(GLfloat *m)
340{
341 GLfloat t[16] =
342 {
343 1.0, 0.0, 0.0, 0.0,
344 0.0, 1.0, 0.0, 0.0,
345 0.0, 0.0, 1.0, 0.0,
346 0.0, 0.0, 0.0, 1.0,
347 };
348
349 memcpy(m, t, sizeof(t));
350}
351
352// Transposes a 4x4 matrix.
353static void
354transpose(GLfloat *m)
355{
356 GLfloat t[16] =
357 {
358 m[0], m[4], m[8], m[12],
359 m[1], m[5], m[9], m[13],
360 m[2], m[6], m[10], m[14],
361 m[3], m[7], m[11], m[15]
362 };
363
364 memcpy(m, t, sizeof(t));
365}
366
367/*
368 Inverts a 4x4 matrix.
369
370 This function can currently handle only pure translation-rotation matrices.
371 Read http://www.gamedev.net/community/forums/topic.asp?topic_id=425118
372 for an explanation.
373 */
374static void
375invert(GLfloat *m)
376{
377 GLfloat t[16];
378 identity(t);
379
380 // Extract and invert the translation part 't'. The inverse of a
381 // translation matrix can be calculated by negating the translation
382 // coordinates.
383 t[12] = -m[12]; t[13] = -m[13]; t[14] = -m[14];
384
385 // Invert the rotation part 'r'. The inverse of a rotation matrix is
386 // equal to its transpose.
387 m[12] = m[13] = m[14] = 0;
388 transpose(m);
389
390 // inv(m) = inv(r) * inv(t)
391 multiply(m, t);
392}
393
394// Calculate a perspective projection transformation.
395static void
396perspective(GLfloat *m, GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar)
397{
398 GLfloat tmp[16];
399 identity(tmp);
400
401 double sine, cosine, cotangent, deltaZ;
402 GLfloat radians = fovy / 2 * M_PI / 180;
403
404 deltaZ = zFar - zNear;
405 sine = sin(radians);
406 cosine = cos(radians);
407
408 if ((deltaZ == 0) || (sine == 0) || (aspect == 0))
409 return;
410
411 cotangent = cosine / sine;
412
413 tmp[0] = cotangent / aspect;
414 tmp[5] = cotangent;
415 tmp[10] = -(zFar + zNear) / deltaZ;
416 tmp[11] = -1;
417 tmp[14] = -2 * zNear * zFar / deltaZ;
418 tmp[15] = 0;
419
420 memcpy(m, tmp, sizeof(tmp));
421}
422
423// Draws a gear
424static void
425draw_gear(GLData *gld, Gear *gear, GLfloat *transform,
426 GLfloat tx, GLfloat ty, GLfloat tz,
427 GLfloat angle, int rotate_gear, const GLfloat color[4])
428{
429 Evas_GL_API *gl = gld->glapi;
430 GLfloat model_view[16];
431 GLfloat normal_matrix[16];
432 GLfloat model_view_projection[16];
433 int i;
434
435 // Translate and rotate the gear
436 memcpy(model_view, transform, sizeof (model_view));
437 translate(model_view, tx, ty, tz);
438 if (rotate_gear)
439 rotate(model_view, 2 * M_PI * 90.0 / 360.0, 0, 1, 0);
440 rotate(model_view, 2 * M_PI * angle / 360.0, 0, 0, 1);
441
442 // Create and set the ModelViewProjectionMatrix
443 memcpy(model_view_projection, gld->proj_mat, sizeof(model_view_projection));
444 multiply(model_view_projection, model_view);
445
446 gl->glUniformMatrix4fv(gld->mvp_loc, 1, GL_FALSE,
447 model_view_projection);
448
449 // Create/set normal matrix: inverse transpose of the mvp
450 memcpy(normal_matrix, model_view, sizeof (normal_matrix));
451 invert(normal_matrix);
452 transpose(normal_matrix);
453
454 gl->glUniformMatrix4fv(gld->norm_mat_loc, 1, GL_FALSE, normal_matrix);
455
456 // Set the gear color
457 gl->glUniform4fv(gld->material_loc, 1, color);
458
459 // Set the vertex buffer object to use
460 gl->glBindBuffer(GL_ARRAY_BUFFER, gear->vbo);
461
462 // Set up the position of the attributes in the vertex buffer object
463 gl->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), NULL);
464 gl->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLfloat *) 0 + 3);
465
466 // Enable the attributes
467 gl->glEnableVertexAttribArray(0);
468 gl->glEnableVertexAttribArray(1);
469
470 // Draw the triangle strips that comprise the gear
471 for (i = 0; i < gear->nstrips; i++)
472 {
473 gl->glDrawArrays(GL_TRIANGLE_STRIP, gear->strips[i].first, gear->strips[i].count);
474 }
475
476
477 // Disable the attributes
478 gl->glDisableVertexAttribArray(1);
479 gl->glDisableVertexAttribArray(0);
480}
481
482static void
483gears_draw(GLData *gld)
484{
485 Evas_GL_API *gl = gld->glapi;
486
487 static const GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
488 static const GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
489 static const GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
490 static const GLfloat yellow[4] = { 1.0, 1.0, 0.2, 1.0 };
491 static const GLfloat purple[4] = { 0.2, 1.0, 1.0, 1.0 };
492 static const GLfloat cyan[4] = { 1.0, 0.2, 1.0, 1.0 };
493 GLfloat transform[16];
494 identity(transform);
495
496 gl->glClearColor(0.0, 0.0, 0.0, 0.0);
497 gl->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
498
499 // Translate and rotate the view
500 translate(transform, 0, -2.5, -20);
501 rotate(transform, 2 * M_PI * gld->view_rot[0] / 360.0, 1, 0, 0);
502 rotate(transform, 2 * M_PI * gld->view_rot[1] / 360.0, 0, 1, 0);
503 rotate(transform, 2 * M_PI * gld->view_rot[2] / 360.0, 0, 0, 1);
504
505 draw_gear(gld, gld->gear[0], transform, -2.0, -4.0, 0.0, gld->angle , 0, red);
506 draw_gear(gld, gld->gear[1], transform, 4.1, -4.0, 0.0, -2 * gld->angle - 9.0 , 0, green);
507 draw_gear(gld, gld->gear[2], transform, -2.1, 2.2, 0.0, -2 * gld->angle - 25.0, 0, blue);
508 draw_gear(gld, gld->gear[3], transform, -2.1, 8.4, 0.0, gld->angle , 0, yellow);
509 draw_gear(gld, gld->gear[4], transform, 6.4, -4.0, -4.5, gld->angle , 1, purple);
510 draw_gear(gld, gld->gear[5], transform, 6.4, 4.4, -4.5, -1 * gld->angle - 23.0, 1, cyan);
511}
512
513//-------------------------//
514
515static void render_gears(GLData *gld)
516{
517 if (gld->recreate_gears)
518 {
519 create_gears(gld);
520 gld->recreate_gears = 0;
521 }
522
523 gears_draw(gld);
524
525 gld->angle += 2.0;
526}
527
528// new window size or exposure
529static void
530gears_reshape(GLData *gld, int width, int height)
531{
532 Evas_GL_API *gl = gld->glapi;
533
534 // Update the projection matrix
535 perspective(gld->proj_mat, 60.0, width / (float)height, 1.0, 1024.0);
536
537 // Set the viewport
538 gl->glViewport(0, 0, (GLint) width, (GLint) height);
539
540}
541
542static const char vertex_shader[] =
543 "attribute vec3 position;\n"
544 "attribute vec3 normal;\n"
545 "uniform mat4 mvp;\n"
546 "uniform mat4 norm_mat;\n"
547 "uniform vec4 light_pos;\n"
548 "uniform vec4 material;\n"
549 "varying vec4 color;\n"
550 "\n"
551 "void main(void)\n"
552 "{\n"
553 " // Transform the normal to eye coordinates\n"
554 " vec3 N = normalize(vec3(norm_mat * vec4(normal, 1.0)));\n"
555 "\n"
556 " // The LightSourcePosition is actually its direction for directional ight\n"
557 " vec3 L = normalize(light_pos.xyz);\n"
558 "\n"
559 " // Multiply the diffuse value by the vertex color (which is fixed in this ase)\n"
560 " // to get the actual color that we will use to draw this vertex with\n"
561 " float diffuse = clamp(dot(N, L), 0.0, 1.0);\n"
562 " color = material*0.2 + diffuse * material;\n"
563 " color.a = 1.0; \n"
564 "\n"
565 " // Transform the position to clip coordinates\n"
566 " gl_Position = mvp * vec4(position, 1.0);\n"
567 "}";
568
569static const char fragment_shader[] =
570 "#ifdef GL_ES\n"
571 "precision mediump float;\n"
572 "#endif\n"
573 "varying vec4 color;\n"
574 "\n"
575 "void main(void)\n"
576 "{\n"
577 " gl_FragColor = color;\n"
578 "}";
579
580static void
581create_gears(GLData *gld)
582{
583 int i;
584
585 for (i = 0; i < 6; i++)
586 if (gld->gear[i]) free_gear(gld, gld->gear[i]);
587
588 // make the gears
589 gld->gear[0] = create_gear(gld, 1.0, 4.0, 1.0, gld->gear_teeth*2, 0.7);
590 gld->gear[1] = create_gear(gld, 0.5, 2.0, 2.0, gld->gear_teeth , 0.7);
591 gld->gear[2] = create_gear(gld, 1.3, 2.0, 0.5, gld->gear_teeth , 0.7);
592 gld->gear[3] = create_gear(gld, 2.0, 4.0, 2.0, gld->gear_teeth*2, 0.7);
593 gld->gear[4] = create_gear(gld, 1.5, 4.0, 0.5, gld->gear_teeth*2, 1.0);
594 gld->gear[5] = create_gear(gld, 2.5, 4.0, 2.5, gld->gear_teeth*2, 1.0);
595
596 gld->tot_vertices = 0;
597 for (i = 0; i < 6; i++)
598 gld->tot_vertices += gld->gear[i]->nvertices;
599
600 //printf("Teeth: %d, Total Number of vertices %d\n", gld->gear_teeth, gld->tot_vertices);
601}
602
603static void
604gears_init(GLData *gld)
605{
606 Evas_GL_API *gl = gld->glapi;
607
608 const char *p;
609 char msg[512];
610
611 gl->glEnable(GL_CULL_FACE);
612 gl->glEnable(GL_DEPTH_TEST);
613
614 p = vertex_shader;
615 gld->vtx_shader = gl->glCreateShader(GL_VERTEX_SHADER);
616 gl->glShaderSource(gld->vtx_shader, 1, &p, NULL);
617 gl->glCompileShader(gld->vtx_shader);
618 gl->glGetShaderInfoLog(gld->vtx_shader, sizeof msg, NULL, msg);
619 printf("vertex shader info: %s\n", msg);
620
621 p = fragment_shader;
622 gld->fgmt_shader = gl->glCreateShader(GL_FRAGMENT_SHADER);
623 gl->glShaderSource(gld->fgmt_shader, 1, &p, NULL);
624 gl->glCompileShader(gld->fgmt_shader);
625 gl->glGetShaderInfoLog(gld->fgmt_shader, sizeof msg, NULL, msg);
626 printf("fragment shader info: %s\n", msg);
627
628 gld->program = gl->glCreateProgram();
629 gl->glAttachShader(gld->program, gld->vtx_shader);
630 gl->glAttachShader(gld->program, gld->fgmt_shader);
631 gl->glBindAttribLocation(gld->program, 0, "position");
632 gl->glBindAttribLocation(gld->program, 1, "normal");
633
634 gl->glLinkProgram(gld->program);
635 gl->glGetProgramInfoLog(gld->program, sizeof msg, NULL, msg);
636 printf("info: %s\n", msg);
637
638 gl->glUseProgram(gld->program);
639 gld->mvp_loc = gl->glGetUniformLocation(gld->program, "mvp");
640 gld->norm_mat_loc = gl->glGetUniformLocation(gld->program, "norm_mat");
641 gld->light_pos_loc = gl->glGetUniformLocation(gld->program, "light_pos");
642 gld->material_loc = gl->glGetUniformLocation(gld->program, "material");
643 gl->glUniform4fv(gld->light_pos_loc, 1, gld->light_pos);
644
645 create_gears(gld);
646}
647
648static void
649gldata_init(GLData *gld)
650{
651 gld->initialized = 0;
652 gld->mouse_down = 0;
653
654 gld->view_rot[0] = 20.0;
655 gld->view_rot[1] = 30.0;
656 gld->view_rot[2] = 0.0;
657
658 gld->light_pos[0] = 5.0;
659 gld->light_pos[1] = 5.0;
660 gld->light_pos[2] = 10.0;
661 gld->light_pos[3] = 1.0;
662
663 gld->angle = 0.0;
664
665 gld->tot_vertices = 0;
666 gld->gear_teeth = 10;
667}
668
669//-------------------------//
670
671static void
672_init_gl(Evas_Object *obj)
673{
674 GLData *gld = evas_object_data_get(obj, "gld");
675 if (!gld) return;
676
677 gears_init(gld);
678}
679
680static void
681_del_gl(Evas_Object *obj)
682{
683 int i;
684 GLData *gld = evas_object_data_get(obj, "gld");
685 if (!gld)
686 {
687 printf("Unable to get GLData. \n");
688 return;
689 }
690 Evas_GL_API *gl = gld->glapi;
691
692 gl->glDeleteShader(gld->vtx_shader);
693 gl->glDeleteShader(gld->fgmt_shader);
694 gl->glDeleteProgram(gld->program);
695
696 for (i = 0; i < 6; i++)
697 if (gld->gear[i]) free_gear(gld, gld->gear[i]);
698
699 evas_object_data_del((Evas_Object*)obj, "gld");
700 free(gld);
701
702 Ecore_Animator *ani = evas_object_data_get(obj, "ani");
703 ecore_animator_del(ani);
704
705}
706
707static void
708_resize_gl(Evas_Object *obj)
709{
710 int w, h;
711 GLData *gld = evas_object_data_get(obj, "gld");
712 if (!gld) return;
713
714 elm_glview_size_get(obj, &w, &h);
715 gears_reshape(gld, w, h);
716}
717
718
719static void
720_draw_gl(Evas_Object *obj)
721{
722 GLData *gld = evas_object_data_get(obj, "gld");
723 if (!gld) return;
724 Evas_GL_API *gl = gld->glapi;
725
726 render_gears(gld);
727 gl->glFinish();
728}
729
730static Eina_Bool
731_anim(void *data)
732{
733 elm_glview_changed_set((Evas_Object*)data);
734 return EINA_TRUE;
735}
736
737static Eina_Bool
738_quit_idler(void *data)
739{
740 evas_object_del(data);
741
742 return ECORE_CALLBACK_CANCEL;
743}
744
745static void
746_on_done(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
747{
748 ecore_idler_add(_quit_idler, data);
749}
750
751static void
752_on_plus(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
753{
754 GLData *gld = evas_object_data_get(data, "gld");
755 gld->gear_teeth += 1;
756 gld->recreate_gears = 1;
757}
758
759static void
760_on_minus(void *data, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
761{
762 GLData *gld = evas_object_data_get(data, "gld");
763 if (gld->gear_teeth > 0)
764 {
765 gld->gear_teeth -= 1;
766 gld->recreate_gears -= 1;
767 }
768}
769
770static void
771_del(void *data EINA_UNUSED, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
772{
773 Ecore_Animator *ani = evas_object_data_get(obj, "ani");
774 ecore_animator_del(ani);
775}
776
777static void
778_key_down(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *obj, void *event_info)
779{
780 Evas_Event_Key_Down *ev;
781 ev = (Evas_Event_Key_Down *)event_info;
782 GLData *gld = evas_object_data_get(obj, "gld");
783
784 if (strcmp(ev->keyname, "Up") == 0)
785 {
786 gld->gear_teeth += 1;
787 gld->recreate_gears = 1;
788 return;
789 }
790
791 if (strcmp(ev->keyname, "Down") == 0)
792 {
793 gld->gear_teeth -= 1;
794 gld->recreate_gears = 1;
795 return;
796 }
797}
798
799static void
800_mouse_down(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
801{
802 GLData *gld = evas_object_data_get(obj, "gld");
803 gld->mouse_down = 1;
804}
805
806static void
807_mouse_move(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *obj, void *event_info)
808{
809 Evas_Event_Mouse_Move *ev;
810 ev = (Evas_Event_Mouse_Move *)event_info;
811 GLData *gld = evas_object_data_get(obj, "gld");
812 float dx = 0, dy = 0;
813
814 if (gld->mouse_down)
815 {
816 dx = ev->cur.canvas.x - ev->prev.canvas.x;
817 dy = ev->cur.canvas.y - ev->prev.canvas.y;
818
819 gld->view_rot[1] += 1.0 * dx;
820 gld->view_rot[0] += 1.0 * dy;
821 }
822}
823
824static void
825_mouse_up(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *obj, void *event_info EINA_UNUSED)
826{
827 GLData *gld = evas_object_data_get(obj, "gld");
828 gld->mouse_down = 0;
829}
830
831//---------------------------//
832void
833test_glview_manygears(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
834{
835 Evas_Object *win, *bg, *bx1, *bx2, *bt_ok, *bt_up, *bt_dn, *gl, *lb;
836 Ecore_Animator *ani;
837 GLData *gld = NULL;
838 char buf[PATH_MAX];
839
840 // alloc a data struct to hold our relevant gl info in
841 if (!(gld = calloc(1, sizeof(GLData)))) return;
842 gldata_init(gld);
843
844 // new window - do the usual and give it a name, title and delete handler
845 win = elm_win_util_standard_add("glview_manygears", "GLView Many Gears");
846 elm_win_autodel_set(win, EINA_TRUE);
847
848
849 // add an image bg
850 bg = elm_bg_add(win);
851 evas_object_size_hint_align_set(bg, EVAS_HINT_FILL, EVAS_HINT_FILL);
852 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
853 elm_bg_color_set(bg, 150, 150, 150);
854 snprintf(buf, sizeof(buf), "%s/images/logo.png", elm_app_data_dir_get());
855 elm_bg_file_set(bg, buf, NULL);
856 elm_win_resize_object_add(win, bg);
857 evas_object_show(bg);
858
859 bx1 = elm_box_add(win);
860 evas_object_size_hint_weight_set(bx1, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
861 elm_win_resize_object_add(win, bx1);
862 evas_object_show(bx1);
863
864
865 // Add a GLView
866 gl = elm_glview_add(win);
867 if (gl)
868 {
869 evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL);
870 evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
871
872 // Add a glview
873 elm_glview_mode_set(gl, 0
874 | ELM_GLVIEW_ALPHA
875 | ELM_GLVIEW_DEPTH
876 );
877 elm_glview_resize_policy_set(gl, ELM_GLVIEW_RESIZE_POLICY_RECREATE);
878 elm_glview_render_policy_set(gl, ELM_GLVIEW_RENDER_POLICY_ALWAYS);
879 elm_glview_init_func_set(gl, (Elm_GLView_Func_Cb)_init_gl);
880 elm_glview_del_func_set(gl, (Elm_GLView_Func_Cb)_del_gl);
881 elm_glview_resize_func_set(gl, (Elm_GLView_Func_Cb)_resize_gl);
882 elm_glview_render_func_set(gl, (Elm_GLView_Func_Cb)_draw_gl);
883 elm_box_pack_end(bx1, gl);
884 evas_object_show(gl);
885
886 // Add Mouse/Key Event Callbacks
887 elm_object_focus_set(gl, EINA_TRUE);
888 evas_object_event_callback_add(gl, EVAS_CALLBACK_KEY_DOWN, _key_down, gl);
889 evas_object_event_callback_add(gl, EVAS_CALLBACK_MOUSE_DOWN, _mouse_down, gl);
890 evas_object_event_callback_add(gl, EVAS_CALLBACK_MOUSE_UP, _mouse_up, gl);
891 evas_object_event_callback_add(gl, EVAS_CALLBACK_MOUSE_MOVE, _mouse_move, gl);
892
893 // Add animator for rendering
894 ani = ecore_animator_add(_anim, gl);
895 gld->glapi = elm_glview_gl_api_get(gl);
896 evas_object_data_set(gl, "ani", ani);
897 evas_object_data_set(gl, "gld", gld);
898 evas_object_event_callback_add(gl, EVAS_CALLBACK_DEL, _del, gl);
899
900 // Add Up/Down Buttons
901 bx2 = elm_box_add(win);
902 elm_box_horizontal_set(bx2, EINA_TRUE);
903 evas_object_size_hint_align_set(bx2, EVAS_HINT_FILL, EVAS_HINT_FILL);
904 evas_object_size_hint_weight_set(bx2, EVAS_HINT_EXPAND, 0.0);
905 evas_object_show(bx2);
906
907 bt_up = elm_button_add(win);
908 elm_object_text_set(bt_up, "+ Teeth");
909 evas_object_size_hint_align_set(bt_up, EVAS_HINT_FILL, EVAS_HINT_FILL);
910 evas_object_size_hint_weight_set(bt_up, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
911 elm_box_pack_end(bx2, bt_up);
912 evas_object_show(bt_up);
913 evas_object_smart_callback_add(bt_up, "clicked", _on_plus, gl);
914
915 bt_dn = elm_button_add(win);
916 elm_object_text_set(bt_dn, "- Teeth");
917 evas_object_size_hint_align_set(bt_dn, EVAS_HINT_FILL, EVAS_HINT_FILL);
918 evas_object_size_hint_weight_set(bt_dn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
919 elm_box_pack_end(bx2, bt_dn);
920 evas_object_show(bt_dn);
921 evas_object_smart_callback_add(bt_dn, "clicked", _on_minus, gl);
922
923 elm_box_pack_end(bx1, bx2);
924 }
925 else
926 {
927 lb = elm_label_add(bx1);
928 elm_object_text_set(lb, "<align=left> GL backend engine is not supported.<br/>"
929 " 1. Check your back-end engine or<br/>"
930 " 2. Run elementary_test with engine option or<br/>"
931 " ex) $ <b>ELM_ENGINE=gl</b> elementary_test<br/>"
932 " 3. Change your back-end engine from elementary_config.<br/></align>");
933 evas_object_size_hint_weight_set(lb, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
934 evas_object_size_hint_align_set(lb, EVAS_HINT_FILL, EVAS_HINT_FILL);
935 elm_box_pack_end(bx1, lb);
936 evas_object_show(lb);
937 free(gld);
938 }
939
940 // Add Close Button
941 bt_ok = elm_button_add(win);
942 elm_object_text_set(bt_ok, "Close");
943 evas_object_size_hint_align_set(bt_ok, EVAS_HINT_FILL, EVAS_HINT_FILL);
944 evas_object_size_hint_weight_set(bt_ok, EVAS_HINT_EXPAND, 0.0);
945 elm_box_pack_end(bx1, bt_ok);
946 evas_object_show(bt_ok);
947 evas_object_smart_callback_add(bt_ok, "clicked", _on_done, win);
948
949 evas_object_resize(win, 320, 480);
950 evas_object_show(win);
951}