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.
This commit is contained in:
Robert Bragg 2010-10-12 12:54:07 +01:00
parent 070d90937b
commit 9498d505dd
5 changed files with 584 additions and 0 deletions

View File

@ -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 \

View File

@ -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
* <http://www.gnu.org/licenses/>.
*
*
*
* Authors:
* Robert Bragg <robert@linux.intel.com>
*/
#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 */

223
cogl/cogl-primitive.c Normal file
View File

@ -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
* <http://www.gnu.org/licenses/>.
*
*
*
* Authors:
* Robert Bragg <robert@linux.intel.com>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "cogl-object-private.h"
#include "cogl-primitive.h"
#include "cogl-primitive-private.h"
#include <stdarg.h>
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);
}

311
cogl/cogl-primitive.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*
*
*
* Authors:
* Robert Bragg <robert@linux.intel.com>
*/
#if !defined(__COGL_H_INSIDE__) && !defined(CLUTTER_COMPILATION)
#error "Only <cogl/cogl.h> can be included directly."
#endif
#ifndef __COGL_PRIMITIVE_H__
#define __COGL_PRIMITIVE_H__
#include <cogl/cogl-vertex-buffer.h> /* for CoglVerticesMode */
#include <cogl/cogl-vertex-attribute.h>
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 #CoglVertexAttribute<!-- -->s 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__ */

View File

@ -58,6 +58,7 @@
#include <cogl/cogl-vertex-array.h>
#include <cogl/cogl-indices.h>
#include <cogl/cogl-vertex-attribute.h>
#include <cogl/cogl-primitive.h>
#endif
G_BEGIN_DECLS