2 # include "elementary_config.h"
4 #include <Elementary.h>
5 #ifndef ELM_LIB_QUICKLAUNCH
7 #include <Elementary.h>
9 #define M_PI 3.14159265
12 typedef struct _Gear Gear;
13 typedef struct _GLData GLData;
21 // GL related data here..
43 GLuint light_location;
44 GLuint color_location;
50 static void gears_init(GLData *gld);
51 static void free_gear(Gear *gear);
52 static void gears_reshape(GLData *gld, int width, int height);
53 static void render_gears(GLData *gld);
55 //--------------------------------//
58 vert(GLfloat *p, GLfloat x, GLfloat y, GLfloat z, GLfloat *n)
70 /* Draw a gear wheel. You'll probably want to call this function when
71 * building a display list since we do a lot of trig here.
73 * Input: inner_radius - radius of hole at center
74 * outer_radius - radius at center of teeth
75 * width - width of gear
76 * teeth - number of teeth
77 * tooth_depth - depth of tooth
80 make_gear(GLData *gld, GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
81 GLint teeth, GLfloat tooth_depth)
90 const int tris_per_tooth = 20;
91 Evas_GL_API *gl = gld->glapi;
93 gear = (Gear*)malloc(sizeof(Gear));
98 r1 = outer_radius - tooth_depth / 2.0;
99 r2 = outer_radius + tooth_depth / 2.0;
101 da = 2.0 * M_PI / teeth / 4.0;
103 gear->vertices = calloc(teeth * tris_per_tooth * 3 * 6,
104 sizeof *gear->vertices);
108 for (i = 0; i < teeth; i++)
112 s[1] = sin(i * 2.0 * M_PI / teeth + da);
113 c[1] = cos(i * 2.0 * M_PI / teeth + da);
114 s[2] = sin(i * 2.0 * M_PI / teeth + da * 2);
115 c[2] = cos(i * 2.0 * M_PI / teeth + da * 2);
116 s[3] = sin(i * 2.0 * M_PI / teeth + da * 3);
117 c[3] = cos(i * 2.0 * M_PI / teeth + da * 3);
118 s[4] = sin(i * 2.0 * M_PI / teeth + da * 4);
119 c[4] = cos(i * 2.0 * M_PI / teeth + da * 4);
125 v = vert(v, r2 * c[1], r2 * s[1], width * 0.5, normal);
127 v = vert(v, r2 * c[1], r2 * s[1], width * 0.5, normal);
128 v = vert(v, r2 * c[2], r2 * s[2], width * 0.5, normal);
129 v = vert(v, r1 * c[0], r1 * s[0], width * 0.5, normal);
130 v = vert(v, r1 * c[3], r1 * s[3], width * 0.5, normal);
131 v = vert(v, r0 * c[0], r0 * s[0], width * 0.5, normal);
132 v = vert(v, r1 * c[4], r1 * s[4], width * 0.5, normal);
133 v = vert(v, r0 * c[4], r0 * s[4], width * 0.5, normal);
135 v = vert(v, r0 * c[4], r0 * s[4], width * 0.5, normal);
136 v = vert(v, r0 * c[0], r0 * s[0], width * 0.5, normal);
137 v = vert(v, r0 * c[4], r0 * s[4], -width * 0.5, normal);
138 v = vert(v, r0 * c[0], r0 * s[0], -width * 0.5, normal);
144 v = vert(v, r0 * c[4], r0 * s[4], -width * 0.5, normal);
146 v = vert(v, r0 * c[4], r0 * s[4], -width * 0.5, normal);
147 v = vert(v, r1 * c[4], r1 * s[4], -width * 0.5, normal);
148 v = vert(v, r0 * c[0], r0 * s[0], -width * 0.5, normal);
149 v = vert(v, r1 * c[3], r1 * s[3], -width * 0.5, normal);
150 v = vert(v, r1 * c[0], r1 * s[0], -width * 0.5, normal);
151 v = vert(v, r2 * c[2], r2 * s[2], -width * 0.5, normal);
152 v = vert(v, r2 * c[1], r2 * s[1], -width * 0.5, normal);
154 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, r1 * c[0], r1 * s[0], -width * 0.5, normal);
158 v = vert(v, r2 * c[1], r2 * s[1], width * 0.5, normal);
159 v = vert(v, r2 * c[1], r2 * s[1], -width * 0.5, normal);
160 v = vert(v, r2 * c[2], r2 * s[2], width * 0.5, normal);
161 v = vert(v, r2 * c[2], r2 * s[2], -width * 0.5, normal);
162 v = vert(v, r1 * c[3], r1 * s[3], width * 0.5, normal);
163 v = vert(v, r1 * c[3], r1 * s[3], -width * 0.5, normal);
164 v = vert(v, r1 * c[4], r1 * s[4], width * 0.5, normal);
165 v = vert(v, r1 * c[4], r1 * s[4], -width * 0.5, normal);
167 v = vert(v, r1 * c[4], r1 * s[4], -width * 0.5, normal);
170 gear->count = (v - gear->vertices) / 6;
172 gl->glGenBuffers(1, &gear->vbo);
173 gl->glBindBuffer(GL_ARRAY_BUFFER, gear->vbo);
174 gl->glBufferData(GL_ARRAY_BUFFER, gear->count * 6 * 4,
175 gear->vertices, GL_STATIC_DRAW);
182 free_gear(Gear *gear)
184 free(gear->vertices);
190 multiply(GLfloat *m, const GLfloat *n)
193 const GLfloat *row, *column;
197 for (i = 0; i < 16; i++)
201 row = n + d.quot * 4;
203 for (j = 0; j < 4; j++)
204 tmp[i] += row[j] * column[j * 4];
206 memcpy(m, &tmp, sizeof tmp);
210 rotate(GLfloat *m, GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
218 x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0,
219 x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) + x * s, 0,
220 x * z * (1 - c) + y * s, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0,
228 translate(GLfloat *m, GLfloat x, GLfloat y, GLfloat z)
230 GLfloat t[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 };
236 draw_gear(GLData *gld, Gear *gear, GLfloat *m,
237 GLfloat x, GLfloat y, GLfloat angle, const GLfloat *color)
239 Evas_GL_API *gl = gld->glapi;
242 memcpy(tmp, m, sizeof tmp);
243 translate(tmp, x, y, 0);
244 rotate(tmp, 2 * M_PI * angle / 360.0, 0, 0, 1);
245 gl->glUniformMatrix4fv(gld->proj_location, 1, GL_FALSE, tmp);
246 gl->glUniform3fv(gld->light_location, 1, gld->light);
247 gl->glUniform4fv(gld->color_location, 1, color);
249 gl->glBindBuffer(GL_ARRAY_BUFFER, gear->vbo);
251 gl->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
252 6 * sizeof(GLfloat), NULL);
253 gl->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,
254 6 * sizeof(GLfloat), (GLfloat *) 0 + 3);
255 gl->glEnableVertexAttribArray(0);
256 gl->glEnableVertexAttribArray(1);
257 gl->glDrawArrays(GL_TRIANGLE_STRIP, 0, gear->count);
261 gears_draw(GLData *gld)
263 Evas_GL_API *gl = gld->glapi;
265 static const GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 };
266 static const GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 };
267 static const GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 };
270 gl->glClearColor(0.8, 0.8, 0.1, 0.5);
271 gl->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
273 memcpy(m, gld->proj, sizeof m);
274 rotate(m, 2 * M_PI * gld->view_rotx / 360.0, 1, 0, 0);
275 rotate(m, 2 * M_PI * gld->view_roty / 360.0, 0, 1, 0);
276 rotate(m, 2 * M_PI * gld->view_rotz / 360.0, 0, 0, 1);
278 draw_gear(gld, gld->gear1, m, -3.0, -2.0, gld->angle, red);
279 draw_gear(gld, gld->gear2, m, 3.1, -2.0, -2 * gld->angle - 9.0, green);
280 draw_gear(gld, gld->gear3, m, -3.1, 4.2, -2 * gld->angle - 25.0, blue);
283 static void render_gears(GLData *gld)
290 /* new window size or exposure */
292 gears_reshape(GLData *gld, int width, int height)
294 Evas_GL_API *gl = gld->glapi;
296 GLfloat ar, m[16] = {
308 m[0] = 0.1 * ar / width;
309 m[5] = 0.1 * ar / height;
310 memcpy(gld->proj, m, sizeof gld->proj);
311 gl->glViewport(0, 0, (GLint) width, (GLint) height);
314 static const char vertex_shader[] =
315 "uniform mat4 proj;\n"
316 "attribute vec4 position;\n"
317 "attribute vec4 normal;\n"
318 "varying vec3 rotated_normal;\n"
319 "varying vec3 rotated_position;\n"
323 " gl_Position = proj * position;\n"
324 " rotated_position = gl_Position.xyz;\n"
325 " tmp = proj * normal;\n"
326 " rotated_normal = tmp.xyz;\n"
329 static const char fragment_shader[] =
331 "precision mediump float;\n"
333 "uniform vec4 color;\n"
334 "uniform vec3 light;\n"
335 "varying vec3 rotated_normal;\n"
336 "varying vec3 rotated_position;\n"
337 "vec3 light_direction;\n"
338 "vec4 white = vec4(0.5, 0.5, 0.5, 1.0);\n"
341 " light_direction = normalize(light - rotated_position);\n"
342 " gl_FragColor = color + white * dot(light_direction, rotated_normal);\n"
346 gears_init(GLData *gld)
348 Evas_GL_API *gl = gld->glapi;
353 gl->glEnable(GL_CULL_FACE);
354 gl->glEnable(GL_DEPTH_TEST);
357 gld->vtx_shader = gl->glCreateShader(GL_VERTEX_SHADER);
358 gl->glShaderSource(gld->vtx_shader, 1, &p, NULL);
359 gl->glCompileShader(gld->vtx_shader);
360 gl->glGetShaderInfoLog(gld->vtx_shader, sizeof msg, NULL, msg);
361 printf("vertex shader info: %s\n", msg);
364 gld->fgmt_shader = gl->glCreateShader(GL_FRAGMENT_SHADER);
365 gl->glShaderSource(gld->fgmt_shader, 1, &p, NULL);
366 gl->glCompileShader(gld->fgmt_shader);
367 gl->glGetShaderInfoLog(gld->fgmt_shader, sizeof msg, NULL, msg);
368 printf("fragment shader info: %s\n", msg);
370 gld->program = gl->glCreateProgram();
371 gl->glAttachShader(gld->program, gld->vtx_shader);
372 gl->glAttachShader(gld->program, gld->fgmt_shader);
373 gl->glBindAttribLocation(gld->program, 0, "position");
374 gl->glBindAttribLocation(gld->program, 1, "normal");
376 gl->glLinkProgram(gld->program);
377 gl->glGetProgramInfoLog(gld->program, sizeof msg, NULL, msg);
378 printf("info: %s\n", msg);
380 gl->glUseProgram(gld->program);
381 gld->proj_location = gl->glGetUniformLocation(gld->program, "proj");
382 gld->light_location = gl->glGetUniformLocation(gld->program, "light");
383 gld->color_location = gl->glGetUniformLocation(gld->program, "color");
386 gld->gear1 = make_gear(gld, 1.0, 4.0, 1.0, 20, 0.7);
387 gld->gear2 = make_gear(gld, 0.5, 2.0, 2.0, 10, 0.7);
388 gld->gear3 = make_gear(gld, 1.3, 2.0, 0.5, 10, 0.7);
392 gldata_init(GLData *gld)
394 gld->initialized = 0;
397 gld->view_rotx = -20.0;
398 gld->view_roty = -30.0;
399 gld->view_rotz = 0.0;
404 gld->light[2] = -5.0;
408 //-------------------------//
411 _init_gl(Evas_Object *obj)
413 GLData *gld = evas_object_data_get(obj, "gld");
419 _del_gl(Evas_Object *obj)
421 GLData *gld = evas_object_data_get(obj, "gld");
424 printf("Unable to get GLData. \n");
427 Evas_GL_API *gl = gld->glapi;
429 gl->glDeleteShader(gld->vtx_shader);
430 gl->glDeleteShader(gld->fgmt_shader);
431 gl->glDeleteProgram(gld->program);
432 gl->glDeleteBuffers(1, &gld->gear1->vbo);
433 gl->glDeleteBuffers(1, &gld->gear2->vbo);
434 gl->glDeleteBuffers(1, &gld->gear3->vbo);
436 free_gear(gld->gear1);
437 free_gear(gld->gear2);
438 free_gear(gld->gear3);
440 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);
458 _draw_gl(Evas_Object *obj)
460 Evas_GL_API *gl = elm_glview_gl_api_get(obj);
461 GLData *gld = evas_object_data_get(obj, "gld");
471 elm_glview_changed_set(data);
476 _on_done(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
478 evas_object_del((Evas_Object*)data);
482 _del(void *data __UNUSED__, Evas *evas __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
484 Ecore_Animator *ani = evas_object_data_get(obj, "ani");
485 ecore_animator_del(ani);
489 _key_down(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info)
491 Evas_Event_Key_Down *ev;
492 ev = (Evas_Event_Key_Down *)event_info;
493 GLData *gld = evas_object_data_get(obj, "gld");
495 if (strcmp(ev->keyname, "Left") == 0)
497 gld->view_roty += 5.0;
501 if (strcmp(ev->keyname, "Right") == 0)
503 gld->view_roty -= 5.0;
507 if (strcmp(ev->keyname, "Up") == 0)
509 gld->view_rotx += 5.0;
513 if (strcmp(ev->keyname, "Down") == 0)
515 gld->view_rotx -= 5.0;
518 if ((strcmp(ev->keyname, "Escape") == 0) ||
519 (strcmp(ev->keyname, "Return") == 0))
521 //_on_done(data, obj, event_info);
527 _mouse_down(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
529 GLData *gld = evas_object_data_get(obj, "gld");
534 _mouse_move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
536 Evas_Event_Mouse_Move *ev;
537 ev = (Evas_Event_Mouse_Move *)event_info;
538 GLData *gld = evas_object_data_get(obj, "gld");
539 float dx = 0, dy = 0;
543 dx = ev->cur.canvas.x - ev->prev.canvas.x;
544 dy = ev->cur.canvas.y - ev->prev.canvas.y;
546 gld->view_roty += -1.0 * dx;
547 gld->view_rotx += -1.0 * dy;
552 _mouse_up(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
554 GLData *gld = evas_object_data_get(obj, "gld");
559 test_glview(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
561 Evas_Object *win, *bx, *bt, *gl;
565 // alloc a data struct to hold our relevant gl info in
566 if (!(gld = calloc(1, sizeof(GLData)))) return;
569 // new window - do the usual and give it a name, title and delete handler
570 // Set the engine to opengl_x11
571 elm_config_preferred_engine_set("opengl_x11");
572 win = elm_win_util_standard_add("glview", "GLView");
573 // Set preferred engine back to default from config
574 elm_config_preferred_engine_set(NULL);
576 elm_win_autodel_set(win, EINA_TRUE);
578 bx = elm_box_add(win);
579 evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
580 elm_win_resize_object_add(win, bx);
581 evas_object_show(bx);
584 gl = elm_glview_add(win);
585 evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL);
586 evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
587 elm_glview_mode_set(gl, ELM_GLVIEW_ALPHA|ELM_GLVIEW_DEPTH);
588 elm_glview_resize_policy_set(gl, ELM_GLVIEW_RESIZE_POLICY_RECREATE);
589 elm_glview_render_policy_set(gl, ELM_GLVIEW_RENDER_POLICY_ALWAYS);
590 elm_glview_init_func_set(gl, _init_gl);
591 elm_glview_del_func_set(gl, _del_gl);
592 elm_glview_resize_func_set(gl, _resize_gl);
593 elm_glview_render_func_set(gl, (Elm_GLView_Func_Cb)_draw_gl);
594 elm_box_pack_end(bx, gl);
595 evas_object_show(gl);
597 // Add Mouse/Key Event Callbacks
598 elm_object_focus_set(gl, EINA_TRUE);
599 evas_object_event_callback_add(gl, EVAS_CALLBACK_KEY_DOWN, _key_down, gl);
600 evas_object_event_callback_add(gl, EVAS_CALLBACK_MOUSE_DOWN, _mouse_down, gl);
601 evas_object_event_callback_add(gl, EVAS_CALLBACK_MOUSE_UP, _mouse_up, gl);
602 evas_object_event_callback_add(gl, EVAS_CALLBACK_MOUSE_MOVE, _mouse_move, gl);
604 // Animator and other vars
605 ani = ecore_animator_add(_anim, gl);
606 gld->glapi = elm_glview_gl_api_get(gl);
607 evas_object_data_set(gl, "ani", ani);
608 evas_object_data_set(gl, "gld", gld);
609 evas_object_event_callback_add(gl, EVAS_CALLBACK_DEL, _del, gl);
611 /* add an ok button */
612 bt = elm_button_add(win);
613 elm_object_text_set(bt, "OK");
614 evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
615 evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, 0.0);
616 elm_box_pack_end(bx, bt);
617 evas_object_show(bt);
618 evas_object_smart_callback_add(bt, "clicked", _on_done, win);
620 evas_object_resize(win, 320, 480);
621 evas_object_show(win);