Exemple #1
0
static void
create_primitives (cg_primitive_t *primitives[2])
{
  static const Vertex vertex_data[8] =
    {
      /* triangle strip 1 */
      {   0,   0, 255, 0, 0, 255 },
      {   0, 100, 255, 0, 0, 255 },
      { 100,   0, 255, 0, 0, 255 },
      { 100, 100, 255, 0, 0, 255 },
      /* triangle strip 2 */
      { 200,   0, 0, 0, 255, 255 },
      { 200, 100, 0, 0, 255, 255 },
      { 300,   0, 0, 0, 255, 255 },
      { 300, 100, 0, 0, 255, 255 },
    };

  primitives[0] = cg_primitive_new_p2c4 (test_dev,
                                           CG_VERTICES_MODE_TRIANGLE_STRIP,
                                           C_N_ELEMENTS (vertex_data),
                                           vertex_data);
  cg_primitive_set_n_vertices (primitives[0], 4);

  primitives[1] = cg_primitive_copy (primitives[0]);
  cg_primitive_set_first_vertex (primitives[1], 4);
  cg_primitive_set_n_vertices (primitives[1], 4);
}
Exemple #2
0
static void
paint (void)
{
  cg_pipeline_t *pipeline = cg_pipeline_new (test_dev);
  int width = cg_framebuffer_get_width (test_fb);
  int half_width = width / 2;
  int height = cg_framebuffer_get_height (test_fb);
  cg_vertex_p2_t tri0_vertices[] = {
        { 0, 0 },
        { 0, height },
        { half_width, height },
  };
  cg_vertex_p2c4_t tri1_vertices[] = {
        { half_width, 0, 0x80, 0x80, 0x80, 0x80 },
        { half_width, height, 0x80, 0x80, 0x80, 0x80 },
        { width, height, 0x80, 0x80, 0x80, 0x80 },
  };
  cg_primitive_t *tri0;
  cg_primitive_t *tri1;

  cg_framebuffer_clear4f (test_fb, CG_BUFFER_BIT_COLOR, 0, 0, 0, 0);

  tri0 = cg_primitive_new_p2 (test_dev, CG_VERTICES_MODE_TRIANGLES,
                                3, tri0_vertices);
  tri1 = cg_primitive_new_p2c4 (test_dev, CG_VERTICES_MODE_TRIANGLES,
                                  3, tri1_vertices);

  /* Check that cogl correctly handles the case where we draw
   * different primitives same pipeline and switch from using the
   * opaque color associated with the pipeline and using a colour
   * attribute with an alpha component which implies blending is
   * required.
   *
   * If Cogl gets this wrong then then in all likelyhood the second
   * primitive will be drawn with blending still disabled.
   */

  cg_primitive_draw (tri0, test_fb, pipeline);
  cg_primitive_draw (tri1, test_fb, pipeline);

  test_cg_check_pixel_and_alpha (test_fb,
                                    half_width + 5,
                                    height - 5,
                                    0x80808080);
}
Exemple #3
0
int
main(int argc, char **argv)
{
    Data data;
    cg_onscreen_t *onscreen;
    cg_error_t *error = NULL;
    cg_vertex_p2c4_t triangle_vertices[] = {
        { 0, 0.7, 0xff, 0x00, 0x00, 0xff },
        { -0.7, -0.7, 0x00, 0xff, 0x00, 0xff },
        { 0.7, -0.7, 0x00, 0x00, 0xff, 0xff }
    };
    cg_renderer_t *renderer;
    cg_display_t *display;
    uv_loop_t *loop = uv_default_loop();

    renderer = cg_renderer_new();
    cg_renderer_add_constraint(renderer,
                               CG_RENDERER_CONSTRAINT_SUPPORTS_CG_GLES2);

    if (!cg_renderer_connect(renderer, &error)) {
        c_error("%s", error->message);
        exit(1);
    }

    display = cg_display_new(renderer, NULL);
    data.dev = cg_device_new();
    cg_device_set_display(data.dev, display);

    if (!cg_device_connect(data.dev, &error)) {
        c_error("%s", error->message);
        exit(1);
    }

    onscreen = cg_onscreen_new(data.dev, 640, 480);
    cg_onscreen_show(onscreen);
    data.fb = onscreen;

    /* Prepare onscreen primitive */
    data.triangle = cg_primitive_new_p2c4(
        data.dev, CG_VERTICES_MODE_TRIANGLES, 3, triangle_vertices);
    data.pipeline = cg_pipeline_new(data.dev);

    data.offscreen_texture = cg_texture_2d_new_with_size(
        data.dev, OFFSCREEN_WIDTH, OFFSCREEN_HEIGHT);
    data.offscreen = cg_offscreen_new_with_texture(data.offscreen_texture);

    data.gles2_ctx = cg_gles2_context_new(data.dev, &error);
    if (!data.gles2_ctx) {
        c_error("Failed to create GLES2 context: %s\n", error->message);
    }

    data.gles2_vtable = cg_gles2_context_get_vtable(data.gles2_ctx);

    /* Draw scene with GLES2 */
    if (!cg_push_gles2_context(
            data.dev, data.gles2_ctx, data.fb, data.fb, &error)) {
        c_error("Failed to push gles2 context: %s\n", error->message);
    }

    cg_pop_gles2_context(data.dev);

    cg_onscreen_add_frame_callback(
        CG_ONSCREEN(data.fb), frame_event_cb, &data, NULL); /* destroy notify */

    uv_idle_init(loop, &data.idle);
    data.idle.data = &data;
    uv_idle_start(&data.idle, paint_cb);

    cg_uv_set_mainloop(data.dev, loop);
    uv_run(loop, UV_RUN_DEFAULT);

    return 0;
}
Exemple #4
0
int
main(int argc, char **argv)
{
    cg_renderer_t *renderer;
    cg_device_t *dev;
    cg_onscreen_t *onscreen;
    cg_error_t *error = NULL;
    cg_vertex_p2c4_t triangle_vertices[] = {
        { 0, 0.7, 0xff, 0x00, 0x00, 0xff },
        { -0.7, -0.7, 0x00, 0xff, 0x00, 0xff },
        { 0.7, -0.7, 0x00, 0x00, 0xff, 0xff }
    };
    Data data;
    SDL_Event event;

    renderer = cg_renderer_new();
    dev = cg_device_new();

    cg_renderer_set_winsys_id(renderer, CG_WINSYS_ID_SDL);
    if (cg_renderer_connect(renderer, &error))
        cg_device_set_renderer(dev, renderer);
    else {
        cg_error_free(error);
        fprintf(stderr, "Failed to create device: %s\n", error->message);
        return 1;
    }

    onscreen = cg_onscreen_new(dev, 800, 600);
    data.fb = onscreen;

    cg_onscreen_add_frame_callback(
        onscreen, frame_cb, &data, NULL /* destroy callback */);
    cg_onscreen_add_dirty_callback(
        onscreen, dirty_cb, &data, NULL /* destroy callback */);

    data.center_x = 0.0f;
    data.center_y = 0.0f;
    data.quit = false;

    /* In SDL2, setting resizable only works before allocating the
     * onscreen */
    cg_onscreen_set_resizable(onscreen, true);

    cg_onscreen_show(onscreen);

    data.triangle = cg_primitive_new_p2c4(dev, CG_VERTICES_MODE_TRIANGLES, 3,
                                          triangle_vertices);
    data.pipeline = cg_pipeline_new(dev);

    data.redraw_queued = false;
    data.ready_to_draw = true;

    while (!data.quit) {
        if (!SDL_PollEvent(&event)) {
            if (data.redraw_queued && data.ready_to_draw) {
                redraw(&data);
                data.redraw_queued = false;
                data.ready_to_draw = false;
                continue;
            }

            cg_sdl_idle(dev);
            if (!SDL_WaitEvent(&event)) {
                fprintf(stderr, "Error waiting for SDL events");
                return 1;
            }
        }

        handle_event(&data, &event);
        cg_sdl_handle_event(dev, &event);
    }

    cg_object_unref(dev);

    return 0;
}