2007-03-27 17:09:11 -04: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 04:17:00 -04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2007-04-27 20:37:11 -04:00
|
|
|
#include "config.h"
|
2007-10-12 04:17:00 -04:00
|
|
|
#endif
|
|
|
|
|
2007-03-27 17:09:11 -04:00
|
|
|
#include "cogl.h"
|
2007-04-27 20:37:11 -04:00
|
|
|
|
|
|
|
#include <string.h>
|
2008-06-23 10:57:36 -04:00
|
|
|
#include <stdlib.h>
|
2007-04-27 20:37:11 -04:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
#include "cogl-internal.h"
|
|
|
|
#include "cogl-util.h"
|
|
|
|
#include "cogl-context.h"
|
2007-04-27 17:13:06 -04:00
|
|
|
|
2008-05-27 13:42:50 -04:00
|
|
|
#include "cogl-gles2-wrapper.h"
|
2009-01-20 11:20:54 -05:00
|
|
|
#include <math.h>
|
2007-05-01 11:26:12 -04:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
/* GL error to string conversion */
|
2007-04-27 17:13:06 -04: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 09:20:33 -04:00
|
|
|
const char*
|
|
|
|
_cogl_error_string(GLenum errorCode)
|
2007-04-27 17:13:06 -04: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 17:09:11 -04:00
|
|
|
CoglFuncPtr
|
|
|
|
cogl_get_proc_address (const gchar* name)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
gboolean
|
2007-03-27 17:09:11 -04:00
|
|
|
cogl_check_extension (const gchar *name, const gchar *ext)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-10-30 12:50:07 -04:00
|
|
|
cogl_paint_init (const CoglColor *color)
|
2007-03-27 17:09:11 -04:00
|
|
|
{
|
2007-05-01 11:26:12 -04:00
|
|
|
#if COGL_DEBUG
|
|
|
|
fprintf(stderr, "\n ============== Paint Start ================ \n");
|
|
|
|
#endif
|
|
|
|
|
2009-01-26 06:07:35 -05: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 17:09:11 -04:00
|
|
|
|
|
|
|
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
2009-01-24 11:55:04 -05:00
|
|
|
glDisable (GL_LIGHTING);
|
|
|
|
glDisable (GL_FOG);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 17:09:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: inline most of these */
|
|
|
|
void
|
|
|
|
cogl_push_matrix (void)
|
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glPushMatrix() );
|
2007-03-27 17:09:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_pop_matrix (void)
|
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glPopMatrix() );
|
2007-03-27 17:09:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-20 11:20:54 -05:00
|
|
|
cogl_scale (float x, float y)
|
2007-03-27 17:09:11 -04:00
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glScalef (x, y, 1.0) );
|
2007-03-27 17:09:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-20 11:20:54 -05:00
|
|
|
cogl_translate (float x, float y, float z)
|
2007-03-27 17:09:11 -04:00
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glTranslatef (x, y, z) );
|
2007-03-27 17:09:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-20 11:20:54 -05:00
|
|
|
cogl_rotate (float angle, float x, float y, float z)
|
2007-03-27 17:09:11 -04:00
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glRotatef (angle, x, y, z) );
|
2007-03-27 17:09:11 -04:00
|
|
|
}
|
2007-04-27 20:37:11 -04:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
static inline gboolean
|
|
|
|
cogl_toggle_flag (CoglContext *ctx,
|
|
|
|
gulong new_flags,
|
|
|
|
gulong flag,
|
|
|
|
GLenum gl_flag)
|
2007-04-27 20:37:11 -04:00
|
|
|
{
|
2008-04-25 09:37:36 -04:00
|
|
|
/* Toggles and caches a single enable flag on or off
|
|
|
|
* by comparing to current state
|
|
|
|
*/
|
|
|
|
if (new_flags & flag)
|
2007-04-27 20:37:11 -04:00
|
|
|
{
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!(ctx->enable_flags & flag))
|
2007-04-27 20:37:11 -04:00
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glEnable (gl_flag) );
|
2008-04-25 09:37:36 -04:00
|
|
|
ctx->enable_flags |= flag;
|
|
|
|
return TRUE;
|
2007-04-27 20:37:11 -04:00
|
|
|
}
|
|
|
|
}
|
2008-04-25 09:37:36 -04:00
|
|
|
else if (ctx->enable_flags & flag)
|
2007-04-27 20:37:11 -04:00
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glDisable (gl_flag) );
|
2008-04-25 09:37:36 -04:00
|
|
|
ctx->enable_flags &= ~flag;
|
2007-04-27 20:37:11 -04:00
|
|
|
}
|
2009-01-24 11:55:04 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2007-04-27 20:37:11 -04:00
|
|
|
|
2008-04-25 09:37:36 -04: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-27 20:37:11 -04:00
|
|
|
{
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!(ctx->enable_flags & flag))
|
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glEnableClientState (gl_flag) );
|
2008-04-25 09:37:36 -04:00
|
|
|
ctx->enable_flags |= flag;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-04-27 20:37:11 -04:00
|
|
|
}
|
2008-04-25 09:37:36 -04:00
|
|
|
else if (ctx->enable_flags & flag)
|
2007-04-27 20:37:11 -04:00
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glDisableClientState (gl_flag) );
|
2008-04-25 09:37:36 -04:00
|
|
|
ctx->enable_flags &= ~flag;
|
2007-04-27 20:37:11 -04:00
|
|
|
}
|
2009-01-24 11:55:04 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2007-04-27 20:37:11 -04:00
|
|
|
|
2008-04-25 09:37:36 -04: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 11:55:04 -05:00
|
|
|
|
2008-05-19 11:02:27 -04:00
|
|
|
cogl_toggle_flag (ctx, flags,
|
|
|
|
COGL_ENABLE_BLEND,
|
|
|
|
GL_BLEND);
|
2009-01-24 11:55:04 -05:00
|
|
|
|
2008-10-27 10:36:52 -04:00
|
|
|
cogl_toggle_flag (ctx, flags,
|
|
|
|
COGL_ENABLE_BACKFACE_CULLING,
|
|
|
|
GL_CULL_FACE);
|
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
cogl_toggle_client_flag (ctx, flags,
|
|
|
|
COGL_ENABLE_VERTEX_ARRAY,
|
|
|
|
GL_VERTEX_ARRAY);
|
2009-01-24 11:55:04 -05:00
|
|
|
|
2008-04-25 09:37:36 -04: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 11:55:04 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return ctx->enable_flags;
|
2008-02-01 13:14:54 -05:00
|
|
|
}
|
2007-04-27 20:37:11 -04:00
|
|
|
|
2008-02-01 13:14:54 -05:00
|
|
|
void
|
|
|
|
cogl_enable_depth_test (gboolean setting)
|
|
|
|
{
|
|
|
|
if (setting)
|
2007-04-27 20:37:11 -04:00
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
glEnable (GL_DEPTH_TEST);
|
|
|
|
glEnable (GL_ALPHA_TEST);
|
2008-02-01 13:14:54 -05:00
|
|
|
glDepthFunc (GL_LEQUAL);
|
2009-01-24 11:55:04 -05:00
|
|
|
glAlphaFunc (GL_GREATER, 0.1);
|
2007-04-27 20:37:11 -04:00
|
|
|
}
|
2008-02-01 13:14:54 -05:00
|
|
|
else
|
2007-04-27 20:37:11 -04:00
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
glDisable (GL_DEPTH_TEST);
|
|
|
|
glDisable (GL_ALPHA_TEST);
|
2007-04-27 20:37:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-27 10:36:52 -04:00
|
|
|
void
|
|
|
|
cogl_enable_backface_culling (gboolean setting)
|
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
ctx->enable_backface_culling = setting;
|
|
|
|
}
|
|
|
|
|
2007-04-27 20:37:11 -04:00
|
|
|
void
|
2008-11-12 08:57:58 -05:00
|
|
|
cogl_set_source_color (const CoglColor *color)
|
2007-04-27 20:37:11 -04:00
|
|
|
{
|
2008-04-25 09:37:36 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2009-01-24 11:55:04 -05:00
|
|
|
|
2009-01-26 06:07:35 -05:00
|
|
|
/* In case cogl_set_source_texture was previously used... */
|
|
|
|
cogl_material_remove_layer (ctx->default_material, 0);
|
2009-01-24 11:55:04 -05:00
|
|
|
|
2009-01-26 06:07:35 -05:00
|
|
|
cogl_material_set_color (ctx->default_material, color);
|
|
|
|
cogl_set_source (ctx->default_material);
|
2007-12-24 07:53:04 -05:00
|
|
|
}
|
|
|
|
|
2008-06-02 08:34:10 -04:00
|
|
|
static void
|
2009-01-20 11:20:54 -05:00
|
|
|
apply_matrix (const float *matrix, float *vertex)
|
2008-06-02 08:34:10 -04:00
|
|
|
{
|
|
|
|
int x, y;
|
2009-01-20 11:20:54 -05:00
|
|
|
float vertex_out[4] = { 0 };
|
2008-06-02 08:34:10 -04:00
|
|
|
|
|
|
|
for (y = 0; y < 4; y++)
|
|
|
|
for (x = 0; x < 4; x++)
|
2009-01-24 11:55:04 -05:00
|
|
|
vertex_out[y] += vertex[x] * matrix[y + x * 4];
|
2008-06-02 08:34:10 -04:00
|
|
|
|
|
|
|
memcpy (vertex, vertex_out, sizeof (vertex_out));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-01-20 11:20:54 -05:00
|
|
|
project_vertex (float *modelview,
|
|
|
|
float *project,
|
|
|
|
float *vertex)
|
2008-06-02 08:34:10 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Apply the modelview matrix */
|
|
|
|
apply_matrix (modelview, vertex);
|
|
|
|
/* Apply the projection matrix */
|
|
|
|
apply_matrix (project, vertex);
|
|
|
|
/* Convert from homogenized coordinates */
|
|
|
|
for (i = 0; i < 4; i++)
|
2009-01-24 11:55:04 -05:00
|
|
|
vertex[i] /= vertex[3];
|
2008-06-02 08:34:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_clip_plane (GLint plane_num,
|
2009-01-20 11:20:54 -05:00
|
|
|
const float *vertex_a,
|
|
|
|
const float *vertex_b)
|
2008-06-02 08:34:10 -04:00
|
|
|
{
|
2009-01-20 11:20:54 -05:00
|
|
|
GLfloat plane[4];
|
|
|
|
GLfloat angle;
|
2008-06-02 08:34:10 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
|
|
|
/* Calculate the angle between the axes and the line crossing the
|
|
|
|
two points */
|
2009-01-20 11:20:54 -05:00
|
|
|
angle = atan2f (vertex_b[1] - vertex_a[1],
|
|
|
|
vertex_b[0] - vertex_a[0]) * (180.0/G_PI);
|
2008-06-02 08:34:10 -04:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glPushMatrix () );
|
2008-06-02 08:34:10 -04:00
|
|
|
/* Load the identity matrix and multiply by the reverse of the
|
|
|
|
projection matrix so we can specify the plane in screen
|
|
|
|
coordinates */
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glLoadIdentity () );
|
|
|
|
GE( glMultMatrixf ((GLfloat *) ctx->inverse_projection) );
|
2008-06-02 08:34:10 -04:00
|
|
|
/* Rotate about point a */
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glTranslatef (vertex_a[0], vertex_a[1], vertex_a[2]) );
|
2008-06-02 08:34:10 -04:00
|
|
|
/* Rotate the plane by the calculated angle so that it will connect
|
|
|
|
the two points */
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glRotatef (angle, 0.0f, 0.0f, 1.0f) );
|
|
|
|
GE( glTranslatef (-vertex_a[0], -vertex_a[1], -vertex_a[2]) );
|
2008-06-02 08:34:10 -04:00
|
|
|
|
|
|
|
plane[0] = 0;
|
2009-01-20 11:20:54 -05:00
|
|
|
plane[1] = -1.0;
|
2008-06-02 08:34:10 -04:00
|
|
|
plane[2] = 0;
|
|
|
|
plane[3] = vertex_a[1];
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glClipPlanef (plane_num, plane) );
|
2008-06-02 08:34:10 -04:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glPopMatrix () );
|
2008-06-02 08:34:10 -04:00
|
|
|
}
|
|
|
|
|
2007-05-28 14:49:34 -04:00
|
|
|
void
|
2009-01-20 11:20:54 -05:00
|
|
|
_cogl_set_clip_planes (float x_offset,
|
|
|
|
float y_offset,
|
|
|
|
float width,
|
|
|
|
float height)
|
2007-05-28 14:49:34 -04:00
|
|
|
{
|
2009-01-20 11:20:54 -05:00
|
|
|
GLfloat modelview[16], projection[16];
|
2008-06-23 10:57:36 -04:00
|
|
|
|
2009-01-20 11:20:54 -05: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 11:55:04 -05:00
|
|
|
0, 1.0 };
|
2008-06-23 10:57:36 -04:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glGetFloatv (GL_MODELVIEW_MATRIX, modelview) );
|
|
|
|
GE( glGetFloatv (GL_PROJECTION_MATRIX, projection) );
|
2008-06-23 10:57:36 -04:00
|
|
|
|
|
|
|
project_vertex (modelview, projection, vertex_tl);
|
|
|
|
project_vertex (modelview, projection, vertex_tr);
|
|
|
|
project_vertex (modelview, projection, vertex_bl);
|
|
|
|
project_vertex (modelview, projection, vertex_br);
|
|
|
|
|
2009-01-24 11:55:04 -05: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 10:57:36 -04:00
|
|
|
if ((vertex_tl[0] < vertex_tr[0] ? 1 : 0)
|
|
|
|
!= (vertex_bl[1] < vertex_tl[1] ? 1 : 0))
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
2009-01-20 11:20:54 -05:00
|
|
|
float temp[4];
|
2008-06-23 10:57:36 -04: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 08:34:10 -04:00
|
|
|
|
2008-06-23 10:57:36 -04: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 08:34:10 -04:00
|
|
|
|
2008-06-23 10:57:36 -04:00
|
|
|
void
|
2009-01-20 11:20:54 -05:00
|
|
|
_cogl_add_stencil_clip (float x_offset,
|
|
|
|
float y_offset,
|
|
|
|
float width,
|
|
|
|
float height,
|
2008-06-23 10:57:36 -04:00
|
|
|
gboolean first)
|
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
2008-06-02 08:34:10 -04:00
|
|
|
|
2009-01-26 06:07:35 -05:00
|
|
|
cogl_material_flush_gl_state (ctx->stencil_material);
|
|
|
|
|
2008-06-23 10:57:36 -04:00
|
|
|
if (first)
|
2008-04-25 09:37:36 -04:00
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glEnable (GL_STENCIL_TEST) );
|
|
|
|
|
2008-06-23 10:57:36 -04:00
|
|
|
/* Initially disallow everything */
|
2008-04-25 09:37:36 -04:00
|
|
|
GE( glClearStencil (0) );
|
|
|
|
GE( glClear (GL_STENCIL_BUFFER_BIT) );
|
2007-05-28 14:49:34 -04:00
|
|
|
|
2008-06-23 10:57:36 -04:00
|
|
|
/* Punch out a hole to allow the rectangle */
|
2008-04-25 09:37:36 -04:00
|
|
|
GE( glStencilFunc (GL_NEVER, 0x1, 0x1) );
|
2008-06-23 10:57:36 -04:00
|
|
|
GE( glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE) );
|
|
|
|
|
2009-01-20 11:20:54 -05:00
|
|
|
cogl_rectangle (x_offset, y_offset, width, height);
|
2008-06-23 10:57:36 -04:00
|
|
|
}
|
2008-12-04 08:45:09 -05:00
|
|
|
else
|
2008-06-23 10:57:36 -04:00
|
|
|
{
|
|
|
|
/* Add one to every pixel of the stencil buffer in the
|
|
|
|
rectangle */
|
|
|
|
GE( glStencilFunc (GL_NEVER, 0x1, 0x3) );
|
2008-04-25 09:37:36 -04:00
|
|
|
GE( glStencilOp (GL_INCR, GL_INCR, GL_INCR) );
|
2009-01-20 11:20:54 -05:00
|
|
|
cogl_rectangle (x_offset, y_offset, width, height);
|
2007-05-28 14:49:34 -04:00
|
|
|
|
2008-06-23 10:57:36 -04: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-01-24 11:55:04 -05:00
|
|
|
GE( glPushMatrix () );
|
|
|
|
GE( glLoadIdentity () );
|
|
|
|
GE( glMatrixMode (GL_PROJECTION) );
|
|
|
|
GE( glPushMatrix () );
|
|
|
|
GE( glLoadIdentity () );
|
2009-01-20 11:20:54 -05:00
|
|
|
cogl_rectangle (-1.0, -1.0, 2, 2);
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glPopMatrix () );
|
|
|
|
GE( glMatrixMode (GL_MODELVIEW) );
|
|
|
|
GE( glPopMatrix () );
|
2008-06-23 10:57:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore the stencil mode */
|
|
|
|
GE( glStencilFunc (GL_EQUAL, 0x1, 0x1) );
|
|
|
|
GE( glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP) );
|
2007-05-28 14:49:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-20 11:20:54 -05:00
|
|
|
_cogl_set_matrix (const float *matrix)
|
2007-05-28 14:49:34 -04:00
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glLoadIdentity () );
|
|
|
|
GE( glMultMatrixf (matrix) );
|
2008-06-23 10:57:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_cogl_disable_stencil_buffer (void)
|
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glDisable (GL_STENCIL_TEST) );
|
2008-06-23 10:57:36 -04:00
|
|
|
}
|
|
|
|
|
2008-12-04 08:45:09 -05:00
|
|
|
void
|
|
|
|
_cogl_enable_clip_planes (void)
|
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glEnable (GL_CLIP_PLANE0) );
|
|
|
|
GE( glEnable (GL_CLIP_PLANE1) );
|
|
|
|
GE( glEnable (GL_CLIP_PLANE2) );
|
|
|
|
GE( glEnable (GL_CLIP_PLANE3) );
|
2008-12-04 08:45:09 -05:00
|
|
|
}
|
|
|
|
|
2008-06-23 10:57:36 -04:00
|
|
|
void
|
|
|
|
_cogl_disable_clip_planes (void)
|
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glDisable (GL_CLIP_PLANE3) );
|
|
|
|
GE( glDisable (GL_CLIP_PLANE2) );
|
|
|
|
GE( glDisable (GL_CLIP_PLANE1) );
|
|
|
|
GE( glDisable (GL_CLIP_PLANE0) );
|
2007-04-27 20:37:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-20 11:20:54 -05:00
|
|
|
cogl_perspective (float fovy,
|
|
|
|
float aspect,
|
|
|
|
float zNear,
|
|
|
|
float zFar)
|
2007-04-27 20:37:11 -04:00
|
|
|
{
|
2009-01-20 11:20:54 -05:00
|
|
|
float xmax, ymax;
|
|
|
|
float x, y, c, d;
|
|
|
|
float fovy_rad_half = (fovy * G_PI) / 360;
|
2007-04-27 20:37:11 -04:00
|
|
|
|
2009-01-20 11:20:54 -05:00
|
|
|
GLfloat m[16];
|
2009-01-24 11:55:04 -05:00
|
|
|
|
2008-06-02 08:34:10 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
2007-04-27 20:37:11 -04:00
|
|
|
memset (&m[0], 0, sizeof (m));
|
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glMatrixMode (GL_PROJECTION) );
|
|
|
|
GE( glLoadIdentity () );
|
2008-08-01 08:23:57 -04:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
/*
|
2007-04-27 20:37:11 -04:00
|
|
|
* Based on the original algorithm in perspective():
|
2009-01-24 11:55:04 -05:00
|
|
|
*
|
2007-04-27 20:37:11 -04:00
|
|
|
* 1) xmin = -xmax => xmax + xmin == 0 && xmax - xmin == 2 * xmax
|
|
|
|
* same true for y, hence: a == 0 && b == 0;
|
|
|
|
*
|
2009-01-24 11:55:04 -05:00
|
|
|
* 2) When working with small numbers, we are loosing significant
|
2008-10-30 12:37:55 -04:00
|
|
|
* precision
|
2007-04-27 20:37:11 -04:00
|
|
|
*/
|
2009-01-20 11:20:54 -05:00
|
|
|
ymax = (zNear * (sinf (fovy_rad_half) / cosf (fovy_rad_half)));
|
2009-01-20 11:20:54 -05:00
|
|
|
xmax = (ymax * aspect);
|
2007-04-27 20:37:11 -04:00
|
|
|
|
2009-01-20 11:20:54 -05:00
|
|
|
x = (zNear / xmax);
|
|
|
|
y = (zNear / ymax);
|
|
|
|
c = (-(zFar + zNear) / ( zFar - zNear));
|
2009-01-20 11:20:54 -05:00
|
|
|
d = (-(2 * zFar) * zNear) / (zFar - zNear);
|
2007-04-27 20:37:11 -04:00
|
|
|
|
|
|
|
#define M(row,col) m[col*4+row]
|
|
|
|
M(0,0) = x;
|
|
|
|
M(1,1) = y;
|
|
|
|
M(2,2) = c;
|
|
|
|
M(2,3) = d;
|
2009-01-20 11:20:54 -05:00
|
|
|
M(3,2) = -1.0;
|
2007-05-01 11:26:12 -04:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glMultMatrixf (m) );
|
2008-06-02 08:34:10 -04:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glMatrixMode (GL_MODELVIEW) );
|
2008-08-01 08:23:57 -04:00
|
|
|
|
2008-06-02 08:34:10 -04:00
|
|
|
/* Calculate and store the inverse of the matrix */
|
2009-01-20 11:20:54 -05:00
|
|
|
memset (ctx->inverse_projection, 0, sizeof (float) * 16);
|
2008-06-02 08:34:10 -04:00
|
|
|
|
|
|
|
#define m ctx->inverse_projection
|
2009-01-20 11:20:54 -05:00
|
|
|
M(0, 0) = (1.0 / x);
|
|
|
|
M(1, 1) = (1.0 / y);
|
|
|
|
M(2, 3) = -1.0;
|
|
|
|
M(3, 2) = (1.0 / d);
|
|
|
|
M(3, 3) = (c / d);
|
2008-06-02 08:34:10 -04:00
|
|
|
#undef m
|
|
|
|
|
2007-04-27 20:37:11 -04:00
|
|
|
#undef M
|
|
|
|
}
|
|
|
|
|
2008-08-01 08:23:57 -04:00
|
|
|
void
|
2009-01-20 11:20:54 -05:00
|
|
|
cogl_frustum (float left,
|
|
|
|
float right,
|
|
|
|
float bottom,
|
|
|
|
float top,
|
|
|
|
float z_near,
|
|
|
|
float z_far)
|
2008-08-01 08:23:57 -04:00
|
|
|
{
|
2009-01-20 11:20:54 -05:00
|
|
|
float c, d;
|
2008-08-01 08:23:57 -04:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glMatrixMode (GL_PROJECTION) );
|
|
|
|
GE( glLoadIdentity () );
|
2008-08-01 08:23:57 -04:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glFrustumf (left,
|
|
|
|
right,
|
|
|
|
bottom,
|
|
|
|
top,
|
|
|
|
z_near,
|
|
|
|
z_far) );
|
2008-08-01 08:23:57 -04:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glMatrixMode (GL_MODELVIEW) );
|
2008-08-01 08:23:57 -04:00
|
|
|
|
|
|
|
/* Calculate and store the inverse of the matrix */
|
2009-01-20 11:20:54 -05:00
|
|
|
memset (ctx->inverse_projection, 0, sizeof (float) * 16);
|
2008-08-01 08:23:57 -04:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
c = - (z_far + z_near) / (z_far - z_near);
|
|
|
|
d = - (2 * (z_far * z_near)) / (z_far - z_near);
|
2008-08-01 08:23:57 -04:00
|
|
|
|
|
|
|
#define M(row,col) ctx->inverse_projection[col*4+row]
|
2009-01-24 11:55:04 -05:00
|
|
|
M(0,0) = (right - left) / (2 * z_near);
|
|
|
|
M(0,3) = (right + left) / (2 * z_near);
|
|
|
|
M(1,1) = (top - bottom) / (2 * z_near);
|
|
|
|
M(1,3) = (top + bottom) / (2 * z_near);
|
2009-01-20 11:20:54 -05:00
|
|
|
M(2,3) = -1.0;
|
2009-01-24 11:55:04 -05:00
|
|
|
M(3,2) = 1.0 / d;
|
|
|
|
M(3,3) = c / d;
|
|
|
|
#undef M
|
2008-08-01 08:23:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cogl_viewport (guint width,
|
|
|
|
guint height)
|
|
|
|
{
|
|
|
|
GE( glViewport (0, 0, width, height) );
|
|
|
|
}
|
|
|
|
|
2007-04-27 20:37:11 -04:00
|
|
|
void
|
2009-01-24 11:55:04 -05:00
|
|
|
cogl_setup_viewport (guint width,
|
|
|
|
guint height,
|
2009-01-20 11:20:54 -05:00
|
|
|
float fovy,
|
|
|
|
float aspect,
|
|
|
|
float z_near,
|
|
|
|
float z_far)
|
2007-04-27 20:37:11 -04:00
|
|
|
{
|
2009-01-20 11:20:54 -05:00
|
|
|
float z_camera;
|
2009-01-22 09:52:34 -05:00
|
|
|
float projection_matrix[16];
|
2009-01-24 11:55:04 -05:00
|
|
|
|
2007-04-27 20:37:11 -04:00
|
|
|
GE( glViewport (0, 0, width, height) );
|
|
|
|
|
2007-05-02 05:01:11 -04:00
|
|
|
/* For Ortho projection.
|
2009-01-24 11:55:04 -05:00
|
|
|
* glOrthof (0, width << 16, 0, height << 16, -1 << 16, 1 << 16);
|
2007-05-01 11:26:12 -04:00
|
|
|
*/
|
|
|
|
|
2007-04-27 20:37:11 -04:00
|
|
|
cogl_perspective (fovy, aspect, z_near, z_far);
|
2009-01-24 11:55:04 -05:00
|
|
|
|
2007-06-26 12:07:14 -04:00
|
|
|
/*
|
2009-01-22 09:52:34 -05:00
|
|
|
* camera distance from screen
|
2007-06-26 12:07:14 -04:00
|
|
|
*
|
|
|
|
* See comments in ../gl/cogl.c
|
|
|
|
*/
|
|
|
|
|
2009-01-22 09:52:34 -05:00
|
|
|
cogl_get_projection_matrix (projection_matrix);
|
|
|
|
z_camera = 0.5 * projection_matrix[0];
|
2007-05-28 14:49:34 -04:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glLoadIdentity () );
|
2009-01-20 11:20:54 -05:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glTranslatef (-0.5f, -0.5f, -z_camera) );
|
2007-05-01 11:26:12 -04:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glScalef (1.0f / width, -1.0f / height, 1.0f / width) );
|
2007-05-01 11:26:12 -04:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glTranslatef (0.0f, -1.0 * height, 0.0f) );
|
2007-05-16 05:08:30 -04:00
|
|
|
}
|
2007-05-01 11:26:12 -04:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
static void
|
|
|
|
_cogl_features_init ()
|
|
|
|
{
|
2008-10-30 12:52:56 -04:00
|
|
|
CoglFeatureFlags flags = 0;
|
|
|
|
int max_clip_planes = 0;
|
2008-12-04 08:45:09 -05:00
|
|
|
GLint num_stencil_bits = 0;
|
2008-04-25 09:37:36 -04:00
|
|
|
|
|
|
|
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
|
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glGetIntegerv (GL_STENCIL_BITS, &num_stencil_bits) );
|
2008-12-04 08:45:09 -05:00
|
|
|
/* We need at least three stencil bits to combine clips */
|
|
|
|
if (num_stencil_bits > 2)
|
2008-04-25 09:37:36 -04:00
|
|
|
flags |= COGL_FEATURE_STENCIL_BUFFER;
|
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
GE( glGetIntegerv (GL_MAX_CLIP_PLANES, &max_clip_planes) );
|
2008-04-25 09:37:36 -04:00
|
|
|
if (max_clip_planes >= 4)
|
|
|
|
flags |= COGL_FEATURE_FOUR_CLIP_PLANES;
|
|
|
|
|
2008-06-02 06:58:57 -04:00
|
|
|
#ifdef HAVE_COGL_GLES2
|
2008-10-29 10:52:48 -04:00
|
|
|
flags |= COGL_FEATURE_SHADERS_GLSL | COGL_FEATURE_OFFSCREEN;
|
2008-06-02 06:58:57 -04:00
|
|
|
#endif
|
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
/* Cache features */
|
2008-04-25 09:37:36 -04:00
|
|
|
ctx->feature_flags = flags;
|
|
|
|
ctx->features_cached = TRUE;
|
|
|
|
}
|
|
|
|
|
2008-10-30 12:52:56 -04:00
|
|
|
CoglFeatureFlags
|
2007-05-16 05:08:30 -04:00
|
|
|
cogl_get_features ()
|
|
|
|
{
|
2008-04-25 09:37:36 -04:00
|
|
|
_COGL_GET_CONTEXT (ctx, 0);
|
2009-01-24 11:55:04 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!ctx->features_cached)
|
|
|
|
_cogl_features_init ();
|
2009-01-24 11:55:04 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return ctx->feature_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
cogl_features_available (CoglFeatureFlags features)
|
|
|
|
{
|
|
|
|
_COGL_GET_CONTEXT (ctx, 0);
|
2009-01-24 11:55:04 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
if (!ctx->features_cached)
|
|
|
|
_cogl_features_init ();
|
2009-01-24 11:55:04 -05:00
|
|
|
|
2008-04-25 09:37:36 -04:00
|
|
|
return (ctx->feature_flags & features) == features;
|
2007-04-27 20:37:11 -04:00
|
|
|
}
|
2007-06-12 07:42:29 -04:00
|
|
|
|
|
|
|
void
|
2009-01-20 11:20:54 -05:00
|
|
|
cogl_get_modelview_matrix (float m[16])
|
2007-06-12 07:42:29 -04:00
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
glGetFloatv (GL_MODELVIEW_MATRIX, m);
|
2007-06-12 07:42:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-20 11:20:54 -05:00
|
|
|
cogl_get_projection_matrix (float m[16])
|
2007-06-12 07:42:29 -04:00
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
glGetFloatv (GL_PROJECTION_MATRIX, m);
|
2007-06-12 07:42:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-20 11:20:54 -05:00
|
|
|
cogl_get_viewport (float v[4])
|
2007-06-12 07:42:29 -04:00
|
|
|
{
|
2008-09-18 07:24:27 -04:00
|
|
|
GLint viewport[4];
|
|
|
|
int i;
|
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
glGetIntegerv (GL_VIEWPORT, viewport);
|
2008-09-18 07:24:27 -04:00
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
2009-01-20 11:20:54 -05:00
|
|
|
v[i] = (float)(viewport[i]);
|
2007-06-12 07:42:29 -04:00
|
|
|
}
|
2007-07-12 06:15:19 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
cogl_get_bitmasks (gint *red, gint *green, gint *blue, gint *alpha)
|
|
|
|
{
|
2009-01-24 11:55:04 -05:00
|
|
|
GLint value;
|
2007-07-12 06:15:19 -04:00
|
|
|
if (red)
|
2009-01-24 11:55:04 -05:00
|
|
|
{
|
|
|
|
GE( glGetIntegerv(GL_RED_BITS, &value) );
|
|
|
|
*red = value;
|
|
|
|
}
|
2007-07-12 06:15:19 -04:00
|
|
|
if (green)
|
2009-01-24 11:55:04 -05:00
|
|
|
{
|
|
|
|
GE( glGetIntegerv(GL_GREEN_BITS, &value) );
|
|
|
|
*green = value;
|
|
|
|
}
|
2007-07-12 06:15:19 -04:00
|
|
|
if (blue)
|
2009-01-24 11:55:04 -05:00
|
|
|
{
|
|
|
|
GE( glGetIntegerv(GL_BLUE_BITS, &value) );
|
|
|
|
*blue = value;
|
|
|
|
}
|
2007-07-12 06:15:19 -04:00
|
|
|
if (alpha)
|
2009-01-24 11:55:04 -05:00
|
|
|
{
|
|
|
|
GE( glGetIntegerv(GL_ALPHA_BITS, &value ) );
|
|
|
|
*alpha = value;
|
|
|
|
}
|
2007-07-12 06:15:19 -04:00
|
|
|
}
|
2007-11-21 06:55:26 -05:00
|
|
|
|
|
|
|
void
|
2008-10-30 12:50:07 -04:00
|
|
|
cogl_fog_set (const CoglColor *fog_color,
|
2009-01-20 11:20:54 -05:00
|
|
|
float density,
|
|
|
|
float z_near,
|
|
|
|
float z_far)
|
2007-11-21 06:55:26 -05:00
|
|
|
{
|
2009-01-20 11:20:54 -05:00
|
|
|
GLfloat fogColor[4];
|
2007-11-21 06:55:26 -05:00
|
|
|
|
2009-01-24 11:55:04 -05: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 06:55:26 -05:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
glEnable (GL_FOG);
|
2007-11-21 06:55:26 -05:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
glFogfv (GL_FOG_COLOR, fogColor);
|
2007-11-21 06:55:26 -05:00
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
glFogf (GL_FOG_MODE, GL_LINEAR);
|
2007-11-21 06:55:26 -05:00
|
|
|
glHint (GL_FOG_HINT, GL_NICEST);
|
|
|
|
|
2009-01-24 11:55:04 -05:00
|
|
|
glFogf (GL_FOG_DENSITY, (GLfloat) density);
|
|
|
|
glFogf (GL_FOG_START, (GLfloat) z_near);
|
|
|
|
glFogf (GL_FOG_END, (GLfloat) z_far);
|
2007-11-21 06:55:26 -05:00
|
|
|
}
|
2009-01-24 11:55:04 -05:00
|
|
|
|