summaryrefslogtreecommitdiff
path: root/examples/cogl-crate.c
diff options
context:
space:
mode:
authorJasper St. Pierre <jstpierre@mecheye.net>2011-09-08 21:35:48 -0400
committerRobert Bragg <robert@linux.intel.com>2011-11-01 12:50:43 +0000
commit5f7cdfed69b97c896f740a03e447f8de907e3e96 (patch)
tree839ccd9df098cf8e39f95e895e5294ea1ba6b6b0 /examples/cogl-crate.c
parentb2b4eba0e7d932c0b6267a21300018be8129a271 (diff)
downloadcogl-5f7cdfed69b97c896f740a03e447f8de907e3e96.tar.gz
crate: Use indices
https://bugzilla.gnome.org/show_bug.cgi?id=661019 Reviewed-by: Robert Bragg <robert@linux.intel.com>
Diffstat (limited to 'examples/cogl-crate.c')
-rw-r--r--examples/cogl-crate.c41
1 files changed, 19 insertions, 22 deletions
diff --git a/examples/cogl-crate.c b/examples/cogl-crate.c
index d997b6d3..da333285 100644
--- a/examples/cogl-crate.c
+++ b/examples/cogl-crate.c
@@ -9,6 +9,7 @@ typedef struct _Data
CoglMatrix view;
+ CoglIndices *indices;
CoglPrimitive *prim;
CoglTexture *texture;
CoglPipeline *crate_pipeline;
@@ -31,63 +32,47 @@ static CoglMatrix identity;
static CoglColor black;
static CoglColor white;
-/* A cube modelled as a list of triangles. Potentially this could be
- * done more efficiently as a triangle strip or using a separate index
- * array, but this way is pretty simple, if a little verbose. */
-CoglVertexP3T2 vertices[] =
+/* 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, 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 = */ 1.0f, 0.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 = */ 0.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 = */ 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, 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 = */ 1.0f, 0.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, 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}
};
@@ -217,9 +202,21 @@ main (int argc, char **argv)
cogl_color_set_from_4ub (&black, 0x00, 0x00, 0x00, 0xff);
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 (6 /* n_rectangles */);
data.prim = cogl_primitive_new_p3t2 (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");