~oem-solutions-group/unity-2d/clutter-1.0

« back to all changes in this revision

Viewing changes to tests/conform/test-vertex-buffer-contiguous.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2010-03-21 13:27:56 UTC
  • mto: (2.1.3 experimental)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20100321132756-nf8yd30yxo3zzwcm
Tags: upstream-1.2.2
ImportĀ upstreamĀ versionĀ 1.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
#include <clutter/clutter.h>
3
 
#include <cogl/cogl.h>
4
 
 
5
 
#include "test-conform-common.h"
6
 
 
7
 
/* This test verifies that the simplest usage of the vertex buffer API,
8
 
 * where we add contiguous (x,y) GLfloat vertices, and RGBA GLubyte color
9
 
 * attributes to a buffer, submit, and draw.
10
 
 *
11
 
 * It also tries to verify that the enable/disable attribute APIs are working
12
 
 * too.
13
 
 *
14
 
 * If you want visual feedback of what this test paints for debugging purposes,
15
 
 * then remove the call to clutter_main_quit() in validate_result.
16
 
 */
17
 
 
18
 
typedef struct _TestState
19
 
{
20
 
  CoglHandle buffer;
21
 
  CoglHandle texture;
22
 
  CoglHandle material;
23
 
  ClutterGeometry stage_geom;
24
 
  guint frame;
25
 
} TestState;
26
 
 
27
 
static void
28
 
validate_result (TestState *state)
29
 
{
30
 
  GLubyte pixel[4];
31
 
  GLint y_off = 90;
32
 
 
33
 
  if (g_test_verbose ())
34
 
    g_print ("y_off = %d\n", y_off);
35
 
 
36
 
  /* NB: We ignore the alpha, since we don't know if our render target is
37
 
   * RGB or RGBA */
38
 
 
39
 
#define RED 0
40
 
#define GREEN 1
41
 
#define BLUE 2
42
 
 
43
 
  /* Should see a blue pixel */
44
 
  cogl_read_pixels (10, y_off, 1, 1,
45
 
                    COGL_READ_PIXELS_COLOR_BUFFER,
46
 
                    COGL_PIXEL_FORMAT_RGBA_8888,
47
 
                    pixel);
48
 
  if (g_test_verbose ())
49
 
    g_print ("pixel 0 = %x, %x, %x\n", pixel[RED], pixel[GREEN], pixel[BLUE]);
50
 
  g_assert (pixel[RED] == 0 && pixel[GREEN] == 0 && pixel[BLUE] != 0);
51
 
 
52
 
  /* Should see a red pixel */
53
 
  cogl_read_pixels (110, y_off, 1, 1,
54
 
                    COGL_READ_PIXELS_COLOR_BUFFER,
55
 
                    COGL_PIXEL_FORMAT_RGBA_8888,
56
 
                    pixel);
57
 
  if (g_test_verbose ())
58
 
    g_print ("pixel 1 = %x, %x, %x\n", pixel[RED], pixel[GREEN], pixel[BLUE]);
59
 
  g_assert (pixel[RED] != 0 && pixel[GREEN] == 0 && pixel[BLUE] == 0);
60
 
 
61
 
  /* Should see a blue pixel */
62
 
  cogl_read_pixels (210, y_off, 1, 1,
63
 
                    COGL_READ_PIXELS_COLOR_BUFFER,
64
 
                    COGL_PIXEL_FORMAT_RGBA_8888,
65
 
                    pixel);
66
 
  if (g_test_verbose ())
67
 
    g_print ("pixel 2 = %x, %x, %x\n", pixel[RED], pixel[GREEN], pixel[BLUE]);
68
 
  g_assert (pixel[RED] == 0 && pixel[GREEN] == 0 && pixel[BLUE] != 0);
69
 
 
70
 
  /* Should see a green pixel, at bottom of 4th triangle */
71
 
  cogl_read_pixels (310, y_off, 1, 1,
72
 
                    COGL_READ_PIXELS_COLOR_BUFFER,
73
 
                    COGL_PIXEL_FORMAT_RGBA_8888,
74
 
                    pixel);
75
 
  if (g_test_verbose ())
76
 
    g_print ("pixel 3 = %x, %x, %x\n", pixel[RED], pixel[GREEN], pixel[BLUE]);
77
 
  g_assert (pixel[GREEN] > pixel[RED] && pixel[GREEN] > pixel[BLUE]);
78
 
 
79
 
  /* Should see a red pixel, at top of 4th triangle */
80
 
  cogl_read_pixels (310, y_off - 70, 1, 1,
81
 
                    COGL_READ_PIXELS_COLOR_BUFFER,
82
 
                    COGL_PIXEL_FORMAT_RGBA_8888,
83
 
                    pixel);
84
 
  if (g_test_verbose ())
85
 
    g_print ("pixel 4 = %x, %x, %x\n", pixel[RED], pixel[GREEN], pixel[BLUE]);
86
 
  g_assert (pixel[RED] > pixel[GREEN] && pixel[RED] > pixel[BLUE]);
87
 
 
88
 
 
89
 
#undef RED
90
 
#undef GREEN
91
 
#undef BLUE
92
 
 
93
 
  /* Comment this out if you want visual feedback of what this test
94
 
   * paints.
95
 
   */
96
 
  clutter_main_quit ();
97
 
}
98
 
 
99
 
static void
100
 
on_paint (ClutterActor *actor, TestState *state)
101
 
{
102
 
  /* Draw a faded blue triangle */
103
 
  cogl_vertex_buffer_enable (state->buffer, "gl_Color::blue");
104
 
  cogl_set_source_color4ub (0xff, 0x00, 0x00, 0xff);
105
 
  cogl_vertex_buffer_draw (state->buffer,
106
 
                           GL_TRIANGLE_STRIP, /* mode */
107
 
                           0, /* first */
108
 
                           3); /* count */
109
 
 
110
 
  /* Draw a red triangle */
111
 
  /* Here we are testing that the disable attribute works; if it doesn't
112
 
   * the triangle will remain faded blue */
113
 
  cogl_translate (100, 0, 0);
114
 
  cogl_vertex_buffer_disable (state->buffer, "gl_Color::blue");
115
 
  cogl_set_source_color4ub (0xff, 0x00, 0x00, 0xff);
116
 
  cogl_vertex_buffer_draw (state->buffer,
117
 
                           GL_TRIANGLE_STRIP, /* mode */
118
 
                           0, /* first */
119
 
                           3); /* count */
120
 
 
121
 
  /* Draw a faded blue triangle */
122
 
  /* Here we are testing that the re-enable works; if it doesn't
123
 
   * the triangle will remain red */
124
 
  cogl_translate (100, 0, 0);
125
 
  cogl_vertex_buffer_enable (state->buffer, "gl_Color::blue");
126
 
  cogl_set_source_color4ub (0xff, 0x00, 0x00, 0xff);
127
 
  cogl_vertex_buffer_draw (state->buffer,
128
 
                           GL_TRIANGLE_STRIP, /* mode */
129
 
                           0, /* first */
130
 
                           3); /* count */
131
 
 
132
 
  /* Draw a textured triangle */
133
 
  cogl_translate (100, 0, 0);
134
 
  cogl_vertex_buffer_disable (state->buffer, "gl_Color::blue");
135
 
  cogl_set_source (state->material);
136
 
  cogl_material_set_color4ub (state->material, 0xff, 0xff, 0xff, 0xff);
137
 
  cogl_vertex_buffer_draw (state->buffer,
138
 
                           GL_TRIANGLE_STRIP, /* mode */
139
 
                           0, /* first */
140
 
                           3); /* count */
141
 
 
142
 
  /* XXX: Experiments have shown that for some buggy drivers, when using
143
 
   * glReadPixels there is some kind of race, so we delay our test for a
144
 
   * few frames and a few seconds:
145
 
   */
146
 
  if (state->frame >= 2)
147
 
    validate_result (state);
148
 
  else
149
 
    g_usleep (G_USEC_PER_SEC);
150
 
 
151
 
  state->frame++;
152
 
}
153
 
 
154
 
static gboolean
155
 
queue_redraw (gpointer stage)
156
 
{
157
 
  clutter_actor_queue_redraw (CLUTTER_ACTOR (stage));
158
 
 
159
 
  return TRUE;
160
 
}
161
 
 
162
 
 
163
 
 
164
 
void
165
 
test_vertex_buffer_contiguous (TestConformSimpleFixture *fixture,
166
 
                               gconstpointer data)
167
 
{
168
 
  TestState state;
169
 
  ClutterActor *stage;
170
 
  ClutterColor stage_clr = {0x0, 0x0, 0x0, 0xff};
171
 
  ClutterActor *group;
172
 
  guint idle_source;
173
 
  guchar tex_data[] = {
174
 
    0xff, 0x00, 0x00, 0xff,
175
 
    0xff, 0x00, 0x00, 0xff,
176
 
    0x00, 0xff, 0x00, 0xff,
177
 
    0x00, 0xff, 0x00, 0xff
178
 
  };
179
 
 
180
 
  state.frame = 0;
181
 
 
182
 
  stage = clutter_stage_get_default ();
183
 
 
184
 
  clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_clr);
185
 
  clutter_actor_get_geometry (stage, &state.stage_geom);
186
 
 
187
 
  group = clutter_group_new ();
188
 
  clutter_actor_set_size (group,
189
 
                          state.stage_geom.width,
190
 
                          state.stage_geom.height);
191
 
  clutter_container_add_actor (CLUTTER_CONTAINER (stage), group);
192
 
 
193
 
  /* We force continuous redrawing of the stage, since we need to skip
194
 
   * the first few frames, and we wont be doing anything else that
195
 
   * will trigger redrawing. */
196
 
  idle_source = g_idle_add (queue_redraw, stage);
197
 
 
198
 
  g_signal_connect (group, "paint", G_CALLBACK (on_paint), &state);
199
 
 
200
 
  state.texture = cogl_texture_new_from_data (2, 2,
201
 
                                              COGL_TEXTURE_NO_SLICING,
202
 
                                              COGL_PIXEL_FORMAT_RGBA_8888,
203
 
                                              COGL_PIXEL_FORMAT_ANY,
204
 
                                              0, /* auto calc row stride */
205
 
                                              tex_data);
206
 
 
207
 
  state.material = cogl_material_new ();
208
 
  cogl_material_set_color4ub (state.material, 0x00, 0xff, 0x00, 0xff);
209
 
  cogl_material_set_layer (state.material, 0, state.texture);
210
 
 
211
 
  {
212
 
    GLfloat triangle_verts[3][2] =
213
 
      {
214
 
        {0.0,   0.0},
215
 
        {100.0, 100.0},
216
 
        {0.0,   100.0}
217
 
      };
218
 
    GLbyte triangle_colors[3][4] =
219
 
      {
220
 
        {0x00, 0x00, 0xff, 0xff}, /* blue */
221
 
        {0x00, 0x00, 0xff, 0x00}, /* transparent blue */
222
 
        {0x00, 0x00, 0xff, 0x00}  /* transparent blue */
223
 
      };
224
 
    GLfloat triangle_tex_coords[3][2] =
225
 
      {
226
 
        {0.0, 0.0},
227
 
        {1.0, 1.0},
228
 
        {0.0, 1.0}
229
 
      };
230
 
    state.buffer = cogl_vertex_buffer_new (3 /* n vertices */);
231
 
    cogl_vertex_buffer_add (state.buffer,
232
 
                            "gl_Vertex",
233
 
                            2, /* n components */
234
 
                            GL_FLOAT,
235
 
                            FALSE, /* normalized */
236
 
                            0, /* stride */
237
 
                            triangle_verts);
238
 
    cogl_vertex_buffer_add (state.buffer,
239
 
                            "gl_Color::blue",
240
 
                            4, /* n components */
241
 
                            GL_UNSIGNED_BYTE,
242
 
                            FALSE, /* normalized */
243
 
                            0, /* stride */
244
 
                            triangle_colors);
245
 
    cogl_vertex_buffer_add (state.buffer,
246
 
                            "gl_MultiTexCoord0",
247
 
                            2, /* n components */
248
 
                            GL_FLOAT,
249
 
                            FALSE, /* normalized */
250
 
                            0, /* stride */
251
 
                            triangle_tex_coords);
252
 
 
253
 
    cogl_vertex_buffer_submit (state.buffer);
254
 
  }
255
 
 
256
 
  clutter_actor_show_all (stage);
257
 
 
258
 
  clutter_main ();
259
 
 
260
 
  cogl_handle_unref (state.buffer);
261
 
  cogl_handle_unref (state.material);
262
 
  cogl_handle_unref (state.texture);
263
 
 
264
 
  g_source_remove (idle_source);
265
 
 
266
 
  if (g_test_verbose ())
267
 
    g_print ("OK\n");
268
 
}
269