struct pipeline_stage *copy_one_new(struct gles *gles, struct geometry *geometry, struct framebuffer *source, struct framebuffer *target) { struct copy_one *stage; stage = calloc(1, sizeof(*stage)); if (!stage) return NULL; stage->base.name = "one-texel copy operation"; stage->base.release = copy_one_release; stage->base.render = copy_one_render; stage->geometry = geometry; stage->source = source; stage->target = target; stage->vertex = glsl_shader_new(GL_VERTEX_SHADER, copy_one_vs, ARRAY_SIZE(copy_one_vs)); if (!stage->vertex) { fprintf(stderr, "failed to create vertex shader\n"); return NULL; } stage->fragment = glsl_shader_new(GL_FRAGMENT_SHADER, copy_one_fs, ARRAY_SIZE(copy_one_fs)); if (!stage->fragment) { fprintf(stderr, "failed to create fragment shader\n"); return NULL; } stage->program = glsl_program_new(stage->vertex, stage->fragment); if (!stage->program) { fprintf(stderr, "failed to create GLSL program\n"); return NULL; } if (glsl_program_link(stage->program) < 0) { fprintf(stderr, "failed to link GLSL program\n"); return NULL; } stage->pos = glGetAttribLocation(stage->program->id, "position"); stage->tex = glGetAttribLocation(stage->program->id, "tex"); stage->input = glGetUniformLocation(stage->program->id, "source"); return &stage->base; }
static void window_setup(struct window *window) { GLint position, texcoord, tex; GLuint vs, fs, program; vs = glsl_shader_load(GL_VERTEX_SHADER, vertex_shader, ARRAY_SIZE(vertex_shader)); fs = glsl_shader_load(GL_FRAGMENT_SHADER, fragment_shader, ARRAY_SIZE(fragment_shader)); program = glsl_program_create(vs, fs); glsl_program_link(program); glUseProgram(program); position = glGetAttribLocation(program, "position"); texcoord = glGetAttribLocation(program, "texcoord"); tex = glGetUniformLocation(program, "tex"); mvp = glGetUniformLocation(program, "mvp"); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glVertexAttribPointer(position, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), vertices); glEnableVertexAttribArray(position); glVertexAttribPointer(texcoord, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), uv); glEnableVertexAttribArray(texcoord); texture = gles_texture_load("data/tegra.png"); if (!texture) return; glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texture->id); glActiveTexture(GL_TEXTURE0 + 0); glUniform1i(tex, 0); }
static void window_draw(struct window *window) { static const GLfloat vertices[] = { /* front */ -0.5f, -0.5f, 0.5f, 1.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, -0.5f, 0.5f, 0.5f, 1.0f, /* back */ -0.5f, -0.5f, -0.5f, 1.0f, 0.5f, -0.5f, -0.5f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, -0.5f, 0.5f, -0.5f, 1.0f, /* left */ -0.5f, -0.5f, 0.5f, 1.0f, -0.5f, 0.5f, 0.5f, 1.0f, -0.5f, 0.5f, -0.5f, 1.0f, -0.5f, -0.5f, -0.5f, 1.0f, /* right */ 0.5f, -0.5f, 0.5f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 0.5f, -0.5f, -0.5f, 1.0f, /* top */ -0.5f, 0.5f, 0.5f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, -0.5f, 0.5f, -0.5f, 1.0f, /* bottom */ -0.5f, -0.5f, 0.5f, 1.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.5f, -0.5f, -0.5f, 1.0f, -0.5f, -0.5f, -0.5f, 1.0f, }; static const GLfloat colors[] = { /* front */ 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, /* back */ 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, /* left */ 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, /* right */ 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, /* top */ 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, /* bottom */ 1.0f, 0.5f, 0.0f, 1.0f, 1.0f, 0.5f, 0.0f, 1.0f, 1.0f, 0.5f, 0.0f, 1.0f, 1.0f, 0.5f, 0.0f, 1.0f, }; static const GLushort indices[] = { /* front */ 0, 1, 2, 0, 2, 3, /* back */ 4, 5, 6, 4, 6, 7, /* left */ 8, 9, 10, 8, 10, 11, /* right */ 12, 13, 14, 12, 14, 15, /* top */ 16, 17, 18, 16, 18, 19, /* bottom */ 20, 21, 22, 20, 22, 23, }; struct mat4 matrix, modelview, projection, transform, result; static GLfloat x = 0.0f, y = 0.0f, z = 0.0f; GLint position, color, mvp; GLuint vs, fs, program; GLfloat aspect; vs = glsl_shader_load(GL_VERTEX_SHADER, vertex_shader, ARRAY_SIZE(vertex_shader)); fs = glsl_shader_load(GL_FRAGMENT_SHADER, fragment_shader, ARRAY_SIZE(fragment_shader)); program = glsl_program_create(vs, fs); glsl_program_link(program); glUseProgram(program); position = glGetAttribLocation(program, "position"); color = glGetAttribLocation(program, "color"); mvp = glGetUniformLocation(program, "mvp"); aspect = window->width / (GLfloat)window->height; mat4_perspective(&projection, 60.0f, aspect, 1.0f, 1024.0f); mat4_identity(&modelview); mat4_rotate_x(&transform, x); mat4_multiply(&result, &modelview, &transform); mat4_rotate_y(&transform, y); mat4_multiply(&modelview, &result, &transform); mat4_rotate_z(&transform, z); mat4_multiply(&result, &modelview, &transform); mat4_translate(&transform, 0.0f, 0.0f, -2.0f); mat4_multiply(&modelview, &transform, &result); mat4_multiply(&matrix, &projection, &modelview); glUniformMatrix4fv(mvp, 1, GL_FALSE, (GLfloat *)&matrix); glViewport(0, 0, window->width, window->height); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glVertexAttribPointer(position, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), vertices); glEnableVertexAttribArray(position); glVertexAttribPointer(color, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), colors); glEnableVertexAttribArray(color); glDrawElements(GL_TRIANGLES, ARRAY_SIZE(indices), GL_UNSIGNED_SHORT, indices); eglSwapBuffers(window->egl.display, window->egl.surface); x += 0.3f; y += 0.2f; z += 0.4f; }
void pbuffer_draw(struct pbuffer *pbuffer) { static const GLfloat vertices[] = { 0.0f, 0.5f, 0.0f, 1.0f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, 0.0f, 1.0f, }; static const GLfloat colors[] = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, }; static const GLushort indices[] = { 0, 1, 2, }; GLuint vs, fs, program; GLint position, color; printf("=== compiling shaders\n"); vs = glsl_shader_load(GL_CG_VERTEX_SHADER_EXT, vertex_shader, ARRAY_SIZE(vertex_shader)); fs = glsl_shader_load(GL_CG_FRAGMENT_SHADER_EXT, fragment_shader, ARRAY_SIZE(fragment_shader)); glFlush(); printf("=== linking program\n"); program = glsl_program_create(vs, fs); glsl_program_link(program); glFlush(); printf("=== using program\n"); glUseProgram(program); glFlush(); position = glGetAttribLocation(program, "position"); color = glGetAttribLocation(program, "color"); printf("=== clearing buffer\n"); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glFlush(); printf("=== setting vertex attribute\n"); glVertexAttribPointer(position, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), vertices); glFlush(); printf("=== enabling vertex attribute\n"); glEnableVertexAttribArray(position); glFlush(); printf("=== setting vertex attribute\n"); glVertexAttribPointer(color, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), colors); glFlush(); printf("=== enabling vertex attribute\n"); glEnableVertexAttribArray(color); glFlush(); printf("=== drawing triangles\n"); glDrawElements(GL_TRIANGLES, ARRAY_SIZE(indices), GL_UNSIGNED_SHORT, indices); glFlush(); printf("=== done\n"); }