#include <Elementary.h>
#include <Evas_GL.h>
#include <stdio.h>
typedef struct _GLData GLData;
struct _GLData
{
GLuint program;
GLuint vtx_shader;
GLuint fgmt_shader;
GLuint vbo;
int initialized : 1;
};
static float red = 1.0;
static GLuint
load_shader( GLData *gld, GLenum type, const char *shader_src )
{
GLuint shader;
GLint compiled;
shader = gl->
glCreateShader(type);
if (shader==0)
return 0;
gl->
glShaderSource(shader, 1, &shader_src, NULL);
gl->
glCompileShader(shader);
gl->
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled)
{
GLint info_len = 0;
gl->glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_len);
if (info_len > 1)
{
char* info_log = malloc(sizeof(char) * info_len);
gl->
glGetShaderInfoLog(shader, info_len, NULL, info_log);
printf("Error compiling shader:\n%s\n======\n%s\n======\n", info_log, shader_src );
free(info_log);
}
gl->
glDeleteShader(shader);
return 0;
}
return shader;
}
static int
init_shaders(GLData *gld)
{
GLbyte vShaderStr[] =
"attribute vec4 vPosition; \n"
"void main() \n"
"{ \n"
" gl_Position = vPosition; \n"
"} \n";
GLbyte fShaderStr[] =
"#ifdef GL_ES \n"
"precision mediump float; \n"
"#endif \n"
"void main() \n"
"{ \n"
" gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n"
"} \n";
GLint linked;
gld->vtx_shader = load_shader(gld, GL_VERTEX_SHADER, (const char*)vShaderStr);
gld->fgmt_shader = load_shader(gld, GL_FRAGMENT_SHADER, (const char*)fShaderStr);
gld->program = gl->
glCreateProgram( );
if (gld->program==0)
return 0;
gl->
glAttachShader(gld->program, gld->vtx_shader);
gl->glAttachShader(gld->program, gld->fgmt_shader);
gl->
glBindAttribLocation(gld->program, 0,
"vPosition");
gl->
glLinkProgram(gld->program);
gl->
glGetProgramiv(gld->program, GL_LINK_STATUS, &linked);
if (!linked)
{
GLint info_len = 0;
gl->glGetProgramiv(gld->program, GL_INFO_LOG_LENGTH, &info_len);
if (info_len > 1)
{
char* info_log = malloc(sizeof(char) * info_len);
gl->
glGetProgramInfoLog(gld->program, info_len, NULL, info_log);
printf("Error linking program:\n%s\n", info_log);
free(info_log);
}
gl->
glDeleteProgram(gld->program);
return 0;
}
return 1;
}
static void
{
GLfloat vVertices[] = {
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f };
if (!init_shaders(gld))
{
printf("Error Initializing Shaders\n");
return;
}
gl->
glGenBuffers(1, &gld->vbo);
gl->
glBindBuffer(GL_ARRAY_BUFFER, gld->vbo);
gl->
glBufferData(GL_ARRAY_BUFFER, 3 * 3 * 4, vVertices, GL_STATIC_DRAW);
}
static void
{
if (!gld)
{
printf("Unable to get GLData. \n");
return;
}
gl->glDeleteShader(gld->vtx_shader);
gl->glDeleteShader(gld->fgmt_shader);
gl->glDeleteProgram(gld->program);
gl->
glDeleteBuffers(1, &gld->vbo);
free(gld);
}
static void
{
int w, h;
gl->
glViewport(0, 0, w, h);
}
static void
{
if (!gld) return;
int w, h;
gl->glViewport(0, 0, w, h);
gl->
glClearColor(red,0.8,0.3,1);
gl->
glClear(GL_COLOR_BUFFER_BIT);
gl->
glUseProgram(gld->program);
gl->glBindBuffer(GL_ARRAY_BUFFER, gld->vbo);
gl->
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
0, 0);
gl->
glEnableVertexAttribArray(0);
gl->
glDrawArrays(GL_TRIANGLES, 0, 3);
red -= 0.1;
if (red < 0.0) red = 1.0;
}
_anim(void *data)
{
}
static void
{
}
static void
{
}
EAPI_MAIN int
{
GLData *gld = NULL;
if (!(gld = calloc(1, sizeof(GLData)))) return 1;
elm_glview_init_func_set(gl, _init_gl);
elm_object_text_set(bt, "OK");
return 0;
}