1
0
Fork 0
mutter-performance-source/examples/cogl-crate.c

300 lines
10 KiB
C
Raw Normal View History

#include <cogl/cogl.h>
#include <cogl-pango/cogl-pango.h>
/* The state for this example... */
typedef struct _Data
{
CoglFramebuffer *fb;
int framebuffer_width;
int framebuffer_height;
CoglMatrix view;
CoglIndices *indices;
CoglPrimitive *prim;
Add a strong CoglTexture type to replace CoglHandle As part of the on going, incremental effort to purge the non type safe CoglHandle type from the Cogl API this patch tackles most of the CoglHandle uses relating to textures. We'd postponed making this change for quite a while because we wanted to have a clearer understanding of how we wanted to evolve the texture APIs towards Cogl 2.0 before exposing type safety here which would be difficult to change later since it would imply breaking APIs. The basic idea that we are steering towards now is that CoglTexture can be considered to be the most primitive interface we have for any object representing a texture. The texture interface would provide roughly these methods: cogl_texture_get_width cogl_texture_get_height cogl_texture_can_repeat cogl_texture_can_mipmap cogl_texture_generate_mipmap; cogl_texture_get_format cogl_texture_set_region cogl_texture_get_region Besides the texture interface we will then start to expose types corresponding to specific texture types: CoglTexture2D, CoglTexture3D, CoglTexture2DSliced, CoglSubTexture, CoglAtlasTexture and CoglTexturePixmapX11. We will then also expose an interface for the high-level texture types we have (such as CoglTexture2DSlice, CoglSubTexture and CoglAtlasTexture) called CoglMetaTexture. CoglMetaTexture is an additional interface that lets you iterate a virtual region of a meta texture and get mappings of primitive textures to sub-regions of that virtual region. Internally we already have this kind of abstraction for dealing with sliced texture, sub-textures and atlas textures in a consistent way, so this will just make that abstraction public. The aim here is to clarify that there is a difference between primitive textures (CoglTexture2D/3D) and some of the other high-level textures, and also enable developers to implement primitives that can support meta textures since they can only be used with the cogl_rectangle API currently. The thing that's not so clean-cut with this are the texture constructors we have currently; such as cogl_texture_new_from_file which no longer make sense when CoglTexture is considered to be an interface. These will basically just become convenient factory functions and it's just a bit unusual that they are within the cogl_texture namespace. It's worth noting here that all the texture type APIs will also have their own type specific constructors so these functions will only be used for the convenience of being able to create a texture without really wanting to know the details of what type of texture you need. Longer term for 2.0 we may come up with replacement names for these factory functions or the other thing we are considering is designing some asynchronous factory functions instead since it's so often detrimental to application performance to be blocked waiting for a texture to be uploaded to the GPU. Reviewed-by: Neil Roberts <neil@linux.intel.com>
2011-08-24 20:30:34 +00:00
CoglTexture *texture;
CoglPipeline *crate_pipeline;
CoglPangoFontMap *pango_font_map;
PangoContext *pango_context;
PangoFontDescription *pango_font_desc;
PangoLayout *hello_label;
int hello_label_width;
int hello_label_height;
GTimer *timer;
CoglBool swap_ready;
} Data;
/* A static identity matrix initialized for convenience. */
static CoglMatrix identity;
/* static colors initialized for convenience. */
static CoglColor white;
/* A cube modelled using 4 vertices for each face.
*
* We use an index buffer when drawing the cube later so the GPU will
* actually read each face as 2 separate triangles.
*/
static CoglVertexP3T2 vertices[] =
{
/* Front face */
{ /* pos = */ -1.0f, -1.0f, 1.0f, /* tex coords = */ 0.0f, 1.0f},
{ /* pos = */ 1.0f, -1.0f, 1.0f, /* tex coords = */ 1.0f, 1.0f},
{ /* pos = */ 1.0f, 1.0f, 1.0f, /* tex coords = */ 1.0f, 0.0f},
{ /* pos = */ -1.0f, 1.0f, 1.0f, /* tex coords = */ 0.0f, 0.0f},
/* Back face */
{ /* pos = */ -1.0f, -1.0f, -1.0f, /* tex coords = */ 1.0f, 0.0f},
{ /* pos = */ -1.0f, 1.0f, -1.0f, /* tex coords = */ 1.0f, 1.0f},
{ /* pos = */ 1.0f, 1.0f, -1.0f, /* tex coords = */ 0.0f, 1.0f},
{ /* pos = */ 1.0f, -1.0f, -1.0f, /* tex coords = */ 0.0f, 0.0f},
/* Top face */
{ /* pos = */ -1.0f, 1.0f, -1.0f, /* tex coords = */ 0.0f, 1.0f},
{ /* pos = */ -1.0f, 1.0f, 1.0f, /* tex coords = */ 0.0f, 0.0f},
{ /* pos = */ 1.0f, 1.0f, 1.0f, /* tex coords = */ 1.0f, 0.0f},
{ /* pos = */ 1.0f, 1.0f, -1.0f, /* tex coords = */ 1.0f, 1.0f},
/* Bottom face */
{ /* pos = */ -1.0f, -1.0f, -1.0f, /* tex coords = */ 1.0f, 1.0f},
{ /* pos = */ 1.0f, -1.0f, -1.0f, /* tex coords = */ 0.0f, 1.0f},
{ /* pos = */ 1.0f, -1.0f, 1.0f, /* tex coords = */ 0.0f, 0.0f},
{ /* pos = */ -1.0f, -1.0f, 1.0f, /* tex coords = */ 1.0f, 0.0f},
/* Right face */
{ /* pos = */ 1.0f, -1.0f, -1.0f, /* tex coords = */ 1.0f, 0.0f},
{ /* pos = */ 1.0f, 1.0f, -1.0f, /* tex coords = */ 1.0f, 1.0f},
{ /* pos = */ 1.0f, 1.0f, 1.0f, /* tex coords = */ 0.0f, 1.0f},
{ /* pos = */ 1.0f, -1.0f, 1.0f, /* tex coords = */ 0.0f, 0.0f},
/* Left face */
{ /* pos = */ -1.0f, -1.0f, -1.0f, /* tex coords = */ 0.0f, 0.0f},
{ /* pos = */ -1.0f, -1.0f, 1.0f, /* tex coords = */ 1.0f, 0.0f},
{ /* pos = */ -1.0f, 1.0f, 1.0f, /* tex coords = */ 1.0f, 1.0f},
{ /* pos = */ -1.0f, 1.0f, -1.0f, /* tex coords = */ 0.0f, 1.0f}
};
static void
paint (Data *data)
{
CoglFramebuffer *fb = data->fb;
float rotation;
cogl_framebuffer_clear4f (fb,
COGL_BUFFER_BIT_COLOR|COGL_BUFFER_BIT_DEPTH,
0, 0, 0, 1);
cogl_framebuffer_push_matrix (fb);
cogl_framebuffer_translate (fb,
data->framebuffer_width / 2,
data->framebuffer_height / 2,
0);
cogl_framebuffer_scale (fb, 75, 75, 75);
/* Update the rotation based on the time the application has been
running so that we get a linear animation regardless of the frame
rate */
rotation = g_timer_elapsed (data->timer, NULL) * 60.0f;
/* Rotate the cube separately around each axis.
*
* Note: Cogl matrix manipulation follows the same rules as for
* OpenGL. We use column-major matrices and - if you consider the
* transformations happening to the model - then they are combined
* in reverse order which is why the rotation is done last, since
* we want it to be a rotation around the origin, before it is
* scaled and translated.
*/
cogl_framebuffer_rotate (fb, rotation, 0, 0, 1);
cogl_framebuffer_rotate (fb, rotation, 0, 1, 0);
cogl_framebuffer_rotate (fb, rotation, 1, 0, 0);
cogl_framebuffer_draw_primitive (fb, data->crate_pipeline, data->prim);
cogl_framebuffer_pop_matrix (fb);
/* And finally render our Pango layouts... */
cogl_pango_render_layout (data->hello_label,
(data->framebuffer_width / 2) -
(data->hello_label_width / 2),
(data->framebuffer_height / 2) -
(data->hello_label_height / 2),
&white, 0);
}
static void
frame_event_cb (CoglOnscreen *onscreen,
CoglFrameEvent event,
CoglFrameInfo *info,
void *user_data)
{
Data *data = user_data;
if (event == COGL_FRAME_EVENT_SYNC)
data->swap_ready = TRUE;
}
int
main (int argc, char **argv)
{
CoglContext *ctx;
CoglOnscreen *onscreen;
CoglFramebuffer *fb;
Adds CoglError api Although we use GLib internally in Cogl we would rather not leak GLib api through Cogl's own api, except through explicitly namespaced cogl_glib_ / cogl_gtype_ feature apis. One of the benefits we see to not leaking GLib through Cogl's public API is that documentation for Cogl won't need to first introduce the Glib API to newcomers, thus hopefully lowering the barrier to learning Cogl. This patch provides a Cogl specific typedef for reporting runtime errors which by no coincidence matches the typedef for GError exactly. If Cogl is built with --enable-glib (default) then developers can even safely assume that a CoglError is a GError under the hood. This patch also enforces a consistent policy for when NULL is passed as an error argument and an error is thrown. In this case we log the error and abort the application, instead of silently ignoring it. In common cases where nothing has been implemented to handle a particular error and/or where applications are just printing the error and aborting themselves then this saves some typing. This also seems more consistent with language based exceptions which usually cause a program to abort if they are not explicitly caught (which passing a non-NULL error signifies in this case) Since this policy for NULL error pointers is stricter than the standard GError convention, there is a clear note in the documentation to warn developers that are used to using the GError api. Reviewed-by: Neil Roberts <neil@linux.intel.com> (cherry picked from commit b068d5ea09ab32c37e8c965fc8582c85d1b2db46) Note: Since we can't change the Cogl 1.x api the patch was changed to not rename _error_quark() functions to be _error_domain() functions and although it's a bit ugly, instead of providing our own CoglError type that's compatible with GError we simply #define CoglError to GError unless Cogl is built with glib disabled. Note: this patch does technically introduce an API break since it drops the cogl_error_get_type() symbol generated by glib-mkenum (Since the CoglError enum was replaced by a CoglSystemError enum) but for now we are assuming that this will not affect anyone currently using the Cogl API. If this does turn out to be a problem in practice then we would be able to fix this my manually copying an implementation of cogl_error_get_type() generated by glib-mkenum into a compatibility source file and we could also define the original COGL_ERROR_ enums for compatibility too. Note: another minor concern with cherry-picking this patch to the 1.14 branch is that an api scanner would be lead to believe that some APIs have changed, and for example the gobject-introspection parser which understands the semantics of GError will not understand the semantics of CoglError. We expect most people that have tried to use gobject-introspection with Cogl already understand though that it is not well suited to generating bindings of the Cogl api anyway and we aren't aware or anyone depending on such bindings for apis involving GErrors. (GnomeShell only makes very-very minimal use of Cogl via the gjs bindings for the cogl_rectangle and cogl_color apis.) The main reason we have cherry-picked this patch to the 1.14 branch even given the above concerns is that without it it would become very awkward for us to cherry-pick other beneficial patches from master.
2012-08-31 18:28:27 +00:00
CoglError *error = NULL;
Data data;
PangoRectangle hello_label_size;
float fovy, aspect, z_near, z_2d, z_far;
CoglDepthState depth_state;
ctx = cogl_context_new (NULL, &error);
if (!ctx) {
fprintf (stderr, "Failed to create context: %s\n", error->message);
return 1;
}
onscreen = cogl_onscreen_new (ctx, 640, 480);
fb = COGL_FRAMEBUFFER (onscreen);
data.fb = fb;
data.framebuffer_width = cogl_framebuffer_get_width (fb);
data.framebuffer_height = cogl_framebuffer_get_height (fb);
data.timer = g_timer_new ();
cogl_onscreen_show (onscreen);
cogl_push_framebuffer (fb);
cogl_set_viewport (0, 0, data.framebuffer_width, data.framebuffer_height);
fovy = 60; /* y-axis field of view */
aspect = (float)data.framebuffer_width/(float)data.framebuffer_height;
z_near = 0.1; /* distance to near clipping plane */
z_2d = 1000; /* position to 2d plane */
z_far = 2000; /* distance to far clipping plane */
cogl_perspective (fovy, aspect, z_near, z_far);
/* Since the pango renderer emits geometry in pixel/device coordinates
* and the anti aliasing is implemented with the assumption that the
* geometry *really* does end up pixel aligned, we setup a modelview
* matrix so that for geometry in the plane z = 0 we exactly map x
* coordinates in the range [0,stage_width] and y coordinates in the
* range [0,stage_height] to the framebuffer extents with (0,0) being
* the top left.
*
* This is roughly what Clutter does for a ClutterStage, but this
* demonstrates how it is done manually using Cogl.
*/
cogl_matrix_init_identity (&data.view);
cogl_matrix_view_2d_in_perspective (&data.view, fovy, aspect, z_near, z_2d,
data.framebuffer_width,
data.framebuffer_height);
cogl_set_modelview_matrix (&data.view);
cogl_pop_framebuffer ();
/* Initialize some convenient constants */
cogl_matrix_init_identity (&identity);
cogl_color_set_from_4ub (&white, 0xff, 0xff, 0xff, 0xff);
/* rectangle indices allow the GPU to interpret a list of quads (the
* faces of our cube) as a list of triangles.
*
* Since this is a very common thing to do
* cogl_get_rectangle_indices() is a convenience function for
* accessing internal index buffers that can be shared.
*/
data.indices = cogl_get_rectangle_indices (ctx, 6 /* n_rectangles */);
data.prim = cogl_primitive_new_p3t2 (ctx, COGL_VERTICES_MODE_TRIANGLES,
G_N_ELEMENTS (vertices),
vertices);
/* Each face will have 6 indices so we have 6 * 6 indices in total... */
cogl_primitive_set_indices (data.prim,
data.indices,
6 * 6);
/* Load a jpeg crate texture from a file */
printf ("crate.jpg (CC by-nc-nd http://bit.ly/9kP45T) ShadowRunner27 http://bit.ly/m1YXLh\n");
data.texture = cogl_texture_new_from_file (COGL_EXAMPLES_DATA "crate.jpg",
COGL_TEXTURE_NO_SLICING,
COGL_PIXEL_FORMAT_ANY,
&error);
if (!data.texture)
g_error ("Failed to load texture: %s", error->message);
/* a CoglPipeline conceptually describes all the state for vertex
* processing, fragment processing and blending geometry. When
* drawing the geometry for the crate this pipeline says to sample a
* single texture during fragment processing... */
data.crate_pipeline = cogl_pipeline_new (ctx);
cogl_pipeline_set_layer_texture (data.crate_pipeline, 0, data.texture);
/* Since the box is made of multiple triangles that will overlap
* when drawn and we don't control the order they are drawn in, we
* enable depth testing to make sure that triangles that shouldn't
* be visible get culled by the GPU. */
cogl_depth_state_init (&depth_state);
cogl_depth_state_set_test_enabled (&depth_state, TRUE);
cogl_pipeline_set_depth_state (data.crate_pipeline, &depth_state, NULL);
/* Setup a Pango font map and context */
data.pango_font_map = COGL_PANGO_FONT_MAP (cogl_pango_font_map_new());
cogl_pango_font_map_set_use_mipmapping (data.pango_font_map, TRUE);
data.pango_context = cogl_pango_font_map_create_context (data.pango_font_map);
data.pango_font_desc = pango_font_description_new ();
pango_font_description_set_family (data.pango_font_desc, "Sans");
pango_font_description_set_size (data.pango_font_desc, 30 * PANGO_SCALE);
/* Setup the "Hello Cogl" text */
data.hello_label = pango_layout_new (data.pango_context);
pango_layout_set_font_description (data.hello_label, data.pango_font_desc);
pango_layout_set_text (data.hello_label, "Hello Cogl", -1);
pango_layout_get_extents (data.hello_label, NULL, &hello_label_size);
data.hello_label_width = PANGO_PIXELS (hello_label_size.width);
data.hello_label_height = PANGO_PIXELS (hello_label_size.height);
cogl_push_framebuffer (fb);
data.swap_ready = TRUE;
cogl_onscreen_add_frame_callback (COGL_ONSCREEN (fb),
frame_event_cb,
&data,
NULL); /* destroy notify */
while (1)
{
CoglPollFD *poll_fds;
int n_poll_fds;
int64_t timeout;
if (data.swap_ready)
{
paint (&data);
cogl_onscreen_swap_buffers (COGL_ONSCREEN (fb));
}
cogl_poll_get_info (ctx, &poll_fds, &n_poll_fds, &timeout);
g_poll ((GPollFD *) poll_fds, n_poll_fds,
timeout == -1 ? -1 : timeout / 1000);
cogl_poll_dispatch (ctx, poll_fds, n_poll_fds);
}
return 0;
}