void init_quad(RenderingContext* ctx) { float vertices[12] = { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0 }; float tex_coords[8] = { 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0 }; _vao = ctx->create_vertex_array(); ScopedBind<VertexArray> bind_vao(ctx, _vao); // vertices { _vertex_vbo = ctx->create_vertex_buffer(); ScopedBind<VertexBuffer> bind_vbo(ctx, _vertex_vbo); ctx->upload(_vertex_vbo, range(vertices, 12).bytes()); ctx->define_vertex_attribute(0, gpu::FLOAT, 3, 0, 0); ctx->enable_vertex_attribute(0); } // texture coordinates { _texcoord_vbo = ctx->create_vertex_buffer(); ScopedBind<VertexBuffer> bind_vbo(ctx, _texcoord_vbo); ctx->upload(_texcoord_vbo, range(tex_coords, 8).bytes()); ctx->define_vertex_attribute(1, gpu::FLOAT, 2, 0, 0); ctx->enable_vertex_attribute(1); } }
void draw_unit_quad(RenderingContext* ctx, DrawMode mode) { ctx->enable_vertex_attribute(0); ctx->enable_vertex_attribute(1); gpu::ScopedBind<VertexArray> bind_vao(ctx, _vao); ctx->draw_arrays(mode, 0, 4); }
void gdk_gl_texture_quads (GdkGLContext *paint_context, guint texture_target, int n_quads, GdkTexturedQuad *quads) { GdkGLContextPaintData *paint_data = gdk_gl_context_get_paint_data (paint_context); GdkGLContextProgram *program; GdkWindow *window = gdk_gl_context_get_window (paint_context); int window_scale = gdk_window_get_scale_factor (window); float w = gdk_window_get_width (window) * window_scale; float h = gdk_window_get_height (window) * window_scale; int i; float *vertex_buffer_data; bind_vao (paint_data); if (paint_data->tmp_vertex_buffer == 0) glGenBuffers(1, &paint_data->tmp_vertex_buffer); if (texture_target == GL_TEXTURE_RECTANGLE_ARB) use_texture_rect_program (paint_data); else use_texture_2d_program (paint_data); program = paint_data->current_program; glActiveTexture (GL_TEXTURE0); glUniform1i(program->map_location, 0); /* Use texture unit 0 */ glEnableVertexAttribArray (program->position_location); glEnableVertexAttribArray (program->uv_location); glBindBuffer (GL_ARRAY_BUFFER, paint_data->tmp_vertex_buffer); glVertexAttribPointer (program->position_location, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, NULL); glVertexAttribPointer (program->uv_location, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (void *) (sizeof(float) * 2)); #define VERTEX_SIZE 4 #define QUAD_N_VERTICES 6 #define QUAD_SIZE (VERTEX_SIZE * QUAD_N_VERTICES) vertex_buffer_data = g_new (float, n_quads * QUAD_SIZE); for (i = 0; i < n_quads; i++) { GdkTexturedQuad *quad = &quads[i]; float vertex_data[] = { (quad->x1 * 2) / w - 1, (quad->y1 * 2) / h - 1, quad->u1, quad->v1, (quad->x1 * 2) / w - 1, (quad->y2 * 2) / h - 1, quad->u1, quad->v2, (quad->x2 * 2) / w - 1, (quad->y1 * 2) / h - 1, quad->u2, quad->v1, (quad->x2 * 2) / w - 1, (quad->y2 * 2) / h - 1, quad->u2, quad->v2, (quad->x1 * 2) / w - 1, (quad->y2 * 2) / h - 1, quad->u1, quad->v2, (quad->x2 * 2) / w - 1, (quad->y1 * 2) / h - 1, quad->u2, quad->v1, }; float *vertex = &vertex_buffer_data[i * QUAD_SIZE]; memcpy (vertex, vertex_data, sizeof(vertex_data)); } glBufferData (GL_ARRAY_BUFFER, sizeof(float) * n_quads * QUAD_SIZE, vertex_buffer_data, GL_STREAM_DRAW); glDrawArrays (GL_TRIANGLES, 0, n_quads * QUAD_N_VERTICES); g_free (vertex_buffer_data); glDisableVertexAttribArray (program->position_location); glDisableVertexAttribArray (program->uv_location); }