2007-03-27 21:09:11 +00:00
|
|
|
/*
|
|
|
|
* Clutter COGL
|
|
|
|
*
|
|
|
|
* A basic GL/GLES Abstraction/Utility Layer
|
|
|
|
*
|
|
|
|
* Authored By Matthew Allum <mallum@openedhand.com>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 OpenedHand
|
|
|
|
*
|
|
|
|
* 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, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2007-10-12 08:17:00 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2007-04-28 00:37:11 +00:00
|
|
|
#include "config.h"
|
2007-10-12 08:17:00 +00:00
|
|
|
#endif
|
|
|
|
|
2007-03-27 21:09:11 +00:00
|
|
|
#include "cogl.h"
|
2007-04-28 00:37:11 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
2009-01-28 14:09:51 +00:00
|
|
|
#include <gmodule.h>
|
|
|
|
#include <math.h>
|
2008-06-23 14:57:36 +00:00
|
|
|
#include <stdlib.h>
|
2007-04-28 00:37:11 +00:00
|
|
|
|
2009-01-28 14:09:51 +00:00
|
|
|
#ifdef HAVE_CLUTTER_GLX
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <GL/glx.h>
|
|
|
|
|
|
|
|
typedef CoglFuncPtr (*GLXGetProcAddressProc) (const guint8 *procName);
|
|
|
|
#endif
|
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
#include "cogl-internal.h"
|
|
|
|
#include "cogl-util.h"
|
|
|
|
#include "cogl-context.h"
|
2007-04-27 21:13:06 +00:00
|
|
|
|
2009-01-28 14:09:51 +00:00
|
|
|
#if defined (HAVE_COGL_GLES2) || defined (HAVE_COGL_GLES)
|
2008-05-27 17:42:50 +00:00
|
|
|
#include "cogl-gles2-wrapper.h"
|
2009-01-28 14:09:51 +00:00
|
|
|
#endif
|
2007-05-01 15:26:12 +00:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
/* GL error to string conversion */
|
2007-04-27 21:13:06 +00:00
|
|
|
#if COGL_DEBUG
|
|
|
|
struct token_string
|
|
|
|
{
|
|
|
|
GLuint Token;
|
|
|
|
const char *String;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct token_string Errors[] = {
|
|
|
|
{ GL_NO_ERROR, "no error" },
|
|
|
|
{ GL_INVALID_ENUM, "invalid enumerant" },
|
|
|
|
{ GL_INVALID_VALUE, "invalid value" },
|
|
|
|
{ GL_INVALID_OPERATION, "invalid operation" },
|
|
|
|
{ GL_STACK_OVERFLOW, "stack overflow" },
|
|
|
|
{ GL_STACK_UNDERFLOW, "stack underflow" },
|
|
|
|
{ GL_OUT_OF_MEMORY, "out of memory" },
|
|
|
|
#ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT
|
|
|
|
{ GL_INVALID_FRAMEBUFFER_OPERATION_EXT, "invalid framebuffer operation" },
|
|
|
|
#endif
|
|
|
|
{ ~0, NULL }
|
|
|
|
};
|
|
|
|
|
2008-05-21 13:20:33 +00:00
|
|
|
const char*
|
|
|
|
_cogl_error_string(GLenum errorCode)
|
2007-04-27 21:13:06 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; Errors[i].String; i++) {
|
|
|
|
if (Errors[i].Token == errorCode)
|
|
|
|
return Errors[i].String;
|
|
|
|
}
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-27 21:09:11 +00:00
|
|
|
CoglFuncPtr
|
|
|
|
cogl_get_proc_address (const gchar* name)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-01-24 16:55:04 +00:00
|
|
|
gboolean
|
2007-03-27 21:09:11 +00:00
|
|
|
cogl_check_extension (const gchar *name, const gchar *ext)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-02-12 13:23:20 +00:00
|
|
|
cogl_clear (const CoglColor *color)
|
2007-03-27 21:09:11 +00:00
|
|
|
{
|
2007-05-01 15:26:12 +00:00
|
|
|
#if COGL_DEBUG
|
|
|
|
fprintf(stderr, "\n ============== Paint Start ================ \n");
|
|
|
|
#endif
|
|
|
|
|
2009-01-26 11:07:35 +00:00
|
|
|
GE( glClearColor (cogl_color_get_red_float (color),
|
|
|
|
cogl_color_get_green_float (color),
|
|
|
|
cogl_color_get_blue_float (color),
|
|
|
|
0.0) );
|
2007-03-27 21:09:11 +00:00
|
|
|
|
|
|
|
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
2009-01-24 16:55:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable the depth test for now as has some strange side effects,
|
|
|
|
* mainly on x/y axis rotation with multiple layers at same depth
|
|
|
|
* (eg rotating text on a bg has very strange effect). Seems no clean
|
|
|
|
* 100% effective way to fix without other odd issues.. So for now
|
|
|
|
* move to application to handle and add cogl_enable_depth_test()
|
|
|
|
* as for custom actors (i.e groups) to enable if need be.
|
|
|
|
*
|
|
|
|
* glEnable (GL_DEPTH_TEST);
|
|
|
|
* glEnable (GL_ALPHA_TEST)
|
|
|
|
* glDepthFunc (GL_LEQUAL);
|
|
|
|
* glAlphaFunc (GL_GREATER, 0.1);
|
|
|
|
*/
|
2007-03-27 21:09:11 +00:00
|
|
|
}
|
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
static inline gboolean
|
|
|
|
cogl_toggle_flag (CoglContext *ctx,
|
|
|
|
gulong new_flags,
|
|
|
|
gulong flag,
|
|
|
|
GLenum gl_flag)
|
2007-04-28 00:37:11 +00:00
|
|
|
{
|
2008-04-25 13:37:36 +00:00
|
|
|
/* Toggles and caches a single enable flag on or off
|
|
|
|
* by comparing to current state
|
|
|
|
*/
|
|
|
|
if (new_flags & flag)
|
2007-04-28 00:37:11 +00:00
|
|
|
{
|
2008-04-25 13:37:36 +00:00
|
|
|
if (!(ctx->enable_flags & flag))
|
2007-04-28 00:37:11 +00:00
|
|
|
{
|
2009-01-24 16:55:04 +00:00
|
|
|
GE( glEnable (gl_flag) );
|
2008-04-25 13:37:36 +00:00
|
|
|
ctx->enable_flags |= flag;
|
|
|
|
return TRUE;
|
2007-04-28 00:37:11 +00:00
|
|
|
}
|
|
|
|
}
|
2008-04-25 13:37:36 +00:00
|
|
|
else if (ctx->enable_flags & flag)
|
2007-04-28 00:37:11 +00:00
|
|
|
{
|
2009-01-24 16:55:04 +00:00
|
|
|
GE( glDisable (gl_flag) );
|
2008-04-25 13:37:36 +00:00
|
|
|
ctx->enable_flags &= ~flag;
|
2007-04-28 00:37:11 +00:00
|
|
|
}
|
2009-01-24 16:55:04 +00:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2007-04-28 00:37:11 +00:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
static inline gboolean
|
|
|
|
cogl_toggle_client_flag (CoglContext *ctx,
|
|
|
|
gulong new_flags,
|
|
|
|
gulong flag,
|
|
|
|
GLenum gl_flag)
|
|
|
|
{
|
|
|
|
/* Toggles and caches a single client-side enable flag
|
|
|
|
* on or off by comparing to current state
|
|
|
|
*/
|
|
|
|
if (new_flags & flag)
|
2007-04-28 00:37:11 +00:00
|
|
|
{
|
2008-04-25 13:37:36 +00:00
|
|
|
if (!(ctx->enable_flags & flag))
|
|
|
|
{
|
2009-01-24 16:55:04 +00:00
|
|
|
GE( glEnableClientState (gl_flag) );
|
2008-04-25 13:37:36 +00:00
|
|
|
ctx->enable_flags |= flag;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-04-28 00:37:11 +00:00
|
|
|
}
|
2008-04-25 13:37:36 +00:00
|
|
|
else if (ctx->enable_flags & flag)
|
2007-04-28 00:37:11 +00:00
|
|
|
{
|
2009-01-24 16:55:04 +00:00
|
|
|
GE( glDisableClientState (gl_flag) );
|
2008-04-25 13:37:36 +00:00
|
|
|
ctx->enable_flags &= ~flag;
|
2007-04-28 00:37:11 +00:00
|
|
|
}
|
2009-01-24 16:55:04 +00:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2007-04-28 00:37:11 +00:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
void
|
|
|
|
cogl_enable (gulong flags)
|
|
|
|
{
|
|
|
|
/* This function essentially caches glEnable state() in the
|
|
|
|
* hope of lessening number GL traffic.
|
|
|
|
*/
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2009-01-24 16:55:04 +00:00
|
|
|
|
2008-05-19 15:02:27 +00:00
|
|
|
cogl_toggle_flag (ctx, flags,
|
|
|
|
COGL_ENABLE_BLEND,
|
|
|
|
GL_BLEND);
|
2009-01-24 16:55:04 +00:00
|
|
|
|
2008-10-27 14:36:52 +00:00
|
|
|
cogl_toggle_flag (ctx, flags,
|
|
|
|
COGL_ENABLE_BACKFACE_CULLING,
|
|
|
|
GL_CULL_FACE);
|
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
cogl_toggle_client_flag (ctx, flags,
|
|
|
|
COGL_ENABLE_VERTEX_ARRAY,
|
|
|
|
GL_VERTEX_ARRAY);
|
2009-01-24 16:55:04 +00:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
cogl_toggle_client_flag (ctx, flags,
|
|
|
|
COGL_ENABLE_COLOR_ARRAY,
|
|
|
|
GL_COLOR_ARRAY);
|
|
|
|
}
|
|
|
|
|
|
|
|
gulong
|
|
|
|
cogl_get_enable ()
|
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, 0);
|
2009-01-24 16:55:04 +00:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
return ctx->enable_flags;
|
2008-02-01 18:14:54 +00:00
|
|
|
}
|
2007-04-28 00:37:11 +00:00
|
|
|
|
2008-02-01 18:14:54 +00:00
|
|
|
void
|
|
|
|
cogl_enable_depth_test (gboolean setting)
|
|
|
|
{
|
|
|
|
if (setting)
|
2007-04-28 00:37:11 +00:00
|
|
|
{
|
2009-01-24 16:55:04 +00:00
|
|
|
glEnable (GL_DEPTH_TEST);
|
|
|
|
glEnable (GL_ALPHA_TEST);
|
2008-02-01 18:14:54 +00:00
|
|
|
glDepthFunc (GL_LEQUAL);
|
2009-01-24 16:55:04 +00:00
|
|
|
glAlphaFunc (GL_GREATER, 0.1);
|
2007-04-28 00:37:11 +00:00
|
|
|
}
|
2008-02-01 18:14:54 +00:00
|
|
|
else
|
2007-04-28 00:37:11 +00:00
|
|
|
{
|
2009-01-24 16:55:04 +00:00
|
|
|
glDisable (GL_DEPTH_TEST);
|
|
|
|
glDisable (GL_ALPHA_TEST);
|
2007-04-28 00:37:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-27 14:36:52 +00:00
|
|
|
void
|
|
|
|
cogl_enable_backface_culling (gboolean setting)
|
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
ctx->enable_backface_culling = setting;
|
|
|
|
}
|
|
|
|
|
2007-04-28 00:37:11 +00:00
|
|
|
void
|
2008-11-12 13:57:58 +00:00
|
|
|
cogl_set_source_color (const CoglColor *color)
|
2007-04-28 00:37:11 +00:00
|
|
|
{
|
2008-04-25 13:37:36 +00:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2009-01-24 16:55:04 +00:00
|
|
|
|
2009-01-26 11:07:35 +00:00
|
|
|
/* In case cogl_set_source_texture was previously used... */
|
|
|
|
cogl_material_remove_layer (ctx->default_material, 0);
|
2009-01-24 16:55:04 +00:00
|
|
|
|
2009-01-26 11:07:35 +00:00
|
|
|
cogl_material_set_color (ctx->default_material, color);
|
|
|
|
cogl_set_source (ctx->default_material);
|
2007-12-24 12:53:04 +00:00
|
|
|
}
|
|
|
|
|
2008-06-02 12:34:10 +00:00
|
|
|
static void
|
2009-03-19 16:46:07 +00:00
|
|
|
project_vertex (const CoglMatrix *modelview_matrix,
|
|
|
|
const CoglMatrix *projection_matrix,
|
2009-01-20 16:20:54 +00:00
|
|
|
float *vertex)
|
2008-06-02 12:34:10 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Apply the modelview matrix */
|
2009-03-19 16:46:07 +00:00
|
|
|
cogl_matrix_transform_point (modelview_matrix,
|
|
|
|
&vertex[0], &vertex[1],
|
|
|
|
&vertex[2], &vertex[3]);
|
2008-06-02 12:34:10 +00:00
|
|
|
/* Apply the projection matrix */
|
2009-03-19 16:46:07 +00:00
|
|
|
cogl_matrix_transform_point (projection_matrix,
|
|
|
|
&vertex[0], &vertex[1],
|
|
|
|
&vertex[2], &vertex[3]);
|
2008-06-02 12:34:10 +00:00
|
|
|
/* Convert from homogenized coordinates */
|
|
|
|
for (i = 0; i < 4; i++)
|
2009-01-24 16:55:04 +00:00
|
|
|
vertex[i] /= vertex[3];
|
2008-06-02 12:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_clip_plane (GLint plane_num,
|
2009-01-20 16:20:54 +00:00
|
|
|
const float *vertex_a,
|
|
|
|
const float *vertex_b)
|
2008-06-02 12:34:10 +00:00
|
|
|
{
|
2009-01-28 14:09:51 +00:00
|
|
|
#if defined (HAVE_COGL_GLES2) || defined (HAVE_COGL_GLES)
|
2009-01-20 16:20:54 +00:00
|
|
|
GLfloat plane[4];
|
2009-01-28 14:09:51 +00:00
|
|
|
#else
|
|
|
|
GLdouble plane[4];
|
|
|
|
#endif
|
2009-01-20 16:20:54 +00:00
|
|
|
GLfloat angle;
|
2009-02-24 18:51:25 +00:00
|
|
|
CoglMatrix inverse_projection;
|
2008-06-02 12:34:10 +00:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
/* Calculate the angle between the axes and the line crossing the
|
|
|
|
two points */
|
2009-01-20 16:20:54 +00:00
|
|
|
angle = atan2f (vertex_b[1] - vertex_a[1],
|
|
|
|
vertex_b[0] - vertex_a[0]) * (180.0/G_PI);
|
2008-06-02 12:34:10 +00:00
|
|
|
|
2009-02-24 18:51:25 +00:00
|
|
|
_cogl_current_matrix_push ();
|
2008-06-02 12:34:10 +00:00
|
|
|
/* Load the identity matrix and multiply by the reverse of the
|
|
|
|
projection matrix so we can specify the plane in screen
|
|
|
|
coordinates */
|
2009-02-24 18:51:25 +00:00
|
|
|
_cogl_current_matrix_identity ();
|
|
|
|
cogl_matrix_init_from_array (&inverse_projection,
|
|
|
|
ctx->inverse_projection);
|
|
|
|
_cogl_current_matrix_multiply (&inverse_projection);
|
2008-06-02 12:34:10 +00:00
|
|
|
/* Rotate about point a */
|
2009-02-24 18:51:25 +00:00
|
|
|
_cogl_current_matrix_translate (vertex_a[0], vertex_a[1], vertex_a[2]);
|
2008-06-02 12:34:10 +00:00
|
|
|
/* Rotate the plane by the calculated angle so that it will connect
|
|
|
|
the two points */
|
2009-02-24 18:51:25 +00:00
|
|
|
_cogl_current_matrix_rotate (angle, 0.0f, 0.0f, 1.0f);
|
|
|
|
_cogl_current_matrix_translate (-vertex_a[0], -vertex_a[1], -vertex_a[2]);
|
2008-06-02 12:34:10 +00:00
|
|
|
|
2009-03-17 00:12:27 +00:00
|
|
|
_cogl_current_matrix_state_flush ();
|
|
|
|
|
2008-06-02 12:34:10 +00:00
|
|
|
plane[0] = 0;
|
2009-01-20 16:20:54 +00:00
|
|
|
plane[1] = -1.0;
|
2008-06-02 12:34:10 +00:00
|
|
|
plane[2] = 0;
|
|
|
|
plane[3] = vertex_a[1];
|
2009-01-28 14:09:51 +00:00
|
|
|
#if defined (HAVE_COGL_GLES2) || defined (HAVE_COGL_GLES)
|
2009-01-24 16:55:04 +00:00
|
|
|
GE( glClipPlanef (plane_num, plane) );
|
2009-01-28 14:09:51 +00:00
|
|
|
#else
|
|
|
|
GE( glClipPlane (plane_num, plane) );
|
|
|
|
#endif
|
2008-06-02 12:34:10 +00:00
|
|
|
|
2009-02-24 18:51:25 +00:00
|
|
|
_cogl_current_matrix_pop ();
|
2008-06-02 12:34:10 +00:00
|
|
|
}
|
|
|
|
|
2007-05-28 18:49:34 +00:00
|
|
|
void
|
2009-01-20 16:20:54 +00:00
|
|
|
_cogl_set_clip_planes (float x_offset,
|
|
|
|
float y_offset,
|
|
|
|
float width,
|
|
|
|
float height)
|
2007-05-28 18:49:34 +00:00
|
|
|
{
|
2009-02-24 18:51:25 +00:00
|
|
|
CoglMatrix modelview_matrix;
|
|
|
|
CoglMatrix projection_matrix;
|
2008-06-23 14:57:36 +00:00
|
|
|
|
2009-01-20 16:20:54 +00:00
|
|
|
float vertex_tl[4] = { x_offset, y_offset, 0, 1.0 };
|
|
|
|
float vertex_tr[4] = { x_offset + width, y_offset, 0, 1.0 };
|
|
|
|
float vertex_bl[4] = { x_offset, y_offset + height, 0, 1.0 };
|
|
|
|
float vertex_br[4] = { x_offset + width, y_offset + height,
|
2009-01-24 16:55:04 +00:00
|
|
|
0, 1.0 };
|
2008-06-23 14:57:36 +00:00
|
|
|
|
2009-02-24 18:51:25 +00:00
|
|
|
_cogl_get_matrix (COGL_MATRIX_PROJECTION,
|
|
|
|
&projection_matrix);
|
|
|
|
_cogl_get_matrix (COGL_MATRIX_MODELVIEW,
|
|
|
|
&modelview_matrix);
|
2008-06-23 14:57:36 +00:00
|
|
|
|
2009-03-19 16:46:07 +00:00
|
|
|
project_vertex (&modelview_matrix, &projection_matrix, vertex_tl);
|
|
|
|
project_vertex (&modelview_matrix, &projection_matrix, vertex_tr);
|
|
|
|
project_vertex (&modelview_matrix, &projection_matrix, vertex_bl);
|
|
|
|
project_vertex (&modelview_matrix, &projection_matrix, vertex_br);
|
2008-06-23 14:57:36 +00:00
|
|
|
|
2009-01-24 16:55:04 +00:00
|
|
|
/* If the order of the top and bottom lines is different from the
|
|
|
|
order of the left and right lines then the clip rect must have
|
|
|
|
been transformed so that the back is visible. We therefore need
|
|
|
|
to swap one pair of vertices otherwise all of the planes will be
|
|
|
|
the wrong way around */
|
2008-06-23 14:57:36 +00:00
|
|
|
if ((vertex_tl[0] < vertex_tr[0] ? 1 : 0)
|
|
|
|
!= (vertex_bl[1] < vertex_tl[1] ? 1 : 0))
|
2008-04-25 13:37:36 +00:00
|
|
|
{
|
2009-01-20 16:20:54 +00:00
|
|
|
float temp[4];
|
2008-06-23 14:57:36 +00:00
|
|
|
memcpy (temp, vertex_tl, sizeof (temp));
|
|
|
|
memcpy (vertex_tl, vertex_tr, sizeof (temp));
|
|
|
|
memcpy (vertex_tr, temp, sizeof (temp));
|
|
|
|
memcpy (temp, vertex_bl, sizeof (temp));
|
|
|
|
memcpy (vertex_bl, vertex_br, sizeof (temp));
|
|
|
|
memcpy (vertex_br, temp, sizeof (temp));
|
|
|
|
}
|
2008-06-02 12:34:10 +00:00
|
|
|
|
2008-06-23 14:57:36 +00:00
|
|
|
set_clip_plane (GL_CLIP_PLANE0, vertex_tl, vertex_tr);
|
|
|
|
set_clip_plane (GL_CLIP_PLANE1, vertex_tr, vertex_br);
|
|
|
|
set_clip_plane (GL_CLIP_PLANE2, vertex_br, vertex_bl);
|
|
|
|
set_clip_plane (GL_CLIP_PLANE3, vertex_bl, vertex_tl);
|
|
|
|
}
|
2008-06-02 12:34:10 +00:00
|
|
|
|
2008-06-23 14:57:36 +00:00
|
|
|
void
|
2009-01-20 16:20:54 +00:00
|
|
|
_cogl_add_stencil_clip (float x_offset,
|
|
|
|
float y_offset,
|
|
|
|
float width,
|
|
|
|
float height,
|
2008-06-23 14:57:36 +00:00
|
|
|
gboolean first)
|
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2008-06-02 12:34:10 +00:00
|
|
|
|
2009-01-29 12:21:56 +00:00
|
|
|
cogl_material_flush_gl_state (ctx->stencil_material, NULL);
|
2009-01-26 11:07:35 +00:00
|
|
|
|
2008-06-23 14:57:36 +00:00
|
|
|
if (first)
|
2008-04-25 13:37:36 +00:00
|
|
|
{
|
2009-01-24 16:55:04 +00:00
|
|
|
GE( glEnable (GL_STENCIL_TEST) );
|
|
|
|
|
2008-06-23 14:57:36 +00:00
|
|
|
/* Initially disallow everything */
|
2008-04-25 13:37:36 +00:00
|
|
|
GE( glClearStencil (0) );
|
|
|
|
GE( glClear (GL_STENCIL_BUFFER_BIT) );
|
2007-05-28 18:49:34 +00:00
|
|
|
|
2008-06-23 14:57:36 +00:00
|
|
|
/* Punch out a hole to allow the rectangle */
|
2008-04-25 13:37:36 +00:00
|
|
|
GE( glStencilFunc (GL_NEVER, 0x1, 0x1) );
|
2008-06-23 14:57:36 +00:00
|
|
|
GE( glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE) );
|
|
|
|
|
2009-01-28 14:47:03 +00:00
|
|
|
cogl_rectangle (x_offset, y_offset,
|
|
|
|
x_offset + width, y_offset + height);
|
2008-06-23 14:57:36 +00:00
|
|
|
}
|
2008-12-04 13:45:09 +00:00
|
|
|
else
|
2008-06-23 14:57:36 +00:00
|
|
|
{
|
|
|
|
/* Add one to every pixel of the stencil buffer in the
|
|
|
|
rectangle */
|
|
|
|
GE( glStencilFunc (GL_NEVER, 0x1, 0x3) );
|
2008-04-25 13:37:36 +00:00
|
|
|
GE( glStencilOp (GL_INCR, GL_INCR, GL_INCR) );
|
2009-01-28 14:47:03 +00:00
|
|
|
cogl_rectangle (x_offset, y_offset,
|
|
|
|
x_offset + width, y_offset + height);
|
2007-05-28 18:49:34 +00:00
|
|
|
|
2008-06-23 14:57:36 +00:00
|
|
|
/* Subtract one from all pixels in the stencil buffer so that
|
|
|
|
only pixels where both the original stencil buffer and the
|
|
|
|
rectangle are set will be valid */
|
|
|
|
GE( glStencilOp (GL_DECR, GL_DECR, GL_DECR) );
|
2009-03-12 14:16:48 +00:00
|
|
|
|
|
|
|
_cogl_set_current_matrix (COGL_MATRIX_PROJECTION);
|
2009-02-24 18:51:25 +00:00
|
|
|
_cogl_current_matrix_push ();
|
|
|
|
_cogl_current_matrix_identity ();
|
2009-03-12 14:16:48 +00:00
|
|
|
|
|
|
|
/* Cogl generally assumes the modelview matrix is current, so since
|
|
|
|
* cogl_rectangle will be flushing GL state and emitting geometry
|
|
|
|
* to OpenGL it will be confused if we leave the projection matrix
|
|
|
|
* active... */
|
|
|
|
_cogl_set_current_matrix (COGL_MATRIX_MODELVIEW);
|
2009-02-24 18:51:25 +00:00
|
|
|
_cogl_current_matrix_push ();
|
|
|
|
_cogl_current_matrix_identity ();
|
2009-03-12 14:16:48 +00:00
|
|
|
|
2009-01-28 14:47:03 +00:00
|
|
|
cogl_rectangle (-1.0, -1.0, 1.0, 1.0);
|
2009-03-12 14:16:48 +00:00
|
|
|
|
2009-02-24 18:51:25 +00:00
|
|
|
_cogl_current_matrix_pop ();
|
2009-03-12 14:16:48 +00:00
|
|
|
|
|
|
|
_cogl_set_current_matrix (COGL_MATRIX_PROJECTION);
|
2009-02-24 18:51:25 +00:00
|
|
|
_cogl_current_matrix_pop ();
|
2009-03-12 14:16:48 +00:00
|
|
|
|
|
|
|
_cogl_set_current_matrix (COGL_MATRIX_MODELVIEW);
|
2008-06-23 14:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore the stencil mode */
|
|
|
|
GE( glStencilFunc (GL_EQUAL, 0x1, 0x1) );
|
|
|
|
GE( glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP) );
|
2007-05-28 18:49:34 +00:00
|
|
|
}
|
|
|
|
|
2008-06-23 14:57:36 +00:00
|
|
|
void
|
|
|
|
_cogl_disable_stencil_buffer (void)
|
|
|
|
{
|
2009-01-24 16:55:04 +00:00
|
|
|
GE( glDisable (GL_STENCIL_TEST) );
|
2008-06-23 14:57:36 +00:00
|
|
|
}
|
|
|
|
|
2008-12-04 13:45:09 +00:00
|
|
|
void
|
|
|
|
_cogl_enable_clip_planes (void)
|
|
|
|
{
|
2009-01-24 16:55:04 +00:00
|
|
|
GE( glEnable (GL_CLIP_PLANE0) );
|
|
|
|
GE( glEnable (GL_CLIP_PLANE1) );
|
|
|
|
GE( glEnable (GL_CLIP_PLANE2) );
|
|
|
|
GE( glEnable (GL_CLIP_PLANE3) );
|
2008-12-04 13:45:09 +00:00
|
|
|
}
|
|
|
|
|
2008-06-23 14:57:36 +00:00
|
|
|
void
|
|
|
|
_cogl_disable_clip_planes (void)
|
|
|
|
{
|
2009-01-24 16:55:04 +00:00
|
|
|
GE( glDisable (GL_CLIP_PLANE3) );
|
|
|
|
GE( glDisable (GL_CLIP_PLANE2) );
|
|
|
|
GE( glDisable (GL_CLIP_PLANE1) );
|
|
|
|
GE( glDisable (GL_CLIP_PLANE0) );
|
2007-04-28 00:37:11 +00:00
|
|
|
}
|
|
|
|
|
2008-08-01 12:23:57 +00:00
|
|
|
void
|
|
|
|
cogl_viewport (guint width,
|
|
|
|
guint height)
|
|
|
|
{
|
|
|
|
GE( glViewport (0, 0, width, height) );
|
|
|
|
}
|
|
|
|
|
2007-04-28 00:37:11 +00:00
|
|
|
void
|
2009-01-24 16:55:04 +00:00
|
|
|
cogl_setup_viewport (guint width,
|
|
|
|
guint height,
|
2009-01-20 16:20:54 +00:00
|
|
|
float fovy,
|
|
|
|
float aspect,
|
|
|
|
float z_near,
|
|
|
|
float z_far)
|
2007-04-28 00:37:11 +00:00
|
|
|
{
|
2009-01-20 16:20:54 +00:00
|
|
|
float z_camera;
|
2009-02-18 18:54:54 +00:00
|
|
|
CoglMatrix projection_matrix;
|
2009-01-24 16:55:04 +00:00
|
|
|
|
2007-04-28 00:37:11 +00:00
|
|
|
GE( glViewport (0, 0, width, height) );
|
|
|
|
|
2007-05-02 09:01:11 +00:00
|
|
|
/* For Ortho projection.
|
2009-02-24 18:51:25 +00:00
|
|
|
* _cogl_current_matrix_ortho (0, width << 16, 0, height << 16, -1 << 16, 1 << 16);
|
|
|
|
*/
|
2007-05-01 15:26:12 +00:00
|
|
|
|
2007-04-28 00:37:11 +00:00
|
|
|
cogl_perspective (fovy, aspect, z_near, z_far);
|
2009-01-24 16:55:04 +00:00
|
|
|
|
2007-06-26 16:07:14 +00:00
|
|
|
/*
|
2009-01-22 14:52:34 +00:00
|
|
|
* camera distance from screen
|
2007-06-26 16:07:14 +00:00
|
|
|
*
|
|
|
|
* See comments in ../gl/cogl.c
|
|
|
|
*/
|
|
|
|
|
2009-02-18 18:54:54 +00:00
|
|
|
cogl_get_projection_matrix (&projection_matrix);
|
|
|
|
z_camera = 0.5 * projection_matrix.xx;
|
2007-05-28 18:49:34 +00:00
|
|
|
|
2009-02-24 18:51:25 +00:00
|
|
|
_cogl_current_matrix_identity ();
|
|
|
|
_cogl_current_matrix_translate (-0.5f, -0.5f, -z_camera);
|
|
|
|
_cogl_current_matrix_scale (1.0f / width, -1.0f / height, 1.0f / width);
|
|
|
|
_cogl_current_matrix_translate (0.0f, -1.0 * height, 0.0f);
|
2007-05-16 09:08:30 +00:00
|
|
|
}
|
2007-05-01 15:26:12 +00:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
static void
|
|
|
|
_cogl_features_init ()
|
|
|
|
{
|
2008-10-30 16:52:56 +00:00
|
|
|
CoglFeatureFlags flags = 0;
|
|
|
|
int max_clip_planes = 0;
|
2008-12-04 13:45:09 +00:00
|
|
|
GLint num_stencil_bits = 0;
|
2008-04-25 13:37:36 +00:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
2009-01-24 16:55:04 +00:00
|
|
|
GE( glGetIntegerv (GL_STENCIL_BITS, &num_stencil_bits) );
|
2008-12-04 13:45:09 +00:00
|
|
|
/* We need at least three stencil bits to combine clips */
|
|
|
|
if (num_stencil_bits > 2)
|
2008-04-25 13:37:36 +00:00
|
|
|
flags |= COGL_FEATURE_STENCIL_BUFFER;
|
|
|
|
|
2009-01-24 16:55:04 +00:00
|
|
|
GE( glGetIntegerv (GL_MAX_CLIP_PLANES, &max_clip_planes) );
|
2008-04-25 13:37:36 +00:00
|
|
|
if (max_clip_planes >= 4)
|
|
|
|
flags |= COGL_FEATURE_FOUR_CLIP_PLANES;
|
|
|
|
|
2008-06-02 10:58:57 +00:00
|
|
|
#ifdef HAVE_COGL_GLES2
|
2008-10-29 14:52:48 +00:00
|
|
|
flags |= COGL_FEATURE_SHADERS_GLSL | COGL_FEATURE_OFFSCREEN;
|
2008-06-02 10:58:57 +00:00
|
|
|
#endif
|
|
|
|
|
2009-01-24 16:55:04 +00:00
|
|
|
/* Cache features */
|
2008-04-25 13:37:36 +00:00
|
|
|
ctx->feature_flags = flags;
|
|
|
|
ctx->features_cached = TRUE;
|
|
|
|
}
|
|
|
|
|
2008-10-30 16:52:56 +00:00
|
|
|
CoglFeatureFlags
|
2007-05-16 09:08:30 +00:00
|
|
|
cogl_get_features ()
|
|
|
|
{
|
2008-04-25 13:37:36 +00:00
|
|
|
_COGL_GET_CONTEXT (ctx, 0);
|
2009-01-24 16:55:04 +00:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
if (!ctx->features_cached)
|
|
|
|
_cogl_features_init ();
|
2009-01-24 16:55:04 +00:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
return ctx->feature_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
cogl_features_available (CoglFeatureFlags features)
|
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, 0);
|
2009-01-24 16:55:04 +00:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
if (!ctx->features_cached)
|
|
|
|
_cogl_features_init ();
|
2009-01-24 16:55:04 +00:00
|
|
|
|
2008-04-25 13:37:36 +00:00
|
|
|
return (ctx->feature_flags & features) == features;
|
2007-04-28 00:37:11 +00:00
|
|
|
}
|
2007-06-12 11:42:29 +00:00
|
|
|
|
|
|
|
void
|
2009-01-20 16:20:54 +00:00
|
|
|
cogl_get_viewport (float v[4])
|
2007-06-12 11:42:29 +00:00
|
|
|
{
|
2009-01-28 14:09:51 +00:00
|
|
|
/* FIXME: cogl_get_viewport should return a gint vec */
|
|
|
|
#if defined (HAVE_COGL_GLES2) || defined (HAVE_COGL_GLES)
|
2008-09-18 11:24:27 +00:00
|
|
|
GLint viewport[4];
|
|
|
|
int i;
|
|
|
|
|
2009-01-24 16:55:04 +00:00
|
|
|
glGetIntegerv (GL_VIEWPORT, viewport);
|
2008-09-18 11:24:27 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
2009-01-20 16:20:54 +00:00
|
|
|
v[i] = (float)(viewport[i]);
|
2009-01-28 14:09:51 +00:00
|
|
|
#else
|
|
|
|
glGetFloatv (GL_VIEWPORT, v);
|
|
|
|
#endif
|
2007-06-12 11:42:29 +00:00
|
|
|
}
|
2007-07-12 10:15:19 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
cogl_get_bitmasks (gint *red, gint *green, gint *blue, gint *alpha)
|
|
|
|
{
|
2009-01-24 16:55:04 +00:00
|
|
|
GLint value;
|
2007-07-12 10:15:19 +00:00
|
|
|
if (red)
|
2009-01-24 16:55:04 +00:00
|
|
|
{
|
|
|
|
GE( glGetIntegerv(GL_RED_BITS, &value) );
|
|
|
|
*red = value;
|
|
|
|
}
|
2007-07-12 10:15:19 +00:00
|
|
|
if (green)
|
2009-01-24 16:55:04 +00:00
|
|
|
{
|
|
|
|
GE( glGetIntegerv(GL_GREEN_BITS, &value) );
|
|
|
|
*green = value;
|
|
|
|
}
|
2007-07-12 10:15:19 +00:00
|
|
|
if (blue)
|
2009-01-24 16:55:04 +00:00
|
|
|
{
|
|
|
|
GE( glGetIntegerv(GL_BLUE_BITS, &value) );
|
|
|
|
*blue = value;
|
|
|
|
}
|
2007-07-12 10:15:19 +00:00
|
|
|
if (alpha)
|
2009-01-24 16:55:04 +00:00
|
|
|
{
|
|
|
|
GE( glGetIntegerv(GL_ALPHA_BITS, &value ) );
|
|
|
|
*alpha = value;
|
|
|
|
}
|
2007-07-12 10:15:19 +00:00
|
|
|
}
|
2007-11-21 11:55:26 +00:00
|
|
|
|
|
|
|
void
|
2009-02-04 09:21:37 +00:00
|
|
|
cogl_set_fog (const CoglColor *fog_color,
|
2009-02-12 13:23:20 +00:00
|
|
|
CoglFogMode mode,
|
2009-02-04 09:21:37 +00:00
|
|
|
float density,
|
|
|
|
float z_near,
|
|
|
|
float z_far)
|
2007-11-21 11:55:26 +00:00
|
|
|
{
|
2009-01-20 16:20:54 +00:00
|
|
|
GLfloat fogColor[4];
|
2009-02-12 13:23:20 +00:00
|
|
|
GLenum gl_mode;
|
2007-11-21 11:55:26 +00:00
|
|
|
|
2009-01-24 16:55:04 +00:00
|
|
|
fogColor[0] = cogl_color_get_red_float (fog_color);
|
|
|
|
fogColor[1] = cogl_color_get_green_float (fog_color);
|
|
|
|
fogColor[2] = cogl_color_get_blue_float (fog_color);
|
|
|
|
fogColor[3] = cogl_color_get_alpha_float (fog_color);
|
2007-11-21 11:55:26 +00:00
|
|
|
|
2009-01-24 16:55:04 +00:00
|
|
|
glEnable (GL_FOG);
|
2007-11-21 11:55:26 +00:00
|
|
|
|
2009-02-12 13:23:20 +00:00
|
|
|
#if HAVE_COGL_GLES
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case COGL_FOG_MODE_LINEAR:
|
|
|
|
gl_mode = GL_LINEAR;
|
|
|
|
break;
|
|
|
|
case COGL_FOG_MODE_EXPONENTIAL:
|
|
|
|
gl_mode = GL_EXP;
|
|
|
|
break;
|
|
|
|
case COGL_FOG_MODE_EXPONENTIAL_SQUARED:
|
|
|
|
gl_mode = GL_EXP2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* TODO: support other modes for GLES2 */
|
|
|
|
gl_mode = GL_LINEAR;
|
|
|
|
#endif
|
|
|
|
|
2009-01-24 16:55:04 +00:00
|
|
|
glFogfv (GL_FOG_COLOR, fogColor);
|
2007-11-21 11:55:26 +00:00
|
|
|
|
2009-01-28 14:09:51 +00:00
|
|
|
/* NB: GLES doesn't have glFogi */
|
2009-02-12 13:23:20 +00:00
|
|
|
glFogf (GL_FOG_MODE, gl_mode);
|
2007-11-21 11:55:26 +00:00
|
|
|
glHint (GL_FOG_HINT, GL_NICEST);
|
|
|
|
|
2009-01-24 16:55:04 +00:00
|
|
|
glFogf (GL_FOG_DENSITY, (GLfloat) density);
|
|
|
|
glFogf (GL_FOG_START, (GLfloat) z_near);
|
|
|
|
glFogf (GL_FOG_END, (GLfloat) z_far);
|
2007-11-21 11:55:26 +00:00
|
|
|
}
|
2009-01-24 16:55:04 +00:00
|
|
|
|
2009-02-12 13:23:20 +00:00
|
|
|
void
|
|
|
|
cogl_disable_fog (void)
|
|
|
|
{
|
|
|
|
glDisable (GL_FOG);
|
|
|
|
}
|
|
|
|
|
2009-02-24 18:51:25 +00:00
|
|
|
void
|
|
|
|
cogl_flush_gl_state (int flags)
|
|
|
|
{
|
|
|
|
_cogl_current_matrix_state_flush ();
|
|
|
|
}
|