2 # include "elementary_config.h"
4 #include <Elementary.h>
5 #ifndef ELM_LIB_QUICKLAUNCH
7 typedef struct _GLData GLData;
9 // GL related data here..
21 static float red = 1.0;
23 //--------------------------------//
25 load_shader( GLData *gld, GLenum type, const char *shader_src )
27 Evas_GL_API *gl = gld->glapi;
31 // Create the shader object
32 shader = gl->glCreateShader(type);
36 // Load/Compile shader source
37 gl->glShaderSource(shader, 1, &shader_src, NULL);
38 gl->glCompileShader(shader);
39 gl->glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
44 gl->glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_len);
47 char* info_log = malloc(sizeof(char) * info_len);
49 gl->glGetShaderInfoLog(shader, info_len, NULL, info_log);
50 printf("Error compiling shader:\n%s\n======\n%s\n======\n", info_log, shader_src );
53 gl->glDeleteShader(shader);
60 // Initialize the shader and program object
62 init_shaders(GLData *gld)
64 Evas_GL_API *gl = gld->glapi;
66 "attribute vec4 vPosition; \n"
69 " gl_Position = vPosition; \n"
74 "precision mediump float; \n"
78 " gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n"
83 // Load the vertex/fragment shaders
84 gld->vtx_shader = load_shader(gld, GL_VERTEX_SHADER, (const char*)vShaderStr);
85 gld->fgmt_shader = load_shader(gld, GL_FRAGMENT_SHADER, (const char*)fShaderStr);
87 // Create the program object
88 gld->program = gl->glCreateProgram( );
92 gl->glAttachShader(gld->program, gld->vtx_shader);
93 gl->glAttachShader(gld->program, gld->fgmt_shader);
95 gl->glBindAttribLocation(gld->program, 0, "vPosition");
96 gl->glLinkProgram(gld->program);
97 gl->glGetProgramiv(gld->program, GL_LINK_STATUS, &linked);
102 gl->glGetProgramiv(gld->program, GL_INFO_LOG_LENGTH, &info_len);
105 char* info_log = malloc(sizeof(char) * info_len);
107 gl->glGetProgramInfoLog(gld->program, info_len, NULL, info_log);
108 printf("Error linking program:\n%s\n", info_log);
111 gl->glDeleteProgram(gld->program);
121 _init_gl(Evas_Object *obj)
123 GLData *gld = evas_object_data_get(obj, "gld");
124 Evas_GL_API *gl = gld->glapi;
125 GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f,
129 if (!init_shaders(gld))
131 printf("Error Initializing Shaders\n");
135 gl->glGenBuffers(1, &gld->vbo);
136 gl->glBindBuffer(GL_ARRAY_BUFFER, gld->vbo);
137 gl->glBufferData(GL_ARRAY_BUFFER, 3 * 3 * 4, vVertices, GL_STATIC_DRAW);
141 _del_gl(Evas_Object *obj)
143 GLData *gld = evas_object_data_get(obj, "gld");
146 printf("Unable to get GLData. \n");
149 Evas_GL_API *gl = gld->glapi;
151 gl->glDeleteShader(gld->vtx_shader);
152 gl->glDeleteShader(gld->fgmt_shader);
153 gl->glDeleteProgram(gld->program);
154 gl->glDeleteBuffers(1, &gld->vbo);
156 evas_object_data_del((Evas_Object*)obj, "..gld");
162 _resize_gl(Evas_Object *obj)
165 GLData *gld = evas_object_data_get(obj, "gld");
166 Evas_GL_API *gl = gld->glapi;
168 elm_glview_size_get(obj, &w, &h);
170 // GL Viewport stuff. you can avoid doing this if viewport is all the
171 // same as last frame if you want
172 gl->glViewport(0, 0, w, h);
178 _draw_gl(Evas_Object *obj)
180 Evas_GL_API *gl = elm_glview_gl_api_get(obj);
181 GLData *gld = evas_object_data_get(obj, "gld");
185 elm_glview_size_get(obj, &w, &h);
187 gl->glViewport(0, 0, w, h);
188 gl->glClearColor(red,0.8,0.3,1);
189 gl->glClear(GL_COLOR_BUFFER_BIT);
192 gl->glEnable(GL_BLEND);
194 gl->glUseProgram(gld->program);
196 gl->glBindBuffer(GL_ARRAY_BUFFER, gld->vbo);
197 gl->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
199 gl->glEnableVertexAttribArray(0);
201 gl->glDrawArrays(GL_TRIANGLES, 0, 3);
203 // Optional - Flush the GL pipeline
207 if (red < 0.0) red = 1.0;
213 elm_glview_changed_set(data);
218 _on_done(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
220 evas_object_del((Evas_Object*)data);
225 _del(void *data __UNUSED__, Evas *evas __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
227 Ecore_Animator *ani = evas_object_data_get(obj, "ani");
228 ecore_animator_del(ani);
233 test_glview_simple(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
235 Evas_Object *win, *bx, *bt, *gl;
239 if (!(gld = calloc(1, sizeof(GLData)))) return;
241 // Set the engine to opengl_x11
242 elm_config_preferred_engine_set("opengl_x11");
243 win = elm_win_util_standard_add("glview simple", "GLView Simple");
244 // Set preferred engine back to default from config
245 elm_config_preferred_engine_set(NULL);
247 elm_win_autodel_set(win, EINA_TRUE);
249 bx = elm_box_add(win);
250 evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
251 elm_win_resize_object_add(win, bx);
252 evas_object_show(bx);
254 gl = elm_glview_add(win);
255 evas_object_size_hint_align_set(gl, EVAS_HINT_FILL, EVAS_HINT_FILL);
256 evas_object_size_hint_weight_set(gl, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
257 elm_glview_mode_set(gl, ELM_GLVIEW_ALPHA | ELM_GLVIEW_DEPTH);
258 elm_glview_resize_policy_set(gl, ELM_GLVIEW_RESIZE_POLICY_RECREATE);
259 elm_glview_render_policy_set(gl, ELM_GLVIEW_RENDER_POLICY_ON_DEMAND);
260 elm_glview_init_func_set(gl, _init_gl);
261 elm_glview_del_func_set(gl, _del_gl);
262 elm_glview_resize_func_set(gl, _resize_gl);
263 elm_glview_render_func_set(gl, _draw_gl);
264 elm_box_pack_end(bx, gl);
265 evas_object_show(gl);
267 elm_object_focus_set(gl, EINA_TRUE);
269 ani = ecore_animator_add(_anim, gl);
270 gld->glapi = elm_glview_gl_api_get(gl);
271 evas_object_data_set(gl, "ani", ani);
272 evas_object_data_set(gl, "gld", gld);
273 evas_object_event_callback_add(gl, EVAS_CALLBACK_DEL, _del, gl);
275 bt = elm_button_add(win);
276 elm_object_text_set(bt, "OK");
277 evas_object_size_hint_align_set(bt, EVAS_HINT_FILL, EVAS_HINT_FILL);
278 evas_object_size_hint_weight_set(bt, EVAS_HINT_EXPAND, 0.0);
279 elm_box_pack_end(bx, bt);
280 evas_object_show(bt);
281 evas_object_smart_callback_add(bt, "clicked", _on_done, win);
283 evas_object_resize(win, 320, 480);
284 evas_object_show(win);