1 #include <cogl/cogl.h>
2 #include <cogl/cogl-gles2.h>
3 #include <glib.h>
4 #include <stdio.h>
5
6 #define OFFSCREEN_WIDTH 100
7 #define OFFSCREEN_HEIGHT 100
8
9 typedef struct _Data
10 {
11 CoglContext *ctx;
12 CoglFramebuffer *fb;
13 CoglPrimitive *triangle;
14 CoglPipeline *pipeline;
15
16 CoglTexture *offscreen_texture;
17 CoglOffscreen *offscreen;
18 CoglGLES2Context *gles2_ctx;
19 const CoglGLES2Vtable *gles2_vtable;
20 } Data;
21
22 static gboolean
paint_cb(void * user_data)23 paint_cb (void *user_data)
24 {
25 Data *data = user_data;
26 CoglError *error = NULL;
27 const CoglGLES2Vtable *gles2 = data->gles2_vtable;
28
29 /* Draw scene with GLES2 */
30 if (!cogl_push_gles2_context (data->ctx,
31 data->gles2_ctx,
32 data->fb,
33 data->fb,
34 &error))
35 {
36 g_error ("Failed to push gles2 context: %s\n", error->message);
37 }
38
39 /* Clear offscreen framebuffer with a random color */
40 gles2->glClearColor (g_random_double (),
41 g_random_double (),
42 g_random_double (),
43 1.0f);
44 gles2->glClear (GL_COLOR_BUFFER_BIT);
45
46 cogl_pop_gles2_context (data->ctx);
47
48 /* Draw scene with Cogl */
49 cogl_primitive_draw (data->triangle, data->fb, data->pipeline);
50
51 cogl_onscreen_swap_buffers (COGL_ONSCREEN (data->fb));
52
53 return FALSE; /* remove the callback */
54 }
55
56 static void
frame_event_cb(CoglOnscreen * onscreen,CoglFrameEvent event,CoglFrameInfo * info,void * user_data)57 frame_event_cb (CoglOnscreen *onscreen,
58 CoglFrameEvent event,
59 CoglFrameInfo *info,
60 void *user_data)
61 {
62 if (event == COGL_FRAME_EVENT_SYNC)
63 paint_cb (user_data);
64 }
65
66 int
main(int argc,char ** argv)67 main (int argc, char **argv)
68 {
69 Data data;
70 CoglOnscreen *onscreen;
71 CoglError *error = NULL;
72 CoglVertexP2C4 triangle_vertices[] = {
73 {0, 0.7, 0xff, 0x00, 0x00, 0xff},
74 {-0.7, -0.7, 0x00, 0xff, 0x00, 0xff},
75 {0.7, -0.7, 0x00, 0x00, 0xff, 0xff}
76 };
77 GSource *cogl_source;
78 GMainLoop *loop;
79 CoglRenderer *renderer;
80 CoglDisplay *display;
81
82 renderer = cogl_renderer_new ();
83 cogl_renderer_add_constraint (renderer,
84 COGL_RENDERER_CONSTRAINT_SUPPORTS_COGL_GLES2);
85 display = cogl_display_new (renderer, NULL);
86 data.ctx = cogl_context_new (display, NULL);
87
88 onscreen = cogl_onscreen_new (data.ctx, 640, 480);
89 cogl_onscreen_show (onscreen);
90 data.fb = onscreen;
91
92 /* Prepare onscreen primitive */
93 data.triangle = cogl_primitive_new_p2c4 (data.ctx,
94 COGL_VERTICES_MODE_TRIANGLES,
95 3, triangle_vertices);
96 data.pipeline = cogl_pipeline_new (data.ctx);
97
98 data.offscreen_texture =
99 cogl_texture_2d_new_with_size (data.ctx,
100 OFFSCREEN_WIDTH,
101 OFFSCREEN_HEIGHT);
102 data.offscreen = cogl_offscreen_new_with_texture (data.offscreen_texture);
103
104 data.gles2_ctx = cogl_gles2_context_new (data.ctx, &error);
105 if (!data.gles2_ctx) {
106 g_error ("Failed to create GLES2 context: %s\n", error->message);
107 }
108
109 data.gles2_vtable = cogl_gles2_context_get_vtable (data.gles2_ctx);
110
111 /* Draw scene with GLES2 */
112 if (!cogl_push_gles2_context (data.ctx,
113 data.gles2_ctx,
114 data.fb,
115 data.fb,
116 &error))
117 {
118 g_error ("Failed to push gles2 context: %s\n", error->message);
119 }
120
121 cogl_pop_gles2_context (data.ctx);
122
123 cogl_source = cogl_glib_source_new (data.ctx, G_PRIORITY_DEFAULT);
124
125 g_source_attach (cogl_source, NULL);
126
127 cogl_onscreen_add_frame_callback (COGL_ONSCREEN (data.fb),
128 frame_event_cb,
129 &data,
130 NULL); /* destroy notify */
131
132 g_idle_add (paint_cb, &data);
133
134 loop = g_main_loop_new (NULL, TRUE);
135 g_main_loop_run (loop);
136
137 return 0;
138 }
139