[cogl] Make sure we draw upside down to offscreen draw buffers

First a few notes about Cogl coordinate systems:
- Cogl defines the window origin, viewport origin and texture coordinates
origin to be top left unlike OpenGL which defines them as bottom left.
- Cogl defines the modelview and projection identity matrices in exactly the
same way as OpenGL.
- I.e. we believe that for 2D centric constructs: windows/framebuffers,
viewports and textures developers are more used to dealing with a top left
origin, but when modeling objects in 3D; an origin at the center with y
going up is quite natural.

The way Cogl handles textures is by uploading data upside down in OpenGL
terms so that bottom left becomes top left.  (Note: This also has the
benefit that we don't need to flip the data we get from image decoding
libraries since they typically also consider top left to be the image
origin.)

The viewport and window coords are mostly handled with various y =
height - y tweaks before we pass y coordinates to OpenGL.

Generally speaking though the handling of coordinate spaces in Cogl is a bit
fragile.  I guess partly because none of it was design to be, it just
evolved from how Clutter defines its coordinates without much consideration
or testing.  I hope to improve this over a number of commits; starting here.

This commit deals with the fact that offscreen draw buffers may be bound to
textures but we don't "upload" the texture data upside down, and so if you
texture from an offscreen draw buffer you need to manually flip the texture
coordinates to get it the right way around.  We now force offscreen
rendering to be flipped upside down by tweaking the projection matrix right
before we submit it to OpenGL to scale y by -1.  The tweak is entirely
hidden from the user such that if you call cogl_get_projection you will not
see this scale.
This commit is contained in:
Robert Bragg 2009-10-22 16:13:01 +01:00
parent 8b4c496f2a
commit e3391b0173
4 changed files with 37 additions and 10 deletions

View File

@ -71,6 +71,10 @@ cogl_create_context (void)
_context->indirect = gl_is_indirect;
cogl_matrix_init_identity (&_context->identity_matrix);
cogl_matrix_init_identity (&_context->y_flip_matrix);
cogl_matrix_scale (&_context->y_flip_matrix, 1, -1, 1);
_context->flushed_matrix_mode = COGL_MATRIX_MODELVIEW;
_context->texture_units = NULL;

View File

@ -52,6 +52,10 @@ typedef struct
gboolean indirect;
/* A few handy matrix constants */
CoglMatrix identity_matrix;
CoglMatrix y_flip_matrix;
/* Client-side matrix stack or NULL if none */
CoglMatrixMode flushed_matrix_mode;
GList *texture_units;

View File

@ -115,6 +115,9 @@ _cogl_draw_buffer_flush_state (CoglHandle handle,
CoglHandle
_cogl_onscreen_new (void);
gboolean
cogl_is_offscreen (CoglHandle handle);
CoglHandle
_cogl_get_draw_buffer (void);
GSList *

View File

@ -22,6 +22,7 @@
*
* Authors:
* Havoc Pennington <hp@pobox.com> for litl
* Robert Bragg <robert@linux.intel.com>
*/
#ifdef HAVE_CONFIG_H
@ -32,6 +33,7 @@
#include "cogl-context.h"
#include "cogl-internal.h"
#include "cogl-matrix-stack.h"
#include "cogl-draw-buffer-private.h"
typedef struct {
CoglMatrix matrix;
@ -412,21 +414,35 @@ _cogl_matrix_stack_flush_to_gl (CoglMatrixStack *stack,
ctx->flushed_matrix_mode = mode;
}
/* In theory it might help the GL implementation if we used our
* local analysis of the matrix and called Translate/Scale rather
* than LoadMatrix to send a 2D matrix
/* Because Cogl defines texture coordinates to have a top left origin and
* because offscreen draw buffers may be used for rendering to textures we
* always render upside down to offscreen buffers.
*/
if (state->is_identity)
if (mode == COGL_MATRIX_PROJECTION &&
cogl_is_offscreen (_cogl_get_draw_buffer ()))
{
if (!stack->flushed_identity)
GE (glLoadIdentity ());
stack->flushed_identity = TRUE;
CoglMatrix flipped_projection;
CoglMatrix *projection =
state->is_identity ? &ctx->identity_matrix : &state->matrix;
cogl_matrix_multiply (&flipped_projection,
&ctx->y_flip_matrix, projection);
GE (glLoadMatrixf (cogl_matrix_get_array (&flipped_projection)));
stack->flushed_identity = FALSE;
}
else
{
GE (glLoadMatrixf (cogl_matrix_get_array (&state->matrix)));
stack->flushed_identity = FALSE;
if (state->is_identity)
{
if (!stack->flushed_identity)
GE (glLoadIdentity ());
stack->flushed_identity = TRUE;
}
else
{
GE (glLoadMatrixf (cogl_matrix_get_array (&state->matrix)));
stack->flushed_identity = FALSE;
}
}
stack->flushed_state = state;
}