2 * @defgroup GLView GLView
4 * A GLView widget allows for simple GL rendering in elementary environment.
5 * GLView hides all the complicated evas_gl details so that the user only
6 * has to deal with registering a few callback functions for rendering
7 * to a surface using OpenGL APIs.
9 * Below is an illustrative example of how to use GLView and and OpenGL
10 * to render in elementary environment.
12 // Simple GLView example
13 #include <Elementary.h>
17 typedef struct _GLData GLData;
19 // GL related data here..
31 static float red = 1.0;
33 //--------------------------------//
34 // a helper funtion to load shaders from a shader source
36 load_shader( GLData *gld, GLenum type, const char *shader_src )
38 Evas_GL_API *gl = gld->glapi;
42 // Create the shader object
43 shader = gl->glCreateShader(type);
47 // Load/Compile shader source
48 gl->glShaderSource(shader, 1, &shader_src, NULL);
49 gl->glCompileShader(shader);
50 gl->glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
55 gl->glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_len);
58 char* info_log = malloc(sizeof(char) * info_len);
60 gl->glGetShaderInfoLog(shader, info_len, NULL, info_log);
61 printf("Error compiling shader:\n%s\n======\n%s\n======\n", info_log, shader_src );
64 gl->glDeleteShader(shader);
71 // Initialize the shader and program object
73 init_shaders(GLData *gld)
75 Evas_GL_API *gl = gld->glapi;
77 "attribute vec4 vPosition; \n"
80 " gl_Position = vPosition; \n"
85 "precision mediump float; \n"
89 " gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n"
94 // Load the vertex/fragment shaders
95 gld->vtx_shader = load_shader(gld, GL_VERTEX_SHADER, (const char*)vShaderStr);
96 gld->fgmt_shader = load_shader(gld, GL_FRAGMENT_SHADER, (const char*)fShaderStr);
98 // Create the program object
99 gld->program = gl->glCreateProgram( );
103 gl->glAttachShader(gld->program, gld->vtx_shader);
104 gl->glAttachShader(gld->program, gld->fgmt_shader);
106 gl->glBindAttribLocation(gld->program, 0, "vPosition");
107 gl->glLinkProgram(gld->program);
108 gl->glGetProgramiv(gld->program, GL_LINK_STATUS, &linked);
113 gl->glGetProgramiv(gld->program, GL_INFO_LOG_LENGTH, &info_len);
116 char* info_log = malloc(sizeof(char) * info_len);
118 gl->glGetProgramInfoLog(gld->program, info_len, NULL, info_log);
119 printf("Error linking program:\n%s\n", info_log);
122 gl->glDeleteProgram(gld->program);
131 // intialize callback that gets called once for intialization
133 _init_gl(Evas_Object *obj)
135 GLData *gld = evas_object_data_get(obj, "gld");
136 Evas_GL_API *gl = gld->glapi;
137 GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f,
141 if (!init_shaders(gld))
143 printf("Error Initializing Shaders\n");
147 gl->glGenBuffers(1, &gld->vbo);
148 gl->glBindBuffer(GL_ARRAY_BUFFER, gld->vbo);
149 gl->glBufferData(GL_ARRAY_BUFFER, 3 * 3 * 4, vVertices, GL_STATIC_DRAW);
152 // delete callback gets called when glview is deleted
154 _del_gl(Evas_Object *obj)
156 GLData *gld = evas_object_data_get(obj, "gld");
159 printf("Unable to get GLData. \n");
162 Evas_GL_API *gl = gld->glapi;
164 gl->glDeleteShader(gld->vtx_shader);
165 gl->glDeleteShader(gld->fgmt_shader);
166 gl->glDeleteProgram(gld->program);
167 gl->glDeleteBuffers(1, &gld->vbo);
169 evas_object_data_del((Evas_Object*)obj, "..gld");
173 // resize callback gets called every time object is resized
175 _resize_gl(Evas_Object *obj)
178 GLData *gld = evas_object_data_get(obj, "gld");
179 Evas_GL_API *gl = gld->glapi;
181 elm_glview_size_get(obj, &w, &h);
183 // GL Viewport stuff. you can avoid doing this if viewport is all the
184 // same as last frame if you want
185 gl->glViewport(0, 0, w, h);
189 // draw callback is where all the main GL rendering happens
191 _draw_gl(Evas_Object *obj)
193 Evas_GL_API *gl = elm_glview_gl_api_get(obj);
194 GLData *gld = evas_object_data_get(obj, "gld");
198 elm_glview_size_get(obj, &w, &h);
200 gl->glViewport(0, 0, w, h);
201 gl->glClearColor(red,0.8,0.3,1);
202 gl->glClear(GL_COLOR_BUFFER_BIT);
205 gl->glEnable(GL_BLEND);
207 gl->glUseProgram(gld->program);
209 gl->glBindBuffer(GL_ARRAY_BUFFER, gld->vbo);
210 gl->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
212 gl->glEnableVertexAttribArray(0);
214 gl->glDrawArrays(GL_TRIANGLES, 0, 3);
216 // Optional - Flush the GL pipeline
220 if (red < 0.0) red = 1.0;
223 // just need to notify that glview has changed so it can render
227 elm_glview_changed_set(data);
232 _on_done(void *data, Evas_Object *obj, void *event_info)
234 evas_object_del((Evas_Object*)data);
239 _del(void *data, Evas *evas, Evas_Object *obj, void *event_info)
241 Ecore_Animator *ani = evas_object_data_get(obj, "ani");
242 ecore_animator_del(ani);
247 elm_main(int argc, char **argv)
249 Evas_Object *win, *bg, *bx, *bt, *gl;
253 if (!(gld = calloc(1, sizeof(GLData)))) return 1;
255 // set the engine to opengl_x11
256 // if commented out, ELM will choose one
257 elm_engine_set("opengl_x11");
259 win = elm_win_add(NULL, "glview simple", ELM_WIN_BASIC);
260 elm_win_title_set(win, "GLView Simple");
261 elm_win_autodel_set(win, EINA_TRUE);
263 bg = elm_bg_add(win);
264 elm_win_resize_object_add(win, bg);
265 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
266 evas_object_show(bg);
268 bx = elm_box_add(win);
269 evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
270 elm_win_resize_object_add(win, bx);
271 evas_object_show(bx);
273 //-//-//-// THIS IS WHERE GL INIT STUFF HAPPENS (ALA EGL)
275 // create a new glview object
276 gl = elm_glview_add(win);
277 gld->glapi = elm_glview_gl_api_get(gl);
278 evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL);
279 evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
280 // mode is simply for supporting alpha, depth buffering, and stencil
282 elm_glview_mode_set(gl, ELM_GLVIEW_ALPHA | ELM_GLVIEW_DEPTH);
283 // resize policy tells glview what to do with the surface when it
284 // reisizes. ELM_GLVIEW_RESIZE_POLICY_RECREATE will tell it to
285 // destroy the current surface and recreate it to the new size
286 elm_glview_resize_policy_set(gl, ELM_GLVIEW_RESIZE_POLICY_RECREATE);
287 // render poicy tells glview how it would like glview to render
288 // gl code. ELM_GLVIEW_RENDER_POLICY_ON_DEMAND will have the gl
289 // calls called in the pixel_get callback, which only gets called
290 // if the object is visible, hence ON_DEMAND. ALWAYS mode renders
291 // it despite the visibility of the object.
292 elm_glview_render_policy_set(gl, ELM_GLVIEW_RENDER_POLICY_ON_DEMAND);
293 // initialize callback function gets registered here
294 elm_glview_init_func_set(gl, _init_gl);
295 // delete callback function gets registered here
296 elm_glview_del_func_set(gl, _del_gl);
297 elm_glview_resize_func_set(gl, _resize_gggl);
298 elm_glview_render_func_set(gl, _draw_gl);
300 //-//-//-// END GL INIT BLOB
302 elm_box_pack_end(bx, gl);
303 evas_object_show(gl);
305 elm_object_focus_set(gl, EINA_TRUE);
307 // animating - just a demo. as long as you trigger an update on the image
308 // object via elm_glview_changed_set() it will be updated.
310 // NOTE: if you delete gl, this animator will keep running trying to access
311 // gl so you'd better delete this animator with ecore_animator_del().
312 ani = ecore_animator_add(_anim, gl);
314 evas_object_data_set(gl, "ani", ani);
315 evas_object_data_set(gl, "gld", gld);
316 evas_object_event_callback_add(gl, EVAS_CALLBACK_DEL, _del, gl);
318 // add an 'OK' button to end the program
319 bt = elm_button_add(win);
320 elm_object_text_set(bt, "OK");
321 evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
322 evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, 0.0);
323 elm_box_pack_end(bx, bt);
324 evas_object_show(bt);
325 evas_object_smart_callback_add(bt, "clicked", _on_done, win);
327 evas_object_resize(win, 320, 480);
328 evas_object_show(win);
330 // run the mainloop and process events and callbacks
345 typedef void (*Elm_GLView_Func_Cb)(Evas_Object *obj);
347 typedef enum _Elm_GLView_Mode
350 ELM_GLVIEW_ALPHA = (1<<1), /**< Alpha channel enabled rendering mode */
351 ELM_GLVIEW_DEPTH = (1<<2), /**< Depth buffer enabled rendering mode */
352 ELM_GLVIEW_STENCIL = (1<<3), /**< Stencil buffer enabled rendering mode */
353 ELM_GLVIEW_DIRECT = (1<<4) /**< Direct rendering optimization hint */
357 * Defines a policy for the glview resizing.
359 * The resizing policy tells glview what to do with the underlying
360 * surface when resize happens. ELM_GLVIEW_RESIZE_POLICY_RECREATE
361 * will destroy the current surface and recreate the surface to the
362 * new size. ELM_GLVIEW_RESIZE_POLICY_SCALE will instead keep the
363 * current surface but only display the result at the desired size
366 * @note Default is ELM_GLVIEW_RESIZE_POLICY_RECREATE
370 ELM_GLVIEW_RESIZE_POLICY_RECREATE = 1, /**< Resize the internal surface along with the image */
371 ELM_GLVIEW_RESIZE_POLICY_SCALE = 2 /**< Only resize the internal image and not the surface */
372 } Elm_GLView_Resize_Policy;
375 * Defines a policy for gl rendering.
377 * The rendering policy tells glview where to run the gl rendering code.
378 * ELM_GLVIEW_RENDER_POLICY_ON_DEMAND tells glview to call the rendering
379 * calls on demand, which means that the rendering code gets called
380 * only when it is visible.
382 * @note Default is ELM_GLVIEW_RENDER_POLICY_ON_DEMAND
386 ELM_GLVIEW_RENDER_POLICY_ON_DEMAND = 1, /**< Render only when there is a need for redrawing */
387 ELM_GLVIEW_RENDER_POLICY_ALWAYS = 2 /**< Render always even when it is not visible */
388 } Elm_GLView_Render_Policy;
391 * Add a new glview to the parent
393 * @param parent The parent object
394 * @return The new object or NULL if it cannot be created
398 EAPI Evas_Object *elm_glview_add(Evas_Object *parent);
401 * Sets the size of the glview
403 * @param obj The glview object
404 * @param w width of the glview object
405 * @param h height of the glview object
409 EAPI void elm_glview_size_set(Evas_Object *obj, Evas_Coord w, Evas_Coord h);
412 * Gets the size of the glview.
414 * @param obj The glview object
415 * @param w width of the glview object
416 * @param h height of the glview object
418 * Note that this function returns the actual image size of the
419 * glview. This means that when the scale policy is set to
420 * ELM_GLVIEW_RESIZE_POLICY_SCALE, it'll return the non-scaled
425 EAPI void elm_glview_size_get(const Evas_Object *obj, Evas_Coord *w, Evas_Coord *h);
428 * Gets the gl api struct for gl rendering
430 * @param obj The glview object
431 * @return The api object or NULL if it cannot be created
435 EAPI Evas_GL_API *elm_glview_gl_api_get(const Evas_Object *obj);
438 * Set the mode of the GLView. Supports alpha, depth, stencil.
440 * @param obj The glview object
441 * @param mode The mode Options OR'ed enabling Alpha, Depth, Stencil, Direct.
442 * @return True if set properly.
444 * Direct is a hint for the elm_glview to render directly to the window
445 * given that the right conditions are met. Otherwise it falls back
446 * to rendering to an offscreen buffer before it gets composited to the
451 EAPI Eina_Bool elm_glview_mode_set(Evas_Object *obj, Elm_GLView_Mode mode);
454 * Set the resize policy for the glview object.
456 * @param obj The glview object.
457 * @param policy The scaling policy.
459 * By default, the resize policy is set to ELM_GLVIEW_RESIZE_POLICY_RECREATE.
460 * When resize is called it destroys the previous surface and recreates the
461 * newly specified size. If the policy is set to
462 * ELM_GLVIEW_RESIZE_POLICY_SCALE, however, glview only scales the image
463 * object and not the underlying GL Surface.
467 EAPI Eina_Bool elm_glview_resize_policy_set(Evas_Object *obj, Elm_GLView_Resize_Policy policy);
470 * Set the render policy for the glview object.
472 * @param obj The glview object.
473 * @param policy The render policy.
475 * By default, the render policy is set to ELM_GLVIEW_RENDER_POLICY_ON_DEMAND.
476 * This policy is set such that during the render loop, glview is only
477 * redrawn if it needs to be redrawn. (i.e. when it is visible) If the policy
478 * is set to ELM_GLVIEWW_RENDER_POLICY_ALWAYS, it redraws regardless of
479 * whether it is visible or needs redrawing.
483 EAPI Eina_Bool elm_glview_render_policy_set(Evas_Object *obj, Elm_GLView_Render_Policy policy);
486 * Set the init function that runs once in the main loop.
488 * @param obj The glview object.
489 * @param func The init function to be registered.
491 * The registered init function gets called once during the render loop.
492 * This function allows glview to hide all the redering context/surface
493 * details and have the user just call GL calls that they desire
494 * for initialization GL calls.
498 EAPI void elm_glview_init_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func);
501 * Set the render function that runs in the main loop.
503 * @param obj The glview object.
504 * @param func The delete function to be registered.
506 * The registered del function gets called when GLView object is deleted.
507 * This function allows glview to hide all the redering context/surface
508 * details and have the user just call GL calls that they desire
509 * when delete happends.
513 EAPI void elm_glview_del_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func);
516 * Set the resize function that gets called when resize happens.
518 * @param obj The glview object.
519 * @param func The resize function to be registered.
521 * The resize function getes called during the render loop.
522 * This function allows glview to hide all the redering context/surface
523 * details and have the user just call GL calls that they desire
524 * when resize happends.
528 EAPI void elm_glview_resize_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func);
531 * Set the render function that runs in the main loop.
533 * The render function gets called in the main loop but whether it runs
534 * depends on the rendering policy and whether elm_glview_changed_set()
537 * @param obj The glview object.
538 * @param func The render function to be registered.
542 EAPI void elm_glview_render_func_set(Evas_Object *obj, Elm_GLView_Func_Cb func);
545 * Notifies that there has been changes in the GLView.
547 * @param obj The glview object.
551 EAPI void elm_glview_changed_set(Evas_Object *obj);