1 #include <Elementary.h>
3 # include "elementary_config.h"
5 #ifndef ELM_LIB_QUICKLAUNCH
8 #define M_PI 3.14159265
11 typedef struct _Gear Gear;
12 typedef struct _GLData GLData;
20 // GL related data here..
42 GLuint light_location;
43 GLuint color_location;
49 static void gears_init(GLData *gld);
50 static void free_gear(Gear *gear);
51 static void gears_reshape(GLData *gld, int width, int height);
52 static void render_gears(GLData *gld);
54 //--------------------------------//
57 vert(GLfloat *p, GLfloat x, GLfloat y, GLfloat z, GLfloat *n)
69 /* Draw a gear wheel. You'll probably want to call this function when
70 * building a display list since we do a lot of trig here.
72 * Input: inner_radius - radius of hole at center
73 * outer_radius - radius at center of teeth
74 * width - width of gear
75 * teeth - number of teeth
76 * tooth_depth - depth of tooth
79 make_gear(GLData *gld, GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
80 GLint teeth, GLfloat tooth_depth)
89 const int tris_per_tooth = 20;
90 Evas_GL_API *gl = gld->glapi;
92 gear = (Gear*)malloc(sizeof(Gear));
97 r1 = outer_radius - tooth_depth / 2.0;
98 r2 = outer_radius + tooth_depth / 2.0;
100 da = 2.0 * M_PI / teeth / 4.0;
102 gear->vertices = calloc(teeth * tris_per_tooth * 3 * 6,
103 sizeof *gear->vertices);
107 for (i = 0; i < teeth; i++)
111 s[1] = sin(i * 2.0 * M_PI / teeth + da);
112 c[1] = cos(i * 2.0 * M_PI / teeth + da);
113 s[2] = sin(i * 2.0 * M_PI / teeth + da * 2);
114 c[2] = cos(i * 2.0 * M_PI / teeth + da * 2);
115 s[3] = sin(i * 2.0 * M_PI / teeth + da * 3);
116 c[3] = cos(i * 2.0 * M_PI / teeth + da * 3);
117 s[4] = sin(i * 2.0 * M_PI / teeth + da * 4);
118 c[4] = cos(i * 2.0 * M_PI / teeth + da * 4);
124 v = vert(v, r2 * c[1], r2 * s[1], width * 0.5, normal);
126 v = vert(v, r2 * c[1], r2 * s[1], width * 0.5, normal);
127 v = vert(v, r2 * c[2], r2 * s[2], width * 0.5, normal);
128 v = vert(v, r1 * c[0], r1 * s[0], width * 0.5, normal);
129 v = vert(v, r1 * c[3], r1 * s[3], width * 0.5, normal);
130 v = vert(v, r0 * c[0], r0 * s[0], width * 0.5, normal);
131 v = vert(v, r1 * c[4], r1 * s[4], width * 0.5, normal);
132 v = vert(v, r0 * c[4], r0 * s[4], width * 0.5, normal);
134 v = vert(v, r0 * c[4], r0 * s[4], width * 0.5, normal);
135 v = vert(v, r0 * c[0], r0 * s[0], width * 0.5, normal);
136 v = vert(v, r0 * c[4], r0 * s[4], -width * 0.5, normal);
137 v = vert(v, r0 * c[0], r0 * s[0], -width * 0.5, normal);
143 v = vert(v, r0 * c[4], r0 * s[4], -width * 0.5, normal);
145 v = vert(v, r0 * c[4], r0 * s[4], -width * 0.5, normal);
146 v = vert(v, r1 * c[4], r1 * s[4], -width * 0.5, normal);
147 v = vert(v, r0 * c[0], r0 * s[0], -width * 0.5, normal);
148 v = vert(v, r1 * c[3], r1 * s[3], -width * 0.5, normal);
149 v = vert(v, r1 * c[0], r1 * s[0], -width * 0.5, normal);
150 v = vert(v, r2 * c[2], r2 * s[2], -width * 0.5, normal);
151 v = vert(v, r2 * c[1], r2 * s[1], -width * 0.5, normal);
153 v = vert(v, r1 * c[0], r1 * s[0], width * 0.5, normal);
155 v = vert(v, r1 * c[0], r1 * s[0], width * 0.5, normal);
156 v = vert(v, r1 * c[0], r1 * s[0], -width * 0.5, normal);
157 v = vert(v, r2 * c[1], r2 * s[1], width * 0.5, normal);
158 v = vert(v, r2 * c[1], r2 * s[1], -width * 0.5, normal);
159 v = vert(v, r2 * c[2], r2 * s[2], width * 0.5, normal);
160 v = vert(v, r2 * c[2], r2 * s[2], -width * 0.5, normal);
161 v = vert(v, r1 * c[3], r1 * s[3], width * 0.5, normal);
162 v = vert(v, r1 * c[3], r1 * s[3], -width * 0.5, normal);
163 v = vert(v, r1 * c[4], r1 * s[4], width * 0.5, normal);
164 v = vert(v, r1 * c[4], r1 * s[4], -width * 0.5, normal);
166 v = vert(v, r1 * c[4], r1 * s[4], -width * 0.5, normal);
169 gear->count = (v - gear->vertices) / 6;
171 gl->glGenBuffers(1, &gear->vbo);
172 gl->glBindBuffer(GL_ARRAY_BUFFER, gear->vbo);
173 gl->glBufferData(GL_ARRAY_BUFFER, gear->count * 6 * 4,
174 gear->vertices, GL_STATIC_DRAW);
181 free_gear(Gear *gear)
183 free(gear->vertices);
189 multiply(GLfloat *m, const GLfloat *n)
192 const GLfloat *row, *column;
196 for (i = 0; i < 16; i++)
200 row = n + d.quot * 4;
202 for (j = 0; j < 4; j++)
203 tmp[i] += row[j] * column[j * 4];
205 memcpy(m, &tmp, sizeof tmp);
209 rotate(GLfloat *m, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
217 x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
218 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) + x * s, 0,
219 x * z * (1 - c) + y * s, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
227 translate(GLfloat *m, GLfloat x, GLfloat y, GLfloat z)
229 GLfloat t[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 };
235 draw_gear(GLData *gld, Gear *gear, GLfloat *m,
236 GLfloat x, GLfloat y, GLfloat angle, const GLfloat *color)
238 Evas_GL_API *gl = gld->glapi;
241 memcpy(tmp, m, sizeof tmp);
242 translate(tmp, x, y, 0);
243 rotate(tmp, 2 * M_PI * angle / 360.0, 0, 0, 1);
244 gl->glUniformMatrix4fv(gld->proj_location, 1, GL_FALSE, tmp);
245 gl->glUniform3fv(gld->light_location, 1, gld->light);
246 gl->glUniform4fv(gld->color_location, 1, color);
248 gl->glBindBuffer(GL_ARRAY_BUFFER, gear->vbo);
250 gl->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
251 6 * sizeof(GLfloat), NULL);
252 gl->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,
253 6 * sizeof(GLfloat), (GLfloat *) 0 + 3);
254 gl->glEnableVertexAttribArray(0);
255 gl->glEnableVertexAttribArray(1);
256 gl->glDrawArrays(GL_TRIANGLE_STRIP, 0, gear->count);
260 gears_draw(GLData *gld)
262 Evas_GL_API *gl = gld->glapi;
264 static const GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
265 static const GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
266 static const GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
269 gl->glClearColor(0.8, 0.8, 0.1, 0.5);
270 gl->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
272 memcpy(m, gld->proj, sizeof m);
273 rotate(m, 2 * M_PI * gld->view_rotx / 360.0, 1, 0, 0);
274 rotate(m, 2 * M_PI * gld->view_roty / 360.0, 0, 1, 0);
275 rotate(m, 2 * M_PI * gld->view_rotz / 360.0, 0, 0, 1);
277 draw_gear(gld, gld->gear1, m, -3.0, -2.0, gld->angle, red);
278 draw_gear(gld, gld->gear2, m, 3.1, -2.0, -2 * gld->angle - 9.0, green);
279 draw_gear(gld, gld->gear3, m, -3.1, 4.2, -2 * gld->angle - 25.0, blue);
282 static void render_gears(GLData *gld)
289 /* new window size or exposure */
291 gears_reshape(GLData *gld, int width, int height)
293 Evas_GL_API *gl = gld->glapi;
295 GLfloat ar, m[16] = {
307 m[0] = 0.1 * ar / width;
308 m[5] = 0.1 * ar / height;
309 memcpy(gld->proj, m, sizeof gld->proj);
310 gl->glViewport(0, 0, (GLint) width, (GLint) height);
313 static const char vertex_shader[] =
314 "uniform mat4 proj;\n"
315 "attribute vec4 position;\n"
316 "attribute vec4 normal;\n"
317 "varying vec3 rotated_normal;\n"
318 "varying vec3 rotated_position;\n"
322 " gl_Position = proj * position;\n"
323 " rotated_position = gl_Position.xyz;\n"
324 " tmp = proj * normal;\n"
325 " rotated_normal = tmp.xyz;\n"
328 static const char fragment_shader[] =
329 "precision mediump float;\n"
330 "uniform vec4 color;\n"
331 "uniform vec3 light;\n"
332 "varying vec3 rotated_normal;\n"
333 "varying vec3 rotated_position;\n"
334 "vec3 light_direction;\n"
335 "vec4 white = vec4(0.5, 0.5, 0.5, 1.0);\n"
338 " light_direction = normalize(light - rotated_position);\n"
339 " gl_FragColor = color + white * dot(light_direction, rotated_normal);\n"
344 gears_init(GLData *gld)
346 Evas_GL_API *gl = gld->glapi;
351 gl->glEnable(GL_CULL_FACE);
352 gl->glEnable(GL_DEPTH_TEST);
355 gld->vtx_shader = gl->glCreateShader(GL_VERTEX_SHADER);
356 gl->glShaderSource(gld->vtx_shader, 1, &p, NULL);
357 gl->glCompileShader(gld->vtx_shader);
358 gl->glGetShaderInfoLog(gld->vtx_shader, sizeof msg, NULL, msg);
359 printf("vertex shader info: %s\n", msg);
362 gld->fgmt_shader = gl->glCreateShader(GL_FRAGMENT_SHADER);
363 gl->glShaderSource(gld->fgmt_shader, 1, &p, NULL);
364 gl->glCompileShader(gld->fgmt_shader);
365 gl->glGetShaderInfoLog(gld->fgmt_shader, sizeof msg, NULL, msg);
366 printf("fragment shader info: %s\n", msg);
368 gld->program = gl->glCreateProgram();
369 gl->glAttachShader(gld->program, gld->vtx_shader);
370 gl->glAttachShader(gld->program, gld->fgmt_shader);
371 gl->glBindAttribLocation(gld->program, 0, "position");
372 gl->glBindAttribLocation(gld->program, 1, "normal");
374 gl->glLinkProgram(gld->program);
375 gl->glGetProgramInfoLog(gld->program, sizeof msg, NULL, msg);
376 printf("info: %s\n", msg);
378 gl->glUseProgram(gld->program);
379 gld->proj_location = gl->glGetUniformLocation(gld->program, "proj");
380 gld->light_location = gl->glGetUniformLocation(gld->program, "light");
381 gld->color_location = gl->glGetUniformLocation(gld->program, "color");
384 gld->gear1 = make_gear(gld, 1.0, 4.0, 1.0, 20, 0.7);
385 gld->gear2 = make_gear(gld, 0.5, 2.0, 2.0, 10, 0.7);
386 gld->gear3 = make_gear(gld, 1.3, 2.0, 0.5, 10, 0.7);
391 gldata_init(GLData *gld)
393 gld->initialized = 0;
396 gld->view_rotx = -20.0;
397 gld->view_roty = -30.0;
398 gld->view_rotz = 0.0;
403 gld->light[2] = -5.0;
407 //-------------------------//
410 _init_gl(Evas_Object *obj)
412 GLData *gld = evas_object_data_get(obj, "gld");
418 _del_gl(Evas_Object *obj)
420 GLData *gld = evas_object_data_get(obj, "gld");
423 printf("Unable to get GLData. \n");
426 Evas_GL_API *gl = gld->glapi;
428 gl->glDeleteShader(gld->vtx_shader);
429 gl->glDeleteShader(gld->fgmt_shader);
430 gl->glDeleteProgram(gld->program);
431 gl->glDeleteBuffers(1, &gld->gear1->vbo);
432 gl->glDeleteBuffers(1, &gld->gear2->vbo);
433 gl->glDeleteBuffers(1, &gld->gear3->vbo);
435 free_gear(gld->gear1);
436 free_gear(gld->gear2);
437 free_gear(gld->gear3);
439 evas_object_data_del((Evas_Object*)obj, "..gld");
445 _resize_gl(Evas_Object *obj)
448 GLData *gld = evas_object_data_get(obj, "gld");
450 elm_glview_size_get(obj, &w, &h);
452 // GL Viewport stuff. you can avoid doing this if viewport is all the
453 // same as last frame if you want
454 gears_reshape(gld, w,h);
460 _draw_gl(Evas_Object *obj)
462 Evas_GL_API *gl = elm_glview_gl_api_get(obj);
463 GLData *gld = evas_object_data_get(obj, "gld");
473 elm_glview_changed_set(data);
478 _on_done(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
480 evas_object_del((Evas_Object*)data);
485 _del(void *data __UNUSED__, Evas *evas __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
487 Ecore_Animator *ani = evas_object_data_get(obj, "ani");
488 ecore_animator_del(ani);
493 _key_down(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info)
495 Evas_Event_Key_Down *ev;
496 ev = (Evas_Event_Key_Down *)event_info;
497 GLData *gld = evas_object_data_get(obj, "gld");
499 if (strcmp(ev->keyname, "Left") == 0)
501 gld->view_roty += 5.0;
505 if (strcmp(ev->keyname, "Right") == 0)
507 gld->view_roty -= 5.0;
511 if (strcmp(ev->keyname, "Up") == 0)
513 gld->view_rotx += 5.0;
517 if (strcmp(ev->keyname, "Down") == 0)
519 gld->view_rotx -= 5.0;
522 if ((strcmp(ev->keyname, "Escape") == 0) ||
523 (strcmp(ev->keyname, "Return") == 0))
525 //_on_done(data, obj, event_info);
531 _mouse_down(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
533 GLData *gld = evas_object_data_get(obj, "gld");
538 _mouse_move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
540 Evas_Event_Mouse_Move *ev;
541 ev = (Evas_Event_Mouse_Move *)event_info;
542 GLData *gld = evas_object_data_get(obj, "gld");
543 float dx = 0, dy = 0;
547 dx = ev->cur.canvas.x - ev->prev.canvas.x;
548 dy = ev->cur.canvas.y - ev->prev.canvas.y;
550 gld->view_roty += -1.0 * dx;
551 gld->view_rotx += -1.0 * dy;
556 _mouse_up(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
558 GLData *gld = evas_object_data_get(obj, "gld");
564 test_glview(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
566 Evas_Object *win, *bg, *bx, *bt, *gl;
570 // alloc a data struct to hold our relevant gl info in
571 if (!(gld = calloc(1, sizeof(GLData)))) return;
574 // new window - do the usual and give it a name, title and delete handler
575 win = elm_win_add(NULL, "glview", ELM_WIN_BASIC);
576 elm_win_title_set(win, "GLView");
577 elm_win_autodel_set(win, 1);
581 bg = elm_bg_add(win);
582 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
583 elm_win_resize_object_add(win, bg);
584 evas_object_show(bg);
586 bx = elm_box_add(win);
587 evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
588 elm_win_resize_object_add(win, bx);
589 evas_object_show(bx);
592 gl = elm_glview_add(win);
593 evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL);
594 evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
595 elm_glview_mode_set(gl, ELM_GLVIEW_ALPHA|ELM_GLVIEW_DEPTH);
596 elm_glview_resize_policy_set(gl, ELM_GLVIEW_RESIZE_POLICY_RECREATE);
597 elm_glview_render_policy_set(gl, ELM_GLVIEW_RENDER_POLICY_ALWAYS);
598 elm_glview_init_func_set(gl, _init_gl);
599 elm_glview_del_func_set(gl, _del_gl);
600 elm_glview_resize_func_set(gl, _resize_gl);
601 elm_glview_render_func_set(gl, (Elm_GLView_Func)_draw_gl);
602 elm_box_pack_end(bx, gl);
603 evas_object_show(gl);
605 // Add Mouse/Key Event Callbacks
606 elm_object_focus(gl);
607 evas_object_event_callback_add(gl, EVAS_CALLBACK_KEY_DOWN, _key_down, gl);
608 evas_object_event_callback_add(gl, EVAS_CALLBACK_MOUSE_DOWN, _mouse_down, gl);
609 evas_object_event_callback_add(gl, EVAS_CALLBACK_MOUSE_UP, _mouse_up, gl);
610 evas_object_event_callback_add(gl, EVAS_CALLBACK_MOUSE_MOVE, _mouse_move, gl);
613 // Animator and other vars
614 ani = ecore_animator_add(_anim, gl);
615 gld->glapi = elm_glview_gl_api_get(gl);
616 evas_object_data_set(gl, "ani", ani);
617 evas_object_data_set(gl, "gld", gld);
618 evas_object_event_callback_add(gl, EVAS_CALLBACK_DEL, _del, gl);
621 /* add an ok button */
622 bt = elm_button_add(win);
623 elm_object_text_set(bt, "OK");
624 evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
625 evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, 0.0);
626 elm_box_pack_end(bx, bt);
627 evas_object_show(bt);
628 evas_object_smart_callback_add(bt, "clicked", _on_done, win);
630 evas_object_resize(win, 320, 480);
631 evas_object_show(win);