rendered paste bodyFrom b1106c9cd92cf388f38d9a0f55a3cd3ac1b944c6 Mon Sep 17 00:00:00 2001
From: Roi Cohen <roico@roico-desktop.(none)>
Date: Tue, 5 Jun 2007 14:37:11 +0300
Subject: [PATCH] cube painting order
---
include/cube.h | 20 ++-
plugins/cube.c | 419 ++++++++++++++++++++++++++++++++++++--------------------
2 files changed, 284 insertions(+), 155 deletions(-)
diff --git a/include/cube.h b/include/cube.h
index f64ddc8..bc44a32 100644
--- a/include/cube.h
+++ b/include/cube.h
@@ -25,7 +25,7 @@
#include <compiz.h>
-#define CUBE_ABIVERSION 20070523
+#define CUBE_ABIVERSION 20070604
#define CUBE_DISPLAY_OPTION_ABI 0
#define CUBE_DISPLAY_OPTION_INDEX 1
@@ -71,6 +71,12 @@ typedef void (*CubePaintTopBottomProc) (CompScreen *s,
CompOutput *output,
int size);
+typedef enum _PaintOrder
+{
+ BTF = 0,
+ FTB
+} PaintOrder;
+
typedef struct _CubeScreen {
PreparePaintScreenProc preparePaintScreen;
DonePaintScreenProc donePaintScreen;
@@ -80,6 +86,7 @@ typedef struct _CubeScreen {
ApplyScreenTransformProc applyScreenTransform;
SetScreenOptionProc setScreenOption;
OutputChangeNotifyProc outputChangeNotify;
+ InitWindowWalkerProc initWindowWalker;
CubeGetRotationProc getRotation;
CubeClearTargetOutputProc clearTargetOutput;
@@ -87,11 +94,12 @@ typedef struct _CubeScreen {
CompOption opt[CUBE_SCREEN_OPTION_NUM];
- int invert;
- int xrotations;
- GLfloat distance;
- GLushort color[3];
- GLfloat tc[12];
+ int invert;
+ int xrotations;
+ PaintOrder paintOrder;
+ GLfloat distance;
+ GLushort color[3];
+ GLfloat tc[12];
int grabIndex;
diff --git a/plugins/cube.c b/plugins/cube.c
index df857ad..f28b9f5 100644
--- a/plugins/cube.c
+++ b/plugins/cube.c
@@ -32,6 +32,37 @@
#include <cube.h>
+#define MULTM(x, y, z) \
+z[0] = x[0] * y[0] + x[4] * y[1] + x[8] * y[2] + x[12] * y[3]; \
+z[1] = x[1] * y[0] + x[5] * y[1] + x[9] * y[2] + x[13] * y[3]; \
+z[2] = x[2] * y[0] + x[6] * y[1] + x[10] * y[2] + x[14] * y[3]; \
+z[3] = x[3] * y[0] + x[7] * y[1] + x[11] * y[2] + x[15] * y[3]; \
+z[4] = x[0] * y[4] + x[4] * y[5] + x[8] * y[6] + x[12] * y[7]; \
+z[5] = x[1] * y[4] + x[5] * y[5] + x[9] * y[6] + x[13] * y[7]; \
+z[6] = x[2] * y[4] + x[6] * y[5] + x[10] * y[6] + x[14] * y[7]; \
+z[7] = x[3] * y[4] + x[7] * y[5] + x[11] * y[6] + x[15] * y[7]; \
+z[8] = x[0] * y[8] + x[4] * y[9] + x[8] * y[10] + x[12] * y[11]; \
+z[9] = x[1] * y[8] + x[5] * y[9] + x[9] * y[10] + x[13] * y[11]; \
+z[10] = x[2] * y[8] + x[6] * y[9] + x[10] * y[10] + x[14] * y[11]; \
+z[11] = x[3] * y[8] + x[7] * y[9] + x[11] * y[10] + x[15] * y[11]; \
+z[12] = x[0] * y[12] + x[4] * y[13] + x[8] * y[14] + x[12] * y[15]; \
+z[13] = x[1] * y[12] + x[5] * y[13] + x[9] * y[14] + x[13] * y[15]; \
+z[14] = x[2] * y[12] + x[6] * y[13] + x[10] * y[14] + x[14] * y[15]; \
+z[15] = x[3] * y[12] + x[7] * y[13] + x[11] * y[14] + x[15] * y[15];
+
+#define MULTMV(m, v) { \
+float v0 = m[0]*v[0] + m[4]*v[1] + m[8]*v[2] + m[12]*v[3]; \
+float v1 = m[1]*v[0] + m[5]*v[1] + m[9]*v[2] + m[13]*v[3]; \
+float v2 = m[2]*v[0] + m[6]*v[1] + m[10]*v[2] + m[14]*v[3]; \
+float v3 = m[3]*v[0] + m[7]*v[1] + m[11]*v[2] + m[15]*v[3]; \
+v[0] = v0; v[1] = v1; v[2] = v2; v[3] = v3; }
+
+#define DIVV(v) \
+v[0] /= v[3]; \
+v[1] /= v[3]; \
+v[2] /= v[3]; \
+v[3] /= v[3];
+
static CompMetadata cubeMetadata;
static int cubeDisplayPrivateIndex;
@@ -812,6 +843,7 @@ cubePaintOutput (CompScreen *s,
}
cs->srcOutput = (output->id != ~0) ? output->id : 0;
+ cs->paintOrder = BTF; // Always use BTF painting on non-transformed screen.
UNWRAP (cs, s, paintOutput);
status = (*s->paintOutput) (s, sAttrib, transform, region, output, mask);
@@ -833,17 +865,79 @@ cubeDonePaintScreen (CompScreen *s)
WRAP (cs, s, donePaintScreen, cubeDonePaintScreen);
}
+static Bool
+cubeCheckFTB (CompScreen *s,
+ const ScreenPaintAttrib *sAttrib,
+ const CompTransform *transform,
+ CompOutput *outputPtr)
+{
+ float mvp[16];
+ CompTransform sTransform = *transform;
+
+ (*s->applyScreenTransform) (s, sAttrib, outputPtr, &sTransform);
+ transformToScreenSpace (s, outputPtr, -sAttrib->zTranslate, &sTransform);
+
+ MULTM(s->projection, sTransform.m, mvp);
+
+ float pntA[4] = { outputPtr->region.extents.x1,
+ outputPtr->region.extents.y1,
+ 0, 1};
+
+ float pntB[4] = { outputPtr->region.extents.x2,
+ outputPtr->region.extents.y1,
+ 0, 1};
+
+ float pntC[4] = { outputPtr->region.extents.x1 + outputPtr->width / 2.0f,
+ outputPtr->region.extents.y1 + outputPtr->height / 2.0f,
+ 0, 1};
+
+ MULTMV(mvp, pntA);
+ DIVV(pntA);
+
+ MULTMV(mvp, pntB);
+ DIVV(pntB);
+
+ MULTMV(mvp, pntC);
+ DIVV(pntC);
+
+ float vecA[3] = { pntC[0] - pntA[0],
+ pntC[1] - pntA[1],
+ pntC[2] - pntA[2]};
+
+ float vecB[3] = { pntC[0] - pntB[0],
+ pntC[1] - pntB[1],
+ pntC[2] - pntB[2]};
+
+ float ortho[3] = { vecA[1] * vecB[2] - vecA[2] * vecB[1],
+ vecA[2] * vecB[0] - vecA[0] * vecB[2],
+ vecA[0] * vecB[1] - vecA[1] * vecB[0]};
+
+ if (ortho[2] > 0.0f) //The viewport is reversed, should be painted front to back.
+ return TRUE;
+
+ return FALSE;
+}
+
static void
cubeMoveViewportAndPaint (CompScreen *s,
const ScreenPaintAttrib *sAttrib,
const CompTransform *transform,
CompOutput *outputPtr,
unsigned int mask,
+ PaintOrder paintOrder,
int dx)
{
+ CUBE_SCREEN (s);
+
+ Bool ftb = cubeCheckFTB(s, sAttrib, transform, outputPtr);
+
+ if ((paintOrder == FTB && !ftb) ||
+ (paintOrder == BTF && ftb))
+ return;
+
int output = (outputPtr->id != ~0) ? outputPtr->id : 0;
- CUBE_SCREEN (s);
+ cs->paintOrder = paintOrder;
if (cs->nOutput > 1)
{
@@ -885,6 +979,103 @@ cubeMoveViewportAndPaint (CompScreen *s,
}
static void
+cubePaintAllViewports (CompScreen *s,
+ ScreenPaintAttrib *sAttrib,
+ const CompTransform *transform,
+ Region region,
+ CompOutput *outputPtr,
+ unsigned int mask,
+ int xMove,
+ float size,
+ int hsize,
+ PaintOrder paintOrder)
+{
+ CUBE_SCREEN(s);
+
+ ScreenPaintAttrib sa = *sAttrib;
+
+ int i;
+ int xMoveAdd;
+ int origXMoveAdd = 0; // dx for the viewport we start painting with (back-most).
+ int iFirstSign; // 1 if we do xMove += i first and -1 if we do xMove -= i first.
+
+ if (cs->invert == 1)
+ {
+ // xMove ==> dx for the viewport which is the nearest to the viewer in z axis.
+ // xMove +/- hsize / 2 ==> dx for the viewport which is the farest to the viewer in z axis.
+
+ if ((sa.xRotate < 0.0f && hsize % 2 == 1) ||
+ (sa.xRotate > 0.0f && hsize % 2 == 0))
+ {
+ origXMoveAdd = hsize / 2;
+ iFirstSign = 1;
+ }
+
+ else
+ {
+ origXMoveAdd = -hsize / 2;
+ iFirstSign = -1;
+ }
+ }
+
+ else
+ {
+ // xMove is already the dx for farest viewport.
+
+ if (sa.xRotate > 0.0f)
+ iFirstSign = -1;
+ else
+ iFirstSign = 1;
+ }
+
+ for (i = 0; i <= hsize / 2; i++)
+ {
+ // move to the correct viewport (back to front).
+ xMoveAdd = origXMoveAdd; // move to farest viewport.
+ xMoveAdd += iFirstSign * i; // move i more viewports to the right / left.
+
+ // needed especially for unfold.
+ // we paint the viewports around xMove viewport. adding or subtracting hsize from xMove has no effect on
+ // what viewport we paint, but can make shorter paths.
+ if (xMoveAdd < -hsize / 2)
+ xMoveAdd += hsize;
+ else if (xMoveAdd > hsize / 2)
+ xMoveAdd -= hsize;
+
+ // paint the viewport.
+ xMove += xMoveAdd;
+
+ sa.yRotate -= cs->invert * xMoveAdd * 360.0f / size;
+ cubeMoveViewportAndPaint (s, &sa, transform, outputPtr, mask,
+ paintOrder, xMove);
+ sa.yRotate += cs->invert * xMoveAdd * 360.0f / size;
+
+ xMove -= xMoveAdd;
+
+ // do the same for an equally far viewport.
+ if (i == 0 || i * 2 == hsize) // same viewport.
+ continue;
+
+ xMoveAdd = origXMoveAdd; // move to farest viewport.
+ xMoveAdd -= iFirstSign * i; // move i more viewports to the left / right (opposite side from the one chose first)
+
+ if (xMoveAdd < -hsize / 2)
+ xMoveAdd += hsize;
+ else if (xMoveAdd > hsize / 2)
+ xMoveAdd -= hsize;
+
+ xMove += xMoveAdd;
+
+ sa.yRotate -= cs->invert * xMoveAdd * 360.0f / size;
+ cubeMoveViewportAndPaint (s, &sa, transform, outputPtr, mask,
+ paintOrder, xMove);
+ sa.yRotate += cs->invert * xMoveAdd * 360.0f / size;
+
+ xMove -= xMoveAdd;
+ }
+}
+
+static void
cubeGetRotation (CompScreen *s,
float *x,
float *v)
@@ -956,26 +1147,35 @@ cubePaintTopBottom (CompScreen *s,
glVertexPointer (3, GL_FLOAT, 0, cs->vertices);
- glNormal3f (0.0f, -1.0f, 0.0f);
-
- if (cs->invert == 1 && size == 4 && cs->texture.name)
+ int i;
+ for (i = 0; i < 2; i++)
{
- enableTexture (s, &cs->texture, COMP_TEXTURE_FILTER_GOOD);
- glTexCoordPointer (2, GL_FLOAT, 0, cs->tc);
- glDrawArrays (GL_TRIANGLE_FAN, 0, cs->nvertices >> 1);
- disableTexture (s, &cs->texture);
- glDisableClientState (GL_TEXTURE_COORD_ARRAY);
- }
- else
- {
- glDisableClientState (GL_TEXTURE_COORD_ARRAY);
- glDrawArrays (GL_TRIANGLE_FAN, 0, cs->nvertices >> 1);
+ if ((i == 0 && sAttrib->vRotate <= 0.0f) ||
+ (i == 1 && sAttrib->vRotate > 0.0f))
+ {
+ glNormal3f (0.0f, -1.0f, 0.0f);
+ if (cs->invert == 1 && size == 4 && cs->texture.name)
+ {
+ enableTexture (s, &cs->texture, COMP_TEXTURE_FILTER_GOOD);
+ glTexCoordPointer (2, GL_FLOAT, 0, cs->tc);
+ glDrawArrays (GL_TRIANGLE_FAN, 0, cs->nvertices >> 1);
+ disableTexture (s, &cs->texture);
+ glDisableClientState (GL_TEXTURE_COORD_ARRAY);
+ }
+ else
+ {
+ glDisableClientState (GL_TEXTURE_COORD_ARRAY);
+ glDrawArrays (GL_TRIANGLE_FAN, 0, cs->nvertices >> 1);
+ }
+ }
+ else
+ {
+ glNormal3f (0.0f, 1.0f, 0.0f);
+ glDrawArrays (GL_TRIANGLE_FAN, cs->nvertices >> 1,
+ cs->nvertices >> 1);
+ }
}
- glNormal3f (0.0f, 1.0f, 0.0f);
-
- glDrawArrays (GL_TRIANGLE_FAN, cs->nvertices >> 1, cs->nvertices >> 1);
-
glNormal3f (0.0f, 0.0f, -1.0f);
glPopMatrix ();
@@ -996,6 +1196,8 @@ cubePaintTransformedOutput (CompScreen *s,
float xRotate, vRotate;
int hsize, xMove = 0;
float size;
+ GLenum filter = s->display->textureFilter;
+ PaintOrder paintOrder;
Bool clear;
int output = 0;
@@ -1047,9 +1249,6 @@ cubePaintTransformedOutput (CompScreen *s,
UNWRAP (cs, s, paintTransformedOutput);
- sa.xTranslate = sAttrib->xTranslate;
- sa.yTranslate = sAttrib->yTranslate;
-
if (cs->grabIndex)
{
sa.vRotate = 0.0f;
@@ -1063,22 +1262,6 @@ cubePaintTransformedOutput (CompScreen *s,
/* distance we move the camera back when unfolding the cube.
currently hardcoded to 1.5 but it should probably be optional. */
sa.zCamera -= cs->unfold * 1.5f;
-
- sa.xRotate = xRotate * cs->invert;
- if (sa.xRotate > 0.0f)
- {
- cs->xrotations = (int) (hsize * sa.xRotate) / 360;
- sa.xRotate = sa.xRotate - (360.0f * cs->xrotations) / hsize;
- }
- else
- {
- cs->xrotations = (int) (hsize * sa.xRotate) / 360;
- sa.xRotate = sa.xRotate -
- (360.0f * cs->xrotations) / hsize + 360.0f / hsize;
- cs->xrotations--;
- }
-
- sa.xRotate = sa.xRotate / size * hsize;
}
else
{
@@ -1090,131 +1273,46 @@ cubePaintTransformedOutput (CompScreen *s,
sa.vRotate = vRotate;
sa.zTranslate = -cs->invert * cs->distance;
- sa.xRotate = xRotate * cs->invert;
- if (sa.xRotate > 0.0f)
- {
- cs->xrotations = (int) (size * sa.xRotate) / 360;
- sa.xRotate = sa.xRotate - (360.0f * cs->xrotations) / size;
- }
- else
- {
- cs->xrotations = (int) (size * sa.xRotate) / 360;
- sa.xRotate = sa.xRotate -
- (360.0f * cs->xrotations) / size + 360.0f / size;
- cs->xrotations--;
- }
- }
-
- if (!clear && cs->grabIndex == 0 && hsize > 2 &&
- (cs->invert != 1 || sa.vRotate != 0.0f || sa.yTranslate != 0.0f))
- {
- (*cs->paintTopBottom) (s, &sa, transform, outputPtr, hsize);
}
- /* outside cube */
- if (cs->invert == 1)
- {
- if (cs->grabIndex || hsize > 4)
- {
- GLenum filter;
- int i;
-
- xMove = cs->xrotations - ((hsize >> 1) - 1);
- sa.yRotate += (360.0f / size) * ((hsize >> 1) - 1);
-
- filter = s->display->textureFilter;
- if (cs->grabIndex && cs->opt[CUBE_SCREEN_OPTION_MIPMAP].value.b)
- s->display->textureFilter = GL_LINEAR_MIPMAP_LINEAR;
-
- for (i = 0; i < hsize; i++)
- {
- cubeMoveViewportAndPaint (s, &sa, transform, outputPtr, mask,
- xMove);
-
- sa.yRotate -= 360.0f / size;
- xMove++;
- }
-
- s->display->textureFilter = filter;
- }
- else
- {
- if (xRotate != 0.0f)
- {
- xMove = cs->xrotations;
-
- cubeMoveViewportAndPaint (s, &sa, transform, outputPtr, mask,
- xMove);
-
- xMove++;
- }
-
- sa.yRotate -= 360.0f / size;
-
- cubeMoveViewportAndPaint (s, &sa, transform, outputPtr, mask,
- xMove);
- }
- }
+ if (sa.xRotate > 0.0f)
+ cs->xrotations = (int) (hsize * sa.xRotate + 180) / 360;
else
- {
- if (sa.xRotate > 180.0f / size)
- {
- sa.yRotate -= 360.0f / size;
- cs->xrotations++;
- }
+ cs->xrotations = (int) (hsize * sa.xRotate - 180) / 360;
- sa.yRotate -= 360.0f / size;
- xMove = -1 - cs->xrotations;
+ sa.xRotate -= (360.0f * cs->xrotations) / hsize;
+ sa.xRotate *= cs->invert;
- if (cs->grabIndex)
- {
- GLenum filter;
- int i;
+ sa.xRotate = sa.xRotate / size * hsize;
- filter = s->display->textureFilter;
- if (cs->opt[CUBE_SCREEN_OPTION_MIPMAP].value.b)
- s->display->textureFilter = GL_LINEAR_MIPMAP_LINEAR;
+ xMove = cs->xrotations;
- if (sa.xRotate > 180.0f / size)
- {
- xMove -= ((hsize >> 1) - 2);
- sa.yRotate -= (360.0f / size) * ((hsize >> 1) - 2);
- }
- else
- {
- xMove -= ((hsize >> 1) - 1);
- sa.yRotate -= (360.0f / size) * ((hsize >> 1) - 1);
- }
+ if (cs->grabIndex && cs->opt[CUBE_SCREEN_OPTION_MIPMAP].value.b)
+ s->display->textureFilter = GL_LINEAR_MIPMAP_LINEAR;
- for (i = 0; i < hsize; i++)
- {
- cubeMoveViewportAndPaint (s, &sa, transform, outputPtr, mask,
- xMove);
+ if (cs->invert == 1)
+ paintOrder = FTB; // Outside cube - start with FTB faces.
+ else
+ paintOrder = BTF; // Inside cube - start with BTF faces.
- sa.yRotate += 360.0f / size;
- xMove++;
- }
+ if (cs->invert == -1)
+ cubePaintAllViewports(s, &sa, transform, region, outputPtr, mask, xMove, size, hsize, paintOrder);
- s->display->textureFilter = filter;
- }
- else
- {
- cubeMoveViewportAndPaint (s, &sa, transform, outputPtr, mask,
- xMove);
-
- sa.yRotate += 360.0f / size;
- xMove = -cs->xrotations;
+ if (!clear && cs->grabIndex == 0 && hsize > 2 &&
+ (cs->invert != 1 || sa.vRotate != 0.0f || sa.yTranslate != 0.0f))
+ {
+ (*cs->paintTopBottom) (s, &sa, transform, outputPtr, hsize);
+ }
- cubeMoveViewportAndPaint (s, &sa, transform, outputPtr, mask,
- xMove);
+ if (cs->invert == 1)
+ paintOrder = BTF; // Outside cube - continue with BTF faces.
+ else
+ paintOrder = FTB; // Inside cube - continue with FTB faces.
- sa.yRotate += 360.0f / size;
- xMove = 1 - cs->xrotations;
+ if (cs->invert == 1)
+ cubePaintAllViewports(s, &sa, transform, region, outputPtr, mask, xMove, size, hsize, paintOrder);
- cubeMoveViewportAndPaint (s, &sa, transform, outputPtr, mask,
- xMove);
- }
- }
+ s->display->textureFilter = filter;
WRAP (cs, s, paintTransformedOutput, cubePaintTransformedOutput);
}
@@ -1320,6 +1418,27 @@ cubePaintBackground (CompScreen *s,
}
static void
+cubeInitWindowWalker (CompScreen *s, CompWalker* walker)
+{
+ CUBE_SCREEN (s);
+
+ UNWRAP (cs, s, initWindowWalker);
+ (*s->initWindowWalker) (s, walker);
+ WRAP (cs, s, initWindowWalker, cubeInitWindowWalker);
+
+ if (cs->paintOrder == FTB)
+ {
+ WalkInitProc tmpInit = walker->first;
+ walker->first = walker->last;
+ walker->last = tmpInit;
+
+ WalkStepProc tmpStep = walker->next;
+ walker->next = walker->prev;
+ walker->prev = tmpStep;
+ }
+}
+
+static void
cubeApplyScreenTransform (CompScreen *s,
const ScreenPaintAttrib *sAttrib,
CompOutput *output,
@@ -1713,6 +1832,7 @@ cubeInitScreen (CompPlugin *p,
WRAP (cs, s, applyScreenTransform, cubeApplyScreenTransform);
WRAP (cs, s, setScreenOption, cubeSetGlobalScreenOption);
WRAP (cs, s, outputChangeNotify, cubeOutputChangeNotify);
+ WRAP (cs, s, initWindowWalker, cubeInitWindowWalker);
return TRUE;
}
@@ -1734,6 +1854,7 @@ cubeFiniScreen (CompPlugin *p,
UNWRAP (cs, s, applyScreenTransform);
UNWRAP (cs, s, setScreenOption);
UNWRAP (cs, s, outputChangeNotify);
+ UNWRAP (cs, s, initWindowWalker);
finiTexture (s, &cs->texture);
finiTexture (s, &cs->sky);
--
1.4.4.2