mutter/tests/interactive/test-cogl-multitexture.c

168 lines
4.7 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include <gmodule.h>
#include <clutter/clutter.h>
#include <cogl/cogl.h>
typedef struct _TestMultiLayerMaterialState
{
ClutterActor *group;
CoglHandle material;
CoglHandle alpha_tex;
CoglHandle redhand_tex;
CoglHandle light_tex0;
gfloat *tex_coords;
CoglMatrix tex_matrix;
CoglMatrix rot_matrix;
} TestMultiLayerMaterialState;
static void
frame_cb (ClutterTimeline *timeline,
gint frame_no,
gpointer data)
{
TestMultiLayerMaterialState *state = data;
cogl_matrix_multiply (&state->tex_matrix,
&state->tex_matrix,
&state->rot_matrix);
cogl_material_set_layer_matrix (state->material, 2, &state->tex_matrix);
}
static void
material_rectangle_paint (ClutterActor *actor, gpointer data)
{
TestMultiLayerMaterialState *state = data;
cogl_set_source (state->material);
cogl_rectangle_with_multitexture_coords (0, 0, 200, 200,
state->tex_coords,
12);
}
G_MODULE_EXPORT int
Fully integrates CoglMaterial throughout the rest of Cogl This glues CoglMaterial in as the fundamental way that Cogl describes how to fill in geometry. It adds cogl_set_source (), which is used to set the material which will be used by all subsequent drawing functions It adds cogl_set_source_texture as a convenience for setting up a default material with a single texture layer, and cogl_set_source_color is now also a convenience for setting up a material with a solid fill. "drawing functions" include, cogl_rectangle, cogl_texture_rectangle, cogl_texture_multiple_rectangles, cogl_texture_polygon (though the cogl_texture_* funcs have been renamed; see below for details), cogl_path_fill/stroke and cogl_vertex_buffer_draw*. cogl_texture_rectangle, cogl_texture_multiple_rectangles and cogl_texture_polygon no longer take a texture handle; instead the current source material is referenced. The functions have also been renamed to: cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords and cogl_polygon respectivly. Most code that previously did: cogl_texture_rectangle (tex_handle, x, y,...); needs to be changed to now do: cogl_set_source_texture (tex_handle); cogl_rectangle_with_texture_coords (x, y,....); In the less likely case where you were blending your source texture with a color like: cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */ cogl_texture_rectangle (tex_handle, x, y,...); you will need your own material to do that: mat = cogl_material_new (); cogl_material_set_color4ub (r,g,b,a); cogl_material_set_layer (mat, 0, tex_handle)); cogl_set_source_material (mat); Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use cog_rectangle_with_texure_coords since these are the coordinates that cogl_rectangle will use. For cogl_texture_polygon; as well as dropping the texture handle, the n_vertices and vertices arguments were transposed for consistency. So code previously written as: cogl_texture_polygon (tex_handle, 3, verts, TRUE); need to be written as: cogl_set_source_texture (tex_handle); cogl_polygon (verts, 3, TRUE); All of the unit tests have been updated to now use the material API and test-cogl-material has been renamed to test-cogl-multitexture since any textured quad is now technically a test of CoglMaterial but this test specifically creates a material with multiple texture layers. Note: The GLES backend has not been updated yet; that will be done in a following commit.
2009-01-23 11:15:40 -05:00
test_cogl_multitexture_main (int argc, char *argv[])
{
GError *error = NULL;
ClutterTimeline *timeline;
ClutterBehaviour *r_behave;
ClutterActor *stage;
ClutterColor stage_color = { 0x61, 0x56, 0x56, 0xff };
TestMultiLayerMaterialState *state = g_new0 (TestMultiLayerMaterialState, 1);
ClutterGeometry geom;
gchar **files;
gfloat tex_coords[] =
{
/* tx1 ty1 tx2 ty2 */
0, 0, 1, 1,
0, 0, 1, 1,
0, 0, 1, 1
};
clutter_init (&argc, &argv);
stage = clutter_stage_get_default ();
clutter_actor_get_geometry (stage, &geom);
clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
/* We create a non-descript actor that we know doesn't have a
* default paint handler, so that we can easily control
* painting in a paint signal handler, without having to
* sub-class anything etc. */
state->group = clutter_group_new ();
clutter_actor_set_position (state->group, geom.width/2, geom.height/2);
g_signal_connect (state->group, "paint",
G_CALLBACK(material_rectangle_paint), state);
files = g_new (gchar*, 4);
files[0] = g_build_filename (TESTS_DATADIR, "redhand_alpha.png", NULL);
files[1] = g_build_filename (TESTS_DATADIR, "redhand.png", NULL);
files[2] = g_build_filename (TESTS_DATADIR, "light0.png", NULL);
files[3] = NULL;
state->alpha_tex =
cogl_texture_new_from_file (files[0],
[cogl] Move the texture filters to be a property of the material layer The texture filters are now a property of the material layer rather than the texture object. Whenever a texture is painted with a material it sets the filters on all of the GL textures in the Cogl texture. The filter is cached so that it won't be changed unnecessarily. The automatic mipmap generation has changed so that the mipmaps are only generated when the texture is painted instead of every time the data changes. Changing the texture sets a flag to mark that the mipmaps are dirty. This works better if the FBO extension is available because we can use glGenerateMipmap. If the extension is not available it will temporarily enable automatic mipmap generation and reupload the first pixel of each slice. This requires tracking the data for the first pixel. The COGL_TEXTURE_AUTO_MIPMAP flag has been replaced with COGL_TEXTURE_NO_AUTO_MIPMAP so that it will default to auto-mipmapping. The mipmap generation is now effectively free if you are not using a mipmap filter mode so you would only want to disable it if you had some special reason to generate your own mipmaps. ClutterTexture no longer has to store its own copy of the filter mode. Instead it stores it in the material and the property is directly set and read from that. This fixes problems with the filters getting out of sync when a cogl handle is set on the texture directly. It also avoids the mess of having to rerealize the texture if the filter quality changes to HIGH because Cogl will take of generating the mipmaps if needed.
2009-06-04 11:04:57 -04:00
COGL_TEXTURE_NO_SLICING,
COGL_PIXEL_FORMAT_ANY,
&error);
if (!state->alpha_tex)
g_critical ("Failed to load redhand_alpha.png: %s", error->message);
state->redhand_tex =
cogl_texture_new_from_file (files[1],
[cogl] Move the texture filters to be a property of the material layer The texture filters are now a property of the material layer rather than the texture object. Whenever a texture is painted with a material it sets the filters on all of the GL textures in the Cogl texture. The filter is cached so that it won't be changed unnecessarily. The automatic mipmap generation has changed so that the mipmaps are only generated when the texture is painted instead of every time the data changes. Changing the texture sets a flag to mark that the mipmaps are dirty. This works better if the FBO extension is available because we can use glGenerateMipmap. If the extension is not available it will temporarily enable automatic mipmap generation and reupload the first pixel of each slice. This requires tracking the data for the first pixel. The COGL_TEXTURE_AUTO_MIPMAP flag has been replaced with COGL_TEXTURE_NO_AUTO_MIPMAP so that it will default to auto-mipmapping. The mipmap generation is now effectively free if you are not using a mipmap filter mode so you would only want to disable it if you had some special reason to generate your own mipmaps. ClutterTexture no longer has to store its own copy of the filter mode. Instead it stores it in the material and the property is directly set and read from that. This fixes problems with the filters getting out of sync when a cogl handle is set on the texture directly. It also avoids the mess of having to rerealize the texture if the filter quality changes to HIGH because Cogl will take of generating the mipmaps if needed.
2009-06-04 11:04:57 -04:00
COGL_TEXTURE_NO_SLICING,
COGL_PIXEL_FORMAT_ANY,
&error);
if (!state->redhand_tex)
g_critical ("Failed to load redhand.png: %s", error->message);
state->light_tex0 =
cogl_texture_new_from_file (files[2],
[cogl] Move the texture filters to be a property of the material layer The texture filters are now a property of the material layer rather than the texture object. Whenever a texture is painted with a material it sets the filters on all of the GL textures in the Cogl texture. The filter is cached so that it won't be changed unnecessarily. The automatic mipmap generation has changed so that the mipmaps are only generated when the texture is painted instead of every time the data changes. Changing the texture sets a flag to mark that the mipmaps are dirty. This works better if the FBO extension is available because we can use glGenerateMipmap. If the extension is not available it will temporarily enable automatic mipmap generation and reupload the first pixel of each slice. This requires tracking the data for the first pixel. The COGL_TEXTURE_AUTO_MIPMAP flag has been replaced with COGL_TEXTURE_NO_AUTO_MIPMAP so that it will default to auto-mipmapping. The mipmap generation is now effectively free if you are not using a mipmap filter mode so you would only want to disable it if you had some special reason to generate your own mipmaps. ClutterTexture no longer has to store its own copy of the filter mode. Instead it stores it in the material and the property is directly set and read from that. This fixes problems with the filters getting out of sync when a cogl handle is set on the texture directly. It also avoids the mess of having to rerealize the texture if the filter quality changes to HIGH because Cogl will take of generating the mipmaps if needed.
2009-06-04 11:04:57 -04:00
COGL_TEXTURE_NO_SLICING,
COGL_PIXEL_FORMAT_ANY,
&error);
if (!state->light_tex0)
g_critical ("Failed to load light0.png: %s", error->message);
g_strfreev (files);
state->material = cogl_material_new ();
cogl_material_set_layer (state->material, 0, state->alpha_tex);
cogl_material_set_layer (state->material, 1, state->redhand_tex);
cogl_material_set_layer (state->material, 2, state->light_tex0);
state->tex_coords = tex_coords;
cogl_matrix_init_identity (&state->tex_matrix);
cogl_matrix_init_identity (&state->rot_matrix);
cogl_matrix_translate (&state->rot_matrix, 0.5, 0.5, 0);
cogl_matrix_rotate (&state->rot_matrix, 10.0, 0, 0, 1.0);
cogl_matrix_translate (&state->rot_matrix, -0.5, -0.5, 0);
clutter_actor_set_anchor_point (state->group, 86, 125);
clutter_container_add_actor (CLUTTER_CONTAINER(stage),
state->group);
timeline = clutter_timeline_new (7692);
clutter_timeline_set_loop (timeline, TRUE);
g_signal_connect (timeline, "new-frame", G_CALLBACK (frame_cb), state);
Fully integrates CoglMaterial throughout the rest of Cogl This glues CoglMaterial in as the fundamental way that Cogl describes how to fill in geometry. It adds cogl_set_source (), which is used to set the material which will be used by all subsequent drawing functions It adds cogl_set_source_texture as a convenience for setting up a default material with a single texture layer, and cogl_set_source_color is now also a convenience for setting up a material with a solid fill. "drawing functions" include, cogl_rectangle, cogl_texture_rectangle, cogl_texture_multiple_rectangles, cogl_texture_polygon (though the cogl_texture_* funcs have been renamed; see below for details), cogl_path_fill/stroke and cogl_vertex_buffer_draw*. cogl_texture_rectangle, cogl_texture_multiple_rectangles and cogl_texture_polygon no longer take a texture handle; instead the current source material is referenced. The functions have also been renamed to: cogl_rectangle_with_texture_coords, cogl_rectangles_with_texture_coords and cogl_polygon respectivly. Most code that previously did: cogl_texture_rectangle (tex_handle, x, y,...); needs to be changed to now do: cogl_set_source_texture (tex_handle); cogl_rectangle_with_texture_coords (x, y,....); In the less likely case where you were blending your source texture with a color like: cogl_set_source_color4ub (r,g,b,a); /* where r,g,b,a isn't just white */ cogl_texture_rectangle (tex_handle, x, y,...); you will need your own material to do that: mat = cogl_material_new (); cogl_material_set_color4ub (r,g,b,a); cogl_material_set_layer (mat, 0, tex_handle)); cogl_set_source_material (mat); Code that uses the texture coordinates, 0, 0, 1, 1 don't need to use cog_rectangle_with_texure_coords since these are the coordinates that cogl_rectangle will use. For cogl_texture_polygon; as well as dropping the texture handle, the n_vertices and vertices arguments were transposed for consistency. So code previously written as: cogl_texture_polygon (tex_handle, 3, verts, TRUE); need to be written as: cogl_set_source_texture (tex_handle); cogl_polygon (verts, 3, TRUE); All of the unit tests have been updated to now use the material API and test-cogl-material has been renamed to test-cogl-multitexture since any textured quad is now technically a test of CoglMaterial but this test specifically creates a material with multiple texture layers. Note: The GLES backend has not been updated yet; that will be done in a following commit.
2009-01-23 11:15:40 -05:00
r_behave =
clutter_behaviour_rotate_new (clutter_alpha_new_full (timeline,
CLUTTER_LINEAR),
CLUTTER_Y_AXIS,
CLUTTER_ROTATE_CW,
0.0, 360.0);
/* Apply it to our actor */
clutter_behaviour_apply (r_behave, state->group);
/* start the timeline and thus the animations */
clutter_timeline_start (timeline);
clutter_actor_show_all (stage);
clutter_main();
cogl_handle_unref (state->material);
cogl_handle_unref (state->alpha_tex);
cogl_handle_unref (state->redhand_tex);
cogl_handle_unref (state->light_tex0);
g_free (state);
g_object_unref (r_behave);
return 0;
}