| 1 |
#include <glib.h> |
| 2 |
#include <gmodule.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include <clutter/clutter.h> |
| 5 |
#include <cogl/cogl.h> |
| 6 |
|
| 7 |
/* Coglbox declaration |
| 8 |
*--------------------------------------------------*/ |
| 9 |
|
| 10 |
G_BEGIN_DECLS |
| 11 |
|
| 12 |
#define TEST_TYPE_COGLBOX test_coglbox_get_type() |
| 13 |
|
| 14 |
#define TEST_COGLBOX(obj) \ |
| 15 |
(G_TYPE_CHECK_INSTANCE_CAST ((obj), \ |
| 16 |
TEST_TYPE_COGLBOX, TestCoglboxClass)) |
| 17 |
|
| 18 |
#define TEST_COGLBOX_CLASS(klass) \ |
| 19 |
(G_TYPE_CHECK_CLASS_CAST ((klass), \ |
| 20 |
TEST_TYPE_COGLBOX, TestCoglboxClass)) |
| 21 |
|
| 22 |
#define TEST_IS_COGLBOX(obj) \ |
| 23 |
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), \ |
| 24 |
TEST_TYPE_COGLBOX)) |
| 25 |
|
| 26 |
#define TEST_IS_COGLBOX_CLASS(klass) \ |
| 27 |
(G_TYPE_CHECK_CLASS_TYPE ((klass), \ |
| 28 |
TEST_TYPE_COGLBOX)) |
| 29 |
|
| 30 |
#define TEST_COGLBOX_GET_CLASS(obj) \ |
| 31 |
(G_TYPE_INSTANCE_GET_CLASS ((obj), \ |
| 32 |
TEST_TYPE_COGLBOX, TestCoglboxClass)) |
| 33 |
|
| 34 |
typedef struct _TestCoglbox TestCoglbox; |
| 35 |
typedef struct _TestCoglboxClass TestCoglboxClass; |
| 36 |
typedef struct _TestCoglboxPrivate TestCoglboxPrivate; |
| 37 |
|
| 38 |
struct _TestCoglbox |
| 39 |
{ |
| 40 |
ClutterActor parent; |
| 41 |
|
| 42 |
/*< private >*/ |
| 43 |
TestCoglboxPrivate *priv; |
| 44 |
}; |
| 45 |
|
| 46 |
struct _TestCoglboxClass |
| 47 |
{ |
| 48 |
ClutterActorClass parent_class; |
| 49 |
|
| 50 |
/* padding for future expansion */ |
| 51 |
void (*_test_coglbox1) (void); |
| 52 |
void (*_test_coglbox2) (void); |
| 53 |
void (*_test_coglbox3) (void); |
| 54 |
void (*_test_coglbox4) (void); |
| 55 |
}; |
| 56 |
|
| 57 |
static GType test_coglbox_get_type (void) G_GNUC_CONST; |
| 58 |
|
| 59 |
G_END_DECLS |
| 60 |
|
| 61 |
/* Coglbox private declaration |
| 62 |
*--------------------------------------------------*/ |
| 63 |
|
| 64 |
G_DEFINE_TYPE (TestCoglbox, test_coglbox, CLUTTER_TYPE_ACTOR); |
| 65 |
|
| 66 |
#define TEST_COGLBOX_GET_PRIVATE(obj) \ |
| 67 |
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), TEST_TYPE_COGLBOX, TestCoglboxPrivate)) |
| 68 |
|
| 69 |
struct _TestCoglboxPrivate |
| 70 |
{ |
| 71 |
CoglHandle texhand_id; |
| 72 |
CoglHandle texture_id; |
| 73 |
CoglHandle offscreen_id; |
| 74 |
}; |
| 75 |
|
| 76 |
/* Coglbox implementation |
| 77 |
*--------------------------------------------------*/ |
| 78 |
|
| 79 |
static void |
| 80 |
test_coglbox_paint (ClutterActor *self) |
| 81 |
{ |
| 82 |
TestCoglboxPrivate *priv = TEST_COGLBOX_GET_PRIVATE (self); |
| 83 |
gfloat texcoords[4] = { 0, 0, 1, 1 }; |
| 84 |
CoglHandle material; |
| 85 |
|
| 86 |
cogl_set_source_color4ub (0x66, 0x66, 0xdd, 0xff); |
| 87 |
cogl_rectangle (0, 0, 400, 400); |
| 88 |
|
| 89 |
cogl_set_source_texture (priv->texhand_id); |
| 90 |
cogl_rectangle_with_texture_coords (0, 0, |
| 91 |
400, 400, |
| 92 |
0, 0, |
| 93 |
6, 6); |
| 94 |
|
| 95 |
cogl_push_framebuffer (priv->offscreen_id); |
| 96 |
|
| 97 |
cogl_set_source_color4ub (0xff, 0, 0, 0xff); |
| 98 |
cogl_rectangle (20, 20, 20 + 100, 20 + 100); |
| 99 |
|
| 100 |
cogl_set_source_color4ub (0, 0xff, 0, 0xff); |
| 101 |
cogl_rectangle (80, 80, 80 + 100, 80 + 100); |
| 102 |
|
| 103 |
cogl_pop_framebuffer (); |
| 104 |
|
| 105 |
material = cogl_material_new (); |
| 106 |
cogl_material_set_color4ub (material, 0x88, 0x88, 0x88, 0x88); |
| 107 |
cogl_material_set_layer (material, 0, priv->texture_id); |
| 108 |
cogl_set_source (material); |
| 109 |
cogl_rectangle_with_texture_coords (100, 100, |
| 110 |
300, 300, |
| 111 |
texcoords[0], |
| 112 |
texcoords[1], |
| 113 |
texcoords[2], |
| 114 |
texcoords[3]); |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
test_coglbox_finalize (GObject *object) |
| 119 |
{ |
| 120 |
G_OBJECT_CLASS (test_coglbox_parent_class)->finalize (object); |
| 121 |
} |
| 122 |
|
| 123 |
static void |
| 124 |
test_coglbox_dispose (GObject *object) |
| 125 |
{ |
| 126 |
TestCoglboxPrivate *priv; |
| 127 |
|
| 128 |
priv = TEST_COGLBOX_GET_PRIVATE (object); |
| 129 |
|
| 130 |
cogl_handle_unref (priv->texture_id); |
| 131 |
cogl_handle_unref (priv->offscreen_id); |
| 132 |
|
| 133 |
G_OBJECT_CLASS (test_coglbox_parent_class)->dispose (object); |
| 134 |
} |
| 135 |
|
| 136 |
/* A newly created Cogl framebuffer will be initialized with a |
| 137 |
* viewport covering the size of the viewport i.e. equavalent to: |
| 138 |
* |
| 139 |
* calling cogl_framebuffer_set_viewport ( |
| 140 |
* fb, |
| 141 |
* 0, 0, |
| 142 |
* cogl_framebuffer_get_viewport_width (fb), |
| 143 |
* cogl_framebuffer_get_viewport_width (fb)); |
| 144 |
* |
| 145 |
* The projection matrix will be an identity matrix. |
| 146 |
* |
| 147 |
* The modelview matrix will be an identity matrix, and this will |
| 148 |
* create a coordinate system - like OpenGL - with the viewport |
| 149 |
* being mapped to a unit cube with the origin (0, 0, 0) in the |
| 150 |
* center, x, y and z ranging from -1 to 1 with (-1, -1) being top |
| 151 |
* left and (1, 1) bottom right. |
| 152 |
* |
| 153 |
* This sets up a Clutter like coordinate system for a Cogl |
| 154 |
* framebuffer |
| 155 |
*/ |
| 156 |
void |
| 157 |
setup_viewport (unsigned int width, |
| 158 |
unsigned int height, |
| 159 |
float fovy, |
| 160 |
float aspect, |
| 161 |
float z_near, |
| 162 |
float z_far) |
| 163 |
{ |
| 164 |
float z_camera; |
| 165 |
CoglMatrix projection_matrix; |
| 166 |
CoglMatrix mv_matrix; |
| 167 |
|
| 168 |
cogl_set_viewport (0, 0, width, height); |
| 169 |
|
| 170 |
/* For Ortho projection. |
| 171 |
* _cogl_matrix_stack_ortho (projection_stack, 0, width, 0, height, -1, 1); |
| 172 |
*/ |
| 173 |
|
| 174 |
cogl_perspective (fovy, aspect, z_near, z_far); |
| 175 |
|
| 176 |
/* |
| 177 |
* In theory, we can compute the camera distance from screen as: |
| 178 |
* |
| 179 |
* 0.5 * tan (FOV) |
| 180 |
* |
| 181 |
* However, it's better to compute the z_camera from our projection |
| 182 |
* matrix so that we get a 1:1 mapping at the screen distance. Consider |
| 183 |
* the upper-left corner of the screen. It has object coordinates |
| 184 |
* (0,0,0), so by the transform below, ends up with eye coordinate |
| 185 |
* |
| 186 |
* x_eye = x_object / width - 0.5 = - 0.5 |
| 187 |
* y_eye = (height - y_object) / width - 0.5 = 0.5 |
| 188 |
* z_eye = z_object / width - z_camera = - z_camera |
| 189 |
* |
| 190 |
* From cogl_perspective(), we know that the projection matrix has |
| 191 |
* the form: |
| 192 |
* |
| 193 |
* (x, 0, 0, 0) |
| 194 |
* (0, y, 0, 0) |
| 195 |
* (0, 0, c, d) |
| 196 |
* (0, 0, -1, 0) |
| 197 |
* |
| 198 |
* Applied to the above, we get clip coordinates of |
| 199 |
* |
| 200 |
* x_clip = x * (- 0.5) |
| 201 |
* y_clip = y * 0.5 |
| 202 |
* w_clip = - 1 * (- z_camera) = z_camera |
| 203 |
* |
| 204 |
* Dividing through by w to get normalized device coordinates, we |
| 205 |
* have, x_nd = x * 0.5 / z_camera, y_nd = - y * 0.5 / z_camera. |
| 206 |
* The upper left corner of the screen has normalized device coordinates, |
| 207 |
* (-1, 1), so to have the correct 1:1 mapping, we have to have: |
| 208 |
* |
| 209 |
* z_camera = 0.5 * x = 0.5 * y |
| 210 |
* |
| 211 |
* If x != y, then we have a non-uniform aspect ration, and a 1:1 mapping |
| 212 |
* doesn't make sense. |
| 213 |
*/ |
| 214 |
|
| 215 |
cogl_get_projection_matrix (&projection_matrix); |
| 216 |
z_camera = 0.5 * projection_matrix.xx; |
| 217 |
|
| 218 |
cogl_matrix_init_identity (&mv_matrix); |
| 219 |
cogl_matrix_translate (&mv_matrix, -0.5f, -0.5f, -z_camera); |
| 220 |
cogl_matrix_scale (&mv_matrix, 1.0f / width, -1.0f / height, 1.0f / width); |
| 221 |
cogl_matrix_translate (&mv_matrix, 0.0f, -1.0 * height, 0.0f); |
| 222 |
cogl_set_modelview_matrix (&mv_matrix); |
| 223 |
} |
| 224 |
|
| 225 |
static void |
| 226 |
test_coglbox_map (ClutterActor *actor) |
| 227 |
{ |
| 228 |
TestCoglboxPrivate *priv = TEST_COGLBOX_GET_PRIVATE (actor); |
| 229 |
ClutterActor *stage; |
| 230 |
ClutterPerspective perspective; |
| 231 |
float stage_width; |
| 232 |
float stage_height; |
| 233 |
|
| 234 |
CLUTTER_ACTOR_CLASS (test_coglbox_parent_class)->map (actor); |
| 235 |
|
| 236 |
printf ("Creating offscreen\n"); |
| 237 |
priv->offscreen_id = cogl_offscreen_new_to_texture (priv->texture_id); |
| 238 |
|
| 239 |
stage = clutter_actor_get_stage (actor); |
| 240 |
clutter_stage_get_perspective (CLUTTER_STAGE (stage), &perspective); |
| 241 |
clutter_actor_get_size (stage, &stage_width, &stage_height); |
| 242 |
|
| 243 |
cogl_push_framebuffer (priv->offscreen_id); |
| 244 |
|
| 245 |
setup_viewport (stage_width, stage_height, |
| 246 |
perspective.fovy, |
| 247 |
perspective.aspect, |
| 248 |
perspective.z_near, |
| 249 |
perspective.z_far); |
| 250 |
|
| 251 |
cogl_pop_framebuffer (); |
| 252 |
|
| 253 |
if (priv->offscreen_id == COGL_INVALID_HANDLE) |
| 254 |
printf ("Failed creating offscreen to texture!\n"); |
| 255 |
} |
| 256 |
|
| 257 |
static void |
| 258 |
test_coglbox_init (TestCoglbox *self) |
| 259 |
{ |
| 260 |
TestCoglboxPrivate *priv; |
| 261 |
gchar *file; |
| 262 |
|
| 263 |
self->priv = priv = TEST_COGLBOX_GET_PRIVATE(self); |
| 264 |
|
| 265 |
printf ("Loading redhand.png\n"); |
| 266 |
file = g_build_filename (TESTS_DATADIR, "redhand.png", NULL); |
| 267 |
priv->texhand_id = cogl_texture_new_from_file (file, |
| 268 |
COGL_TEXTURE_NONE, |
| 269 |
COGL_PIXEL_FORMAT_ANY, |
| 270 |
NULL); |
| 271 |
g_free (file); |
| 272 |
|
| 273 |
printf ("Creating texture with size\n"); |
| 274 |
priv->texture_id = cogl_texture_new_with_size (200, 200, |
| 275 |
COGL_TEXTURE_NONE, |
| 276 |
COGL_PIXEL_FORMAT_RGB_888); |
| 277 |
|
| 278 |
if (priv->texture_id == COGL_INVALID_HANDLE) |
| 279 |
printf ("Failed creating texture with size!\n"); |
| 280 |
} |
| 281 |
|
| 282 |
static void |
| 283 |
test_coglbox_class_init (TestCoglboxClass *klass) |
| 284 |
{ |
| 285 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 286 |
ClutterActorClass *actor_class = CLUTTER_ACTOR_CLASS (klass); |
| 287 |
|
| 288 |
gobject_class->finalize = test_coglbox_finalize; |
| 289 |
gobject_class->dispose = test_coglbox_dispose; |
| 290 |
|
| 291 |
actor_class->map = test_coglbox_map; |
| 292 |
actor_class->paint = test_coglbox_paint; |
| 293 |
|
| 294 |
g_type_class_add_private (gobject_class, sizeof (TestCoglboxPrivate)); |
| 295 |
} |
| 296 |
|
| 297 |
static ClutterActor* |
| 298 |
test_coglbox_new (void) |
| 299 |
{ |
| 300 |
return g_object_new (TEST_TYPE_COGLBOX, NULL); |
| 301 |
} |
| 302 |
|
| 303 |
G_MODULE_EXPORT int |
| 304 |
test_cogl_offscreen_main (int argc, char *argv[]) |
| 305 |
{ |
| 306 |
ClutterActor *stage; |
| 307 |
ClutterActor *coglbox; |
| 308 |
|
| 309 |
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS) |
| 310 |
return 1; |
| 311 |
|
| 312 |
/* Stage */ |
| 313 |
stage = clutter_stage_new (); |
| 314 |
clutter_actor_set_size (stage, 400, 400); |
| 315 |
clutter_stage_set_title (CLUTTER_STAGE (stage), "Cogl Offscreen Buffers"); |
| 316 |
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL); |
| 317 |
|
| 318 |
/* Cogl Box */ |
| 319 |
coglbox = test_coglbox_new (); |
| 320 |
clutter_container_add_actor (CLUTTER_CONTAINER (stage), coglbox); |
| 321 |
|
| 322 |
clutter_actor_show_all (stage); |
| 323 |
|
| 324 |
clutter_main (); |
| 325 |
|
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
G_MODULE_EXPORT const char * |
| 330 |
test_cogl_offscreen_describe (void) |
| 331 |
{ |
| 332 |
return "Offscreen buffer support in Cogl."; |
| 333 |
} |