From 9498d505dd94657e1af72147515c9fb746519115 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Tue, 12 Oct 2010 12:54:07 +0100 Subject: [PATCH] cogl: Adds experimental CoglPrimitive API A CoglPrimitive is a retainable object for drawing a single primitive, such as a triangle strip, fan or list. CoglPrimitives build on CoglVertexAttributes and CoglIndices which themselves build on CoglVertexArrays and CoglIndexArrays respectively. A CoglPrimitive encapsulates enough information such that it can be retained in a queue (e.g. the Cogl Journal, or renderlists in the future) and drawn at some later time. --- cogl/Makefile.am | 3 + cogl/cogl-primitive-private.h | 46 +++++ cogl/cogl-primitive.c | 223 ++++++++++++++++++++++++ cogl/cogl-primitive.h | 311 ++++++++++++++++++++++++++++++++++ cogl/cogl.h | 1 + 5 files changed, 584 insertions(+) create mode 100644 cogl/cogl-primitive-private.h create mode 100644 cogl/cogl-primitive.c create mode 100644 cogl/cogl-primitive.h diff --git a/cogl/Makefile.am b/cogl/Makefile.am index a909bdf19..4616245b7 100644 --- a/cogl/Makefile.am +++ b/cogl/Makefile.am @@ -76,6 +76,7 @@ cogl_public_h = \ $(srcdir)/cogl-vertex-array.h \ $(srcdir)/cogl-indices.h \ $(srcdir)/cogl-vertex-attribute.h \ + $(srcdir)/cogl-primitive.h \ $(srcdir)/cogl.h \ $(NULL) @@ -217,6 +218,8 @@ cogl_sources_c = \ $(srcdir)/cogl-indices.c \ $(srcdir)/cogl-vertex-attribute-private.h \ $(srcdir)/cogl-vertex-attribute.c \ + $(srcdir)/cogl-primitive-private.h \ + $(srcdir)/cogl-primitive.c \ $(srcdir)/cogl-matrix.c \ $(srcdir)/cogl-vector.c \ $(srcdir)/cogl-matrix-private.h \ diff --git a/cogl/cogl-primitive-private.h b/cogl/cogl-primitive-private.h new file mode 100644 index 000000000..e3de65f69 --- /dev/null +++ b/cogl/cogl-primitive-private.h @@ -0,0 +1,46 @@ +/* + * Cogl + * + * An object oriented GL/GLES Abstraction/Utility Layer + * + * Copyright (C) 2010 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + * + * + * Authors: + * Robert Bragg + */ + +#ifndef __COGL_PRIMITIVE_PRIVATE_H +#define __COGL_PRIMITIVE_PRIVATE_H + +#include "cogl-object-private.h" +#include "cogl-vertex-array-private.h" + +struct _CoglPrimitive +{ + CoglObject _parent; + + CoglVerticesMode mode; + int first_vertex; + int n_vertices; + CoglIndices *indices; + GArray *attributes; +}; + +#endif /* __COGL_PRIMITIVE_PRIVATE_H */ + diff --git a/cogl/cogl-primitive.c b/cogl/cogl-primitive.c new file mode 100644 index 000000000..61b3dd656 --- /dev/null +++ b/cogl/cogl-primitive.c @@ -0,0 +1,223 @@ +/* + * Cogl + * + * An object oriented GL/GLES Abstraction/Utility Layer + * + * Copyright (C) 2010 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + * + * + * Authors: + * Robert Bragg + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "cogl-object-private.h" +#include "cogl-primitive.h" +#include "cogl-primitive-private.h" + +#include + +static void _cogl_primitive_free (CoglPrimitive *primitive); + +COGL_OBJECT_DEFINE (Primitive, primitive); + +/* XXX: should we have an n_attributes arg instead of NULL terminating? */ +CoglPrimitive * +cogl_primitive_new_with_attributes_array (CoglVerticesMode mode, + int n_vertices, + CoglVertexAttribute **attributes) +{ + CoglPrimitive *primitive = g_slice_new (CoglPrimitive); + int i; + + primitive->mode = mode; + primitive->first_vertex = 0; + primitive->n_vertices = n_vertices; + primitive->indices = NULL; + primitive->attributes = + g_array_new (TRUE, FALSE, sizeof (CoglVertexAttribute *)); + + for (i = 0; attributes[i]; i++) + { + CoglVertexAttribute *attribute = attributes[i]; + cogl_object_ref (attribute); + + g_return_val_if_fail (cogl_is_vertex_attribute (attributes), NULL); + + g_array_append_val (primitive->attributes, attribute); + } + + return _cogl_primitive_object_new (primitive); +} + +CoglPrimitive * +cogl_primitive_new (CoglVerticesMode mode, + int n_vertices, + ...) +{ + va_list ap; + int n_attributes; + CoglVertexAttribute **attributes; + int i; + CoglVertexAttribute *attribute; + + va_start (ap, n_vertices); + for (n_attributes = 0; va_arg (ap, CoglVertexAttribute *); n_attributes++) + ; + va_end (ap); + + attributes = g_alloca (sizeof (CoglVertexAttribute *) * (n_attributes + 1)); + attributes[n_attributes] = NULL; + + va_start (ap, n_vertices); + for (i = 0; (attribute = va_arg (ap, CoglVertexAttribute *)); i++) + attributes[i] = attribute; + va_end (ap); + + return cogl_primitive_new_with_attributes_array (mode, n_vertices, + attributes); +} + +static void +free_attributes_list (CoglPrimitive *primitive) +{ + int i; + + for (i = 0; i < primitive->attributes->len; i++) + { + CoglVertexAttribute *attribute = + g_array_index (primitive->attributes, CoglVertexAttribute *, i); + cogl_object_unref (attribute); + } + g_array_set_size (primitive->attributes, 0); +} + +static void +_cogl_primitive_free (CoglPrimitive *primitive) +{ + free_attributes_list (primitive); + + g_array_free (primitive->attributes, TRUE); + + g_slice_free (CoglPrimitive, primitive); +} + +void +cogl_primitive_set_attributes (CoglPrimitive *primitive, + CoglVertexAttribute **attributes) +{ + int i; + + free_attributes_list (primitive); + + g_array_set_size (primitive->attributes, 0); + for (i = 0; attributes[i]; i++) + { + cogl_object_ref (attributes[i]); + g_return_if_fail (cogl_is_vertex_attribute (attributes[i])); + g_array_append_val (primitive->attributes, attributes[i]); + } +} + +int +cogl_primitive_get_first_vertex (CoglPrimitive *primitive) +{ + g_return_val_if_fail (cogl_is_primitive (primitive), 0); + + return primitive->first_vertex; +} + +void +cogl_primitive_set_first_vertex (CoglPrimitive *primitive, + int first_vertex) +{ + g_return_if_fail (cogl_is_primitive (primitive)); + + primitive->first_vertex = first_vertex; +} + +int +cogl_primitive_get_n_vertices (CoglPrimitive *primitive) +{ + g_return_val_if_fail (cogl_is_primitive (primitive), 0); + + return primitive->n_vertices; +} + +void +cogl_primitive_set_n_vertices (CoglPrimitive *primitive, + int n_vertices) +{ + g_return_if_fail (cogl_is_primitive (primitive)); + + primitive->n_vertices = n_vertices; +} + +CoglVerticesMode +cogl_primitive_get_mode (CoglPrimitive *primitive) +{ + g_return_val_if_fail (cogl_is_primitive (primitive), 0); + + return primitive->mode; +} + +void +cogl_primitive_set_mode (CoglPrimitive *primitive, + CoglVerticesMode mode) +{ + g_return_if_fail (cogl_is_primitive (primitive)); + + primitive->mode = mode; +} + +void +cogl_primitive_set_indices (CoglPrimitive *primitive, + CoglIndices *indices) +{ + g_return_if_fail (cogl_is_primitive (primitive)); + + if (indices) + cogl_object_ref (indices); + if (primitive->indices) + cogl_object_unref (primitive->indices); + primitive->indices = indices; +} + +/* XXX: cogl_draw_primitive() ? */ +void +cogl_primitive_draw (CoglPrimitive *primitive) +{ + CoglVertexAttribute **attributes = + (CoglVertexAttribute **)primitive->attributes->data; + + if (primitive->indices) + cogl_draw_indexed_vertex_attributes_array (primitive->mode, + primitive->first_vertex, + primitive->n_vertices, + primitive->indices, + attributes); + else + cogl_draw_vertex_attributes_array (primitive->mode, + primitive->first_vertex, + primitive->n_vertices, + attributes); +} + diff --git a/cogl/cogl-primitive.h b/cogl/cogl-primitive.h new file mode 100644 index 000000000..b4a8e90f0 --- /dev/null +++ b/cogl/cogl-primitive.h @@ -0,0 +1,311 @@ +/* + * Cogl + * + * An object oriented GL/GLES Abstraction/Utility Layer + * + * Copyright (C) 2010 Intel Corporation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + * + * + * + * Authors: + * Robert Bragg + */ + +#if !defined(__COGL_H_INSIDE__) && !defined(CLUTTER_COMPILATION) +#error "Only can be included directly." +#endif + +#ifndef __COGL_PRIMITIVE_H__ +#define __COGL_PRIMITIVE_H__ + +#include /* for CoglVerticesMode */ +#include + +G_BEGIN_DECLS + +/** + * SECTION:cogl-primitive + * @short_description: Functions for creating, manipulating and drawing + * primitive + * + * FIXME + */ + +typedef struct _CoglPrimitive CoglPrimitive; + +/** + * CoglV2Vertex: + * @x: The x component of a position attribute + * @y: The y component of a position attribute + * + * A convenience vertex definition that can be used with + * cogl_primitive_new_with_v2_attributes(). + * + * Since: 1.6 + * Stability: Unstable + */ +typedef struct +{ + float x, y; +} CoglV2Vertex; + +/** + * CoglV3Vertex: + * @x: The x component of a position attribute + * @y: The y component of a position attribute + * @z: The z component of a position attribute + * + * A convenience vertex definition that can be used with + * cogl_primitive_new_with_v3_attributes(). + * + * Since: 1.6 + * Stability: Unstable + */ +typedef struct +{ + float x, y, z; +} CoglV3Vertex; + +/** + * CoglV2C4Vertex: + * @x: The x component of a position attribute + * @y: The y component of a position attribute + * @r: The red component of a color attribute + * @b: The green component of a color attribute + * @g: The blue component of a color attribute + * @a: The alpha component of a color attribute + * + * A convenience vertex definition that can be used with + * cogl_primitive_new_with_v2c4_attributes(). + * + * Since: 1.6 + * Stability: Unstable + */ +typedef struct +{ + float x, y; + guint8 r, g, b, a; +} CoglV2C4Vertex; + +/** + * CoglV3C4Vertex: + * @x: The x component of a position attribute + * @y: The y component of a position attribute + * @z: The z component of a position attribute + * @r: The red component of a color attribute + * @b: The green component of a color attribute + * @g: The blue component of a color attribute + * @a: The alpha component of a color attribute + * + * A convenience vertex definition that can be used with + * cogl_primitive_new_with_v3c4_attributes(). + * + * Since: 1.6 + * Stability: Unstable + */ +typedef struct +{ + float x, y, z; + guint8 r, g, b, a; +} CoglV3C4Vertex; + +/** + * CoglV2T2Vertex: + * @x: The x component of a position attribute + * @y: The y component of a position attribute + * @s: The s component of a texture coordinate attribute + * @t: The t component of a texture coordinate attribute + * + * A convenience vertex definition that can be used with + * cogl_primitive_new_with_v2t2_attributes(). + * + * Since: 1.6 + * Stability: Unstable + */ +typedef struct +{ + float x, y; + float s, t; +} CoglV2T2Vertex; + +/** + * CoglV3T2Vertex: + * @x: The x component of a position attribute + * @y: The y component of a position attribute + * @z: The z component of a position attribute + * @s: The s component of a texture coordinate attribute + * @t: The t component of a texture coordinate attribute + * + * A convenience vertex definition that can be used with + * cogl_primitive_new_with_v3t2_attributes(). + * + * Since: 1.6 + * Stability: Unstable + */ +typedef struct +{ + float x, y, z; + float s, t; +} CoglV3T2Vertex; + + +/** + * CoglV2T2C4Vertex: + * @x: The x component of a position attribute + * @y: The y component of a position attribute + * @s: The s component of a texture coordinate attribute + * @t: The t component of a texture coordinate attribute + * @r: The red component of a color attribute + * @b: The green component of a color attribute + * @g: The blue component of a color attribute + * @a: The alpha component of a color attribute + * + * A convenience vertex definition that can be used with + * cogl_primitive_new_with_v3t2c4_attributes(). + * + * Since: 1.6 + * Stability: Unstable + */ +typedef struct +{ + float x, y; + float s, t; + guint8 r, g, b, a; +} CoglV2T2C4Vertex; + +/** + * CoglV3T2C4Vertex: + * @x: The x component of a position attribute + * @y: The y component of a position attribute + * @z: The z component of a position attribute + * @s: The s component of a texture coordinate attribute + * @t: The t component of a texture coordinate attribute + * @r: The red component of a color attribute + * @b: The green component of a color attribute + * @g: The blue component of a color attribute + * @a: The alpha component of a color attribute + * + * A convenience vertex definition that can be used with + * cogl_primitive_new_with_v3t2c4_attributes(). + * + * Since: 1.6 + * Stability: Unstable + */ +typedef struct +{ + float x, y, z; + float s, t; + guint8 r, g, b, a; +} CoglV3T2C4Vertex; + +/** + * cogl_primitive_new: + * @mode: A #CoglVerticesMode defining how to draw the vertices + * @n_vertices: The number of vertices to process when drawing + * @Varargs: A %NULL terminated list of attributes + * + * Combines a set of #CoglVertexAttributes with a specific draw @mode + * and defines a vertex count so a #CoglPrimitive object can be retained and + * drawn later with no addition information required. + * + * Returns: A newly allocated #CoglPrimitive object + * + * Since: 1.6 + * Stability: Unstable + */ +CoglPrimitive * +cogl_primitive_new (CoglVerticesMode mode, + int n_vertices, + ...); + +/* XXX: how about just: cogl_primitive_new_with_attributes () ? */ +CoglPrimitive * +cogl_primitive_new_with_attributes_array (CoglVerticesMode mode, + int n_vertices, + CoglVertexAttribute **attributes); + +int +cogl_primitive_get_first_vertex (CoglPrimitive *primitive); + +void +cogl_primitive_set_first_vertex (CoglPrimitive *primitive, + int first_vertex); + +int +cogl_primitive_get_n_vertices (CoglPrimitive *primitive); + +void +cogl_primitive_set_n_vertices (CoglPrimitive *primitive, + int n_vertices); + +CoglVerticesMode +cogl_primitive_get_mode (CoglPrimitive *primitive); + +void +cogl_primitive_set_mode (CoglPrimitive *primitive, + CoglVerticesMode mode); + +/** + * cogl_primitive_set_attributes: + * @primitive: A #CoglPrimitive object + * @attributes: A %NULL terminated array of #CoglVertexAttribute + * pointers + * + * Replaces all the attributes of the given #CoglPrimitive object. + * + * Since: 1.6 + * Stability: Unstable + */ +void +cogl_primitive_set_attributes (CoglPrimitive *primitive, + CoglVertexAttribute **attributes); + + +void +cogl_primitive_set_indices (CoglPrimitive *primitive, + CoglIndices *indices); + +/** + * cogl_primitive_draw: + * @primitive: A #CoglPrimitive object + * + * Draw the given @primitive with the current source material. + * + * Since: 1.6 + * Stability: Unstable + */ +void +cogl_primitive_draw (CoglPrimitive *primitive); + +/** + * cogl_is_primitive: + * @object: A #CoglObject + * + * Gets whether the given object references a #CoglPrimitive. + * + * Returns: %TRUE if the handle references a #CoglPrimitive, + * %FALSE otherwise + * + * Since: 1.6 + * Stability: Unstable + */ +gboolean +cogl_is_primitive (void *object); + +G_END_DECLS + +#endif /* __COGL_PRIMITIVE_H__ */ + diff --git a/cogl/cogl.h b/cogl/cogl.h index 21200708b..864806916 100644 --- a/cogl/cogl.h +++ b/cogl/cogl.h @@ -58,6 +58,7 @@ #include #include #include +#include #endif G_BEGIN_DECLS